From: momoyon Date: Sat, 12 Apr 2025 16:43:54 +0000 (+0500) Subject: Update precedence table X-Git-Url: https://www.git.momoyon.org/?a=commitdiff_plain;h=104289d5c66d1cb64b5d60bd49c3aca25161aa58;p=lang.git Update precedence table --- diff --git a/main.c b/main.c index 0760eb0..a7bd6eb 100644 --- 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); diff --git a/main.momo b/main.momo index 3a2e3f4..16a562a 100644 --- a/main.momo +++ b/main.momo @@ -1 +1 @@ --1 +690 / (34 + 35) * 69;