mod state; #[cfg (test)] mod tests; fn main() { use state::{ Block, Chunk, Instruction as Inst, State, }; /* local function bool_to_x (b) if b then return 99 else return 98 end end local x = bool_to_x (not not arg [1]) print (x) return x */ let chunk = Chunk { blocks: vec! [ Block { instructions: vec! [ Inst::VarArgPrep (0), Inst::Closure (0, 0), Inst::GetTabUp (1, 0, 0), Inst::Move (2, 0), Inst::LoadFalse (3), Inst::Call (2, 2, 0), Inst::Call (1, 0, 1), Inst::GetTabUp (1, 0, 0), Inst::Move (2, 0), Inst::LoadTrue (3), Inst::Call (2, 2, 0), Inst::Call (1, 0, 1), Inst::Move (1, 0), Inst::GetTabUp (2, 0, 1), Inst::GetI (2, 2, 1), Inst::Not (2, 2), Inst::Not (2, 2), Inst::Call (1, 2, 2), Inst::GetTabUp (2, 0, 0), Inst::Move (3, 1), Inst::Call (2, 2, 1), Inst::Return (1, 2, 1), Inst::Return (2, 1, 1), ], constants: vec! [ "print".into (), "arg".into (), ], }, Block { instructions: vec! [ Inst::Test (0, 0), Inst::Jmp (3), Inst::LoadI (1, 99), Inst::Return1 (1), Inst::Jmp (2), Inst::LoadI (1, 98), Inst::Return1 (1), Inst::Return0, ], constants: vec! [], }, ], }; let mut vm = State::default (); if std::env::var("LUA_DEBUG").is_ok() { vm.debug_print = true; } let upvalues = State::upvalues_from_args (std::env::args ()); println! ("Returned: {:?}", vm.execute_chunk (&chunk, &upvalues)); }