From bc157814570adf30189cb5c12d42e4016ee3f3e9 Mon Sep 17 00:00:00 2001 From: _ <_@_> Date: Tue, 26 Sep 2023 13:46:01 -0500 Subject: [PATCH] :bug: bug: fix up printing to mostly match PUC Lua The numbers are still off, since I didn't separate floats and ints yet --- src/loader.rs | 4 ++++ src/main.rs | 2 +- src/state.rs | 24 +++++++++++++++++++++++- test_vectors/hello.lua | 5 +++++ 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/loader.rs b/src/loader.rs index 471203d..a761dd2 100644 --- a/src/loader.rs +++ b/src/loader.rs @@ -58,7 +58,11 @@ pub fn parse_inst (buf: [u8; 4]) -> Option Some (match opcode { 0x00 => Inst::Move (a, b), 0x01 => Inst::LoadI (a, sbx), + 0x02 => Inst::LoadF (a, sbx), 0x03 => Inst::LoadK (a, bx), + 0x05 => Inst::LoadFalse (a), + 0x07 => Inst::LoadTrue (a), + 0x08 => Inst::LoadNil (a), 0x09 => Inst::GetUpVal (a, b), 0x0b => Inst::GetTabUp (a, b, c), 0x0d => Inst::GetI (a, b, c), diff --git a/src/main.rs b/src/main.rs index deadf44..703664d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,5 +26,5 @@ fn main () { program_counter: 0, }); - println! ("Returned: {:?}", vm.execute_chunk (&lua_file, &upvalues)); + vm.execute_chunk (&lua_file, &upvalues); } diff --git a/src/state.rs b/src/state.rs index 86cefa4..b48e71a 100644 --- a/src/state.rs +++ b/src/state.rs @@ -21,6 +21,9 @@ pub enum Instruction { // Jump Jmp (i32), + // Load F (Float?) + LoadF (u8, i32), + LoadFalse (u8), // Load Integer? @@ -29,6 +32,8 @@ pub enum Instruction { // Load Constant LoadK (u8, i32), + LoadNil (u8), + LoadTrue (u8), // MetaMethod, Binary @@ -304,7 +309,16 @@ impl State { // assert_eq! (*b, 2); assert_eq! (*c, 1); - println! ("{:?}", r.get (a + 1).unwrap ()); + let value = r.get (a + 1).unwrap (); + let s = match value { + Value::Nil => println! ("nil"), + Value::Boolean (false) => println! ("false"), + Value::Boolean (true) => println! ("true"), + Value::Float (x) => println! ("{}", x), + Value::String (s) => println! ("{}", s), + _ => unimplemented! (), + }; + r [a] = r [a + 1].take (); }, _ => { @@ -389,6 +403,10 @@ impl State { self.register_window_mut ()[a] = upvalues [b].clone (); }, Instruction::Jmp (s_j) => next_pc += s_j, + Instruction::LoadF (a, sbx) => { + let a = usize::try_from (*a).unwrap (); + self.register_window_mut ()[a] = Value::Float (*sbx as f64); + } Instruction::LoadFalse (a) => { let a = usize::try_from (*a).unwrap (); self.register_window_mut ()[a] = Value::Boolean (false); @@ -404,6 +422,10 @@ impl State { self.register_window_mut ()[a] = k [bx].clone (); }, + Instruction::LoadNil (a) => { + let a = usize::try_from (*a).unwrap (); + self.register_window_mut ()[a] = Value::Nil; + }, Instruction::LoadTrue (a) => { let a = usize::try_from (*a).unwrap (); self.register_window_mut ()[a] = Value::Boolean (true); diff --git a/test_vectors/hello.lua b/test_vectors/hello.lua index dcfbcfc..64c4446 100644 --- a/test_vectors/hello.lua +++ b/test_vectors/hello.lua @@ -1 +1,6 @@ +print (nil) +print (false) +print (true) +print (1993) +print (1993.0) print "Hello."