From 60140d90caa056cd3cea57d86781bebd07284e4c Mon Sep 17 00:00:00 2001 From: mike lowis Date: Tue, 11 May 2021 22:03:58 -0400 Subject: [PATCH] added grammar rule for record fields. adding fields doesnt work yet though --- cerise/src/grammar.c | 81 +++++++++++++++++++++++++++++++++++++++---- cerise/tests/Module.m | 12 ++----- 2 files changed, 77 insertions(+), 16 deletions(-) diff --git a/cerise/src/grammar.c b/cerise/src/grammar.c index 2bbdf9b..f303994 100644 --- a/cerise/src/grammar.c +++ b/cerise/src/grammar.c @@ -70,16 +70,22 @@ static Symbol RealSym = { *****************************************************************************/ static void init_item(Item* item, Symbol* sym) { + item->type = sym->type; if (SYM_VAR == sym->class) { item->mode = (sym->global ? ITEM_MVAR : ITEM_VAR); + item->imm.s = sym->name; + } + else if (SYM_PROC == sym->class) + { + item->mode = ITEM_MVAR; + item->imm.s = sym->name; } else { item->mode = ITEM_CONST; + item->imm = sym->imm; } - item->imm = sym->imm; - item->type = sym->type; } /* Grammar Definition @@ -91,10 +97,6 @@ RULE(qualident) char* name = expect_text(p, IDENT); Symbol* sym = symbol_get(p, -1, name); init_item(item, sym); - if (sym->class == SYM_VAR || sym->class == SYM_PROC) - { - item->imm.s = sym->name; - } // if (accept(p, '.')) // { @@ -122,7 +124,6 @@ RULE(designator) if (item->type->form == FORM_ARRAY) { codegen_index(p, item, &index); -// item->type = item->type->base; } else { @@ -345,6 +346,72 @@ RULE(type) } else if (accept(p, RECORD)) { + + +// do +// { +// int nsyms = 0; +// Symbol* first = NULL; +// Symbol* sym = NULL; +//// Symbol* type = NULL; +// char* name = NULL; +// bool export = false; +// +// do +// { +// name = expect_text(p, IDENT); +// export = accept(p, '*'); +// sym = symbol_new(p, name, SYM_VAR, export); +// first = (nsyms == 0 ? sym : first); +// sym->global = (p->level <= 1 ? 1 : 0); +// nsyms++; +// } +// while (accept(p, ',')); +// +// Item base_type = {0}; +// expect(p, ':'); +// type(p, &base_type); +// +//// name = expect_text(p, IDENT); +//// type = symbol_get(p, SYM_TYPE, name); +// +// /* apply the type to the newly created symbols */ +// for (int i = 0; i < nsyms; i++) +// { +// first->type = base_type.type; +// codegen_var(p, first); +// sym = first->next; +// } +// } +// while (matches(p, IDENT)); + + while (peek(p)->type != END) + { + int nsyms = 0; + Symbol head = {0}; + Symbol **prev = &(head.next); + do + { + char* name = expect_text(p, IDENT); + bool export = accept(p, '*'); + *prev = symbol_new(p, name, SYM_VAR, export); + nsyms++; + } + while (accept(p, ',')); + + Item base_type = {0}; + expect(p, ':'); + type(p, &base_type); + +// /* apply the type to the newly created symbols */ +// for (int i = 0; i < nsyms; i++) +// { +// first->type = base_type.type; +// codegen_var(p, first); +// sym = first->next; +// } + } + expect(p, END); } else diff --git a/cerise/tests/Module.m b/cerise/tests/Module.m index 7e69119..f0c7e9a 100644 --- a/cerise/tests/Module.m +++ b/cerise/tests/Module.m @@ -13,7 +13,8 @@ type TypeB = array 5 of Int TypeC = array 5 of array 10 of Int TypeD = record - + x,y : Int + label : array 10 of Real end var @@ -29,11 +30,6 @@ procedure Foo*(e : Int, z : Int) : Int var z : Int q : array 5 of array 10 of Int - -# procedure Bar() : Int -# begin -# end - begin c = 1; z = 2; @@ -103,7 +99,7 @@ begin # c = 3; # end # - # Function calls +# # Function calls # c = Foo(1,2); # e[0] = 1; # Foo(1,2); @@ -112,5 +108,3 @@ begin e[1][2] = 1; c = e[1][c]; end - -# _T5 _T10 \ No newline at end of file -- 2.49.0