From f3d00b84bb65ae344a28e1bebfb5ce8b0efff8d1 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sat, 27 Feb 2021 22:15:25 +0200 Subject: chore(tazjin/rlox): Fill in minor missing implementations 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 Tested-by: BuildkiteCI --- users/tazjin/rlox/src/bytecode/compiler/mod.rs | 41 +++++++++++++------------- 1 file changed, 21 insertions(+), 20 deletions(-) (limited to 'users/tazjin') 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> Compiler { self.consume( &TokenKind::Eof, ErrorKind::ExpectedToken("Expected end of expression"), - )?; + ); self.end_compiler() } @@ -129,11 +129,13 @@ impl> Compiler { 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> Compiler { self.consume( &TokenKind::RightParen, ErrorKind::ExpectedToken("Expected ')' after expression"), - ) + ); + Ok(()) } fn unary(&mut self) -> LoxResult<()> { @@ -177,7 +180,7 @@ impl> Compiler { _ => unreachable!("only called for binary operator tokens"), } - unimplemented!() + Ok(()) } fn parse_precedence(&mut self, precedence: Precedence) -> LoxResult<()> { @@ -205,12 +208,13 @@ impl> Compiler { 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> Compiler { .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> { compiler.compile()?; if compiler.errors.is_empty() { - Ok(unimplemented!()) + Ok(compiler.chunk) } else { Err(compiler.errors) } -- cgit 1.4.1