From 448ecc08d29f55526303d03e4898e05d3fe74cfa Mon Sep 17 00:00:00 2001 From: _ <_@_> Date: Tue, 26 Sep 2023 16:47:51 -0500 Subject: [PATCH] :recycle: refactor: extract module --- src/instruction.rs | 65 ++++++++++++++++++++++++++++++++++++++ src/loader.rs | 10 +++--- src/main.rs | 1 + src/state.rs | 77 +++++----------------------------------------- src/tests.rs | 2 +- 5 files changed, 80 insertions(+), 75 deletions(-) create mode 100644 src/instruction.rs diff --git a/src/instruction.rs b/src/instruction.rs new file mode 100644 index 0000000..e723f55 --- /dev/null +++ b/src/instruction.rs @@ -0,0 +1,65 @@ +#[derive (Debug, PartialEq)] +pub enum Instruction { + Add (u8, u8, u8), + + Call (u8, u8, u8), + Closure (u8, u32), + + // Equals Constant? + EqK (u8, u8, bool), + + ExtraArg (u32), + + // Get Immediate? + GetI (u8, u8, u8), + + // Get Table, Upvalue + GetTabUp (u8, u8, u8), + + GetUpVal (u8, u8), + + // Jump + Jmp (i32), + + // Load F (Float?) + LoadF (u8, i32), + + LoadFalse (u8), + + // Load Integer? + LoadI (u8, i32), + + // Load Constant + LoadK (u8, u32), + + LoadNil (u8), + + LoadTrue (u8), + + // MetaMethod, Binary + MmBin (u8, u8, u8), + + Move (u8, u8), + + Mul (u8, u8, u8), + + NewTable (u8), + + Not (u8, u8), + + // (A, B, _C, k) Return B - 1 registers starting with A + Return (u8, u8, u8, bool), + + Return0, + + // Return just one register + Return1 (u8), + + SetTabUp (u8, u8, u8), + + TailCall (u8, u8, u8, bool), + + Test (u8, bool), + + VarArgPrep (i32), +} diff --git a/src/loader.rs b/src/loader.rs index a7cf343..c112129 100644 --- a/src/loader.rs +++ b/src/loader.rs @@ -1,9 +1,11 @@ use std::io::Read; -use crate::state::{ - Block, - Chunk, - Instruction as Inst, +use crate::{ + instruction::Instruction as Inst, + state::{ + Block, + Chunk, + } }; /// Invoke `luac` as a subprocess diff --git a/src/main.rs b/src/main.rs index 0aee6ef..b3be927 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +mod instruction; mod loader; mod state; mod value; diff --git a/src/state.rs b/src/state.rs index 30d01b8..1911eec 100644 --- a/src/state.rs +++ b/src/state.rs @@ -1,77 +1,14 @@ use std::collections::HashMap; -use crate::value::{ - BogusClosure, - Table, - Value, +use crate::{ + instruction::Instruction, + value::{ + BogusClosure, + Table, + Value, + }, }; -#[derive (Debug, PartialEq)] -pub enum Instruction { - Add (u8, u8, u8), - - Call (u8, u8, u8), - Closure (u8, u32), - - // Equals Constant? - EqK (u8, u8, bool), - - ExtraArg (u32), - - // Get Immediate? - GetI (u8, u8, u8), - - // Get Table, Upvalue - GetTabUp (u8, u8, u8), - - GetUpVal (u8, u8), - - // Jump - Jmp (i32), - - // Load F (Float?) - LoadF (u8, i32), - - LoadFalse (u8), - - // Load Integer? - LoadI (u8, i32), - - // Load Constant - LoadK (u8, u32), - - LoadNil (u8), - - LoadTrue (u8), - - // MetaMethod, Binary - MmBin (u8, u8, u8), - - Move (u8, u8), - - Mul (u8, u8, u8), - - NewTable (u8), - - Not (u8, u8), - - // (A, B, _C, k) Return B - 1 registers starting with A - Return (u8, u8, u8, bool), - - Return0, - - // Return just one register - Return1 (u8), - - SetTabUp (u8, u8, u8), - - TailCall (u8, u8, u8, bool), - - Test (u8, bool), - - VarArgPrep (i32), -} - pub struct Block { pub instructions: Vec , pub constants: Vec , diff --git a/src/tests.rs b/src/tests.rs index 90cd600..6b19036 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,11 +1,11 @@ use std::hash::Hash; use crate::{ + instruction::Instruction as Inst, loader, state::{ Block, Chunk, - Instruction as Inst, State, }, value::Value,