]> www.git.momoyon.org Git - lang.git/commitdiff
[main.c] parse_unary() -> parse_term()
authormomoyon <momoyon@momoyon.org>
Thu, 15 May 2025 06:14:46 +0000 (11:14 +0500)
committermomoyon <momoyon@momoyon.org>
Thu, 15 May 2025 06:16:06 +0000 (11:16 +0500)
- [tests] Update parse_unary.momo -> parse_unary_term.momo

19 files changed:
main.c
tests/.parse_unary.build.code.expected [deleted file]
tests/.parse_unary.build.err.expected [deleted file]
tests/.parse_unary.build.in.expected [deleted file]
tests/.parse_unary.build.out.expected [deleted file]
tests/.parse_unary.code.expected [deleted file]
tests/.parse_unary.err.expected [deleted file]
tests/.parse_unary.in.expected [deleted file]
tests/.parse_unary.out.expected [deleted file]
tests/.parse_unary_term.build.code.expected [new file with mode: 0644]
tests/.parse_unary_term.build.err.expected [new file with mode: 0644]
tests/.parse_unary_term.build.in.expected [new file with mode: 0644]
tests/.parse_unary_term.build.out.expected [new file with mode: 0644]
tests/.parse_unary_term.code.expected [new file with mode: 0644]
tests/.parse_unary_term.err.expected [new file with mode: 0644]
tests/.parse_unary_term.in.expected [new file with mode: 0644]
tests/.parse_unary_term.out.expected [new file with mode: 0644]
tests/parse_unary.momo [deleted file]
tests/parse_unary_term.momo [new file with mode: 0644]

diff --git a/main.c b/main.c
index da61b130075be3fc7a004d787185f9fd90e12b69..caae653947bf863de77e7becf7ba54cca6cf394e 100644 (file)
--- a/main.c
+++ b/main.c
@@ -31,8 +31,9 @@ static bool DEBUG_PRINT = false;
 // comparison      -> term ( ( ">" | ">=" | "<" | "<=" ) term )* ;
 // term            -> factor ( ( "-" | "+" ) factor )* ;
 // factor          -> unary ( ( "/" | "*" ) unary )* ;
-// unary           -> ( "!" | "-" ) unary
+// unary_not       -> ( "!" | "~" ) unary
 //                | primary ;
+// unary_term      -> ( "-" | "+" ) unary
 // prefix          -> ( "++" | "--" ) IDENT
 // comp.lit        -> Skipped...
 // access          -> IDENT "." ( access | IDENT )
@@ -285,7 +286,7 @@ typedef struct Binary_expr Binary_expr;
 typedef struct Funcall_AST Funcall_AST;
 typedef struct Subscript_AST Subscript_AST;
 typedef struct Access_AST Access_AST;
-typedef struct Unary_expr Unary_expr;
+typedef struct Unary_term_AST Unary_term_AST;
 typedef struct Suffix_AST Suffix_AST;
 typedef struct Prefix_AST Prefix_AST;
 typedef struct Primary_expr Primary_expr;
