about summary refs log tree commit diff
path: root/users/tazjin/rlox/src/bytecode
diff options
context:
space:
mode:
Diffstat (limited to 'users/tazjin/rlox/src/bytecode')
-rw-r--r--users/tazjin/rlox/src/bytecode/compiler.rs44
-rw-r--r--users/tazjin/rlox/src/bytecode/opcode.rs3
-rw-r--r--users/tazjin/rlox/src/bytecode/vm.rs19
3 files changed, 61 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>> {
diff --git a/users/tazjin/rlox/src/bytecode/opcode.rs b/users/tazjin/rlox/src/bytecode/opcode.rs
index 78c1b2d8e3a5..7a3355460473 100644
--- a/users/tazjin/rlox/src/bytecode/opcode.rs
+++ b/users/tazjin/rlox/src/bytecode/opcode.rs
@@ -25,4 +25,7 @@ pub enum OpCode {
     OpSubtract,
     OpMultiply,
     OpDivide,
+
+    // Built in operations
+    OpPrint,
 }
diff --git a/users/tazjin/rlox/src/bytecode/vm.rs b/users/tazjin/rlox/src/bytecode/vm.rs
index 02db30de58a9..c43b197279be 100644
--- a/users/tazjin/rlox/src/bytecode/vm.rs
+++ b/users/tazjin/rlox/src/bytecode/vm.rs
@@ -72,6 +72,10 @@ impl VM {
 
             match op {
                 OpCode::OpReturn => {
+                    if self.stack.is_empty() {
+                        return Ok(Value::Nil);
+                    }
+
                     let val = self.pop();
                     return Ok(self.return_value(val));
                 }
@@ -135,6 +139,11 @@ impl VM {
                         })
                     }
                 }
+
+                OpCode::OpPrint => {
+                    let val = self.pop();
+                    println!("{}", self.print_value(val));
+                }
             }
 
             #[cfg(feature = "disassemble")]
@@ -159,6 +168,16 @@ impl VM {
             LoxString::Interned(id) => self.strings.lookup(*id),
         }
     }
+
+    fn print_value(&self, val: Value) -> String {
+        match val {
+            Value::String(LoxString::Heap(s)) => s,
+            Value::String(LoxString::Interned(id)) => {
+                self.strings.lookup(id).into()
+            }
+            _ => format!("{:?}", val),
+        }
+    }
 }
 
 pub fn interpret(strings: Interner, chunk: chunk::Chunk) -> LoxResult<Value> {