feat(tazjin/rlox): Partial implementation of binary operators
Change-Id: I6f0bc9f58b51dec2673c918e08b199b52e793ed4 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2559 Reviewed-by: tazjin <mail@tazj.in> Tested-by: BuildkiteCI
This commit is contained in:
parent
1d3d9d32e3
commit
d2f24c925d
1 changed files with 49 additions and 1 deletions
|
@ -33,6 +33,27 @@ enum Precedence {
|
|||
Primary,
|
||||
}
|
||||
|
||||
impl Precedence {
|
||||
// Return the next highest precedence, if there is one.
|
||||
fn next(&self) -> Self {
|
||||
match self {
|
||||
Precedence::None => Precedence::Assignment,
|
||||
Precedence::Assignment => Precedence::Or,
|
||||
Precedence::Or => Precedence::And,
|
||||
Precedence::And => Precedence::Equality,
|
||||
Precedence::Equality => Precedence::Comparison,
|
||||
Precedence::Comparison => Precedence::Term,
|
||||
Precedence::Term => Precedence::Factor,
|
||||
Precedence::Factor => Precedence::Unary,
|
||||
Precedence::Unary => Precedence::Call,
|
||||
Precedence::Call => Precedence::Primary,
|
||||
Precedence::Primary => panic!(
|
||||
"invalid parser state: no higher precedence than Primary"
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Iterator<Item = Token>> Compiler<T> {
|
||||
fn compile(&mut self) -> LoxResult<()> {
|
||||
self.advance();
|
||||
|
@ -68,7 +89,10 @@ impl<T: Iterator<Item = Token>> Compiler<T> {
|
|||
)
|
||||
}
|
||||
|
||||
fn unary(&mut self, kind: &TokenKind) -> LoxResult<()> {
|
||||
fn unary(&mut self) -> LoxResult<()> {
|
||||
// TODO(tazjin): Avoid clone
|
||||
let kind = self.previous().kind.clone();
|
||||
|
||||
// Compile the operand
|
||||
self.parse_precedence(Precedence::Unary)?;
|
||||
|
||||
|
@ -81,6 +105,30 @@ impl<T: Iterator<Item = Token>> Compiler<T> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn binary(&mut self) -> LoxResult<()> {
|
||||
// Remember the operator
|
||||
let operator = self.previous().kind.clone();
|
||||
|
||||
// Compile the right operand
|
||||
let rule = self.get_rule(&operator);
|
||||
self.parse_precedence(unimplemented!("rule.precendece.next()"))?;
|
||||
|
||||
// Emit operator instruction
|
||||
match operator {
|
||||
TokenKind::Minus => self.emit_op(OpCode::OpSubtract),
|
||||
TokenKind::Plus => self.emit_op(OpCode::OpAdd),
|
||||
TokenKind::Star => self.emit_op(OpCode::OpMultiply),
|
||||
TokenKind::Slash => self.emit_op(OpCode::OpDivide),
|
||||
_ => unreachable!("only called for binary operator tokens"),
|
||||
}
|
||||
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
fn get_rule(&mut self, op: &TokenKind) -> usize {
|
||||
unimplemented!();
|
||||
}
|
||||
|
||||
fn parse_precedence(&mut self, precedence: Precedence) -> LoxResult<()> {
|
||||
unimplemented!("what goes here?")
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue