From: Michael D. Lowis Date: Thu, 1 Dec 2022 21:13:47 +0000 (-0500) Subject: added ability to parse without generating code X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=7ee61ac7fbad5dd2639983d5476cd50a7435fa84;p=proto%2Fobnc.git added ability to parse without generating code --- diff --git a/cerise/backend/ssa/codegen.c b/cerise/backend/ssa/codegen.c index a6de7c5..5f414cc 100644 --- a/cerise/backend/ssa/codegen.c +++ b/cerise/backend/ssa/codegen.c @@ -105,7 +105,7 @@ void emit_type(Type* type) void codegen_init(Parser* p) { - (void)p; + p->codegen = true; printf("%%Bool = type i1\n"); printf("%%Int = type i64\n"); printf("%%Real = type double\n"); @@ -114,7 +114,8 @@ void codegen_init(Parser* p) void codegen_symbol(Parser* p, Symbol* sym) { - (void)p; + if (!p->codegen) return; // Bail if just importing symbols + if (sym->class == SYM_VAR) { char* modname = (sym->module == 0 @@ -474,6 +475,8 @@ fflush(stdout); void codegen_block(Parser* p, SsaBlock* block) { + if (!p->codegen) return; // Bail if just importing symbols + /* perform a topological sort of the nodes */ SsaBlock* sorted = NULL; Bitset* set = bitset_new(p->blockid); diff --git a/cerise/inc/cerise.h b/cerise/inc/cerise.h index 952740f..a6b6acc 100644 --- a/cerise/inc/cerise.h +++ b/cerise/inc/cerise.h @@ -229,6 +229,8 @@ typedef struct { size_t mtypes; size_t ntypes; Type** types; + + bool codegen; } Parser; // src/stdlib.c @@ -259,8 +261,6 @@ size_t symbol_getid(Parser* p, size_t module, char* name, int class); Symbol* symbol_getbyid(Parser* p, size_t id); size_t symbol_openscope(Parser* p); void symbol_closescope(Parser* p, size_t scope); -void symbol_import(Parser* p, char* name, char* alias); -void symbol_export(Parser* p, char* path); // src/type_checks.c void check_int(Parser* p, SsaNode* a); diff --git a/cerise/src/grammar.c b/cerise/src/grammar.c index f6e0546..96f6bd9 100644 --- a/cerise/src/grammar.c +++ b/cerise/src/grammar.c @@ -613,7 +613,8 @@ static void import_list(Parser* p) alias = name; name = expect_text(p, IDENT); } - symbol_import(p, name, alias); + (void)name, (void)alias; +// symbol_import(p, name, alias); } while (matches(p, IDENT)); @@ -658,36 +659,34 @@ static void module(Parser* p) sym->type = proctype; codegen_symbol(p, sym); + SsaBlock* block = ssa_block(p); + p->curr_join = ssa_block(p); + block->links[1] = p->curr_join; + if (accept(p, BEGIN)) { - SsaBlock* block = ssa_block(p); - p->curr_join = ssa_block(p); - block->links[1] = p->curr_join; - if (!matches(p, END)) { - SsaBlock* seqblock = statement_seq(p); - block->links[0] = seqblock; - ssa_join(p); - codegen_block(p, block); + block->links[0] = statement_seq(p); } else { - printf("{\n ret void\n}\n"); +// block->links[0] = ssa_block(p); +// ssa_return(p, NULL); } expect(p, END); } else { - printf("{\n ret void\n}\n"); } + ssa_join(p); + codegen_block(p, block); if (!matches(p, END_FILE)) { error(p, "expected end of file"); } - symbol_export(p, NULL); symbol_closescope(p, scope); EXIT_RULE(); } @@ -712,11 +711,30 @@ static inline char* file_load(char* path) return contents; } +static char* get_module_name(char* path) +{ + char* name = NULL; + char* last_slash = strrchr(path, '/'); + if (last_slash) + { + char* last_dot = strrchr(last_slash, '.'); + if (last_dot) + { + name = strndup(last_slash+1, last_dot-last_slash-1); + if (*name == '\0') + { + name = NULL; + } + } + } + return name; +} + void compile(char* fname) { char* fcontents = file_load(fname); - Parser* p = &(Parser){ - .name = NULL, + Parser p = { + .name = get_module_name(fname), .file = &(LexFile){ .path = fname, .fbeg = fcontents, @@ -725,18 +743,23 @@ void compile(char* fname) .curr_reg = 0 }; - p->mtypes = 8; - p->types = calloc(p->mtypes, sizeof(Type*)); - symbol_new(p, 0, "$", SYM_VAR, 0); - symbol_new(p, 0, "Bool", SYM_TYPE, 0)->type = &BoolType; - p->types[p->ntypes++] = &BoolType; - symbol_new(p, 0, "Int", SYM_TYPE, 0)->type = &IntType; - p->types[p->ntypes++] = &IntType; - symbol_new(p, 0, "Real", SYM_TYPE, 0)->type = &RealType; - p->types[p->ntypes++] = &RealType; - symbol_new(p, 0, "String", SYM_TYPE, 0)->type = &StringType; - p->types[p->ntypes++] = &StringType; - - codegen_init(p); - module(p); + if (p.name == NULL) + { + error(&p, "could not determine module name from path"); + } + + p.mtypes = 8; + p.types = calloc(p.mtypes, sizeof(Type*)); + symbol_new(&p, 0, "$", SYM_VAR, 0); + symbol_new(&p, 0, "Bool", SYM_TYPE, 0)->type = &BoolType; + p.types[p.ntypes++] = &BoolType; + symbol_new(&p, 0, "Int", SYM_TYPE, 0)->type = &IntType; + p.types[p.ntypes++] = &IntType; + symbol_new(&p, 0, "Real", SYM_TYPE, 0)->type = &RealType; + p.types[p.ntypes++] = &RealType; + symbol_new(&p, 0, "String", SYM_TYPE, 0)->type = &StringType; + p.types[p.ntypes++] = &StringType; + + codegen_init(&p); + module(&p); } diff --git a/cerise/src/sym.c b/cerise/src/sym.c index 6c3704a..5bb73f6 100644 --- a/cerise/src/sym.c +++ b/cerise/src/sym.c @@ -146,88 +146,6 @@ void symbol_closescope(Parser* p, size_t scope) p->nsyms = 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 SymTypes[5] = { - [SYM_MODULE] = 'M', - [SYM_CONST] = 'C', - [SYM_VAR] = 'V', - [SYM_TYPE] = 'T', - [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 void print_type(Parser* p, Type* type) -{ - (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->nsyms; i++) - { - Symbol* sym = &(p->syms[i]); - if (!sym->export) continue; - - printf("%c %s ", - SymTypes[sym->class], - sym->name); - print_type(p, sym->type); -// puts(""); - } -} - -/* Symbol File Parsing - *****************************************************************************/ - -void symbol_import(Parser* p, char* name, char* alias) -{ - Symbol* mod = symbol_new(p, 0, name, SYM_MODULE, true); - size_t modid = symbol_getid(p, 0, mod->name, SYM_MODULE); - if (alias) - { - Symbol* modalias = symbol_new(p, 0, alias, SYM_MODULE, true); - modalias->module = modid; // Points to the aliased module - } - - /* TODO: read symbols from real symbol file */ - // All of these should set ->module = modid - - Symbol* sym = symbol_new(p, 0, "testint", SYM_VAR, true); - sym->module = modid; -} - /* Symbol Table Unit Tests *****************************************************************************/ #ifdef CERISE_TESTS diff --git a/cerise/tests/Module.m b/cerise/tests/Module.m index 9e9f11a..c961300 100644 --- a/cerise/tests/Module.m +++ b/cerise/tests/Module.m @@ -195,4 +195,5 @@ begin end #begin +# TestReturnVoid(); #end