From 822e5ae57f8253e83d40b6a8d686d774b1500a3f Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Wed, 3 Mar 2021 13:08:40 +0200 Subject: fix(tazjin/rlox): Resynchronise after panicking Change-Id: I60939f7a2c523b6ca1e9782e58c97959da38cfff Reviewed-on: https://cl.tvl.fyi/c/depot/+/2591 Reviewed-by: tazjin Tested-by: BuildkiteCI --- users/tazjin/rlox/src/bytecode/compiler.rs | 33 +++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) (limited to 'users/tazjin') diff --git a/users/tazjin/rlox/src/bytecode/compiler.rs b/users/tazjin/rlox/src/bytecode/compiler.rs index 9a8f80ae2b7d..8e70067e3182 100644 --- a/users/tazjin/rlox/src/bytecode/compiler.rs +++ b/users/tazjin/rlox/src/bytecode/compiler.rs @@ -174,7 +174,13 @@ impl> Compiler { } fn declaration(&mut self) -> LoxResult<()> { - self.statement() + self.statement()?; + + if self.panic { + self.synchronise(); + } + + Ok(()) } fn statement(&mut self) -> LoxResult<()> { @@ -396,6 +402,31 @@ impl> Compiler { fn check(&self, token: &TokenKind) -> bool { return self.current().kind == *token; } + + fn synchronise(&mut self) { + self.panic = false; + + while self.current().kind != TokenKind::Eof { + if self.previous().kind == TokenKind::Semicolon { + return; + } + + match self.current().kind { + TokenKind::Class + | TokenKind::Fun + | TokenKind::Var + | TokenKind::For + | TokenKind::If + | TokenKind::While + | TokenKind::Print + | TokenKind::Return => return, + + _ => { + self.advance(); + } + } + } + } } pub fn compile(code: &str) -> Result<(Interner, Chunk), Vec> { -- cgit 1.4.1