about summary refs log tree commit diff
path: root/users/tazjin/rlox/src/bytecode/compiler.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/compiler.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/compiler.rs')
-rw-r--r--users/tazjin/rlox/src/bytecode/compiler.rs16
1 files changed, 13 insertions, 3 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));