From 4cfbb12168c9440f0e58a6c084ac3e4928fe47ef Mon Sep 17 00:00:00 2001 From: mike lowis Date: Tue, 20 Apr 2021 23:33:30 -0400 Subject: [PATCH] implemented constant identifier lookups --- cerise/parser.c | 67 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 45 insertions(+), 22 deletions(-) diff --git a/cerise/parser.c b/cerise/parser.c index 6fb18d1..bdebddb 100644 --- a/cerise/parser.c +++ b/cerise/parser.c @@ -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 -- 2.49.0