diff options
-rw-r--r-- | users/tazjin/rlox/src/bytecode/chunk.rs | 2 | ||||
-rw-r--r-- | users/tazjin/rlox/src/bytecode/compiler.rs | 2 | ||||
-rw-r--r-- | users/tazjin/rlox/src/bytecode/errors.rs | 1 | ||||
-rw-r--r-- | users/tazjin/rlox/src/bytecode/tests.rs | 38 | ||||
-rw-r--r-- | users/tazjin/rlox/src/bytecode/value.rs | 7 | ||||
-rw-r--r-- | users/tazjin/rlox/src/bytecode/vm.rs | 34 |
6 files changed, 59 insertions, 25 deletions
diff --git a/users/tazjin/rlox/src/bytecode/chunk.rs b/users/tazjin/rlox/src/bytecode/chunk.rs index b2a7c7b6a2c1..7132be430a0f 100644 --- a/users/tazjin/rlox/src/bytecode/chunk.rs +++ b/users/tazjin/rlox/src/bytecode/chunk.rs @@ -49,7 +49,7 @@ impl Chunk { } } - fn get_line(&self, offset: usize) -> usize { + pub fn get_line(&self, offset: usize) -> usize { let mut pos = 0; for span in &self.lines { pos += span.count; diff --git a/users/tazjin/rlox/src/bytecode/compiler.rs b/users/tazjin/rlox/src/bytecode/compiler.rs index 4c6a99a181c6..63f34fad3ea7 100644 --- a/users/tazjin/rlox/src/bytecode/compiler.rs +++ b/users/tazjin/rlox/src/bytecode/compiler.rs @@ -128,7 +128,7 @@ impl<T: Iterator<Item = Token>> Compiler<T> { fn number(&mut self) -> LoxResult<()> { if let TokenKind::Number(num) = self.previous().kind { - self.emit_constant(num); + self.emit_constant(Value::Number(num)); return Ok(()); } diff --git a/users/tazjin/rlox/src/bytecode/errors.rs b/users/tazjin/rlox/src/bytecode/errors.rs index 4d6daff0f3f0..c6b86172f86d 100644 --- a/users/tazjin/rlox/src/bytecode/errors.rs +++ b/users/tazjin/rlox/src/bytecode/errors.rs @@ -8,6 +8,7 @@ pub enum ErrorKind { UnterminatedString, ExpectedToken(&'static str), InternalError(&'static str), + TypeError(String), } #[derive(Debug)] diff --git a/users/tazjin/rlox/src/bytecode/tests.rs b/users/tazjin/rlox/src/bytecode/tests.rs index 543d9cab515d..5a0901be6d79 100644 --- a/users/tazjin/rlox/src/bytecode/tests.rs +++ b/users/tazjin/rlox/src/bytecode/tests.rs @@ -2,47 +2,47 @@ use super::*; use crate::Lox; -fn expect(code: &str, value: value::Value) { +fn expect_num(code: &str, value: f64) { let result = Interpreter::create() .interpret(code.into()) .expect("evaluation failed"); - assert_eq!(result, value); + assert_eq!(result, value::Value::Number(value)); } #[test] fn numbers() { - expect("1", 1.0); - expect("13.37", 13.37); + expect_num("1", 1.0); + expect_num("13.37", 13.37); } #[test] fn negative_numbers() { // Note: This technically tests unary operators. - expect("-1", -1.0); - expect("-13.37", -13.37); + expect_num("-1", -1.0); + expect_num("-13.37", -13.37); } #[test] fn terms() { - expect("1 + 2", 3.0); - expect("3 - 1", 2.0); - expect("0.7 + 0.3", 1.0); - expect("1 + -3", -2.0); - expect("-1 - -1", 0.0); - expect("10 - -10 + 10", 30.0); + expect_num("1 + 2", 3.0); + expect_num("3 - 1", 2.0); + expect_num("0.7 + 0.3", 1.0); + expect_num("1 + -3", -2.0); + expect_num("-1 - -1", 0.0); + expect_num("10 - -10 + 10", 30.0); } #[test] fn factors() { - expect("1 * 2", 2.0); - expect("10 / 5", 2.0); - expect("0.7 * 4 / 1.4", 2.0); - expect("10 * -10 / 10", -10.0); + expect_num("1 * 2", 2.0); + expect_num("10 / 5", 2.0); + expect_num("0.7 * 4 / 1.4", 2.0); + expect_num("10 * -10 / 10", -10.0); } #[test] fn arithmetic() { - expect("10 - 3 * 2", 4.0); - expect("-4 * -4 + (14 - 5)", 25.0); - expect("(702 + 408) - ((239 - 734) / -5) + -4", 1007.0); + expect_num("10 - 3 * 2", 4.0); + expect_num("-4 * -4 + (14 - 5)", 25.0); + expect_num("(702 + 408) - ((239 - 734) / -5) + -4", 1007.0); } diff --git a/users/tazjin/rlox/src/bytecode/value.rs b/users/tazjin/rlox/src/bytecode/value.rs index 343d142ac830..88ff8a695138 100644 --- a/users/tazjin/rlox/src/bytecode/value.rs +++ b/users/tazjin/rlox/src/bytecode/value.rs @@ -1 +1,6 @@ -pub type Value = f64; +#[derive(Clone, Debug, PartialEq)] +pub enum Value { + Nil, + Bool(bool), + Number(f64), +} diff --git a/users/tazjin/rlox/src/bytecode/vm.rs b/users/tazjin/rlox/src/bytecode/vm.rs index 87c2aa440bf4..ee3abbd6cc18 100644 --- a/users/tazjin/rlox/src/bytecode/vm.rs +++ b/users/tazjin/rlox/src/bytecode/vm.rs @@ -23,11 +23,34 @@ impl VM { } } +macro_rules! with_type { + ( $self:ident, $val:ident, $type:pat, $body:expr ) => { + match $val { + $type => $body, + _ => { + return Err(Error { + line: $self.chunk.get_line($self.ip - 1), + kind: ErrorKind::TypeError(format!( + "Expected type {}, but found value: {:?}", + stringify!($type), + $val, + )), + }) + } + } + }; +} + macro_rules! binary_op { ( $vm:ident, $op:tt ) => {{ let b = $vm.pop(); let a = $vm.pop(); - $vm.push(a $op b); + + with_type!($vm, b, Value::Number(num_b), { + with_type!($vm, a, Value::Number(num_a), { + $vm.push(Value::Number(num_a $op num_b)) + }) + }) }} } @@ -45,13 +68,18 @@ impl VM { OpCode::OpReturn => return Ok(self.pop()), OpCode::OpConstant(idx) => { - let c = *self.chunk.constant(*idx); + let c = self.chunk.constant(*idx).clone(); self.push(c); } OpCode::OpNegate => { let v = self.pop(); - self.push(-v) + with_type!( + self, + v, + Value::Number(num), + self.push(Value::Number(-num)) + ); } OpCode::OpAdd => binary_op!(self, +), |