]> www.git.momoyon.org Git - lang.git/commitdiff
Update precedence table
authormomoyon <ahmedsamyh10@gmail.com>
Sat, 12 Apr 2025 16:43:54 +0000 (21:43 +0500)
committermomoyon <ahmedsamyh10@gmail.com>
Sat, 12 Apr 2025 16:43:54 +0000 (21:43 +0500)
main.c
main.momo

diff --git a/main.c b/main.c
index 0760eb0ef845b8eeb72d68adbf123f5ab743a836..a7bd6ebc968c25b2562c0854d32dc2fbf0531a01 100644 (file)
--- a/main.c
+++ b/main.c
@@ -12,7 +12,7 @@
 
 static bool DEBUG_PRINT = false;
 
-// TODO:Implement every expression parsing for C: https://en.cppreference.com/w/c/language/operator_precedence
+// TODO:Implement every expression parsing for C:
 // expression     → equality ;
 // equality       → comparison ( ( "!=" | "==" ) comparison )* ;
 // comparison     → term ( ( ">" | ">=" | "<" | "<=" ) term )* ;
@@ -23,6 +23,81 @@ static bool DEBUG_PRINT = false;
 // primary        → NUMBER | STRING | "true" | "false" | "nil"
 //                | "(" expression ")" ;
 
+/* NOTE: We are referencing this table: https://en.cppreference.com/w/c/language/operator_precedence
+ * PRECEDENCE TABLE
+ *
+ * LOW
+ *  |
+ *  v
+ * HIGH
+ *
+ * NAME                | OP                                | ASSOCIATE
+ * --------------------+-----------------------------------+-----------
+ * Comma               | ,                                 | Left
+ * --------------------+-----------------------------------+-----------
+ * Bitwise Assignment  | &= |= ^=                          | Right
+ * --------------------+-----------------------------------+-----------
+ * Bitshift Assignment | <<= >>=                           | Right
+ * --------------------+-----------------------------------+-----------
+ * Factor Assignment   | /= *= %=                          | Right
+ * --------------------+-----------------------------------+-----------
+ * Term Assignment     | += -=                             | Right
+ * --------------------+-----------------------------------+-----------
+ * Simple Assignment   | =                                 | Right
+ * --------------------+-----------------------------------+-----------
+ * Ternary Condition   | ?:                                | Right
+ * --------------------+-----------------------------------+-----------
+ * Logical OR          | ||                                | Left
+ * --------------------+-----------------------------------+-----------
+ * Logical AND         | &&                                | Left
+ * --------------------+-----------------------------------+-----------
+ * Bitwise OR          | |                                 | Left
+ * --------------------+-----------------------------------+-----------
+ * Bitwise XOR         | ^                                 | Left
+ * --------------------+-----------------------------------+-----------
+ * Bitwise AND         | &                                 | Left
+ * --------------------+-----------------------------------+-----------
+ * Equality            | == !=                             | Left
+ * --------------------+-----------------------------------+-----------
+ * Comparision         | > >= < <=                         | Left
+ * --------------------+-----------------------------------+-----------
+ * Bit shift           | << >>                             | Left
+ * --------------------+-----------------------------------+-----------
+ * Term                | - +                               | Left
+ * --------------------+-----------------------------------+-----------
+ * Factor              | / * %                             | Left
+ * --------------------+-----------------------------------+-----------
+ * sizeof              | sizeof                            | Right
+ * --------------------+-----------------------------------+-----------
+ * Address-of          | &                                 | Right
+ * --------------------+-----------------------------------+-----------
+ * Dereference         | *                                 | Right
+ * --------------------+-----------------------------------+-----------
+ * Cast                | (type)                            | Right
+ * --------------------+-----------------------------------+-----------
+ * L/B NOT             | ! ~                               | Right
+ * --------------------+-----------------------------------+-----------
+ * Unary Plus/Minus    | + -                               | Right
+ * --------------------+-----------------------------------+-----------
+ * Prefix Inc/Dec      | ++ --                             | Right
+ * --------------------+-----------------------------------+-----------
+ * Compound Lit        | (type){list}                      | Left
+ * --------------------+-----------------------------------+-----------
+ * Struct/Union access | .                                 | Left      NOTE: We use . to access through pointers as well
+ * --------------------+-----------------------------------+-----------
+ * Array Subscripting  | []                                | Left
+ * --------------------+-----------------------------------+-----------
+ * Function Call       | ()                                | Left
+ * --------------------+-----------------------------------+-----------
+ * Suffix Inc/Dec      | ++ --                             | Left
+ * --------------------+-----------------------------------+-----------
+ *
+ * --------------------+-----------------------------------+-----------
+ * Primary             | IDENTS NUMBERS (expr)             | -
+ * --------------------+-----------------------------------+-----------
+ */
+
+
 /// NOTE: Location
 typedef struct {
     const char *filename;
@@ -340,30 +415,6 @@ void print_expression(FILE *f, Expression e) {
 }
 
 
-/*
- * PRECEDENCE TABLE
- *
- * LOW
- *  |
- *  v
- * HIGH
- *
- * NAME         | OP                    | ASSOCIATE
- * -------------+-----------------------+-----------
- * Equality     | == !=                 | Left
- * -------------+-----------------------+-----------
- * Comparision  | > >= < <=             | Left
- * -------------+-----------------------+-----------
- * Term         | - +                   | Left
- * -------------+-----------------------+-----------
- * Factor       | / *                   | Left
- * -------------+-----------------------+-----------
- * Unary        | ! -                   | Right
- * -------------+-----------------------+-----------
- * Primary      | IDENTS NUMBERS (expr) | -
- *
- */
-
 const char *token_type_as_str(Token_type t) {
     switch (t) {
         case TK_IDENT: return "IDENT";
@@ -702,7 +753,8 @@ Expression *equality(Arena *arena, Parser *p) {
 }
 
 Expression *expression(Arena *arena, Parser *p) {
-    return equality(arena, p);
+    Expression *expr = equality(arena, p);
+    return expr;
 }
 
 Lexer make_lexer(const char *filename) {
@@ -1393,6 +1445,11 @@ int main(int argc, char **argv) {
 
     Expression *expr = expression(&expr_arena, &p);
 
+    if (!parser_match(&p, TK_SEMICOLON)) {
+        compiler_error(parser_previous(&p).loc, "Expected semicolon but got '%s'", token_type_as_str(parser_previous(&p).type));
+        return 1;
+    }
+
     print_expression(stdout, *expr); printf("\n");
 
     arena_free(&expr_arena);
index 3a2e3f4984a0ee55900f8c7894844c563d2c2744..16a562a2e8d97cc8c2e69b0716a06ba710a42c86 100644 (file)
--- a/main.momo
+++ b/main.momo
@@ -1 +1 @@
--1
+690 / (34 + 35) * 69;