From: Michael D. Lowis Date: Mon, 12 Jul 2021 17:54:51 +0000 (-0400) Subject: re-enabled variable and type declarations X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=361ab2d6000dee9a2d38e9b4beb61d70fa07bacd;p=proto%2Fobnc.git re-enabled variable and type declarations --- diff --git a/cerise/src/grammar.c b/cerise/src/grammar.c index 2070d3b..afcb070 100644 --- a/cerise/src/grammar.c +++ b/cerise/src/grammar.c @@ -326,8 +326,7 @@ static Type* type(Parser* p) if (matches(p, IDENT)) { char* text = expect_text(p, IDENT); - Symbol* sym = symbol_get(p, text, SYM_TYPE); - ret = sym->type; + ret = symbol_get(p, text, SYM_TYPE)->type; } else if (accept(p, ARRAY)) { @@ -344,49 +343,48 @@ static Type* type(Parser* p) ret->size = length; ret->base = base; } -// else if (accept(p, RECORD)) -// { -// long offset = 0; -// item->type = calloc(1, sizeof(Type)); -// item->type->form = FORM_RECORD; -// -// while (peek(p)->type != END) -// { -// /* parse the field name list */ -// Field* first = NULL; -// int nfields = 0; -// do -// { -// char* name = expect_text(p, IDENT); -// bool export = accept(p, '*'); -// Field* f = add_field(p, item->type, name, export); -// nfields++; -// if (!first) -// { -// first = f; -// } -// } -// while (accept(p, ',')); -// -// /* now parse the type designation */ -// Item field_type = {0}; -// expect(p, ':'); -// type(p, &field_type); -// -// /* apply the type to the newly created symbols */ -// for (int i = 0; i < nfields; i++) -// { -// offset = align_item(offset, field_type.type->size); -// first->type = field_type.type; -// first->offset = offset; -// offset += size_of(first->type); -// first = first->next; -// } -// } -// item->type->size = offset; -// -// expect(p, END); -// } + else if (accept(p, RECORD)) + { + long offset = 0; + ret = calloc(1, sizeof(Type)); + ret->form = FORM_RECORD; + + while (peek(p)->type != END) + { + /* parse the field name list */ + Field* first = NULL; + int nfields = 0; + do + { + char* name = expect_text(p, IDENT); + bool export = accept(p, '*'); + Field* f = add_field(p, ret, name, export); + nfields++; + if (!first) + { + first = f; + } + } + while (accept(p, ',')); + + /* now parse the type designation */ + expect(p, ':'); + Type* field_type = type(p); + + /* apply the type to the newly created symbols */ + for (int i = 0; i < nfields; i++) + { + offset = align_item(offset, field_type->size); + first->type = field_type; + first->offset = offset; + offset += size_of(first->type); + first = first->next; + } + } + ret->size = offset; + + expect(p, END); + } else { error(p, "expected a type"); @@ -444,23 +442,20 @@ static Type* type(Parser* p) // EXIT_RULE(); //} -//RULE(var_decl, Item* item) -//{ -// ENTER_RULE(); -// (void)item; -// do -// { -// char* name = expect_text(p, IDENT); -// bool export = accept(p, '*'); -// Symbol* sym = symbol_new(p, 0, name, SYM_VAR, export); -// Item base_type = {0}; -// expect(p, ':'); -// type(p, &base_type); -// sym->type = base_type.type; -// } -// while (matches(p, IDENT)); -// EXIT_RULE(); -//} +static void var_decl(Parser* p) +{ + ENTER_RULE(); + do + { + char* name = expect_text(p, IDENT); + bool export = accept(p, '*'); + Symbol* sym = symbol_new(p, 0, name, SYM_VAR, export); + expect(p, ':'); + sym->type = type(p); + } + while (matches(p, IDENT)); + EXIT_RULE(); +} static void type_decl(Parser* p) { @@ -633,10 +628,10 @@ static void module(Parser* p) type_decl(p); } -// if (accept(p, VAR)) -// { -// var_decl(p); -// } + if (accept(p, VAR)) + { + var_decl(p); + } // while (matches(p, PROCEDURE)) // { diff --git a/cerise/tests/Module.m b/cerise/tests/Module.m index c934edd..7904aee 100644 --- a/cerise/tests/Module.m +++ b/cerise/tests/Module.m @@ -18,28 +18,28 @@ type TypeA = Int TypeB = array 5*B of Int TypeC = array 5 of array 10 of Int -# TypeD = record -# x,y : Int -# label : array 10 of Int -# dim : record -# w,h : Int -# end -# end -# TypeE = record -# i : Int -# a : array 5 of Int -# end -# TypeF = array 5 of TypeE + TypeD = record + x,y : Int + label : array 10 of Int + dim : record + w,h : Int + end + end + TypeE = record + i : Int + a : array 5 of Int + end + TypeF = array 5 of TypeE -#var -# a : Bool -# b : Int -# c : Int -# d : Real -# e : array 5 of array 10 of Int -# f : TypeD -# g : array 5 of Int -# h : TypeF +var + a : Bool + b : Int + c : Int + d : Real + e : array 5 of array 10 of Int + f : TypeD + g : array 5 of Int + h : TypeF #procedure Foo*(e : Int, z : Int, q1 : TypeD, q2 : array 5 of Int) : Int # const FOO = 2