feat(tazjin/rlox): Implement parser precedence rules

Change-Id: Idcf3c84126603086bbf7e8d54773bccb3ae3b5ab
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2561
Reviewed-by: tazjin <mail@tazj.in>
Tested-by: BuildkiteCI
This commit is contained in:
Vincent Ambo 2021-02-27 21:51:34 +02:00 committed by tazjin
parent 7778a58f8d
commit 56f6a6e9f2

View file

@ -183,12 +183,26 @@ impl<T: Iterator<Item = Token>> Compiler<T> {
fn parse_precedence(&mut self, precedence: Precedence) -> LoxResult<()> { fn parse_precedence(&mut self, precedence: Precedence) -> LoxResult<()> {
self.advance(); self.advance();
let rule: ParseRule<T> = rule_for(&self.previous().kind); let rule: ParseRule<T> = 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 { prefix_fn(self)?;
func(self)?;
while precedence <= rule_for::<T>(&self.current().kind).precedence {
self.advance();
match rule_for::<T>(&self.previous().kind).infix {
Some(func) => {
func(self)?;
}
None => {
unreachable!("invalid compiler state: error in parse rules")
}
}
} }
unimplemented!("expect expression?") Ok(())
} }
fn consume( fn consume(
@ -224,6 +238,12 @@ impl<T: Iterator<Item = Token>> Compiler<T> {
.expect("invalid internal compiler state: missing previous token") .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) { fn error_at(&mut self, token: &Token, kind: ErrorKind) {
if self.panic { if self.panic {
return; return;