commit fe5ec6d8ff5cc1f23df0b06fb39c562c0e8ace04
parent 21aa89bf1a4b9798995d96322d4abccd52a85bb0
Author: Drew DeVault <sir@cmpwn.com>
Date: Sun, 18 Apr 2021 13:41:37 -0400
hare::parse: implement delete expressions
Diffstat:
3 files changed, 18 insertions(+), 2 deletions(-)
diff --git a/hare/parse/+test/expr.ha b/hare/parse/+test/expr.ha
@@ -38,6 +38,9 @@
append(x, 10);
append(x, 10, 20, 30);
append(x, y, z, q...);
+ delete(x[10]);
+ delete(x[10..20]);
+ delete(x[..]);
};
");
};
diff --git a/hare/parse/expr.ha b/hare/parse/expr.ha
@@ -216,7 +216,7 @@ fn builtin(lexer: *lex::lexer) (ast::expr | error) = {
return switch (tok.0) {
ltok::ALLOC => alloc_expr(lexer)?,
ltok::APPEND => append_expr(lexer)?,
- ltok::DELETE => abort(), // TODO
+ ltok::DELETE => delete_expr(lexer)?,
ltok::FREE => abort(), // TODO
ltok::ASSERT => abort(), // TODO
ltok::STATIC => {
@@ -335,6 +335,16 @@ fn control(lexer: *lex::lexer) (ast::expr | error) = {
};
};
+fn delete_expr(lexer: *lex::lexer) (ast::expr | error) = {
+ want(lexer, ltok::DELETE)?;
+ want(lexer, ltok::LPAREN)?;
+ const expr = plain_expression(lexer)?;
+ want(lexer, ltok::LBRACKET)?;
+ const expr = alloc(indexing(lexer, expr)?);
+ want(lexer, ltok::RPAREN)?;
+ return expr: ast::delete_expr;
+};
+
fn expression_list(lexer: *lex::lexer) (ast::expr | error) = {
let items: ast::list_expr = [];
diff --git a/hare/unparse/expr.ha b/hare/unparse/expr.ha
@@ -185,7 +185,10 @@ export fn expr(
},
e: ast::defer_expr =>
fmt::fprint(out, "defer ")? + expr(out, indent, *e)?,
- e: ast::delete_expr => abort(),
+ e: ast::delete_expr =>
+ fmt::fprint(out, "delete(")?
+ + expr(out, indent, *e)?
+ + fmt::fprint(out, ")")?,
e: ast::for_expr => for_expr(out, indent, e)?,
e: ast::free_expr => abort(),
e: ast::if_expr => {