From 8d80ebc05298028d692336f3bdb321aefd95a484 Mon Sep 17 00:00:00 2001 From: _ <_@_> Date: Tue, 26 Sep 2023 14:23:57 -0500 Subject: [PATCH] :star: add integers --- src/state.rs | 10 +++++++--- src/tests.rs | 4 ++-- test_vectors/hello.lua | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/state.rs b/src/state.rs index 2f9c713..5744f94 100644 --- a/src/state.rs +++ b/src/state.rs @@ -76,6 +76,7 @@ pub enum Value { Nil, Boolean (bool), Float (f64), + Integer (i64), String (Rc ), // These are all bogus, I haven't figured out how to implement @@ -107,7 +108,7 @@ impl From <&str> for Value { impl From for Value { fn from (x: i32) -> Self { - Self::Float (f64::try_from (x).unwrap ()) + Self::Integer (i64::try_from (x).unwrap ()) } } @@ -121,6 +122,8 @@ impl Value { fn as_float (&self) -> Option { match self { Self::Float (x) => Some (*x), + // FloatToInt isn't stable yet, so only ints in i32 space can practically be used for now + Self::Integer (x) => f64::try_from (i32::try_from (*x).ok ()?).ok (), _ => None, } } @@ -319,7 +322,8 @@ impl State { Value::Nil => println! ("nil"), Value::Boolean (false) => println! ("false"), Value::Boolean (true) => println! ("true"), - Value::Float (x) => println! ("{}", x), + Value::Float (x) => println! ("{:?}", x), + Value::Integer (x) => println! ("{}", x), Value::String (s) => println! ("{}", s), _ => unimplemented! (), }; @@ -419,7 +423,7 @@ impl State { Instruction::LoadI (a, sbx) => { let a = usize::try_from (*a).unwrap (); - self.register_window_mut ()[a] = (*sbx).into (); + self.register_window_mut ()[a] = Value::Integer (*sbx as i64); }, Instruction::LoadK (a, bx) => { let a = usize::try_from (*a).unwrap (); diff --git a/src/tests.rs b/src/tests.rs index db2a4ca..604a57f 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -91,8 +91,8 @@ fn closure () { for (arg, expected) in [ // Run the same test twice so clippy won't complain about a vec of 1 element - (vec! ["_exe_name"], vec! [23.into ()]), - (vec! ["_exe_name"], vec! [23.into ()]), + (vec! ["_exe_name"], vec! [23.0.into ()]), + (vec! ["_exe_name"], vec! [23.0.into ()]), ] { let mut vm = State::default (); let upvalues = State::upvalues_from_args (arg.into_iter ().map (|s| s.to_string ())); diff --git a/test_vectors/hello.lua b/test_vectors/hello.lua index 64c4446..8a91042 100644 --- a/test_vectors/hello.lua +++ b/test_vectors/hello.lua @@ -2,5 +2,5 @@ print (nil) print (false) print (true) print (1993) -print (1993.0) +print (1993.00) print "Hello."