commit 6a0d142c181d2f1601c6f4f991d0f75c265cc871
parent fe5ec6d8ff5cc1f23df0b06fb39c562c0e8ace04
Author: Drew DeVault <sir@cmpwn.com>
Date: Sun, 18 Apr 2021 13:46:47 -0400
hare::parse: implement free expressions
Diffstat:
3 files changed, 14 insertions(+), 2 deletions(-)
diff --git a/hare/parse/+test/expr.ha b/hare/parse/+test/expr.ha
@@ -41,6 +41,7 @@
delete(x[10]);
delete(x[10..20]);
delete(x[..]);
+ free(x);
};
");
};
diff --git a/hare/parse/expr.ha b/hare/parse/expr.ha
@@ -217,7 +217,7 @@ fn builtin(lexer: *lex::lexer) (ast::expr | error) = {
ltok::ALLOC => alloc_expr(lexer)?,
ltok::APPEND => append_expr(lexer)?,
ltok::DELETE => delete_expr(lexer)?,
- ltok::FREE => abort(), // TODO
+ ltok::FREE => free_expr(lexer)?,
ltok::ASSERT => abort(), // TODO
ltok::STATIC => {
want(lexer, ltok::STATIC)?;
@@ -406,6 +406,14 @@ fn for_expr(lexer: *lex::lexer) (ast::expr | error) = {
};
};
+fn free_expr(lexer: *lex::lexer) (ast::expr | error) = {
+ want(lexer, ltok::FREE)?;
+ want(lexer, ltok::LPAREN)?;
+ const expr = alloc(expression(lexer)?);
+ want(lexer, ltok::RPAREN)?;
+ return expr: ast::free_expr;
+};
+
fn if_expr(lexer: *lex::lexer) (ast::expr | error) = {
want(lexer, ltok::IF)?;
want(lexer, ltok::LPAREN)?;
diff --git a/hare/unparse/expr.ha b/hare/unparse/expr.ha
@@ -190,7 +190,10 @@ export fn expr(
+ expr(out, indent, *e)?
+ fmt::fprint(out, ")")?,
e: ast::for_expr => for_expr(out, indent, e)?,
- e: ast::free_expr => abort(),
+ e: ast::free_expr =>
+ fmt::fprint(out, "free(")?
+ + expr(out, indent, *e)?
+ + fmt::fprint(out, ")")?,
e: ast::if_expr => {
let z = fmt::fprint(out, "if (")?;
z += expr(out, indent, *e.cond)?;