about summary refs log tree commit diff
path: root/users
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2021-02-27T20·15+0200
committertazjin <mail@tazj.in>2021-02-27T20·19+0000
commitf3d00b84bb65ae344a28e1bebfb5ce8b0efff8d1 (patch)
tree22529233539a1fced1333d3f55e608b5f8366037 /users
parent758730d25db055ff28f7b1f76d59b69d85047795 (diff)
chore(tazjin/rlox): Fill in minor missing implementations r/2244
This should clean up everything in the way of actually running this
end-to-end.

Change-Id: Ie89d82472a458256a251a4fddc1c36d88d21f5f2
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2563
Reviewed-by: tazjin <mail@tazj.in>
Tested-by: BuildkiteCI
Diffstat (limited to 'users')
-rw-r--r--users/tazjin/rlox/src/bytecode/compiler/mod.rs41
1 files changed, 21 insertions, 20 deletions
diff --git a/users/tazjin/rlox/src/bytecode/compiler/mod.rs b/users/tazjin/rlox/src/bytecode/compiler/mod.rs
index 547c1ab18fd7..5de8fd9f6f06 100644
--- a/users/tazjin/rlox/src/bytecode/compiler/mod.rs
+++ b/users/tazjin/rlox/src/bytecode/compiler/mod.rs
@@ -1,4 +1,4 @@
-use super::chunk::Chunk;
+use super::chunk::{self, Chunk};
 use super::errors::{Error, ErrorKind, LoxResult};
 use super::opcode::OpCode;
 use super::value::Value;
@@ -115,7 +115,7 @@ impl<T: Iterator<Item = Token>> Compiler<T> {
         self.consume(
             &TokenKind::Eof,
             ErrorKind::ExpectedToken("Expected end of expression"),
-        )?;
+        );
 
         self.end_compiler()
     }
@@ -129,11 +129,13 @@ impl<T: Iterator<Item = Token>> Compiler<T> {
         self.parse_precedence(Precedence::Assignment)
     }
 
-    // TODO(tazjin): Assumption is that we have access to the previous
-    // token wherever this ends up invoked. True?
     fn number(&mut self) -> LoxResult<()> {
-        let num = unimplemented!("get out of previous()");
-        self.emit_constant(num);
+        if let TokenKind::Number(num) = self.previous().kind {
+            self.emit_constant(num);
+            return Ok(());
+        }
+
+        unreachable!("internal parser error: entered number() incorrectly")
     }
 
     fn grouping(&mut self) -> LoxResult<()> {
@@ -141,7 +143,8 @@ impl<T: Iterator<Item = Token>> Compiler<T> {
         self.consume(
             &TokenKind::RightParen,
             ErrorKind::ExpectedToken("Expected ')' after expression"),
-        )
+        );
+        Ok(())
     }
 
     fn unary(&mut self) -> LoxResult<()> {
@@ -177,7 +180,7 @@ impl<T: Iterator<Item = Token>> Compiler<T> {
             _ => unreachable!("only called for binary operator tokens"),
         }
 
-        unimplemented!()
+        Ok(())
     }
 
     fn parse_precedence(&mut self, precedence: Precedence) -> LoxResult<()> {
@@ -205,12 +208,13 @@ impl<T: Iterator<Item = Token>> Compiler<T> {
         Ok(())
     }
 
-    fn consume(
-        &mut self,
-        expected: &TokenKind,
-        err: ErrorKind,
-    ) -> LoxResult<()> {
-        unimplemented!()
+    fn consume(&mut self, expected: &TokenKind, err: ErrorKind) {
+        if (self.current().kind == *expected) {
+            self.advance();
+            return;
+        }
+
+        self.error_at(self.current().line, err);
     }
 
     fn current_chunk(&mut self) -> &mut Chunk {
@@ -248,16 +252,13 @@ impl<T: Iterator<Item = Token>> Compiler<T> {
             .expect("invalid internal compiler state: missing current token")
     }
 
-    fn error_at(&mut self, token: &Token, kind: ErrorKind) {
+    fn error_at(&mut self, line: usize, kind: ErrorKind) {
         if self.panic {
             return;
         }
 
         self.panic = true;
-        self.errors.push(Error {
-            kind,
-            line: token.line,
-        })
+        self.errors.push(Error { kind, line })
     }
 }
 
@@ -279,7 +280,7 @@ pub fn compile(code: &str) -> Result<Chunk, Vec<Error>> {
     compiler.compile()?;
 
     if compiler.errors.is_empty() {
-        Ok(unimplemented!())
+        Ok(compiler.chunk)
     } else {
         Err(compiler.errors)
     }