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 )* ;
// 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;
}
-/*
- * 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";
}
Expression *expression(Arena *arena, Parser *p) {
- return equality(arena, p);
+ Expression *expr = equality(arena, p);
+ return expr;
}
Lexer make_lexer(const char *filename) {
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);