]> www.git.momoyon.org Git - lang.git/commitdiff
[main.c] Can parse_prefix().
authormomoyon <momoyon@momoyon.org>
Thu, 15 May 2025 05:46:10 +0000 (10:46 +0500)
committermomoyon <momoyon@momoyon.org>
Thu, 15 May 2025 05:46:10 +0000 (10:46 +0500)
NOTE: Skipped Compound Literals.

- [tests] Add new test parse_prefix.momo

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

diff --git a/main.c b/main.c
index 62f05b3dac29051c1e80d7d214cf617fe0875de2..604786277bee7ddf21bc42c84aab15b4cf0e4fef 100644 (file)
--- a/main.c
+++ b/main.c
@@ -33,6 +33,8 @@ static bool DEBUG_PRINT = false;
 // factor          -> unary ( ( "/" | "*" ) unary )* ;
 // unary           -> ( "!" | "-" ) unary
 //                | primary ;
+// prefix          -> ( "++" | "--" ) IDENT
+// comp.lit        -> Skipped...
 // access          -> IDENT "." ( access | IDENT )
 // subscript       -> IDENT "[" ast "]"
 // funcalls        -> IDENT "(" ( ast "," )* ")" | IDENT "(" ast ")"
@@ -98,9 +100,9 @@ static bool DEBUG_PRINT = false;
  * --------------------+-------------------------------------+-----------+-----
  * Prefix Inc/Dec      | ++ --                               | Right     |
  * --------------------+-------------------------------------+-----------+-----
- * Compound Lit        | (type){list}                        | Left      |
+ * Compound Lit        | (type){list}                        | Left      |     // TODO: We skipped this
  * --------------------+-------------------------------------+-----------+-----
- * Struct/Union access | .                                   | Left      |      NOTE: We use . to access through pointers as well
+ * Struct/Union access | .                                   | Left      | X    NOTE: We use . to access through pointers as well
  * --------------------+-------------------------------------+-----------+-----
  * Array Subscripting  | []                                  | Left      | X
  * --------------------+-------------------------------------+-----------+-----
@@ -795,6 +797,7 @@ AST *parse_suffix(Arena *arena, Parser *p);
 AST *parse_funcall(Arena *arena, Parser *p);
 AST *parse_subscript(Arena *arena, Parser *p);
 AST *parse_access(Arena *arena, Parser *p);
+AST *parse_prefix(Arena *arena, Parser *p);
 AST *parse_unary(Arena *arena, Parser *p);
 AST *parse_factor(Arena *arena, Parser *p);
 AST *parse_comparision(Arena *arena, Parser *p);
@@ -898,10 +901,8 @@ AST *parse_suffix(Arena *arena, Parser *p) {
     if (t.type == TK_IDENT &&
             (parser_peek_by(p, 1).type == TK_PLUS_PLUS ||
              parser_peek_by(p, 1).type == TK_MINUS_MINUS)) {
-        //
         AST *operand = parse_primary(arena, p);
         ASSERT(operand, "We should be able to parse identifiers using parse_primary()!");
-        // printf("INFO: CURRENT TOKEN: "); print_token(stdout, parser_peek(p));
 
         AST *ast = (AST *)arena_alloc(arena, sizeof(AST));
         ast->loc = t.loc;
@@ -1027,6 +1028,27 @@ AST *parse_access(Arena *arena, Parser *p) {
     return parse_subscript(arena, p);
 }
 
+AST *parse_prefix(Arena *arena, Parser *p) {
+    Token t = parser_peek(p);
+
+    if (t.type == TK_PLUS_PLUS ||
+            t.type == TK_MINUS_MINUS) {
+
+        AST *ast = (AST *)arena_alloc(arena, sizeof(AST));
+        ast->loc = t.loc;
+        ast->unary_expr = (Unary_expr *)arena_alloc(arena, sizeof(Unary_expr));
+        ast->kind = AST_UNARY;
+        Unary_expr *unary_expr = ast->unary_expr;
+        unary_expr->operator = parser_advance(p);
+        unary_expr->operand = parse_primary(arena, p);
+        ASSERT(unary_expr->operand, "We should be able to parse identifiers using parse_primary()!");
+        unary_expr->suffix = false;
+        return ast;
+    }
+
+    return parse_access(arena, p);
+}
+
 AST *parse_unary(Arena *arena, Parser *p) {
     Token t = parser_peek(p);
 
@@ -1041,7 +1063,7 @@ AST *parse_unary(Arena *arena, Parser *p) {
         return ast;
     }
 
-    return parse_access(arena, p);
+    return parse_prefix(arena, p);
 }
 
 AST *parse_factor(Arena *arena, Parser *p) {
diff --git a/tests/.parse_prefix.build.code.expected b/tests/.parse_prefix.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_prefix.build.err.expected b/tests/.parse_prefix.build.err.expected
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/.parse_prefix.build.in.expected b/tests/.parse_prefix.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_prefix.build.out.expected b/tests/.parse_prefix.build.out.expected
new file mode 100644 (file)
index 0000000..51c5a6d
--- /dev/null
@@ -0,0 +1,4 @@
+parse_prefix.momo:1:0 [UNARY] ' ++ ('foo': ???)'
+parse_prefix.momo:2:0 [UNARY] ' -- ('bar': ???)'
+parse_prefix.momo:3:0 [UNARY] ' ++ ('a': ???)'
+parse_prefix.momo:3:5 [UNARY] ' -- ('b': ???)'
diff --git a/tests/.parse_prefix.code.expected b/tests/.parse_prefix.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_prefix.err.expected b/tests/.parse_prefix.err.expected
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/.parse_prefix.in.expected b/tests/.parse_prefix.in.expected
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/.parse_prefix.out.expected b/tests/.parse_prefix.out.expected
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/parse_prefix.momo b/tests/parse_prefix.momo
new file mode 100644 (file)
index 0000000..1d39757
--- /dev/null
@@ -0,0 +1,3 @@
+++foo;
+--bar;
+++a; --b;