diff --git a/src/compile.rs b/src/compile.rs index 4c02be2..2ad1e51 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -9,8 +9,8 @@ use crate::{ Literal, Node, Statement, UnaryOperator, }, vm::{ - Constant, Instruction, Prototype, - value::{LuaBool, LuaInteger}, + Instruction, Prototype, + value::{Constant, LuaBool, LuaInteger}, }, }; diff --git a/src/lib.rs b/src/lib.rs index 75f682b..bb547de 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -56,7 +56,7 @@ pub enum CompilationError { pub struct CompilationUnit { pub instructions: Vec, pub state: compile::State, - pub constants: Vec, + pub constants: Vec, } impl Debug for CompilationUnit { diff --git a/src/vm/mod.rs b/src/vm/mod.rs index af7a0e6..c008112 100644 --- a/src/vm/mod.rs +++ b/src/vm/mod.rs @@ -1,42 +1,15 @@ use thiserror::Error; -use std::{cell::RefCell, collections::HashMap, fmt::Debug, hash::Hash, rc::Rc}; +use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc}; use crate::{ CompilationUnit, ast::{BinaryOperator, UnaryOperator}, - vm::value::{IndexableValue, LuaBool, LuaInteger, VMFloat, Value}, + vm::value::{Constant, IndexableValue, LuaInteger, Value}, }; pub mod value; -#[derive(Clone, Hash, PartialEq, Eq)] -pub enum Constant { - String(String), - Float(VMFloat), - Integer(LuaInteger), - Bool(LuaBool), - Nil, -} - -impl From<&str> for Constant { - fn from(value: &str) -> Self { - Constant::String(value.to_owned()) - } -} - -impl Debug for Constant { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Self::String(arg0) => f.debug_tuple("String").field(arg0).finish(), - Self::Float(arg0) => f.debug_tuple("Float").field(&arg0.lua_number()).finish(), - Self::Integer(arg0) => f.debug_tuple("Integer").field(arg0).finish(), - Self::Bool(arg0) => f.debug_tuple("Boolean").field(arg0).finish(), - Self::Nil => f.debug_tuple("Nil").finish(), - } - } -} - #[derive(Clone, Copy)] pub enum Instruction { /// R(A) := R(B) diff --git a/src/vm/value.rs b/src/vm/value.rs index de459cb..70ba33f 100644 --- a/src/vm/value.rs +++ b/src/vm/value.rs @@ -87,6 +87,32 @@ impl From for Value { Self::RustFunction(Rc::new(RefCell::new(value))) } } +#[derive(Clone, Hash, PartialEq, Eq)] +pub enum Constant { + String(String), + Float(VMFloat), + Integer(LuaInteger), + Bool(LuaBool), + Nil, +} + +impl From<&str> for Constant { + fn from(value: &str) -> Self { + Constant::String(value.to_owned()) + } +} + +impl Debug for Constant { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::String(arg0) => f.debug_tuple("String").field(arg0).finish(), + Self::Float(arg0) => f.debug_tuple("Float").field(&arg0.lua_number()).finish(), + Self::Integer(arg0) => f.debug_tuple("Integer").field(arg0).finish(), + Self::Bool(arg0) => f.debug_tuple("Boolean").field(arg0).finish(), + Self::Nil => f.debug_tuple("Nil").finish(), + } + } +} #[derive(Clone)] pub enum Value {