diff options
author | Vincent Ambo <mail@tazj.in> | 2021-02-28T12·37+0200 |
---|---|---|
committer | tazjin <mail@tazj.in> | 2021-02-28T12·54+0000 |
commit | 127ef984865500d70176347861b2e8bad29a39be (patch) | |
tree | cb3dc7b380cd58d3c72af1ab0ac65a35b9896bc1 /users/tazjin/rlox/src/bytecode/tests.rs | |
parent | 6b990a757186da0f2766fefac53ce5de140d9174 (diff) |
refactor(tazjin/rlox): Represent VM values as enums r/2251
Introduces a new enum which represents the different types of possible values, and modifies the rest of the existing code to wrap/unwrap these enum variants correctly. Notably in the vm module, a new macro has been introduced that makes it possible to encode a type expectation and return a runtime error in case of a type mismatch. Change-Id: I325b5e31e395c62d8819ab2af6d398e1277333c0 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2570 Reviewed-by: tazjin <mail@tazj.in> Tested-by: BuildkiteCI
Diffstat (limited to 'users/tazjin/rlox/src/bytecode/tests.rs')
-rw-r--r-- | users/tazjin/rlox/src/bytecode/tests.rs | 38 |
1 files changed, 19 insertions, 19 deletions
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); } |