diff --git a/examples/test.rs b/examples/test.rs index 69e175a..d760ffc 100644 --- a/examples/test.rs +++ b/examples/test.rs @@ -1,6 +1,7 @@ use ferrite_lua::{ - compile, value, - vm::{self, RuntimeError, RustFunction, VirtualMachine}, + compile, + value::{self, RustFunction}, + vm::{self, RuntimeError, VirtualMachine}, }; static TEST: &str = include_str!("../examples/test.lua"); diff --git a/src/ast.rs b/src/ast.rs index d7b2be6..d2f56f8 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -5,7 +5,7 @@ use crate::{ Parse, TokenRange, TokenStream, TokenStreamError, lexer::{Keyword, Position, Token}, }, - vm::{LuaFloat, LuaInteger}, + value::{LuaFloat, LuaInteger}, }; #[derive(Debug, Clone)] diff --git a/src/compile.rs b/src/compile.rs index f5acc95..8f794a4 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -8,7 +8,8 @@ use crate::{ AccessModifier, BinaryOperator, Block, Expression, ExpressionList, IdentOrEllipsis, Literal, Node, Statement, UnaryOperator, }, - vm::{Constant, Instruction, LuaBool, LuaInteger, Prototype}, + value::{LuaBool, LuaInteger}, + vm::{Constant, Instruction, Prototype}, }; #[derive(Clone, Debug)] diff --git a/src/lib.rs b/src/lib.rs index 0f7a590..981665c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,7 @@ //! //! #[derive(Debug, PartialEq, Eq)] //! pub struct Print; -//! impl vm::RustFunction for Print { +//! impl value::RustFunction for Print { //! fn execute(&self, parameters: Vec) -> Result, vm::RuntimeError> { //! println!("{:?}", parameters); //! Ok(Vec::new()) diff --git a/src/value.rs b/src/value.rs index 07ca88a..de459cb 100644 --- a/src/value.rs +++ b/src/value.rs @@ -2,9 +2,92 @@ use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc}; use crate::{ ast::{BinaryOperator, UnaryOperator}, - vm::{Closure, LuaBool, LuaFloat, LuaInteger, RuntimeError, RustFunction, VMFloat}, + vm::{Closure, RuntimeError}, }; +#[derive(Clone, Hash, PartialEq, Eq, Copy, PartialOrd, Ord)] +pub struct VMFloat(u64); + +impl VMFloat { + pub fn lua_number(&self) -> LuaFloat { + LuaFloat(f64::from_bits(self.0)) + } +} + +impl Debug for VMFloat { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.lua_number().fmt(f) + } +} + +#[derive(Clone, Copy)] +pub struct LuaFloat(pub f64); + +impl LuaFloat { + pub fn vm_number(&self) -> VMFloat { + VMFloat(f64::to_bits(self.0)) + } +} + +impl Debug for LuaFloat { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.0.fmt(f) + } +} + +#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)] +pub struct LuaInteger(pub i64); + +impl Debug for LuaInteger { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.0.fmt(f) + } +} + +impl From for LuaFloat { + fn from(value: LuaInteger) -> Self { + LuaFloat(value.0 as f64) + } +} + +impl From<&LuaInteger> for LuaFloat { + fn from(value: &LuaInteger) -> Self { + LuaFloat(value.0 as f64) + } +} + +#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)] +pub struct LuaBool(pub bool); + +impl Debug for LuaBool { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.0.fmt(f) + } +} + +impl From for LuaInteger { + fn from(value: LuaBool) -> Self { + LuaInteger(value.0 as i64) + } +} + +impl From<&LuaBool> for LuaInteger { + fn from(value: &LuaBool) -> Self { + LuaInteger(value.0 as i64) + } +} + +pub trait RustFunction: Debug { + fn execute(&self, parameters: Vec) -> Result, RuntimeError>; + fn as_indexable(&self) -> String; +} + +impl From for Value { + fn from(value: T) -> Self { + Self::RustFunction(Rc::new(RefCell::new(value))) + } +} + #[derive(Clone)] pub enum Value { String(String), diff --git a/src/vm.rs b/src/vm.rs index 327aa30..05d5f74 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -5,81 +5,9 @@ use std::{cell::RefCell, collections::HashMap, fmt::Debug, hash::Hash, rc::Rc}; use crate::{ CompilationUnit, ast::{BinaryOperator, UnaryOperator}, - value::{IndexableValue, Value}, + value::{IndexableValue, LuaBool, LuaInteger, VMFloat, Value}, }; -#[derive(Clone, Hash, PartialEq, Eq, Copy, PartialOrd, Ord)] -pub struct VMFloat(u64); - -impl VMFloat { - pub fn lua_number(&self) -> LuaFloat { - LuaFloat(f64::from_bits(self.0)) - } -} - -impl Debug for VMFloat { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - self.lua_number().fmt(f) - } -} - -#[derive(Clone, Copy)] -pub struct LuaFloat(pub f64); - -impl LuaFloat { - pub fn vm_number(&self) -> VMFloat { - VMFloat(f64::to_bits(self.0)) - } -} - -impl Debug for LuaFloat { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - self.0.fmt(f) - } -} - -#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)] -pub struct LuaInteger(pub i64); - -impl Debug for LuaInteger { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - self.0.fmt(f) - } -} - -impl From for LuaFloat { - fn from(value: LuaInteger) -> Self { - LuaFloat(value.0 as f64) - } -} - -impl From<&LuaInteger> for LuaFloat { - fn from(value: &LuaInteger) -> Self { - LuaFloat(value.0 as f64) - } -} - -#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)] -pub struct LuaBool(pub bool); - -impl Debug for LuaBool { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - self.0.fmt(f) - } -} - -impl From for LuaInteger { - fn from(value: LuaBool) -> Self { - LuaInteger(value.0 as i64) - } -} - -impl From<&LuaBool> for LuaInteger { - fn from(value: &LuaBool) -> Self { - LuaInteger(value.0 as i64) - } -} - #[derive(Clone, Hash, PartialEq, Eq)] pub enum Constant { String(String), @@ -270,17 +198,6 @@ impl Environment { } } -pub trait RustFunction: Debug { - fn execute(&self, parameters: Vec) -> Result, RuntimeError>; - fn as_indexable(&self) -> String; -} - -impl From for Value { - fn from(value: T) -> Self { - Self::RustFunction(Rc::new(RefCell::new(value))) - } -} - #[derive(Debug, Clone, Default)] pub struct Prototype { pub instructions: Vec,