]> git.mdlowis.com Git - proto/obnc.git/commitdiff
implemented constant identifier lookups
authormike lowis <mike@mdlowis.com>
Wed, 21 Apr 2021 03:33:30 +0000 (23:33 -0400)
committermike lowis <mike@mdlowis.com>
Wed, 21 Apr 2021 03:33:30 +0000 (23:33 -0400)
cerise/parser.c

index 6fb18d1559e3571bfb4dfc0494cdc8888259816f..bdebddbb617c21488bb41f8434e4f44905990b68 100644 (file)
@@ -111,6 +111,20 @@ static Symbol* symbol_new(Parser* p, char* name, int class, bool export)
     return sym;
 }
 
+static Symbol* symbol_get(Parser* p, char* name)
+{
+    Symbol* sym = p->scope;
+    for (; sym; sym = sym->next)
+    {
+        if (!strcmp(sym->name, name))
+        {
+            return sym;
+        }
+    }
+    error(p, "unknown identifier '%s'", name);
+    return NULL;
+}
+
 /* Type Checking
  *****************************************************************************/
 static void check_int(Parser* p, Item* item)
@@ -476,25 +490,25 @@ RULE(factor)
             check_bool(p, item);
             codegen_unop(NOT, item);
             break;
-//
-//        case IDENT:
-//            designator(p, item);
-////            actual_params(p);
-//            break;
+
+        case IDENT:
+            designator(p, item);
+//            actual_params(p);
+            break;
     }
 }
 
-///*
-//    designator = qualident {selector}.
-//    selector = "." ident | "[" ExpList "]" | "^" | "(" qualident ")".
-//    ExpList = expression {"," expression}.
-//    qualident = [ident "."] ident.
-//
-//    ActualParameters = "(" [ExpList] ")" .
-//*/
-//RULE(designator)
-//{
-//    qualident(p);
+/*
+    designator = qualident {selector}.
+    selector = "." ident | "[" ExpList "]" | "^" | "(" qualident ")".
+    ExpList = expression {"," expression}.
+    qualident = [ident "."] ident.
+
+    ActualParameters = "(" [ExpList] ")" .
+*/
+RULE(designator)
+{
+    qualident(p, item);
 //    /* selector */
 //    switch ((int)peek(p)->type)
 //    {
@@ -516,16 +530,23 @@ RULE(factor)
 //            expect(p, ')');
 //            break;
 //    }
-//}
-//
-//RULE(qualident)
-//{
-//    expect(p, IDENT);
+}
+
+RULE(qualident)
+{
+    char* name = expect_text(p, IDENT);
+    Symbol* sym = symbol_get(p, name);
+
+    if (sym->class == SYM_CONST)
+    {
+        item->mode = ITEM_CONST;
+        item->imm = sym->imm;
+    }
 //    if (accept(p, '.'))
 //    {
 //        expect(p, IDENT);
 //    }
-//}
+}
 //
 //RULE(expr_list)
 //{
@@ -646,6 +667,8 @@ TEST_SUITE(Grammar)
             "FOO = 5 > 3");
         parse_rule(const_decl, 0,
             "FOO = 5 >= 3");
+        parse_rule(const_decl, 0,
+            "FOO = 5, BAR = FOO - 3");
     }
 }
 #endif