🐛 bug: fix up printing to mostly match PUC Lua

The numbers are still off, since I didn't separate floats and ints yet
main
_ 2023-09-26 13:46:01 -05:00
parent 7b1fb49775
commit bc15781457
4 changed files with 33 additions and 2 deletions

View File

@ -58,7 +58,11 @@ pub fn parse_inst (buf: [u8; 4]) -> Option <Inst>
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),

View File

@ -26,5 +26,5 @@ fn main () {
program_counter: 0,
});
println! ("Returned: {:?}", vm.execute_chunk (&lua_file, &upvalues));
vm.execute_chunk (&lua_file, &upvalues);
}

View File

@ -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);

View File

@ -1 +1,6 @@
print (nil)
print (false)
print (true)
print (1993)
print (1993.0)
print "Hello."