]> www.git.momoyon.org Git - lang.git/commitdiff
[main.c] Use Prefix_AST instead of Unary_expr...
authormomoyon <momoyon@momoyon.org>
Thu, 15 May 2025 06:08:05 +0000 (11:08 +0500)
committermomoyon <momoyon@momoyon.org>
Thu, 15 May 2025 06:08:05 +0000 (11:08 +0500)
- [tests] Update both parse_suffix and parse_prefix output.

main.c
tests/.parse_prefix.build.out.expected
tests/.parse_suffix.build.out.expected

diff --git a/main.c b/main.c
index d73a51178790637d457d97d5d928c32ca5b0a152..da61b130075be3fc7a004d787185f9fd90e12b69 100644 (file)
--- a/main.c
+++ b/main.c
@@ -98,7 +98,7 @@ static bool DEBUG_PRINT = false;
  * --------------------+-------------------------------------+-----------+-----
  * Unary Plus/Minus    | + -                                 | Right     |
  * --------------------+-------------------------------------+-----------+-----
- * Prefix Inc/Dec      | ++ --                               | Right     |
+ * Prefix Inc/Dec      | ++ --                               | Right     | X
  * --------------------+-------------------------------------+-----------+-----
  * Compound Lit        | (type){list}                        | Left      |     // TODO: We skipped this
  * --------------------+-------------------------------------+-----------+-----
@@ -287,6 +287,7 @@ typedef struct Subscript_AST Subscript_AST;
 typedef struct Access_AST Access_AST;
 typedef struct Unary_expr Unary_expr;
 typedef struct Suffix_AST Suffix_AST;
+typedef struct Prefix_AST Prefix_AST;
 typedef struct Primary_expr Primary_expr;
 typedef struct AST AST;
 typedef enum   Primary_expr_kind Primary_expr_kind;
@@ -363,6 +364,11 @@ struct Suffix_AST {
     AST *operator;
 };
 
+struct Prefix_AST {
+    Token ident;
+    AST *operator;
+};
+
 enum Primary_expr_kind {
     PRIMARY_VALUE,
     PRIMARY_IDENT,
@@ -384,6 +390,7 @@ typedef enum {
     AST_FUNCALL,
     AST_SUBSCRIPT,
     AST_ACCESS,
+    AST_PREFIX,
     AST_UNARY,
     AST_BINARY,
     AST_COUNT,
@@ -398,6 +405,7 @@ struct AST {
     Funcall_AST   *funcall;
     Subscript_AST *subscript;
     Access_AST    *access;
+    Prefix_AST    *prefix;
     Unary_expr    *unary_expr;
     Binary_expr   *bin_expr;
     Location loc;
@@ -508,6 +516,7 @@ const char *expr_kind_as_str(AST_kind k) {
         case AST_FUNCALL: return "FUNCALL";
         case AST_SUBSCRIPT: return "SUBSCRIPT";
         case AST_ACCESS: return "ACCESS";
+        case AST_PREFIX: return "PREFIX";
         case AST_UNARY: return "UNARY";
         case AST_BINARY: return "BINARY";
         case AST_COUNT:
@@ -583,13 +592,6 @@ void print_ast_as_value(FILE *f, AST e) {
             fprintf(f, SV_FMT, SV_ARG(e.subscript->identifier_key));
             fprintf(f, "[");
             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) {
-            //     print_loc(f, e.subscript->index_expr->value);
-            // } else {
-            //     ASSERT(false, "This shouldnt happen");
-            // }
             fprintf(f, "]");
         } break;
         case AST_ACCESS: {
@@ -607,6 +609,10 @@ void print_ast_as_value(FILE *f, AST e) {
                     ASSERT(false, "UNREACHABLE!");
             }
         } break;
+        case AST_PREFIX: {
+            print_ast_as_value(f, *e.prefix->operator);
+            fprintf(f, SV_FMT, SV_ARG(e.prefix->ident.lexeme));
+        } break;
 
         case AST_COUNT:
         default: ASSERT(false, "UNREACHABLE!");
@@ -1047,13 +1053,11 @@ AST *parse_prefix(Arena *arena, Parser *p) {
 
         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;
+        ast->prefix = (Prefix_AST *)arena_alloc(arena, sizeof(Prefix_AST));
+        ast->kind = AST_PREFIX;
+        ast->prefix->ident = parser_advance(p);
+        ast->prefix->operator = parse_primary(arena, p);
+        ASSERT(ast->prefix->operator, "We should be able to parse identifiers using parse_primary()!");
         return ast;
     }
 
index 51c5a6d527f967c0a368a5b8a37cda52f366accf..f4026e832b2a5d54faf7ba6171075343a8b88249 100644 (file)
@@ -1,4 +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': ???)'
+parse_prefix.momo:1:0 [PREFIX] ''foo': ???++'
+parse_prefix.momo:2:0 [PREFIX] ''bar': ???--'
+parse_prefix.momo:3:0 [PREFIX] ''a': ???++'
+parse_prefix.momo:3:5 [PREFIX] ''b': ???--'
index 5ff559b0f48bca35bd321cd5f747531ce77fd395..bd403743653cf37086f77783b916d04cf71b1d26 100644 (file)
@@ -1,4 +1,4 @@
-parse_suffix.momo:1:0 [UNARY] '('foo': ???) ++ '
-parse_suffix.momo:2:0 [UNARY] '('bar': ???) -- '
-parse_suffix.momo:3:0 [UNARY] '('a': ???) ++ '
-parse_suffix.momo:3:5 [UNARY] '('b': ???) -- '
+parse_suffix.momo:1:0 [SUFFIX] '++'foo': ???'
+parse_suffix.momo:2:0 [SUFFIX] '--'bar': ???'
+parse_suffix.momo:3:0 [SUFFIX] '++'a': ???'
+parse_suffix.momo:3:5 [SUFFIX] '--'b': ???'