]> www.git.momoyon.org Git - lang.git/commitdiff
[main.c] Rename/Refactor somethings...
authormomoyon <momoyon@momoyon.org>
Mon, 12 May 2025 18:52:05 +0000 (23:52 +0500)
committermomoyon <momoyon@momoyon.org>
Mon, 12 May 2025 18:52:05 +0000 (23:52 +0500)
- [tests] Add new tests for parsing functions.

main.c
tests/parse_comparision.momo [new file with mode: 0644]
tests/parse_equality.momo [new file with mode: 0644]
tests/parse_factor.momo [new file with mode: 0644]
tests/parse_primary.momo [new file with mode: 0644]
tests/parse_term.momo [new file with mode: 0644]
tests/parse_unary.momo [new file with mode: 0644]

diff --git a/main.c b/main.c
index 5ad46a998ad90b9b776153bcbc99616a2758dec6..2dc815e2888ff452cd6c48b8e5ece88233e52891 100644 (file)
--- a/main.c
+++ b/main.c
 static bool DEBUG_PRINT = false;
 
 // TODO:Implement every expr parsing for C:
-// ast            -> equality ;
-// equality       -> comparison ( ( "!=" | "==" ) comparison )* ;
-// comparison     -> term ( ( ">" | ">=" | "<" | "<=" ) term )* ;
-// term           -> factor ( ( "-" | "+" ) factor )* ;
-// factor         -> unary ( ( "/" | "*" ) unary )* ;
-// unary          -> ( "!" | "-" ) unary
+// ast             -> equality ;
+// equality        -> comparison ( ( "!=" | "==" ) comparison )* ;
+// comparison      -> term ( ( ">" | ">=" | "<" | "<=" ) term )* ;
+// term            -> factor ( ( "-" | "+" ) factor )* ;
+// factor          -> unary ( ( "/" | "*" ) unary )* ;
+// unary           -> ( "!" | "-" ) unary
 //                | primary ;
-// funcalls       -> IDENT "(" ( ast "," )* ")"
-//                  | IDENT "(" IDENT ")"
-// suffixes       -> IDENT ( "++" | "--" )
-// primary        -> NUMBER | STRING | IDENT | "true" | "false" | "null"
+// array subscript -> IDENT "[" NUMBER "]"
+// funcalls        -> IDENT "(" ( ast "," )* ")"
+//                   | IDENT "(" IDENT ")"
+// suffix          -> IDENT ( "++" | "--" )
+// primary         -> NUMBER | STRING | IDENT | "true" | "false" | "null"
 //                | "(" ast ")" ;
 
 /* NOTE: We are referencing this table: https://en.cppreference.com/w/c/language/operator_precedence
@@ -103,11 +104,11 @@ static bool DEBUG_PRINT = false;
  * --------------------+-------------------------------------+-----------+-----
  * Array Subscripting  | []                                  | Left      |
  * --------------------+-------------------------------------+-----------+-----
- * Function Call       | IDENT()                             | Left      |
+ * Function Call       | ()                                  | Left      | X
  * --------------------+-------------------------------------+-----------+-----
  * Suffix Inc/Dec      | ++ --                               | Left      | X
  * --------------------+-------------------------------------+-----------+-----
- * Primary             | IDENTS NUMBERS STRINGS CHARS (ast) | -         | X
+ * Primary             | IDENTS NUMBERS STRINGS CHARS (ast)  | -         | X
  * --------------------+-------------------------------------+-----------+-----
  */
 
@@ -734,8 +735,9 @@ bool parser_eof(Parser *p) {
 
 // Predecls
 AST *parse_primary(Arena *arena, Parser *p);
-AST *parse_suffixes(Arena *arena, Parser *p);
+AST *parse_suffix(Arena *arena, Parser *p);
 AST *parse_funcall(Arena *arena, Parser *p);
+AST *parse_array_subscript(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);
@@ -811,6 +813,13 @@ AST *parse_primary(Arena *arena, Parser *p) {
             ast->prim_expr->value_kind = LIT_BOOL;
             ast->prim_expr->value.as.b = sv_equals(t.lexeme, SV("true"));
             return ast;
+        } else if (t.type == TK_NULL) {
+            ast->prim_expr->value_kind = LIT_INT; // TODO: Should we introduce a LIT_NULL?
+            ast->prim_expr->value.as.i = 0;
+            return ast;
+        } else {
+            printf("Unexpected Token: "), print_token(stdout, t); printf("\n");
+            ASSERT(false, "^");
         }
     } else {
         parser_advance(p); // Skip (
@@ -828,7 +837,7 @@ AST *parse_primary(Arena *arena, Parser *p) {
     return NULL;
 }
 
-AST *parse_suffixes(Arena *arena, Parser *p) {
+AST *parse_suffix(Arena *arena, Parser *p) {
     Token t = parser_peek(p);
 
     if (t.type == TK_IDENT &&
@@ -895,7 +904,11 @@ AST *parse_funcall(Arena *arena, Parser *p) {
         ASSERT(false, "This should be unreachable!");
     }
 
-    return parse_suffixes(arena, p);
+    return parse_suffix(arena, p);
+}
+
+AST *parse_array_subscript(Arena *arena, Parser *p) {
+    return parse_funcall(arena, p);
 }
 
 AST *parse_unary(Arena *arena, Parser *p) {
@@ -912,7 +925,7 @@ AST *parse_unary(Arena *arena, Parser *p) {
         return ast;
     }
 
-    return parse_funcall(arena, p);
+    return parse_array_subscript(arena, p);
 }
 
 AST *parse_factor(Arena *arena, Parser *p) {
diff --git a/tests/parse_comparision.momo b/tests/parse_comparision.momo
new file mode 100644 (file)
index 0000000..9d830ac
--- /dev/null
@@ -0,0 +1,4 @@
+123 > 2;
+5.0 <= foo;
+bar < baz;
+two >= (a * b);
diff --git a/tests/parse_equality.momo b/tests/parse_equality.momo
new file mode 100644 (file)
index 0000000..63838cb
--- /dev/null
@@ -0,0 +1,4 @@
+123 == 2;
+5.0 != foo;
+bar == baz;
+two != (a * b);
diff --git a/tests/parse_factor.momo b/tests/parse_factor.momo
new file mode 100644 (file)
index 0000000..1b36ced
--- /dev/null
@@ -0,0 +1,4 @@
+8 * 1024;
+40.0 / 1.5555;
+foo * 2;
+bar / baz;
diff --git a/tests/parse_primary.momo b/tests/parse_primary.momo
new file mode 100644 (file)
index 0000000..c119825
--- /dev/null
@@ -0,0 +1,7 @@
+123;
+"string";
+identifier;
+true;
+false;
+null;
+(expression_inside_parenthesis);
diff --git a/tests/parse_term.momo b/tests/parse_term.momo
new file mode 100644 (file)
index 0000000..8fb8cd6
--- /dev/null
@@ -0,0 +1,4 @@
+123 + 2;
+5.0 + foo;
+bar - baz;
+two - (a * b);
diff --git a/tests/parse_unary.momo b/tests/parse_unary.momo
new file mode 100644 (file)
index 0000000..e118844
--- /dev/null
@@ -0,0 +1,7 @@
+!1;
+!true;
+!!true;
+-1;
+-number;
+!bar;
+-baz;