From 878e181d3d0d0add1b7a45dd91244a089d1e7b19 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Thu, 17 Nov 2022 07:36:13 -0500 Subject: [PATCH] started implementingmodule symbol files --- cerise/src/grammar.c | 121 +----------------------------------------- cerise/src/sym.c | 85 ++++++++++++----------------- cerise/tests/Module.m | 12 ++--- 3 files changed, 40 insertions(+), 178 deletions(-) diff --git a/cerise/src/grammar.c b/cerise/src/grammar.c index 8948a42..f6e0546 100644 --- a/cerise/src/grammar.c +++ b/cerise/src/grammar.c @@ -625,10 +625,6 @@ static void module(Parser* p) ENTER_RULE(); size_t scope = symbol_openscope(p); - expect(p, MODULE); - p->name = expect_text(p, IDENT); - /* TODO: Check that it matches filename here */ - if (matches(p, IMPORT)) { import_list(p); @@ -691,7 +687,7 @@ static void module(Parser* p) error(p, "expected end of file"); } -// symbol_export(p, NULL); + symbol_export(p, NULL); symbol_closescope(p, scope); EXIT_RULE(); } @@ -744,118 +740,3 @@ void compile(char* fname) codegen_init(p); module(p); } - -/* Grammar Unit Tests - *****************************************************************************/ -#ifdef CERISE_TESTS -#include "atf.h" - -//Parser Ctx = {0}; -// -//static void parse_init(char* fname, char* string) -//{ -// memset(&Ctx, 0, sizeof(Ctx)); -// symbol_new(&Ctx, 0, "Bool", SYM_TYPE, 0)->type = &BoolType; -// symbol_new(&Ctx, 0, "Int", SYM_TYPE, 0)->type = &IntType; -// symbol_new(&Ctx, 0, "Real", SYM_TYPE, 0)->type = &RealType; -// symbol_new(&Ctx, 0, "String", SYM_TYPE, 0)->type = &StringType; -// LexFile* file = calloc(sizeof(LexFile), 1u); -// file->path = strdup(fname); -// file->fbeg = file->fpos = strdup(string); -// file->next = Ctx.file; -// Ctx.file = file; -//} -// -//static void parse_rule(void (*rule)(Parser*, Item*), Item* item, char* string) -//{ -//// puts(""); -//// printf("%s\n", string); -// parse_init("test_input", string); -// rule(&Ctx, (item ? item : &(Item){0})); -//} -// -//static void parse_module(char* fname, char* string) -//{ -// parse_init(fname, string); -// module(&Ctx, &(Item){0}); -//} - -TEST_SUITE(Grammar) -{ -// TEST(Should parse basic module syntax) -// { -// parse_module("Empty", "module Empty"); -// parse_module("ModA", "module ModA import ModB"); -// parse_module("ModA", "module ModA const FOO = 42"); -// parse_module("ModA", "module ModA var foo : Int"); -// } -// -// TEST(Should parse imports) -// { -// parse_rule(import_list, 0, "import A;"); -// parse_rule(import_list, 0, "import A = ModA;"); -// parse_rule(import_list, 0, "import A, B;"); -// parse_rule(import_list, 0, "import A, B = ModB, C;"); -// } -// -// TEST(Should parse constant declarations and expressions) -// { -// parse_rule(const_decl, 0, "FOO = 123"); -// parse_rule(const_decl, 0, "FOO = 123.123"); -// parse_rule(const_decl, 0, "FOO = \"\""); -// parse_rule(const_decl, 0, "FOO = true"); -// parse_rule(const_decl, 0, "FOO = false"); -//// parse_rule(const_decl, 0, "FOO = nil"); -// parse_rule(const_decl, 0, "FOO = not true"); -// parse_rule(const_decl, 0, "FOO = (not false)"); -// parse_rule(const_decl, 0, "FOO = +1"); -// parse_rule(const_decl, 0, "FOO = -1"); -// parse_rule(const_decl, 0, "FOO = +1.0"); -// parse_rule(const_decl, 0, "FOO = -1.0"); -// parse_rule(const_decl, 0, "FOO = 1 + 2"); -// parse_rule(const_decl, 0, "FOO = 1.0 + 2.0"); -// parse_rule(const_decl, 0, "FOO = 1 - 2"); -// parse_rule(const_decl, 0, "FOO = 1.0 - 2.0"); -// parse_rule(const_decl, 0, "FOO = 3 * 2"); -// parse_rule(const_decl, 0, "FOO = 3.0 * 2.0"); -// parse_rule(const_decl, 0, "FOO = 4 / 2"); -// parse_rule(const_decl, 0, "FOO = 3.0 / 4.0"); -// parse_rule(const_decl, 0, "FOO = 5 % 3"); -// parse_rule(const_decl, 0, "FOO = 5 == 3"); -// parse_rule(const_decl, 0, "FOO = 5 != 3"); -// parse_rule(const_decl, 0, "FOO = 5 < 3"); -// parse_rule(const_decl, 0, "FOO = 5 <= 3"); -// parse_rule(const_decl, 0, "FOO = 5 > 3"); -// parse_rule(const_decl, 0, "FOO = 5 >= 3"); -// parse_rule(const_decl, 0, "FOO = 5, BAR = FOO - 3"); -// } -// -// TEST(Should parse module level type declarations) -// { -// parse_rule(type_decl, 0, "a = Int"); -// parse_rule(type_decl, 0, "a = Real"); -// parse_rule(type_decl, 0, "a = Bool"); -// parse_rule(type_decl, 0, "a = array 42 of Int"); -// parse_rule(type_decl, 0, "a = array 42 of array 42 of Int"); -// parse_rule(type_decl, 0, "a = record end"); -// } -// -// TEST(Should parse module level variable declarations) -// { -// parse_rule(var_decl, 0, "i : Int"); -// parse_rule(var_decl, 0, "i : Real"); -// parse_rule(var_decl, 0, "i : Bool"); -//// parse_rule(var_decl, 0, "x,y,z : Int"); -// } -// -// TEST(Should parse module level procedure declarations) -// { -// parse_rule(proc_decl, 0, -// "procedure Foo*() : Int\n" -// "begin\n" -// " return 1;\n" -// "end" -// ); -// } -} -#endif diff --git a/cerise/src/sym.c b/cerise/src/sym.c index ca7764e..6c3704a 100644 --- a/cerise/src/sym.c +++ b/cerise/src/sym.c @@ -149,16 +149,16 @@ void symbol_closescope(Parser* p, size_t scope) /* Symbol File Generation *****************************************************************************/ -static const char TypeIdents[FORM_COUNT] = { - [FORM_BOOL] = 'b', - [FORM_INT] = 'i', - [FORM_REAL] = 'r', - [FORM_ARRAY] = 'a', - [FORM_STRING] = 's', - [FORM_RECORD] = 'r', - [FORM_PROC] = 'p', - [FORM_VOID] = 'v', -}; +//static const char TypeIdents[FORM_COUNT] = { +// [FORM_BOOL] = 'b', +// [FORM_INT] = 'i', +// [FORM_REAL] = 'r', +// [FORM_ARRAY] = 'a', +// [FORM_STRING] = 's', +// [FORM_RECORD] = 'r', +// [FORM_PROC] = 'p', +// [FORM_VOID] = 'v', +//}; static const char SymTypes[5] = { [SYM_MODULE] = 'M', @@ -168,62 +168,43 @@ static const char SymTypes[5] = { [SYM_PROC] = 'P', }; -static size_t typeid(Parser* p, Type* type) -{ - if (type == NULL) return -1; - size_t i; - for (i = 0; i < p->ntypes; i++) - { - if (p->types[i] == type) - break; - } - assert(i < p->ntypes); - return i; -} - -static char* modname(Parser* p, Symbol* sym) +//static size_t typeid(Parser* p, Type* type) +//{ +// if (type == NULL) return -1; +// size_t i; +// for (i = 0; i < p->ntypes; i++) +// { +// if (p->types[i] == type) +// break; +// } +// assert(i < p->ntypes); +// return i; +//} + +static void print_type(Parser* p, Type* type) { - char* name = p->name; - if (sym->module > 0) - { - sym = symbol_getbyid(p, sym->module); - name = sym->name; - } - return name; + (void)p; + printf("%p\n", type); } void symbol_export(Parser* p, char* path) { (void)path; - for (size_t i = 0; i < p->ntypes; i++) - { - printf("t %ld %c\n", i, TypeIdents[p->types[i]->form]); - } +// for (size_t i = 0; i < p->ntypes; i++) +// { +// printf("t %ld %c\n", i, TypeIdents[p->types[i]->form]); +// } for (size_t i = 0; i < p->nsyms; i++) { Symbol* sym = &(p->syms[i]); if (!sym->export) continue; - if (sym->class == SYM_MODULE && sym->module != 0) continue; - printf("%c %s %s ", + printf("%c %s ", SymTypes[sym->class], - modname(p, sym), sym->name); - - switch (sym->class) - { - case SYM_MODULE: - printf("\n"); - break; - - case SYM_CONST: - case SYM_VAR: - case SYM_TYPE: - case SYM_PROC: - printf("%ld\n", typeid(p, sym->type)); - break; - } + print_type(p, sym->type); +// puts(""); } } diff --git a/cerise/tests/Module.m b/cerise/tests/Module.m index 91333c9..9e9f11a 100644 --- a/cerise/tests/Module.m +++ b/cerise/tests/Module.m @@ -1,7 +1,7 @@ -module Module - +const + FOO* = 42 type - FooRec = record + FooRec* = record a : Int b : record c : Int @@ -21,11 +21,11 @@ var b : array 5 of Int end -procedure TestReturnVoid() +procedure TestReturnVoid*() begin end -procedure TestReturnIntLiteral() : Int +procedure TestReturnIntLiteral*() : Int begin return 0; end @@ -182,7 +182,7 @@ begin vRec1.b.c = vRec1.b.c + vRec1.b.c; end -procedure Sum(a: Int, b : Int) : Int +procedure Sum*(a: Int, b : Int) : Int begin return a + b; end -- 2.49.0