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·15+0200
committertazjin <mail@tazj.in>2021-03-03T10·50+0000
commit2cd77ea26d76b20ff820f1ebe5e77f1360f5d1f5 (patch)
treeaada926332ef86b2252f1b3ebcfc6d2c768ce40d /users/tazjin/rlox/src/bytecode/compiler.rs
parent432e7a7dddf56224297285c9f47f0aa3963eb5b5 (diff)
feat(tazjin/rlox): Add support for print statement r/2264
Change-Id: Ic3e7e722325c8784b848c0bcd573c2e51e123c40
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2583
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.rs44
1 files changed, 39 insertions, 5 deletions
diff --git a/users/tazjin/rlox/src/bytecode/compiler.rs b/users/tazjin/rlox/src/bytecode/compiler.rs
index ae38654c5d38..1408f519bb2d 100644
--- a/users/tazjin/rlox/src/bytecode/compiler.rs
+++ b/users/tazjin/rlox/src/bytecode/compiler.rs
@@ -156,11 +156,10 @@ fn rule_for<T: Iterator<Item = Token>>(token: &TokenKind) -> ParseRule<T> {
 impl<T: Iterator<Item = Token>> Compiler<T> {
     fn compile(&mut self) -> LoxResult<()> {
         self.advance();
-        self.expression()?;
-        self.consume(
-            &TokenKind::Eof,
-            ErrorKind::ExpectedToken("Expected end of expression"),
-        );
+
+        while !self.match_token(&TokenKind::Eof) {
+            self.declaration()?;
+        }
 
         self.end_compiler()
     }
@@ -174,6 +173,28 @@ impl<T: Iterator<Item = Token>> Compiler<T> {
         self.parse_precedence(Precedence::Assignment)
     }
 
+    fn declaration(&mut self) -> LoxResult<()> {
+        self.statement()
+    }
+
+    fn statement(&mut self) -> LoxResult<()> {
+        if self.match_token(&TokenKind::Print) {
+            return self.print_statement();
+        }
+
+        Ok(())
+    }
+
+    fn print_statement(&mut self) -> LoxResult<()> {
+        self.expression()?;
+        self.consume(
+            &TokenKind::Semicolon,
+            ErrorKind::ExpectedToken("Expected ';' after value"),
+        );
+        self.emit_op(OpCode::OpPrint);
+        Ok(())
+    }
+
     fn number(&mut self) -> LoxResult<()> {
         if let TokenKind::Number(num) = self.previous().kind {
             self.emit_constant(Value::Number(num));
@@ -352,6 +373,19 @@ impl<T: Iterator<Item = Token>> Compiler<T> {
         self.panic = true;
         self.errors.push(Error { kind, line })
     }
+
+    fn match_token(&mut self, token: &TokenKind) -> bool {
+        if !self.check(token) {
+            return false;
+        }
+
+        self.advance();
+        true
+    }
+
+    fn check(&self, token: &TokenKind) -> bool {
+        return self.current().kind == *token;
+    }
 }
 
 pub fn compile(code: &str) -> Result<(Interner, Chunk), Vec<Error>> {