From: Michael D. Lowis Date: Fri, 1 Jun 2018 01:31:25 +0000 (-0400) Subject: added symbol table implementation and registered builtins X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=cd151d70fb5e68e026415ff992c2962cdd5cc0da;p=proto%2Fsclpl.git added symbol table implementation and registered builtins --- diff --git a/source/main.c b/source/main.c index 8f9ed76..adc3d7a 100644 --- a/source/main.c +++ b/source/main.c @@ -19,7 +19,8 @@ static void builtins(Parser* p) { sym_addtype(&(p->syms), "i16", IntType(16u)); sym_addtype(&(p->syms), "i32", IntType(32u)); sym_addtype(&(p->syms), "i64", IntType(64u)); - sym_addtype(&(p->syms), "string", ArrayOf(sym_get(&(p->syms), "byte"), -1)); + sym_addtype(&(p->syms), "string", + ArrayOf(sym_get(&(p->syms), "byte")->type, -1)); } /* Driver Modes @@ -40,6 +41,7 @@ static int emit_tokens(void) { static int emit_ast(void) { AST* tree = NULL; Parser ctx = { .input = stdin }; + builtins(&ctx); while(NULL != (tree = toplevel(&ctx))) pprint_tree(stdout, tree, 0); return 0; diff --git a/source/sclpl.h b/source/sclpl.h index 8bb748c..69df984 100644 --- a/source/sclpl.h +++ b/source/sclpl.h @@ -100,7 +100,7 @@ typedef struct { void sym_adddef(SymTable* syms, char* name, Type* type); void sym_addtype(SymTable* syms, char* name, Type* type); -Type* sym_get(SymTable* syms, char* name); +Sym* sym_get(SymTable* syms, char* name); /* AST Types *****************************************************************************/ diff --git a/source/syms.c b/source/syms.c index f356b0a..0ba2934 100644 --- a/source/syms.c +++ b/source/syms.c @@ -1,13 +1,26 @@ #include -void sym_adddef(SymTable* syms, char* name, Type* type) { +Sym* mksym(char* name, Type* type, bool is_typedef, Sym* next) { + Sym* sym = emalloc(sizeof(Sym)); + sym->name = name; + sym->type = type; + sym->is_typedef = is_typedef; + sym->next = next; + return sym; +} +void sym_adddef(SymTable* syms, char* name, Type* type) { + syms->syms = mksym(name, type, false, syms->syms); } void sym_addtype(SymTable* syms, char* name, Type* type) { - + syms->syms = mksym(name, type, true, syms->syms); } -Type* sym_get(SymTable* syms, char* name) { +Sym* sym_get(SymTable* syms, char* name) { + Sym* sym = syms->syms; + for (; sym; sym = sym->next) + if (!strcmp(sym->name, name)) + return sym; return NULL; }