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));
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();
}
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,
.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);
}
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