commit e144f293c553e1275fdf9e712f7fd59eaf490215
parent 374e0e2138ebee4dad1d6891f18793cc15741255
Author: Sebastian <sebastian@sebsite.pw>
Date: Mon, 22 May 2023 04:06:01 -0400
hare::ast: add error_assert_expr
Instead of using propagate_expr to represent both error propagation and
assertion, a separate type for error assertion has been added.
Signed-off-by: Sebastian <sebastian@sebsite.pw>
Diffstat:
4 files changed, 23 insertions(+), 19 deletions(-)
diff --git a/hare/ast/expr.ha b/hare/ast/expr.ha
@@ -334,10 +334,12 @@ export type offset_expr = *expr;
// An error propagation expression.
//
// foo?
-export type propagate_expr = struct {
- is_abort: bool,
- expr: *expr,
-};
+export type propagate_expr = *expr;
+
+// An error assertion expression.
+//
+// foo!
+export type error_assert_expr = *expr;
// A return statement.
//
@@ -428,10 +430,10 @@ export type expr = struct {
assert_expr | assign_expr | binarithm_expr | binding_expr |
break_expr | call_expr | cast_expr | constant_expr |
continue_expr | defer_expr | delete_expr | for_expr |
- free_expr | if_expr | insert_expr | compound_expr | match_expr |
- len_expr | size_expr | offset_expr | propagate_expr |
- return_expr | slice_expr | switch_expr | unarithm_expr |
- variadic_expr | yield_expr),
+ free_expr | error_assert_expr | if_expr | insert_expr |
+ compound_expr | match_expr | len_expr | size_expr |
+ offset_expr | propagate_expr | return_expr | slice_expr |
+ switch_expr | unarithm_expr | variadic_expr | yield_expr),
};
// Frees resources associated with a Hare [[expr]]ession.
@@ -542,6 +544,8 @@ case let e: expr =>
expr_finish(d: *expr);
case let d: delete_expr =>
expr_finish(d.object);
+ case let e: error_assert_expr =>
+ expr_finish(e);
case let f: for_expr =>
expr_finish(f.bindings);
expr_finish(f.cond);
@@ -585,7 +589,7 @@ case let e: expr =>
case let o: offset_expr =>
expr_finish(o: *expr);
case let p: propagate_expr =>
- expr_finish(p.expr);
+ expr_finish(p);
case let r: return_expr =>
expr_finish(r: *expr);
case let s: size_expr =>
diff --git a/hare/parse/expr.ha b/hare/parse/expr.ha
@@ -1046,19 +1046,13 @@ fn postfix(lexer: *lex::lexer, lvalue: (ast::expr | void)) (ast::expr | error) =
yield ast::expr {
start = lvalue.start,
end = lex::prevloc(lexer),
- expr = ast::propagate_expr {
- is_abort = false,
- expr = alloc(lvalue),
- },
+ expr = alloc(lvalue): ast::propagate_expr,
};
case ltok::LNOT =>
yield ast::expr {
start = lvalue.start,
end = lex::prevloc(lexer),
- expr = ast::propagate_expr {
- is_abort = true,
- expr = alloc(lvalue),
- },
+ expr = alloc(lvalue): ast::error_assert_expr,
};
case => abort();
};
diff --git a/hare/unit/process.ha b/hare/unit/process.ha
@@ -119,6 +119,8 @@ fn process_expr(
abort(); // TODO
case ast::delete_expr =>
abort(); // TODO
+ case ast::error_assert_expr =>
+ abort(); // TODO
case ast::for_expr =>
abort(); // TODO
case ast::free_expr =>
diff --git a/hare/unparse/expr.ha b/hare/unparse/expr.ha
@@ -282,6 +282,10 @@ export fn expr(
z += expr(out, indent, *e.object)?;
z += fmt::fprint(out, ")")?;
return z;
+ case let e: ast::error_assert_expr =>
+ let z = expr(out, indent, *e)?;
+ z += fmt::fprint(out, "!")?;
+ return z;
case let e: ast::for_expr =>
return for_expr(out, indent, e)?;
case let e: ast::free_expr =>
@@ -354,8 +358,8 @@ export fn expr(
z += fmt::fprint(out, ")")?;
return z;
case let e: ast::propagate_expr =>
- let z = expr(out, indent, *e.expr)?;
- z += fmt::fprintf(out, if (e.is_abort) "!" else "?")?;
+ let z = expr(out, indent, *e)?;
+ z += fmt::fprint(out, "?")?;
return z;
case let e: ast::return_expr =>
let z = fmt::fprint(out, "return")?;