]> www.git.momoyon.org Git - lang.git/commitdiff
[main.c] Fix parse_unary_term to not pares +"string" or -true...
authormomoyon <momoyon@momoyon.org>
Thu, 15 May 2025 06:26:53 +0000 (11:26 +0500)
committermomoyon <momoyon@momoyon.org>
Thu, 15 May 2025 06:26:53 +0000 (11:26 +0500)
- [tests] Update parse_unary.momo

main.c
tests/.parse_unary_term.build.out.expected
tests/parse_unary_term.momo

diff --git a/main.c b/main.c
index caae653947bf863de77e7becf7ba54cca6cf394e..01316b362d19c4496818b3b974684bbc0cfdc9ca 100644 (file)
--- a/main.c
+++ b/main.c
@@ -31,17 +31,15 @@ static bool DEBUG_PRINT = false;
 // comparison      -> term ( ( ">" | ">=" | "<" | "<=" ) term )* ;
 // term            -> factor ( ( "-" | "+" ) factor )* ;
 // factor          -> unary ( ( "/" | "*" ) unary )* ;
-// unary_not       -> ( "!" | "~" ) unary
-//                | primary ;
-// unary_term      -> ( "-" | "+" ) unary
-// prefix          -> ( "++" | "--" ) IDENT
+// unary_not       -> ( "!" | "~" ) unary | primary ;
+// unary_term      -> ( "-" | "+" ) ( NUMBER | IDENT ) ;
+// prefix          -> ( "++" | "--" ) IDENT ;
 // comp.lit        -> Skipped...
-// access          -> IDENT "." ( access | IDENT )
-// subscript       -> IDENT "[" ast "]"
-// funcalls        -> IDENT "(" ( ast "," )* ")" | IDENT "(" ast ")"
-// suffix          -> IDENT ( "++" | "--" )
-// primary         -> NUMBER | STRING | IDENT | "true" | "false" | "null"
-//                | "(" ast ")" ;
+// access          -> IDENT "." ( access | IDENT ) ;
+// subscript       -> IDENT "[" ast "]" ;
+// funcalls        -> IDENT "(" ( ast "," )* ")" | IDENT "(" ast ")" ;
+// 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
  * PRECEDENCE TABLE
@@ -97,7 +95,7 @@ static bool DEBUG_PRINT = false;
  * --------------------+-------------------------------------+-----------+-----
  * L/B NOT             | ! ~                                 | Right     |
  * --------------------+-------------------------------------+-----------+-----
- * Unary Plus/Minus    | + -                                 | Right     |
+ * Unary Plus/Minus    | + -                                 | Right     | X
  * --------------------+-------------------------------------+-----------+-----
  * Prefix Inc/Dec      | ++ --                               | Right     | X
  * --------------------+-------------------------------------+-----------+-----
@@ -1063,8 +1061,21 @@ AST *parse_unary_term(Arena *arena, Parser *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;
+
+        Token next = parser_peek(p);
+
+        if (next.type == TK_INT ||
+            next.type == TK_FLOAT ||
+            next.type == TK_IDENT ||
+            next.type == TK_PLUS ||   // NOTE: Since it could be like Eg: +-ident
+            next.type == TK_MINUS     // Ditto ^
+            ) {
+            ast->unary_term->operand  = parse_unary_term(arena, p);
+            return ast;
+        }
+
+        error_pretty(next.loc, (*p->lexer), "Expected a number or identifier after `"SV_FMT"` but got `%s`", SV_ARG(parser_previous(p).lexeme), token_type_as_str(next.type));
+        return NULL;
     }
 
     return parse_prefix(arena, p);
index dad8c7a70719273a1a7f118ee6a68f0eae5fc0db..5c1304f48281491a43c23d7b03ddb085738299f9 100644 (file)
@@ -3,3 +3,4 @@ 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': ???'
+parse_unary_term.momo:6:0 [UNARY_TERM] ' +  -  +  - 'baz': ???'
index 46ad7ef1173efd5351d7377d25517a713ed0d3b7..8baf0a2b72ce0b7ab70822250213828d0e9bb0ff 100644 (file)
@@ -3,3 +3,4 @@
 -number;
 +bar;
 -+baz;
++-+-baz;