From 56f6a6e9f20236cb97d2e6bc2a7819762ecb0fe7 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sat, 27 Feb 2021 21:51:34 +0200 Subject: [PATCH] feat(tazjin/rlox): Implement parser precedence rules Change-Id: Idcf3c84126603086bbf7e8d54773bccb3ae3b5ab Reviewed-on: https://cl.tvl.fyi/c/depot/+/2561 Reviewed-by: tazjin Tested-by: BuildkiteCI --- .../tazjin/rlox/src/bytecode/compiler/mod.rs | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/users/tazjin/rlox/src/bytecode/compiler/mod.rs b/users/tazjin/rlox/src/bytecode/compiler/mod.rs index fab06fe39..58a5c1b98 100644 --- a/users/tazjin/rlox/src/bytecode/compiler/mod.rs +++ b/users/tazjin/rlox/src/bytecode/compiler/mod.rs @@ -183,12 +183,26 @@ impl> Compiler { fn parse_precedence(&mut self, precedence: Precedence) -> LoxResult<()> { self.advance(); let rule: ParseRule = rule_for(&self.previous().kind); + let prefix_fn = match rule.prefix { + None => unimplemented!("expected expression or something, unclear"), + Some(func) => func, + }; - if let Some(func) = rule.prefix { - func(self)?; + prefix_fn(self)?; + + while precedence <= rule_for::(&self.current().kind).precedence { + self.advance(); + match rule_for::(&self.previous().kind).infix { + Some(func) => { + func(self)?; + } + None => { + unreachable!("invalid compiler state: error in parse rules") + } + } } - unimplemented!("expect expression?") + Ok(()) } fn consume( @@ -224,6 +238,12 @@ impl> Compiler { .expect("invalid internal compiler state: missing previous token") } + fn current(&self) -> &Token { + self.current + .as_ref() + .expect("invalid internal compiler state: missing current token") + } + fn error_at(&mut self, token: &Token, kind: ErrorKind) { if self.panic { return;