From b13a6736ddf87e9689a207d5980dd28b9aa83dd4 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sat, 27 Feb 2021 15:45:44 +0200 Subject: chore(tazjin/rlox): Set up some scaffolding for panic mode This lets us suppress reporting of additional errors from the compiler until a synchronisation point is reached. Change-Id: Iacf90949f868fbdb4349750065b5e458cf74d32a Reviewed-on: https://cl.tvl.fyi/c/depot/+/2557 Reviewed-by: tazjin Tested-by: BuildkiteCI --- users/tazjin/rlox/src/bytecode/compiler.rs | 27 ++++++++++++++++++++++----- users/tazjin/rlox/src/bytecode/mod.rs | 5 ++++- 2 files changed, 26 insertions(+), 6 deletions(-) (limited to 'users/tazjin') diff --git a/users/tazjin/rlox/src/bytecode/compiler.rs b/users/tazjin/rlox/src/bytecode/compiler.rs index 0197885a52d2..ab5a970ffd12 100644 --- a/users/tazjin/rlox/src/bytecode/compiler.rs +++ b/users/tazjin/rlox/src/bytecode/compiler.rs @@ -4,10 +4,10 @@ use super::opcode::OpCode; use crate::scanner; struct Compiler> { - // panic: bool, - errors: Vec, tokens: T, chunk: Chunk, + panic: bool, + errors: Vec, // TODO(tazjin): Restructure so that these don't need to be Option? current: Option, @@ -48,16 +48,32 @@ impl> Compiler { } fn end_compiler(&mut self) -> LoxResult<()> { - let line = self.previous().line; - self.current_chunk().add_op(OpCode::OpReturn, line); + self.emit_op(OpCode::OpReturn); Ok(()) } + fn emit_op(&mut self, op: OpCode) { + let line = self.previous().line; + self.current_chunk().add_op(op, line); + } + fn previous(&self) -> &scanner::Token { self.previous .as_ref() .expect("invalid internal compiler state: missing previous token") } + + fn error_at(&mut self, token: &scanner::Token, kind: ErrorKind) { + if self.panic { + return; + } + + self.panic = true; + self.errors.push(Error { + kind, + line: token.line, + }) + } } pub fn compile(code: &str) -> Result> { @@ -68,10 +84,11 @@ pub fn compile(code: &str) -> Result> { let mut compiler = Compiler { tokens: tokens.into_iter().peekable(), + chunk: Default::default(), + panic: false, errors: vec![], current: None, previous: None, - chunk: Default::default(), }; compiler.compile()?; diff --git a/users/tazjin/rlox/src/bytecode/mod.rs b/users/tazjin/rlox/src/bytecode/mod.rs index 362d8bb1252f..97316e66aa05 100644 --- a/users/tazjin/rlox/src/bytecode/mod.rs +++ b/users/tazjin/rlox/src/bytecode/mod.rs @@ -20,7 +20,10 @@ impl crate::Lox for Interpreter { Interpreter {} } - fn interpret(&mut self, code: String) -> Result> { + fn interpret( + &mut self, + code: String, + ) -> Result> { let chunk = compiler::compile(&code)?; vm::interpret(chunk).map_err(|e| vec![e]) } -- cgit 1.4.1