cl-ast: Unify break, return, and unary expressions

This commit is contained in:
2024-07-30 20:16:07 -05:00
parent 0e545077c6
commit adb0fd229c
8 changed files with 15 additions and 123 deletions

View File

@@ -21,9 +21,12 @@ fn desugar_while(extents: Span, kind: ExprKind) -> ExprKind {
match kind {
// work backwards: fail -> break -> if -> loop
ExprKind::While(While { cond, pass, fail: Else { body } }) => {
let Expr { extents, kind } = body
.map(|b| *b)
.unwrap_or(Expr { extents, kind: ExprKind::Empty });
// Preserve the else-expression's extents, if present, or use the parent's extents
let fail_span = body.as_ref().map(|body| body.extents).unwrap_or(extents);
let break_expr = Expr { extents: fail_span, kind: ExprKind::Break(Break { body }) };
let kind = ExprKind::Unary(Unary { kind: UnaryKind::Break, tail: kind.into() });
let break_expr = Expr { extents, kind };
let loop_body = If { cond, pass, fail: Else { body: Some(Box::new(break_expr)) } };
let loop_body = ExprKind::If(loop_body);