about summary refs log tree commit diff
path: root/users/tazjin/rlox/src/bytecode/tests.rs
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2021-03-02T20·26+0200
committertazjin <mail@tazj.in>2021-03-03T10·51+0000
commited3fce2b19fa0d28054382093b019967a9a16177 (patch)
tree102ade5adc95667ebedaa56e20cee387c2ff524b /users/tazjin/rlox/src/bytecode/tests.rs
parent2cd77ea26d76b20ff820f1ebe5e77f1360f5d1f5 (diff)
feat(tazjin/rlox): Implement expression statements r/2265
These aren't particularly useful without side effects, but one step at
a time.

This diverges slightly from the book, in that OpPop retains the last
value it "forgot" from the stack in a special field on the
interpreter.

This makes it possible to return values from expression statements,
which helps in cases where Lox is embedded as a scripting
language (please don't do this ever) or in tests.

Change-Id: Ided0bc04c6e80ddb23ba4693d61ac9e08b002d58
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2584
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.rs92
1 files changed, 46 insertions, 46 deletions
diff --git a/users/tazjin/rlox/src/bytecode/tests.rs b/users/tazjin/rlox/src/bytecode/tests.rs
index b29769f460a1..b346da19810d 100644
--- a/users/tazjin/rlox/src/bytecode/tests.rs
+++ b/users/tazjin/rlox/src/bytecode/tests.rs
@@ -24,87 +24,87 @@ fn expect_str(code: &str, value: &str) {
 
 #[test]
 fn numbers() {
-    expect_num("1", 1.0);
-    expect_num("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_num("-1", -1.0);
-    expect_num("-13.37", -13.37);
+    expect_num("-1;", -1.0);
+    expect_num("-13.37;", -13.37);
 }
 
 #[test]
 fn terms() {
-    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);
+    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_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);
+    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_num("10 - 3 * 2", 4.0);
-    expect_num("-4 * -4 + (14 - 5)", 25.0);
-    expect_num("(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);
 }
 
 #[test]
 fn trivial_literals() {
-    expect("true", Value::Bool(true));
-    expect("false", Value::Bool(false));
-    expect("nil", Value::Nil);
+    expect("true;", Value::Bool(true));
+    expect("false;", Value::Bool(false));
+    expect("nil;", Value::Nil);
 }
 
 #[test]
 fn negation() {
-    expect_bool("!true", false);
-    expect_bool("!false", true);
-    expect_bool("!nil", true);
-    expect_bool("!13.5", false);
-    expect_bool("!-42", false);
+    expect_bool("!true;", false);
+    expect_bool("!false;", true);
+    expect_bool("!nil;", true);
+    expect_bool("!13.5;", false);
+    expect_bool("!-42;", false);
 }
 
 #[test]
 fn equality() {
-    expect_bool("42 == 42", true);
-    expect_bool("42 != 42", false);
-    expect_bool("42 == 42.0", true);
-
-    expect_bool("true == true", true);
-    expect_bool("true == false", false);
-    expect_bool("true == !false", true);
-    expect_bool("true != true", false);
-    expect_bool("true != false", true);
-
-    expect_bool("42 == false", false);
-    expect_bool("42 == true", false);
-    expect_bool("!42 == !true", true);
+    expect_bool("42 == 42;", true);
+    expect_bool("42 != 42;", false);
+    expect_bool("42 == 42.0;", true);
+
+    expect_bool("true == true;", true);
+    expect_bool("true == false;", false);
+    expect_bool("true == !false;", true);
+    expect_bool("true != true;", false);
+    expect_bool("true != false;", true);
+
+    expect_bool("42 == false;", false);
+    expect_bool("42 == true;", false);
+    expect_bool("!42 == !true;", true);
 }
 
 #[test]
 fn comparisons() {
-    expect_bool("42 > 23", true);
-    expect_bool("42 < 23", false);
-    expect_bool("42 <= 42", true);
-    expect_bool("42 <= 23", false);
-    expect_bool("42 >= 42", true);
-    expect_bool("42 >= 23", true);
+    expect_bool("42 > 23;", true);
+    expect_bool("42 < 23;", false);
+    expect_bool("42 <= 42;", true);
+    expect_bool("42 <= 23;", false);
+    expect_bool("42 >= 42;", true);
+    expect_bool("42 >= 23;", true);
 }
 
 #[test]
 fn strings() {
-    expect_str("\"hello\"", "hello");
-    expect_str("\"hello\" + \" world\"", "hello world");
+    expect_str("\"hello\";", "hello");
+    expect_str("\"hello\" + \" world\";", "hello world");
 }