From fe185496f3bb05719f5bfe384447196fc1bf778b Mon Sep 17 00:00:00 2001 From: momoyon Date: Mon, 12 May 2025 23:52:05 +0500 Subject: [PATCH] [main.c] Rename/Refactor somethings... - [tests] Add new tests for parsing functions. --- main.c | 45 +++++++++++++++++++++++------------- tests/parse_comparision.momo | 4 ++++ tests/parse_equality.momo | 4 ++++ tests/parse_factor.momo | 4 ++++ tests/parse_primary.momo | 7 ++++++ tests/parse_term.momo | 4 ++++ tests/parse_unary.momo | 7 ++++++ 7 files changed, 59 insertions(+), 16 deletions(-) create mode 100644 tests/parse_comparision.momo create mode 100644 tests/parse_equality.momo create mode 100644 tests/parse_factor.momo create mode 100644 tests/parse_primary.momo create mode 100644 tests/parse_term.momo create mode 100644 tests/parse_unary.momo diff --git a/main.c b/main.c index 5ad46a9..2dc815e 100644 --- a/main.c +++ b/main.c @@ -26,17 +26,18 @@ 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 index 0000000..9d830ac --- /dev/null +++ b/tests/parse_comparision.momo @@ -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 index 0000000..63838cb --- /dev/null +++ b/tests/parse_equality.momo @@ -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 index 0000000..1b36ced --- /dev/null +++ b/tests/parse_factor.momo @@ -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 index 0000000..c119825 --- /dev/null +++ b/tests/parse_primary.momo @@ -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 index 0000000..8fb8cd6 --- /dev/null +++ b/tests/parse_term.momo @@ -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 index 0000000..e118844 --- /dev/null +++ b/tests/parse_unary.momo @@ -0,0 +1,7 @@ +!1; +!true; +!!true; +-1; +-number; +!bar; +-baz; -- 2.39.5