@@ -316,10 +317,9 @@ struct Literal {
 
 void print_literal(FILE *f, Literal value);
 
-struct Unary_expr {
+struct Unary_term_AST {
     Token operator;
     AST *operand;
-    bool suffix;
 };
 
 typedef struct {
@@ -391,7 +391,7 @@ typedef enum {
     AST_SUBSCRIPT,
     AST_ACCESS,
     AST_PREFIX,
-    AST_UNARY,
+    AST_UNARY_TERM,
     AST_BINARY,
     AST_COUNT,
 } AST_kind;
@@ -400,14 +400,14 @@ const char *ast_kind_as_str(AST_kind k);
 
 struct AST {
     AST_kind kind;
-    Primary_expr  *prim_expr;
-    Suffix_AST    *suffix;
-    Funcall_AST   *funcall;
-    Subscript_AST *subscript;
-    Access_AST    *access;
-    Prefix_AST    *prefix;
-    Unary_expr    *unary_expr;
-    Binary_expr   *bin_expr;
+    Primary_expr    *prim_expr;
+    Suffix_AST      *suffix;
+    Funcall_AST     *funcall;
+    Subscript_AST   *subscript;
+    Access_AST      *access;
+    Prefix_AST      *prefix;
+    Unary_term_AST  *unary_term;
+    Binary_expr     *bin_expr;
     Location loc;
 };
 
@@ -517,7 +517,7 @@ const char *expr_kind_as_str(AST_kind k) {
         case AST_SUBSCRIPT: return "SUBSCRIPT";
         case AST_ACCESS: return "ACCESS";
         case AST_PREFIX: return "PREFIX";
-        case AST_UNARY: return "UNARY";
+        case AST_UNARY_TERM: return "UNARY_TERM";
         case AST_BINARY: return "BINARY";
         case AST_COUNT:
         default: ASSERT(false, "UNREACHABLE!");
@@ -558,19 +558,9 @@ void print_ast_as_value(FILE *f, AST e) {
             fprintf(f, ")");
 
         } break;
-        case AST_UNARY: {
-            if (e.unary_expr->suffix) {
-                fprintf(f, "(");
-                print_ast_as_value(f, *e.unary_expr->operand);
-                fprintf(f, ")");
-                fprintf(f, " %s ", token_type_as_str(e.unary_expr->operator.type));
-            } else {
-                fprintf(f, " %s ", token_type_as_str(e.unary_expr->operator.type));
-                fprintf(f, "(");
-                print_ast_as_value(f, *e.unary_expr->operand);
-                fprintf(f, ")");
-            }
-
+        case AST_UNARY_TERM: {
+             fprintf(f, " %s ", token_type_as_str(e.unary_term->operator.type));
+             print_ast_as_value(f, *e.unary_term->operand);
         } break;
         case AST_PRIMARY: {
             print_primary_expr(f, e.prim_expr);
@@ -817,7 +807,7 @@ 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_unary_term(Arena *arena, Parser *p);
 AST *parse_factor(Arena *arena, Parser *p);
 AST *parse_comparision(Arena *arena, Parser *p);
 AST *parse_term(Arena *arena, Parser *p);
@@ -1064,17 +1054,16 @@ AST *parse_prefix(Arena *arena, Parser *p) {
     return parse_access(arena, p);
 }
 
-AST *parse_unary(Arena *arena, Parser *p) {
+AST *parse_unary_term(Arena *arena, Parser *p) {
     Token t = parser_peek(p);
 
-    if (t.type == TK_NOT || t.type == TK_MINUS) {
+    if (t.type == TK_MINUS || t.type == TK_PLUS) {
         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_unary(arena, p);
+        ast->unary_term = (Unary_term_AST *)arena_alloc(arena, sizeof(Unary_term_AST));
+        ast->kind = AST_UNARY_TERM;
+        ast->unary_term->operator = parser_advance(p);
+        ast->unary_term->operand = parse_unary_term(arena, p);
         return ast;
     }
 
@@ -1082,12 +1071,12 @@ AST *parse_unary(Arena *arena, Parser *p) {
 }
 
 AST *parse_factor(Arena *arena, Parser *p) {
-    AST *ast = parse_unary(arena, p);
+    AST *ast = parse_unary_term(arena, p);
     if (ast == NULL) return NULL;
 
     while (parser_match(p, TK_DIVIDE) || parser_match(p, TK_MULTIPLY)) {
         Token op = parser_previous(p);
-        AST *rhs = parse_unary(arena, p);
+        AST *rhs = parse_unary_term(arena, p);
         if (rhs == NULL) return rhs;
 
         AST *new_ast = (AST *)arena_alloc(arena, sizeof(AST));
diff --git a/tests/.parse_unary.build.code.expected b/tests/.parse_unary.build.code.expected
deleted file mode 100644 (file)
index c227083..0000000
+++ /dev/null
@@ -1 +0,0 @@
-0
\ No newline at end of file
diff --git a/tests/.parse_unary.build.err.expected b/tests/.parse_unary.build.err.expected
deleted file mode 100644 (file)
index e69de29..0000000
diff --git a/tests/.parse_unary.build.in.expected b/tests/.parse_unary.build.in.expected
deleted file mode 100644 (file)
index c4d955f..0000000
+++ /dev/null
@@ -1 +0,0 @@
-dump_ast
\ No newline at end of file
diff --git a/tests/.parse_unary.build.out.expected b/tests/.parse_unary.build.out.expected
deleted file mode 100644 (file)
index 75dff12..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-parse_unary.momo:1:0 [UNARY] ' ! (1)'
-parse_unary.momo:2:0 [UNARY] ' ! (true)'
-parse_unary.momo:3:0 [UNARY] ' ! ( ! (true))'
-parse_unary.momo:4:0 [UNARY] ' - (1)'
-parse_unary.momo:5:0 [UNARY] ' - ('number': ???)'
-parse_unary.momo:6:0 [UNARY] ' ! ('bar': ???)'
-parse_unary.momo:7:0 [UNARY] ' - ('baz': ???)'
diff --git a/tests/.parse_unary.code.expected b/tests/.parse_unary.code.expected
deleted file mode 100644 (file)
index d7d17fc..0000000
+++ /dev/null
@@ -1 +0,0 @@
--1
\ No newline at end of file
diff --git a/tests/.parse_unary.err.expected b/tests/.parse_unary.err.expected
deleted file mode 100644 (file)
index e69de29..0000000
diff --git a/tests/.parse_unary.in.expected b/tests/.parse_unary.in.expected
deleted file mode 100644 (file)
index e69de29..0000000
diff --git a/tests/.parse_unary.out.expected b/tests/.parse_unary.out.expected
deleted file mode 100644 (file)
index e69de29..0000000
diff --git a/tests/.parse_unary_term.build.code.expected b/tests/.parse_unary_term.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_unary_term.build.err.expected b/tests/.parse_unary_term.build.err.expected
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/.parse_unary_term.build.in.expected b/tests/.parse_unary_term.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_unary_term.build.out.expected b/tests/.parse_unary_term.build.out.expected
new file mode 100644 (file)
index 0000000..dad8c7a
--- /dev/null
@@ -0,0 +1,5 @@
+parse_unary_term.momo:1:0 [UNARY_TERM] ' + 1'
+parse_unary_term.momo:2:0 [UNARY_TERM] ' - 8'
+parse_unary_term.momo:3:0 [UNARY_TERM] ' - 'number': ???'
+parse_unary_term.momo:4:0 [UNARY_TERM] ' + 'bar': ???'
+parse_unary_term.momo:5:0 [UNARY_TERM] ' -  + 'baz': ???'
diff --git a/tests/.parse_unary_term.code.expected b/tests/.parse_unary_term.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_unary_term.err.expected b/tests/.parse_unary_term.err.expected
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/.parse_unary_term.in.expected b/tests/.parse_unary_term.in.expected
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/.parse_unary_term.out.expected b/tests/.parse_unary_term.out.expected
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/parse_unary.momo b/tests/parse_unary.momo
deleted file mode 100644 (file)
index e118844..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-!1;
-!true;
-!!true;
--1;
--number;
-!bar;
--baz;
diff --git a/tests/parse_unary_term.momo b/tests/parse_unary_term.momo
new file mode 100644 (file)
index 0000000..46ad7ef
--- /dev/null
@@ -0,0 +1,5 @@
++1;
+-8;
+-number;
++bar;
+-+baz;