diff options
Diffstat (limited to 'compiler/main.c')
-rw-r--r-- | compiler/main.c | 113 |
1 files changed, 83 insertions, 30 deletions
diff --git a/compiler/main.c b/compiler/main.c index 3666859..e614be6 100644 --- a/compiler/main.c +++ b/compiler/main.c @@ -11,6 +11,7 @@ #include <string.h> #include "debug.h" #include "hashmap.h" +#include "parser/proc.h" #include "parser/type.h" #include "parser.h" @@ -196,15 +197,11 @@ print_enum(struct type *typ) for (size_t r = 0; r < typ->members.n_rows; r++) { mem = (struct enum_member*)typ->members.rows[r].head; - if (mem == (struct enum_member*)&typ->members.rows[r]) { - continue; - } - - do { + while (mem != (struct enum_member*)&typ->members.rows[r]) { printf(" %.*s,\n", (int)mem->name_len, mem->name); mem = (struct enum_member*)mem->hashmap_entry.list_entry.next; - } while (mem != (struct enum_member*)&typ->members.rows[r]); + } } printf("}\n"); @@ -219,17 +216,13 @@ print_struct(struct type *typ) for (size_t r = 0; r < typ->members.n_rows; r++) { mem = (struct struct_member*)typ->members.rows[r].head; - if (mem == (struct struct_member*)&typ->members.rows[r]) { - continue; - } - - do { - printf(" %.*s", (int)mem->typ->name_len, mem->typ->name); + while (mem != (struct struct_member*)&typ->members.rows[r]) { + printf(" %.*s ", (int)mem->typ->name_len, mem->typ->name); print_ptrs(mem->n_ptrs); - printf(" %.*s;\n", (int)mem->name_len, mem->name); + printf("%.*s;\n", (int)mem->name_len, mem->name); mem = (struct struct_member*)mem->hashmap_entry.list_entry.next; - } while (mem != (struct struct_member*)&typ->members.rows[r]); + } } printf("}\n"); @@ -251,11 +244,84 @@ print_type(struct type *typ) print_struct(typ); break; default: - printf("(unknown);\n"); + printf("unknown;\n"); break; } } +static void +print_proc(struct procedure *proc) +{ + struct list_entry *list_entry; + struct parameter *param; + int p; + + printf("proc %.*s(", (int)proc->name_len, proc->name); + + list_entry = proc->params.head; + p = 0; + while (list_entry != (struct list_entry*)&proc->params) { + param = (struct parameter*)((uint8_t*)list_entry - __builtin_offsetof(struct parameter, list_entry)); + + if (p) { + printf(", "); + } + printf("%.*s ", (int)param->var.typ->name_len, param->var.typ->name); + print_ptrs(param->var.n_ptrs); + printf("%.*s", (int)param->var.name_len, param->var.name); + + list_entry = list_entry->next; + p++; + } + + printf(")"); + + if (proc->ret_typ != NULL) { + printf(" -> %.*s", (int)proc->ret_typ->name_len, proc->ret_typ->name); + print_ptrs(proc->ret_n_ptrs); + } + + printf(";\n"); +} + +static void +dump_types(struct hashmap *map) +{ + struct type *typ, *next; + + for (size_t r = 0; r < map->n_rows; r++) { + typ = (struct type*)map->rows[r].head; + while (typ != (struct type*)&map->rows[r]) { + print_type(typ); + + next = (struct type*)typ->hashmap_entry.list_entry.next; + free(typ); + typ = next; + } + } + + free(map->rows); +} + +static void +dump_procs(struct hashmap *map) +{ + struct procedure *proc, *next; + + for (size_t r = 0; r < map->n_rows; r++) { + proc = (struct procedure*)map->rows[r].head; + while (proc != (struct procedure*)&map->rows[r]) { + print_proc(proc); + + next = (struct procedure*)proc->hashmap_entry.list_entry.next; + free(proc); + proc = next; + } + } + + free(map->rows); +} + int main(int argc, char **argv) { @@ -263,7 +329,6 @@ main(int argc, char **argv) struct parser parser; struct list *types_rows, *procs_rows; struct hashmap types, procs; - struct type *typ; argv0 = argv[0]; if (argc < 2) { @@ -297,21 +362,9 @@ main(int argc, char **argv) parser_init(&parser, input); parser_parse(&parser); - /* Print parsed types */ - for (size_t r = 0; r < types.n_rows; r++) { - typ = (struct type*)types.rows[r].head; - if (typ == (struct type*)&types.rows[r]) { - continue; - } - - do { - print_type(typ); - typ = (struct type*)typ->hashmap_entry.list_entry.next; - } while (typ != (struct type*)&types.rows[r]); - } + dump_types(parser.types); + dump_procs(parser.procs); - free(procs_rows); - free(types_rows); free(input); return 0; } |