Interpreter - Implemented new version of the loop statement

This commit is contained in:
Emmanuel BENOîT 2023-01-02 12:18:00 +01:00
parent 4c54643a8d
commit 2f0798bfe1

View file

@ -94,11 +94,12 @@ 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 { ast::StmtNode::LoopStmt {
label, label,
condition, condition,
body, body,
} => self.on_while_statement(environment, label, condition, body), after_body
} => self.on_loop_statement(environment, label, condition, body, after_body),
ast::StmtNode::LoopControlStmt { ast::StmtNode::LoopControlStmt {
is_break, is_break,
loop_name, loop_name,
@ -171,12 +172,13 @@ impl ast::StmtNode {
} }
/// Execute a while statement. /// Execute a while statement.
fn on_while_statement( fn on_loop_statement(
&self, &self,
environment: &EnvironmentRef, environment: &EnvironmentRef,
label: &Option<Token>, label: &Option<Token>,
condition: &ast::ExprNode, condition: &ast::ExprNode,
body: &ast::StmtNode, body: &ast::StmtNode,
after_body: &Option<Box<ast::StmtNode>>,
) -> InterpreterResult { ) -> InterpreterResult {
let ln = match label { let ln = match label {
None => None, None => None,
@ -190,6 +192,15 @@ impl ast::StmtNode {
InterpreterFlowControl::Break(lv) if lv == &ln => break, InterpreterFlowControl::Break(lv) if lv == &ln => break,
_ => return Ok(result), _ => return Ok(result),
} }
if let Some(stmt) = after_body {
let result = stmt.interprete(environment)?;
match &result {
InterpreterFlowControl::Result(_) => (),
InterpreterFlowControl::Continue(lv) if lv == &ln => (),
InterpreterFlowControl::Break(lv) if lv == &ln => break,
_ => return Ok(result),
}
}
} }
Ok(InterpreterFlowControl::default()) Ok(InterpreterFlowControl::default())
} }