Interpreter - While loops

This commit is contained in:
Emmanuel BENOîT 2023-01-01 18:44:35 +01:00
parent daec88c2f8
commit ddf6625326

View file

@ -56,6 +56,9 @@ impl Interpretable for ast::StmtNode {
then_branch, then_branch,
else_branch, else_branch,
} => self.on_if_statement(environment, condition, then_branch, else_branch), } => self.on_if_statement(environment, condition, then_branch, else_branch),
ast::StmtNode::WhileStmt { condition, body } => {
self.on_while_statement(environment, condition, body)
}
} }
} }
} }
@ -119,6 +122,19 @@ impl ast::StmtNode {
Ok(Value::Nil) Ok(Value::Nil)
} }
} }
/// Execute a while statement.
fn on_while_statement(
&self,
environment: &EnvironmentRef,
condition: &ast::ExprNode,
body: &ast::StmtNode,
) -> InterpreterResult {
while condition.interprete(environment)?.is_truthy() {
body.interprete(environment)?;
}
Ok(Value::Nil)
}
} }
/* -------------------------------- * /* -------------------------------- *