hare

The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 924d2c2c89fd3f79013109a158cfe1365df5c154
parent 8418a9af181c31f956d912e16a954b968dc3401e
Author: Drew DeVault <sir@cmpwn.com>
Date:   Fri, 16 Apr 2021 09:59:38 -0400

hare::parse: implement indexing expressions

Diffstat:
Mhare/parse/+test/expr.ha | 4+++-
Mhare/parse/expr.ha | 9++++++++-
Mhare/unparse/expr.ha | 8+++++++-
3 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/hare/parse/+test/expr.ha b/hare/parse/+test/expr.ha @@ -28,7 +28,9 @@ "export fn main() void = x.42;\n" "export fn main() void = x().y.0.q;\n" "export fn main() void = x?;\n" - "export fn main() void = x()?.y;\n"); + "export fn main() void = x()?.y;\n" + "export fn main() void = x[10];\n" + "export fn main() void = x[10 + 10][20];\n"); }; @test fn unarithm() void = { diff --git a/hare/parse/expr.ha b/hare/parse/expr.ha @@ -213,7 +213,14 @@ fn postfix(lexer: *lex::lexer, lvalue: (ast::expr | void)) (ast::expr | error) = tok: lex::token => switch (tok.0) { ltok::LPAREN => call(lexer, lvalue)?, ltok::DOT => postfix_dot(lexer, lvalue)?, - ltok::LBRACKET => abort(), // TODO: Indexing + ltok::LBRACKET => { + let expr = ast::access_index { + object = alloc(lvalue), + index = alloc(expression(lexer)?), + }; + want(lexer, ltok::RBRACKET)?; + expr; + }, ltok::QUESTION => alloc(lvalue): ast::propagate_expr, * => abort(), }, diff --git a/hare/unparse/expr.ha b/hare/unparse/expr.ha @@ -11,7 +11,13 @@ export fn expr( return match (t) { e: ast::access_expr => match (e) { id: ast::access_identifier => ident(out, id), - ix: ast::access_index => abort(), + ix: ast::access_index => { + let z = expr(out, indent, *ix.object)?; + z += fmt::fprintf(out, "[")?; + z += expr(out, indent, *ix.index)?; + z += fmt::fprintf(out, "]")?; + z; + }, fi: ast::access_field => { let z = expr(out, indent, *fi.object)?; z + fmt::fprintf(out, ".{}", fi.field)?;