Interpreter - While loops
This commit is contained in:
parent
daec88c2f8
commit
ddf6625326
1 changed files with 16 additions and 0 deletions
|
@ -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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* -------------------------------- *
|
/* -------------------------------- *
|
||||||
|
|
Loading…
Reference in a new issue