feat(tazjin/rlox): Parse & interpret while statements
Change-Id: Iee772274de95dfd6a6d4af973402859aeda17b1d Reviewed-on: https://cl.tvl.fyi/c/depot/+/2325 Reviewed-by: tazjin <mail@tazj.in> Tested-by: BuildkiteCI
This commit is contained in:
parent
0a0335ae6c
commit
d47e55ba4d
2 changed files with 40 additions and 0 deletions
|
|
@ -128,6 +128,7 @@ impl Interpreter {
|
|||
Statement::Var(var) => return self.interpret_var(var),
|
||||
Statement::Block(block) => return self.interpret_block(block),
|
||||
Statement::If(if_stmt) => return self.interpret_if(if_stmt),
|
||||
Statement::While(while_stmt) => return self.interpret_while(while_stmt),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
@ -172,6 +173,14 @@ impl Interpreter {
|
|||
}
|
||||
}
|
||||
|
||||
fn interpret_while<'a>(&mut self, stmt: &parser::While<'a>) -> Result<(), Error> {
|
||||
while eval_truthy(&self.eval(&stmt.condition)?) {
|
||||
self.interpret_stmt(&stmt.body)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn eval<'a>(&mut self, expr: &Expr<'a>) -> Result<Literal, Error> {
|
||||
match expr {
|
||||
Expr::Assign(assign) => self.eval_assign(assign),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue