add integers

main
_ 2023-09-26 14:23:57 -05:00
parent e87265373c
commit 8d80ebc052
3 changed files with 10 additions and 6 deletions

View File

@ -76,6 +76,7 @@ pub enum Value {
Nil,
Boolean (bool),
Float (f64),
Integer (i64),
String (Rc <String>),
// These are all bogus, I haven't figured out how to implement
@ -107,7 +108,7 @@ impl From <&str> for Value {
impl From <i32> 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 <f64> {
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 ();

View File

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

View File

@ -2,5 +2,5 @@ print (nil)
print (false)
print (true)
print (1993)
print (1993.0)
print (1993.00)
print "Hello."