diff --git a/valuescript_vm/src/instruction.rs b/valuescript_common/src/instruction_byte.rs similarity index 91% rename from valuescript_vm/src/instruction.rs rename to valuescript_common/src/instruction_byte.rs index 9edba40..ce6323d 100644 --- a/valuescript_vm/src/instruction.rs +++ b/valuescript_common/src/instruction_byte.rs @@ -1,7 +1,5 @@ -// TODO: Fix duplication with assembler (requires internal crate?) - #[derive(Debug, Clone)] -pub enum Instruction { +pub enum InstructionByte { End = 0x00, Mov = 0x01, OpInc = 0x02, @@ -48,9 +46,9 @@ pub enum Instruction { New = 0x2b, } -impl Instruction { - pub fn from_byte(byte: u8) -> Instruction { - use Instruction::*; +impl InstructionByte { + pub fn from_byte(byte: u8) -> InstructionByte { + use InstructionByte::*; return match byte { 0x00 => End, diff --git a/valuescript_common/src/lib.rs b/valuescript_common/src/lib.rs index ef3c876..4d67a31 100644 --- a/valuescript_common/src/lib.rs +++ b/valuescript_common/src/lib.rs @@ -1,3 +1,5 @@ mod builtins; +mod instruction_byte; pub use builtins::*; +pub use instruction_byte::*; diff --git a/valuescript_compiler/src/assembly_parser.rs b/valuescript_compiler/src/assembly_parser.rs index 39818b3..5683693 100644 --- a/valuescript_compiler/src/assembly_parser.rs +++ b/valuescript_compiler/src/assembly_parser.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use std::str::FromStr; use num_bigint::BigInt; -use valuescript_common::BUILTIN_NAMES; +use valuescript_common::{InstructionByte, BUILTIN_NAMES}; use crate::asm::{ Array, Builtin, Class, Definition, DefinitionContent, Function, Instruction, InstructionOrLabel, @@ -919,54 +919,6 @@ pub fn parse_module(content: &str) -> Module { assembler.module() } -#[derive(Debug, Clone)] -enum InstructionByte { - End = 0x00, - Mov = 0x01, - OpInc = 0x02, - OpDec = 0x03, - OpPlus = 0x04, - OpMinus = 0x05, - OpMul = 0x06, - OpDiv = 0x07, - OpMod = 0x08, - OpExp = 0x09, - OpEq = 0x0a, - OpNe = 0x0b, - OpTripleEq = 0x0c, - OpTripleNe = 0x0d, - OpAnd = 0x0e, - OpOr = 0x0f, - OpNot = 0x10, - OpLess = 0x11, - OpLessEq = 0x12, - OpGreater = 0x13, - OpGreaterEq = 0x14, - OpNullishCoalesce = 0x15, - OpOptionalChain = 0x16, - OpBitAnd = 0x17, - OpBitOr = 0x18, - OpBitNot = 0x19, - OpBitXor = 0x1a, - OpLeftShift = 0x1b, - OpRightShift = 0x1c, - OpRightShiftUnsigned = 0x1d, - TypeOf = 0x1e, - InstanceOf = 0x1f, - In = 0x20, - Call = 0x21, - Apply = 0x22, - Bind = 0x23, - Sub = 0x24, - SubMov = 0x25, - SubCall = 0x26, - Jmp = 0x27, - JmpIf = 0x28, - UnaryPlus = 0x29, - UnaryMinus = 0x2a, - New = 0x2b, -} - fn is_leading_identifier_char(c: char) -> bool { return c == '_' || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'); } diff --git a/valuescript_vm/src/bytecode_decoder.rs b/valuescript_vm/src/bytecode_decoder.rs index e24964c..59bd17b 100644 --- a/valuescript_vm/src/bytecode_decoder.rs +++ b/valuescript_vm/src/bytecode_decoder.rs @@ -3,17 +3,16 @@ use std::rc::Rc; use num_bigint::BigInt; use num_bigint::Sign; +use valuescript_common::InstructionByte; use crate::builtins::BUILTIN_VALS; - -use super::instruction::Instruction; -use super::vs_array::VsArray; -use super::vs_class::VsClass; -use super::vs_function::VsFunction; -use super::vs_object::VsObject; -use super::vs_pointer::VsPointer; -use super::vs_value::Val; -use super::vs_value::ValTrait; +use crate::vs_array::VsArray; +use crate::vs_class::VsClass; +use crate::vs_function::VsFunction; +use crate::vs_object::VsObject; +use crate::vs_pointer::VsPointer; +use crate::vs_value::Val; +use crate::vs_value::ValTrait; pub struct BytecodeDecoder { // TODO: Enable borrow usage to avoid the rc overhead @@ -257,7 +256,7 @@ impl BytecodeDecoder { })); } - pub fn decode_instruction(&mut self) -> Instruction { - return Instruction::from_byte(self.decode_byte()); + pub fn decode_instruction(&mut self) -> InstructionByte { + return InstructionByte::from_byte(self.decode_byte()); } } diff --git a/valuescript_vm/src/bytecode_stack_frame.rs b/valuescript_vm/src/bytecode_stack_frame.rs index 051057c..6e7c201 100644 --- a/valuescript_vm/src/bytecode_stack_frame.rs +++ b/valuescript_vm/src/bytecode_stack_frame.rs @@ -1,8 +1,9 @@ use std::rc::Rc; +use valuescript_common::InstructionByte; + use crate::bytecode_decoder::BytecodeDecoder; use crate::bytecode_decoder::BytecodeType; -use crate::instruction::Instruction; use crate::operations; use crate::stack_frame::FrameStepOk; use crate::stack_frame::FrameStepResult; @@ -93,7 +94,7 @@ impl StackFrameTrait for BytecodeStackFrame { } fn step(&mut self) -> FrameStepResult { - use Instruction::*; + use InstructionByte::*; match self.decoder.decode_instruction() { End => { diff --git a/valuescript_vm/src/lib.rs b/valuescript_vm/src/lib.rs index 1da4b90..505e2bf 100644 --- a/valuescript_vm/src/lib.rs +++ b/valuescript_vm/src/lib.rs @@ -5,7 +5,6 @@ mod bytecode_decoder; mod bytecode_stack_frame; mod first_stack_frame; mod helpers; -mod instruction; mod macros; mod native_frame_function; mod native_function;