about summary refs log tree commit diff
path: root/users/tazjin
diff options
context:
space:
mode:
Diffstat (limited to 'users/tazjin')
-rw-r--r--users/tazjin/rlox/src/bytecode/compiler.rs16
-rw-r--r--users/tazjin/rlox/src/bytecode/opcode.rs1
-rw-r--r--users/tazjin/rlox/src/bytecode/tests.rs92
-rw-r--r--users/tazjin/rlox/src/bytecode/vm.rs22
4 files changed, 78 insertions, 53 deletions
diff --git a/users/tazjin/rlox/src/bytecode/compiler.rs b/users/tazjin/rlox/src/bytecode/compiler.rs
index 1408f519bb2d..9a8f80ae2b7d 100644
--- a/users/tazjin/rlox/src/bytecode/compiler.rs
+++ b/users/tazjin/rlox/src/bytecode/compiler.rs
@@ -179,10 +179,10 @@ impl<T: Iterator<Item = Token>> Compiler<T> {
 
     fn statement(&mut self) -> LoxResult<()> {
         if self.match_token(&TokenKind::Print) {
-            return self.print_statement();
+            self.print_statement()
+        } else {
+            self.expression_statement()
         }
-
-        Ok(())
     }
 
     fn print_statement(&mut self) -> LoxResult<()> {
@@ -195,6 +195,16 @@ impl<T: Iterator<Item = Token>> Compiler<T> {
         Ok(())
     }
 
+    fn expression_statement(&mut self) -> LoxResult<()> {
+        self.expression()?;
+        self.consume(
+            &TokenKind::Semicolon,
+            ErrorKind::ExpectedToken("Expected ';' after expression"),
+        );
+        self.emit_op(OpCode::OpPop);
+        Ok(())
+    }
+
     fn number(&mut self) -> LoxResult<()> {
         if let TokenKind::Number(num) = self.previous().kind {
             self.emit_constant(Value::Number(num));
diff --git a/users/tazjin/rlox/src/bytecode/opcode.rs b/users/tazjin/rlox/src/bytecode/opcode.rs
index 7a3355460473..37466b30564c 100644
--- a/users/tazjin/rlox/src/bytecode/opcode.rs
+++ b/users/tazjin/rlox/src/bytecode/opcode.rs
@@ -28,4 +28,5 @@ pub enum OpCode {
 
     // Built in operations
     OpPrint,
+    OpPop,
 }
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");
 }
diff --git a/users/tazjin/rlox/src/bytecode/vm.rs b/users/tazjin/rlox/src/bytecode/vm.rs
index c43b197279be..9161bdfe53b8 100644
--- a/users/tazjin/rlox/src/bytecode/vm.rs
+++ b/users/tazjin/rlox/src/bytecode/vm.rs
@@ -13,6 +13,12 @@ pub struct VM {
 
     stack: Vec<Value>,
     strings: Interner,
+
+    // Operations that consume values from the stack without pushing
+    // anything leave their last value in this slot, which makes it
+    // possible to return values from interpreters that ran code which
+    // ended with a statement.
+    last_drop: Option<Value>,
 }
 
 impl VM {
@@ -72,12 +78,15 @@ impl VM {
 
             match op {
                 OpCode::OpReturn => {
-                    if self.stack.is_empty() {
+                    if !self.stack.is_empty() {
+                        let val = self.pop();
+                        return Ok(self.return_value(val));
+                    } else if self.last_drop.is_some() {
+                        let val = self.last_drop.take().unwrap();
+                        return Ok(self.return_value(val));
+                    } else {
                         return Ok(Value::Nil);
                     }
-
-                    let val = self.pop();
-                    return Ok(self.return_value(val));
                 }
 
                 OpCode::OpConstant(idx) => {
@@ -144,6 +153,10 @@ impl VM {
                     let val = self.pop();
                     println!("{}", self.print_value(val));
                 }
+
+                OpCode::OpPop => {
+                    self.last_drop = Some(self.pop());
+                }
             }
 
             #[cfg(feature = "disassemble")]
@@ -186,6 +199,7 @@ pub fn interpret(strings: Interner, chunk: chunk::Chunk) -> LoxResult<Value> {
         strings,
         ip: 0,
         stack: vec![],
+        last_drop: None,
     };
 
     vm.run()