]> www.git.momoyon.org Git - lang.git/commitdiff
[main.c] Can parse_subscript!
authormomoyon <momoyon@momoyon.org>
Tue, 13 May 2025 07:53:34 +0000 (12:53 +0500)
committermomoyon <momoyon@momoyon.org>
Tue, 13 May 2025 07:53:34 +0000 (12:53 +0500)
- [tests] Add new test parse_subscript.momo;

main.c
tests/.parse_subscript.build.code.expected [new file with mode: 0644]
tests/.parse_subscript.build.err.expected [new file with mode: 0644]
tests/.parse_subscript.build.in.expected [new file with mode: 0644]
tests/.parse_subscript.build.out.expected [new file with mode: 0644]
tests/.parse_subscript.code.expected [new file with mode: 0644]
tests/.parse_subscript.err.expected [new file with mode: 0644]
tests/.parse_subscript.in.expected [new file with mode: 0644]
tests/.parse_subscript.out.expected [new file with mode: 0644]
tests/parse_subscript.momo [new file with mode: 0644]

diff --git a/main.c b/main.c
index 3c8db1b4ac4e04c1df8ad14bc7fd20544e26865b..f7d0ffb1e1b41624f176717ef2168fddc94a1270 100644 (file)
--- 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 (file)
index 0000000..c227083
--- /dev/null
@@ -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 (file)
index 0000000..e69de29
diff --git a/tests/.parse_subscript.build.in.expected b/tests/.parse_subscript.build.in.expected
new file mode 100644 (file)
index 0000000..c4d955f
--- /dev/null
@@ -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 (file)
index 0000000..2999d44
--- /dev/null
@@ -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 (file)
index 0000000..d7d17fc
--- /dev/null
@@ -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 (file)
index 0000000..e69de29
diff --git a/tests/.parse_subscript.in.expected b/tests/.parse_subscript.in.expected
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/.parse_subscript.out.expected b/tests/.parse_subscript.out.expected
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/parse_subscript.momo b/tests/parse_subscript.momo
new file mode 100644 (file)
index 0000000..f2f10ee
--- /dev/null
@@ -0,0 +1,6 @@
+foo[69];
+foo[bar];
+a[b(0)];
+c[ 1-1 ];
+d[b==c];
+baz[0010];