(void)p;
}
-void codegen_global(Parser* p, Symbol* sym)
+void codegen_var(Parser* p, Symbol* sym)
{
- printf("%s%s %s_%s;\n",
- (sym->export ? "" : "static "), TypeNames[sym->type->form], p->name, sym->name);
+ if (sym->global)
+ {
+ printf("%s%s %s_%s;\n",
+ (sym->export ? "" : "static "), TypeNames[sym->type->form], p->name, sym->name);
+ }
+ else
+ {
+ printf(" %s %s;\n", TypeNames[sym->type->form], sym->name);
+ }
}
void codegen_main(Parser* p)
}
else
{
- assert(!"not implemented");
+ printf("\n%svoid %s_%s(void) {\n", export, p->name, name);
}
}
// TypeNames[item->type->form], p->curr_reg, item->imm.i);
// item->reg = p->curr_reg;
// p->curr_reg++;
-// printf(" const long _T%d = %s(", p->curr_reg, item->imm.s);
-// item->reg = p->curr_reg;
-// p->curr_reg++;
+ printf(" const long _T%d = %s(", p->curr_reg, item->imm.s);
+ item->reg = p->curr_reg;
+ p->curr_reg++;
}
void codegen_call(Parser* p, Item* item)
void codegen_setarg(Parser* p, Item* item, bool firstarg)
{
load_var(p, item);
-// (void)p, (void)item;
-// if (item->mode == ITEM_CONST)
-// {
-// printf("%s%lld", (!firstarg ? ", " : ""), item->imm.i);
-// }
-// else
-// {
-// printf("%s_T%ld", (!firstarg ? ", " : ""), item->reg);
-// }
+ (void)p, (void)item;
+ if (item->mode == ITEM_CONST)
+ {
+ printf("%s%lld", (!firstarg ? ", " : ""), item->imm.i);
+ }
+ else
+ {
+ printf("%s_T%d", (!firstarg ? ", " : ""), item->reg);
+ }
}
(void)p;
}
-void codegen_global(Parser* p, Symbol* sym)
+void codegen_var(Parser* p, Symbol* sym)
{
(void)p, (void)sym;
}
}
}
-void codegen_global(Parser* p, Symbol* sym)
+void codegen_var(Parser* p, Symbol* sym)
{
printf(" .data\n");
printf("%s_%s:\n", p->name, sym->name);
Symbol* scope;
char* name;
int curr_reg;
+ int level;
} Parser;
static inline void item_dump(Item* a)
void codegen_setreal(Item* item, double val);
void codegen_setstr(Item* item, char* val);
void codegen_imports(Parser* p);
-void codegen_global(Parser* p, Symbol* sym);
+void codegen_var(Parser* p, Symbol* sym);
void codegen_main(Parser* p);
void codegen_startproc(Parser* p, char* name, bool exported);
void codegen_endproc(Parser* p);
(void)item;
int nargs = 0;
Item argdef = {0};
+ (void)argdef;
if (!matches(p, ')'))
{
Item arg = {0};
do
{
int nsyms = 0;
+ Symbol* first = NULL;
Symbol* sym = NULL;
Symbol* type = NULL;
char* name = NULL;
name = expect_text(p, IDENT);
export = accept(p, '*');
sym = symbol_new(p, name, SYM_VAR, export);
- sym->global = 1;
+ first = (nsyms == 0 ? sym : first);
+ sym->global = (p->level <= 1 ? 1 : 0);
nsyms++;
}
while (accept(p, ','));
/* apply the type to the newly created symbols */
for (int i = 0; i < nsyms; i++)
{
- sym->type = type->type;
- codegen_global(p, sym);
- sym = sym->next;
+ first->type = type->type;
+ codegen_var(p, first);
+ sym = first->next;
}
}
while (matches(p, IDENT));
Symbol* sym = symbol_new(p, name, SYM_PROC, export);
Symbol** args = &(sym->desc);
symbol_openscope(p);
+ codegen_startproc(p, name, export);
+ (void)args;
/* construct the proc type */
expect(p, '(');
type(p, item);
sym->nargs++;
// TODO: Added argument symbol here...
+ (void)name;
}
expect(p, ')');
if (accept(p, ':'))
}
expect(p, END);
symbol_closescope(p);
+ codegen_endproc(p);
}
Parser Ctx = {0};
static Symbol InitialScope = {
- .next = &IntSym,
+ .next = &RealSym,
.class = SYM_SCOPE,
};
-
static void parse_init(char* fname, char* string)
{
memset(&Ctx, 0, sizeof(Ctx));
Ctx.scope = &InitialScope;
+ InitialScope.desc = NULL;
LexFile* file = calloc(sizeof(LexFile), 1u);
file->path = strdup(fname);
file->fbeg = file->fpos = strdup(string);
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}));
}
"end"
);
}
-
}
#endif
Symbol* symbol_new(Parser* p, char* name, int class, bool export)
{
-// Symbol* scope = p->scope;
-// while ()
-// {
-// }
+ Symbol* prev = NULL;
+ Symbol* curr = p->scope->desc;
+
+// printf("\nsymbol_new(%s)\n", name);
+ while (curr)
+ {
+// printf(" %s <- %s\n", curr->name, name);
+ if (curr->name && !strcmp(curr->name, name))
+ {
+ error(p, "multiple definitions of '%s' in scope", name);
+ }
+ prev = curr;
+ curr = curr->next;
+ }
Symbol* sym = calloc(1, sizeof(Symbol));
sym->name = name;
sym->class = class;
sym->export = export;
- sym->next = p->scope;
- p->scope = sym;
+ if (prev)
+ {
+ prev->next = sym;
+ }
+ else
+ {
+ p->scope->desc = sym;
+ }
+// p->scope->desc = sym;
+ assert(sym);
return sym;
}
Symbol* symbol_get(Parser* p, int class, char* name)
{
- Symbol* scope = p->scope;
- Symbol* sym = NULL;
-
for (Symbol* scope = p->scope; scope; scope = scope->next)
{
- if (scope)
- {
- printf("SCOPE:\n");
- }
+// printf("SCOPE(n: %p):\n", scope->next);
if (sym_matches(p, class, name, scope))
{
for (Symbol* sym = scope->desc; sym; sym = sym->next)
{
- printf(" %s == %s %d\n", name, sym->name, sym_matches(p, class, name, scope));
-
+// printf(" %s == %s %d\n", name, sym->name, sym_matches(p, class, name, scope));
if (sym_matches(p, class, name, sym))
{
return sym;
}
}
}
-
error(p, "unknown identifier '%s'", name);
return NULL;
}
{
Symbol* scope = calloc(1, sizeof(Symbol));
scope->class = SYM_SCOPE;
- scope->desc = p->scope;
- scope->next = NULL;
+ scope->desc = NULL;
+ scope->next = p->scope;
p->scope = scope;
+ p->level++;
}
void symbol_closescope(Parser* p)
{
- p->scope = p->scope->desc;
+ p->scope = p->scope->next;
+ p->level--;
}
+
+/* Symbol Table Unit Tests
+ *****************************************************************************/
+#ifdef CERISE_TESTS
+#include "atf.h"
+
+
+
+#endif
\ No newline at end of file
var
a : Bool
+ z : Int
b : Int
c : Int