From 5b37e6cb41239aeaafa5b059b9e2240cfe4c6bcd Mon Sep 17 00:00:00 2001 From: momoyon Date: Sun, 27 Apr 2025 21:02:43 +0500 Subject: [PATCH] [main.c] Able to use identifiers before declaring them. --- main.c | 42 ++++++++++++++++++++++++++++-------------- main.momo | 8 +------- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/main.c b/main.c index f311b25..304b203 100644 --- a/main.c +++ b/main.c @@ -352,6 +352,8 @@ typedef struct { String_view name; Literal value; Literal_kind value_kind; + bool not_declared; + Primary_expression *prim_expr; } Identifier; typedef struct { @@ -440,15 +442,16 @@ void print_primary_expression(FILE *f, Primary_expression *pe) { if (pe->kind == PRIMARY_VALUE) { print_literal(f, pe->value, pe->value_kind); } else if (pe->kind == PRIMARY_IDENT) { - /*Identifier ident = identifiers.items[pe->identifier_id];*/ - /**/ - /*fprintf(f, "[IDENT] '"SV_FMT"': ", SV_ARG(ident.name));*/ - /*if (ident.value_set) {*/ - /* print_literal(f, ident.value, ident.value_kind);*/ - /*} else {*/ - /* fprintf(f, "");*/ - /*}*/ - ASSERT(false, "UNREACHABLE!"); + Identifier_KV *ident_kv = hmgetp_null(identifier_map, pe->identifier_key); + ASSERT(ident_kv != NULL, "The identifier should be in the identifier_map!"); + Identifier ident = ident_kv->value; + + fprintf(f, "[IDENT] '"SV_FMT"': ", SV_ARG(ident.name)); + if (ident.not_declared) { + fprintf(f, ""); + } else { + print_literal(f, ident.value, ident.value_kind); + } } else { ASSERT(false, "UNREACHABLE!"); } @@ -683,16 +686,27 @@ Expression *parse_primary(Arena *arena, Parser *p) { parser_advance(p); if (t.type == TK_IDENT) { Identifier_KV *ident_kv = hmgetp_null(identifier_map, t.lexeme); + // We found a non-declared identifier being used, add it to the identifier_map marking it as non-declared if (ident_kv == NULL) { - error_pretty(t.loc, (*p->lexer), "Undeclared identifier `"SV_FMT"`", SV_ARG(t.lexeme)); - return NULL; - } + Identifier ident = {0}; + ident.name = t.lexeme; + ident.not_declared = true; + hmput(identifier_map, t.lexeme, ident); + ident_kv = hmgetp_null(identifier_map, t.lexeme); + } + ASSERT(ident_kv != NULL, "We fucked something up above!"); Identifier ident = ident_kv->value; expr->prim_expr->identifier_key = t.lexeme; - expr->prim_expr->value = ident.value; - expr->prim_expr->value_kind = ident.value_kind; + if (ident.not_declared) { + // If the ident is not declared yet, mark the primary_expr it needs to update the value of for later. + ident_kv->value.prim_expr = expr->prim_expr; + } else { + // If the ident is declared, set the value of the expression! + expr->prim_expr->value = ident.value; + expr->prim_expr->value_kind = ident.value_kind; + } expr->prim_expr->kind = PRIMARY_IDENT; return expr; diff --git a/main.momo b/main.momo index 68603fe..6c124d0 100644 --- a/main.momo +++ b/main.momo @@ -1,7 +1 @@ -1 / (1 - 0); -2 * (1 + 3); -(5 + 3) * (2 / 1) - (10 / (2 - 2)); -(4 * 4) + (6 * (3 - 1)) / (2 + 2) % 3; -(7 * (2 + 3) - (1 * 1)) / (5 - 2) + (8 % 3); -(10 / (2 + 3)) + (3 * 3) * (1 - 4) % 2; -(2 * 2 * 2 * 2 * 2) - (3 * (4 + 1) / (2 - 2)) + (6 % 4); +foo + 1; -- 2.39.5