]> www.git.momoyon.org Git - lang.git/commitdiff
[main.c] Able to use identifiers before declaring them.
authormomoyon <ahmedsamyh10@gmail.com>
Sun, 27 Apr 2025 16:02:43 +0000 (21:02 +0500)
committermomoyon <ahmedsamyh10@gmail.com>
Sun, 27 Apr 2025 16:15:25 +0000 (21:15 +0500)
main.c
main.momo

diff --git a/main.c b/main.c
index f311b254c8080cba4a62a6aeed903898118c0587..304b203ad6d2b509366f5711d52f1ccb5b60342a 100644 (file)
--- 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, "<NOTSET>");*/
-        /*}*/
-        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, "<NOTSET>");
+        } 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;
index 68603fe9ed80a8dcae10c7ca9acb68dbd99d866f..6c124d0c0a1adf3a77b01e533defc2f0ea8c95c2 100644 (file)
--- 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;