Interpreter - If statement

This commit is contained in:
Emmanuel BENOîT 2023-01-01 11:10:48 +01:00
parent 8711fde112
commit 9a84283245

View file

@ -51,6 +51,11 @@ impl Interpretable for ast::StmtNode {
ast::StmtNode::Print(expr) => self.on_print(environment, expr),
ast::StmtNode::VarDecl(name, expr) => self.on_var_decl(environment, name, expr),
ast::StmtNode::Block(statements) => self.on_block(environment, statements),
ast::StmtNode::IfStmt {
condition,
then_branch,
else_branch,
} => self.on_if_statement(environment, condition, then_branch, else_branch),
}
}
}
@ -86,13 +91,34 @@ impl ast::StmtNode {
}
/// Execute the contents of a block.
fn on_block(&self, environment: &EnvironmentRef, stmts: &Vec<Box<ast::StmtNode>>) -> InterpreterResult {
fn on_block(
&self,
environment: &EnvironmentRef,
stmts: &Vec<Box<ast::StmtNode>>,
) -> InterpreterResult {
let child = Environment::create_child(environment);
for stmt in stmts.iter() {
stmt.interprete(&child)?;
}
Ok(Value::Nil)
}
/// Execute an if statement.
fn on_if_statement(
&self,
environment: &EnvironmentRef,
condition: &ast::ExprNode,
then_branch: &ast::StmtNode,
else_branch: &Option<Box<ast::StmtNode>>,
) -> InterpreterResult {
if condition.interprete(environment)?.is_truthy() {
then_branch.interprete(environment)
} else if let Some(else_stmt) = else_branch {
else_stmt.interprete(environment)
} else {
Ok(Value::Nil)
}
}
}
/* -------------------------------- *