]> www.git.momoyon.org Git - lang.git/commitdiff
[main.c] WIP: Expression parsing.
authorahmedsamyh <ahmedsamyh10@gmail.com>
Tue, 8 Apr 2025 23:52:08 +0000 (04:52 +0500)
committerahmedsamyh <ahmedsamyh10@gmail.com>
Tue, 8 Apr 2025 23:52:08 +0000 (04:52 +0500)
main.c

diff --git a/main.c b/main.c
index ac183c0c1fa44a7852f047bfb0861ff0ae98f073..e47315cc98c5aaca75d324ef5dc1cfc7e87082e5 100644 (file)
--- a/main.c
+++ b/main.c
@@ -120,8 +120,14 @@ typedef struct {
     size_t capacity;
 } Tokens;
 
+
+bool token_is_number(Token t) {
+    return t.type == TK_INT || t.type == TK_FLOAT;
+}
+
 typedef struct Expression Expression;
-typedef struct Binary_expression Binary_expression;
+typedef struct Unary_expression Unary_expression;
+typedef struct Primary_expression Primary_expression;
 typedef struct Grouping Grouping;
 typedef struct Literal Literal;
 
@@ -139,16 +145,71 @@ struct Literal {
     } as;
 };
 
-struct Binary_expression {
-    Expression *lhs;
-    Token      *op;
-    Expression *rhs;
+struct Unary_expression {
+    Token operator;
+    Expression *operand;
+};
+
+struct Primary_expression {
+    Literal value;
 };
 
+typedef enum {
+    EXPR_EQUALITY,
+    EXPR_COMPARISION,
+    EXPR_TERM,
+    EXPR_FACTOR,
+    EXPR_UNARY,
+    EXPR_PRIMARY,
+    EXPR_COUNT,
+} Expression_kind;
+
+const char *expression_kind_as_str(Expression_kind k) {
+    switch (k) {
+        case EXPR_EQUALITY: return "EQUALITY";
+        case EXPR_COMPARISION: return "COMPARISION";
+        case EXPR_TERM: return "TERM";
+        case EXPR_FACTOR: return "FACTOR";
+        case EXPR_UNARY: return "UNARY";
+        case EXPR_PRIMARY: return "PRIMARY";
+        case EXPR_COUNT:
+        default: ASSERT(false, "UNREACHABLE!");
+    }
+
+    return "YOU SHOULD NOT SEE THIS!";
+}
+
 struct Expression {
-    Binary_expression bin_expr;
+    Expression_kind kind;
+    Primary_expression prim_expr;
+    Unary_expression una_expr;
 };
 
+
+/*
+ * PRECEDENCE TABLE
+ *
+ * LOW
+ *  |
+ *  v
+ * HIGH
+ *
+ * NAME         | OP             | ASSOCIATE
+ * -------------+----------------+-----------
+ * Equality     | == !=          | Left
+ * -------------+----------------+-----------
+ * Comparision  | > >= < <=      | Left
+ * -------------+----------------+-----------
+ * Term         | - +            | Left
+ * -------------+----------------+-----------
+ * Factor       | / *            | Left
+ * -------------+----------------+-----------
+ * Unary        | ! -            | Right
+ * -------------+----------------+-----------
+ * Primary      | NUMBERS (expr) | -
+ *
+ */
+
 typedef struct {
     // NOTE: src gets data from a heap allocated string!!!
     String_view src;
@@ -294,6 +355,56 @@ void print_token(FILE *f, Token t) {
     fprintf(f, " [%s] '"SV_FMT"'", token_type_as_str(t.type), SV_ARG(t.lexeme));
 }
 
+
+Expression *primary(Arena *arena, Parser *p) {
+    Tokens tokens = p->tokens;
+    Token t = da_shift(tokens);
+
+    Expression *expr = (Expression *)arena_alloc(arena, sizeof(Expression));
+    expr->type = EXPR_PRIMARY;
+
+    if (token_is_number(t)) {
+        if (t.type == TK_INT) expr->prim_expr.value.as.i = t.
+    } else if (t.type == TK_STRING) {
+
+    } else if (t.type == TK_BOOL) {
+
+    } else if (t.type == TK_NULL) {
+
+    }
+    // TODO: Else Grouping
+    //
+    ASSERT(false, "UNREACHABLE!");
+}
+
+Expression *unary(Arena *arena, Parser *p) {
+    Tokens tokens = p->tokens;
+    Token t = da_shift(tokens);
+
+    Expression *expr = (Expression *)arena_alloc(arena, sizeof(Expression));
+
+
+    if (t.type == TK_NOT || t.type == TK_MINUS) {
+
+        expr->kind = EXPR_UNARY;
+        Unary_expression *unary = (Unary_expression *)arena_alloc(arena, sizeof(Expression));
+        unary->operator = t;
+        unary->operand = unary(arena, p);
+
+        return expr;
+    }
+
+
+}
+
+void equality(Parser *p) {
+    ASSERT(false, "UNIMPLEMENTED");
+}
+
+void expression(Parser *p) {
+    equality(p);
+}
+
 Lexer make_lexer(const char *filename) {
     bool ok = false;
     const char *buf = slurp_file(filename, &ok);
@@ -921,6 +1032,8 @@ int main(int argc, char **argv) {
 
     Parser p = make_parser(tokens);
 
+    primary(&p);
+
     free_parser(&p);
     free_lexer(&l);
     da_free(flags);