From 0625046f69073696d468764ddd46e5cc05f67202 Mon Sep 17 00:00:00 2001 From: momoyon Date: Tue, 13 May 2025 12:53:34 +0500 Subject: [PATCH] [main.c] Can parse_subscript! - [tests] Add new test parse_subscript.momo; --- main.c | 24 ++++++---------------- tests/.parse_subscript.build.code.expected | 1 + tests/.parse_subscript.build.err.expected | 0 tests/.parse_subscript.build.in.expected | 1 + tests/.parse_subscript.build.out.expected | 6 ++++++ tests/.parse_subscript.code.expected | 1 + tests/.parse_subscript.err.expected | 0 tests/.parse_subscript.in.expected | 0 tests/.parse_subscript.out.expected | 0 tests/parse_subscript.momo | 6 ++++++ 10 files changed, 21 insertions(+), 18 deletions(-) create mode 100644 tests/.parse_subscript.build.code.expected create mode 100644 tests/.parse_subscript.build.err.expected create mode 100644 tests/.parse_subscript.build.in.expected create mode 100644 tests/.parse_subscript.build.out.expected create mode 100644 tests/.parse_subscript.code.expected create mode 100644 tests/.parse_subscript.err.expected create mode 100644 tests/.parse_subscript.in.expected create mode 100644 tests/.parse_subscript.out.expected create mode 100644 tests/parse_subscript.momo diff --git a/main.c b/main.c index 3c8db1b..f7d0ffb 100644 --- a/main.c +++ b/main.c @@ -33,11 +33,8 @@ static bool DEBUG_PRINT = false; // factor -> unary ( ( "/" | "*" ) unary )* ; // unary -> ( "!" | "-" ) unary // | primary ; -// // TODO: Ig there could be a funcall here aswell? -// V -// array subscript -> IDENT "[" ( IDENT | NUMBER ) "]" -// funcalls -> IDENT "(" ( ast "," )* ")" -// | IDENT "(" IDENT ")" +// subscript -> IDENT "[" ast "]" +// funcalls -> IDENT "(" ( ast "," )* ")" | IDENT "(" ast ")" // suffix -> IDENT ( "++" | "--" ) // primary -> NUMBER | STRING | IDENT | "true" | "false" | "null" // | "(" ast ")" ; @@ -332,7 +329,7 @@ struct Funcall_AST { struct Subscript_AST { String_view identifier_key; - Primary_expr *index_expr; + AST *index_ast; }; struct Binary_expr { @@ -550,7 +547,7 @@ void print_ast_as_value(FILE *f, AST e) { case AST_SUBSCRIPT: { fprintf(f, SV_FMT, SV_ARG(e.subscript->identifier_key)); fprintf(f, "["); - print_primary_expr(f, e.subscript->index_expr); + print_ast_as_value(f, *e.subscript->index_ast); // if (e.subscript->index_expr->kind == PRIMARY_VALUE) { // print_literal(f, e.subscript->index_expr->value); // } else if (e.subscript->index_expr->kind == PRIMARY_IDENT) { @@ -942,18 +939,9 @@ AST *parse_subscript(Arena *arena, Parser *p) { ast->subscript->identifier_key = parser_advance(p).lexeme; // Eat IDENT parser_advance(p); // Skip [ - Token next = parser_peek(p); - if (next.type != TK_INT && - next.type != TK_IDENT) { - error_pretty(next.loc, (*p->lexer), "Expected a positive integer literal or variable but got `%s`", token_type_as_str(next.type)); - return NULL; - } - AST *index_ast = parse_primary(arena, p); + AST *index_ast = parse(arena, p); if (index_ast == NULL) return NULL; - ASSERT(index_ast->prim_expr->value.kind == LIT_INT || - index_ast->prim_expr->kind == PRIMARY_IDENT, - "parse_primary() error; should be either integer primary or ident primary!"); - ast->subscript->index_expr = index_ast->prim_expr; + ast->subscript->index_ast = index_ast; if (!parser_match(p, TK_RIGHT_SQUARE_BRACE)) { error_pretty(parser_peek(p).loc, (*p->lexer), "Expected ] but got `%s`", token_type_as_str(parser_peek(p).type)); diff --git a/tests/.parse_subscript.build.code.expected b/tests/.parse_subscript.build.code.expected new file mode 100644 index 0000000..c227083 --- /dev/null +++ b/tests/.parse_subscript.build.code.expected @@ -0,0 +1 @@ +0 \ No newline at end of file diff --git a/tests/.parse_subscript.build.err.expected b/tests/.parse_subscript.build.err.expected new file mode 100644 index 0000000..e69de29 diff --git a/tests/.parse_subscript.build.in.expected b/tests/.parse_subscript.build.in.expected new file mode 100644 index 0000000..c4d955f --- /dev/null +++ b/tests/.parse_subscript.build.in.expected @@ -0,0 +1 @@ +dump_ast \ No newline at end of file diff --git a/tests/.parse_subscript.build.out.expected b/tests/.parse_subscript.build.out.expected new file mode 100644 index 0000000..2999d44 --- /dev/null +++ b/tests/.parse_subscript.build.out.expected @@ -0,0 +1,6 @@ +parse_subscript.momo:1:0 [SUBSCRIPT] 'foo[69]' +parse_subscript.momo:2:0 [SUBSCRIPT] 'foo['bar': ???]' +parse_subscript.momo:3:0 [SUBSCRIPT] 'a[b(0)]' +parse_subscript.momo:4:0 [SUBSCRIPT] 'c[(1 - 1)]' +parse_subscript.momo:5:0 [SUBSCRIPT] 'd[('b': ??? == 'c': ???)]' +parse_subscript.momo:6:0 [SUBSCRIPT] 'baz[10]' diff --git a/tests/.parse_subscript.code.expected b/tests/.parse_subscript.code.expected new file mode 100644 index 0000000..d7d17fc --- /dev/null +++ b/tests/.parse_subscript.code.expected @@ -0,0 +1 @@ +-1 \ No newline at end of file diff --git a/tests/.parse_subscript.err.expected b/tests/.parse_subscript.err.expected new file mode 100644 index 0000000..e69de29 diff --git a/tests/.parse_subscript.in.expected b/tests/.parse_subscript.in.expected new file mode 100644 index 0000000..e69de29 diff --git a/tests/.parse_subscript.out.expected b/tests/.parse_subscript.out.expected new file mode 100644 index 0000000..e69de29 diff --git a/tests/parse_subscript.momo b/tests/parse_subscript.momo new file mode 100644 index 0000000..f2f10ee --- /dev/null +++ b/tests/parse_subscript.momo @@ -0,0 +1,6 @@ +foo[69]; +foo[bar]; +a[b(0)]; +c[ 1-1 ]; +d[b==c]; +baz[0010]; -- 2.39.5