commit ca7d1b9b80ce7eeb1b1ba9c39d98c7bb85f3b6c3
parent 18956d45e246e570b0bd308ee8b08658a9b930e2
Author: Drew DeVault <sir@cmpwn.com>
Date: Sun, 18 Apr 2021 10:25:28 -0400
hare::parse: implement defer expressions
Diffstat:
3 files changed, 10 insertions(+), 2 deletions(-)
diff --git a/hare/parse/+test/expr.ha b/hare/parse/+test/expr.ha
@@ -54,6 +54,10 @@
");
};
+@test fn defer_expr() void = {
+ roundtrip("export fn main() void = defer foo();\n");
+};
+
@test fn for_expr() void = {
roundtrip("export fn main() void = {
for (true) {
diff --git a/hare/parse/expr.ha b/hare/parse/expr.ha
@@ -123,7 +123,10 @@ fn builtin(lexer: *lex::lexer) (ast::expr | error) = {
};
},
ltok::SIZE, ltok::LEN, ltok::OFFSET => abort(),
- ltok::DEFER => abort(),
+ ltok::DEFER => {
+ want(lexer, ltok::DEFER)?;
+ alloc(expression(lexer)?): ast::defer_expr;
+ },
* => abort(), // Invariant
};
};
diff --git a/hare/unparse/expr.ha b/hare/unparse/expr.ha
@@ -126,7 +126,8 @@ export fn expr(
};
z;
},
- e: ast::defer_expr => abort(),
+ e: ast::defer_expr =>
+ fmt::fprint(out, "defer ")? + expr(out, indent, *e)?,
e: ast::delete_expr => abort(),
e: ast::for_expr => for_expr(out, indent, e)?,
e: ast::free_expr => abort(),