From 5d4b4ef80d8dc427b0e2803d50e439f76f06e17a Mon Sep 17 00:00:00 2001 From: Kimplul Date: Wed, 4 Dec 2024 11:04:16 +0200 Subject: implement expression handling further + Add some notes about returning functions that I started thinking about as a result of the fib example --- src/lower.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'src/lower.c') diff --git a/src/lower.c b/src/lower.c index ad3d1b9..46ce2c3 100644 --- a/src/lower.c +++ b/src/lower.c @@ -44,6 +44,13 @@ static int lower_binop(struct state *state, struct ast *binop) return -1; switch (binop->k) { + case AST_ADD: printf(" + "); break; + case AST_SUB: printf(" - "); break; + case AST_MUL: printf(" * "); break; + case AST_DIV: printf(" / "); break; + case AST_REM: printf(" %% "); break; + case AST_LSHIFT: printf(" << "); break; + case AST_RSHIFT: printf(" >> "); break; default: internal_error("missing binop lowering"); return -1; @@ -63,6 +70,12 @@ static int lower_comparison(struct state *state, struct ast *comp) return -1; switch (comp->k) { + case AST_LT: printf(" < "); break; + case AST_GT: printf(" > "); break; + case AST_LE: printf(" <= "); break; + case AST_GE: printf(" <= "); break; + case AST_NE: printf(" != "); break; + case AST_EQ: printf(" == "); break; default: internal_error("missing comparison lowering"); return -1; @@ -148,8 +161,25 @@ static int lower_init(struct state *state, struct ast *init) return 0; } +static int lower_unop(struct state *state, struct ast *expr) +{ + switch (expr->k) { + case AST_LNOT: printf("-"); break; + case AST_NOT: printf("~"); break; + case AST_NEG: printf("-"); break; + default: + internal_error("missing unop lowering"); + return -1; + } + + return lower_expr(state, unop_expr(expr)); +} + static int lower_expr(struct state *state, struct ast *expr) { + if (is_unop(expr)) + return lower_unop(state, expr); + if (is_binop(expr)) return lower_binop(state, expr); -- cgit v1.2.3