use crate::state::{ Chunk, Instruction as Inst, State, Value, }; #[test] fn floats () { /* local a = 0.5 local b = 3 local x = a + b print (x) return x */ let chunk = Chunk { instructions: vec! [ Inst::VarArgPrep (0), Inst::LoadK (0, 0), Inst::LoadI (1, 3), Inst::Add (2, 0, 1), Inst::MmBin (0, 1, 6), Inst::GetTabUp (3, 0, 1), Inst::Move (4, 2), Inst::Call (3, 2, 1), Inst::Return (2, 2, 1), Inst::Return (3, 1, 1), ], constants: vec! [ 0.5.into (), "print".into (), ], }; for (arg, expected) in [ (vec! ["_exe_name"], vec! [3.5.into ()]), (vec! ["_exe_name", " "], vec! [3.5.into ()]), ] { let mut vm = State::default (); let upvalues = State::upvalues_from_args (arg.into_iter ().map (|s| s.to_string ())); let actual = vm.execute_chunk (&chunk, &upvalues); assert_eq! (actual, expected); } } #[test] fn is_93 () { /* if arg [1] == "93" then print "it's 93" return 0 else print "it's not 93" return 1 end */ let chunk = Chunk { instructions: vec! [ Inst::VarArgPrep (0), Inst::GetTabUp (1, 0, 0), Inst::GetI (1, 1, 1), Inst::EqK (1, 1, 0), Inst::Jmp (6), Inst::GetTabUp (1, 0, 2), Inst::LoadK (2, 3), Inst::Call (1, 2, 1), Inst::LoadI (1, 0), Inst::Return (1, 2, 1), Inst::Jmp (5), Inst::GetTabUp (1, 0, 2), Inst::LoadK (2, 4), Inst::Call (1, 2, 1), Inst::LoadI (1, 1), Inst::Return (1, 2, 1), Inst::Return (1, 1, 1), ], constants: vec! [ "arg", "93", "print", "it's 93", "it's not 93", ].into_iter ().map (Value::from).collect (), }; for (arg, expected) in [ (vec! ["_exe_name"], vec! [1.into ()]), (vec! ["_exe_name", "93"], vec! [0.into ()]), (vec! ["_exe_name", "94"], vec! [1.into ()]), ] { let mut vm = State::default (); let upvalues = State::upvalues_from_args (arg.into_iter ().map (|s| s.to_string ())); let actual = vm.execute_chunk (&chunk, &upvalues); assert_eq! (actual, expected); } }