commit a8ea19b4793d0332b5af65e2e7425dc5092313b7
parent 6a0d142c181d2f1601c6f4f991d0f75c265cc871
Author: Alexey Yerin <yyp@disroot.org>
Date: Sun, 18 Apr 2021 21:53:38 +0300
hare/parse,unparse: add support for len expression
Diffstat:
3 files changed, 23 insertions(+), 2 deletions(-)
diff --git a/hare/parse/+test/expr.ha b/hare/parse/+test/expr.ha
@@ -42,6 +42,7 @@
delete(x[10..20]);
delete(x[..]);
free(x);
+ len([1, 2, 3, 4]);
};
");
};
diff --git a/hare/parse/expr.ha b/hare/parse/expr.ha
@@ -136,6 +136,22 @@ fn append_expr(lexer: *lex::lexer) (ast::expr | error) = {
};
};
+fn measurement(lexer: *lex::lexer) (ast::expr | error) = {
+ let tok = want(lexer, ltok::LEN, ltok::SIZE, ltok::OFFSET)?;
+
+ return switch (tok.0) {
+ ltok::LEN => {
+ want(lexer, ltok::LPAREN)?;
+ let e = expression(lexer)?;
+ want(lexer, ltok::RPAREN)?;
+
+ alloc(e): ast::len_expr;
+ },
+ ltok::SIZE => abort(), // TODO
+ ltok::OFFSET => abort(), // TODO
+ };
+};
+
fn binarithm(
lexer: *lex::lexer,
lvalue: (ast::expr | void),
@@ -234,7 +250,7 @@ fn builtin(lexer: *lex::lexer) (ast::expr | error) = {
* => abort(),
};
},
- ltok::SIZE, ltok::LEN, ltok::OFFSET => abort(), // TODO
+ ltok::SIZE, ltok::LEN, ltok::OFFSET => measurement(lexer)?,
ltok::DEFER => {
want(lexer, ltok::DEFER)?;
alloc(expression(lexer)?): ast::defer_expr;
diff --git a/hare/unparse/expr.ha b/hare/unparse/expr.ha
@@ -220,7 +220,11 @@ export fn expr(
z;
},
e: ast::match_expr => abort(),
- e: ast::len_expr => abort(),
+ e: ast::len_expr => {
+ let z = fmt::fprint(out, "len(")?;
+ z += expr(out, indent, *e)?;
+ z + fmt::fprint(out, ")")?;
+ },
e: ast::size_expr => abort(),
e: ast::offset_expr => abort(),
e: ast::propagate_expr => {