AST - Break/Continue with label

This commit is contained in:
Emmanuel BENOîT 2023-01-02 09:57:42 +01:00
parent 154d072ca8
commit 48b86d5087

View file

@ -30,10 +30,11 @@ pub enum StmtNode {
condition: ExprNode, condition: ExprNode,
body: Box<StmtNode>, body: Box<StmtNode>,
}, },
/// Break statement /// Break or continue statement.
BreakStmt, LoopControlStmt {
/// Continue statement is_break: bool,
ContinueStmt, loop_name: Option<Token>,
},
} }
/// An AST node that represents an expression. /// An AST node that represents an expression.
@ -97,8 +98,6 @@ impl AstDumper for StmtNode {
match self { match self {
Self::VarDecl(name, Some(expr)) => format!("( var {} {} )", name.lexeme, expr.dump()), Self::VarDecl(name, Some(expr)) => format!("( var {} {} )", name.lexeme, expr.dump()),
Self::VarDecl(name, None) => format!("( var {} nil )", name.lexeme), Self::VarDecl(name, None) => format!("( var {} nil )", name.lexeme),
Self::BreakStmt => String::from("break"),
Self::ContinueStmt => String::from("continue"),
Self::Expression(expr) => format!("{}", expr.dump()), Self::Expression(expr) => format!("{}", expr.dump()),
Self::Print(expr) => format!("(print {})", expr.dump()), Self::Print(expr) => format!("(print {})", expr.dump()),
@ -128,6 +127,17 @@ impl AstDumper for StmtNode {
Self::WhileStmt { condition, body } => { Self::WhileStmt { condition, body } => {
format!("( while {} {} )", condition.dump(), body.dump()) format!("( while {} {} )", condition.dump(), body.dump())
} }
Self::LoopControlStmt {
is_break,
loop_name,
} => {
let stmt = if *is_break { "break" } else { "continue" };
match loop_name {
Some(name) => format!("( {} {} )", stmt, name.lexeme),
None => format!("( {} )", stmt),
}
}
} }
} }
} }