#include "scope.h" #include #include #include #define DEFAULT_SIZE 16 static void scope_init(struct scope* scope) { scope->types = calloc(DEFAULT_SIZE, sizeof(struct type_def*)); scope->type_cap = DEFAULT_SIZE; scope->vars = calloc(DEFAULT_SIZE, sizeof(struct var_def*)); scope->var_cap = DEFAULT_SIZE; } static void scope_destroy(struct scope* scope) { for (unsigned long long i = 0; i < scope->type_cap; i++) { if (scope->types[i] != NULL) free(scope->types[i]); } free(scope->types); for (unsigned long long i = 0; i < scope->var_cap; i++) { if (scope->vars[i] != NULL) free(scope->vars[i]); } free(scope->vars); } unsigned long long hash_name(const char* name) { unsigned long long hash = 0, i = 0; while (name[i] != 0) hash = (hash << 5) - hash + name[i++]; return hash; } static struct type_def** type_cell( const struct scope* scope, const char* name ) { unsigned long long orig_idx = hash_name(name) % scope->type_cap; unsigned long long idx = orig_idx; do { if (scope->types[idx] == NULL || strcmp(name, scope->types[idx]->name) == 0) return &scope->types[idx]; } while ((idx = (idx + 1) % scope->type_cap) != orig_idx); return NULL; } static void rehash_types(struct scope* scope) { struct type_def** old_types = scope->types; unsigned long long old_cap = scope->type_cap; scope->type_cap *= 2; scope->types = calloc(scope->type_cap, sizeof(struct type_def*)); if (scope->types == NULL) { fprintf(stderr, "ccc: out of memory\n"); exit(1); } for (unsigned long long i = 0; i < old_cap; i++) { if (old_types[i] == NULL) continue; struct type_def** cell = type_cell(scope, old_types[i]->name); if (cell == NULL) { fprintf(stderr, "ccc: types rehash failed, likely a bug\n"); exit(1); } *cell = old_types[i]; } free(old_types); } static struct var_def** var_cell( const struct scope* scope, const char* name ) { unsigned long long orig_idx = hash_name(name) % scope->var_cap; unsigned long long idx = orig_idx; do { if (scope->vars[idx] == NULL || strcmp(name, scope->vars[idx]->name) == 0) return &scope->vars[idx]; } while ((idx = (idx + 1) % scope->var_cap) != orig_idx); return NULL; } static void rehash_vars(struct scope* scope) { struct var_def** old_vars = scope->vars; unsigned long long old_cap = scope->var_cap; scope->var_cap *= 2; scope->vars = calloc(scope->var_cap, sizeof(struct var_def*)); if (scope->vars == NULL) { fprintf(stderr, "ccc: out of memory\n"); exit(1); } for (unsigned long long i = 0; i < old_cap; i++) { if (old_vars[i] == NULL) continue; struct var_def** cell = var_cell(scope, old_vars[i]->name); if (cell == NULL) { fprintf(stderr, "ccc: vars rehash failed, likely a bug\n"); exit(1); } *cell = old_vars[i]; } free(old_vars); } void scope_push(struct scope** p_scope) { struct scope* inner_scope = calloc(1, sizeof(struct scope)); scope_init(inner_scope); inner_scope->next_out = *p_scope; *p_scope = inner_scope; } void scope_pop(struct scope** p_scope) { struct scope* discarded_scope = *p_scope; *p_scope = (*p_scope)->next_out; scope_destroy(discarded_scope); free(discarded_scope); } void scope_install_default_types(struct scope* scope) { scope_define_type(scope, (struct type_def) { .name = "void", .sz = 0, }); scope_define_type(scope, (struct type_def) { .name = "bool", .sz = 1, }); scope_define_type(scope, (struct type_def) { .name = "char", .sz = 1, }); scope_define_type(scope, (struct type_def) { .name = "short", .sz = 2, }); scope_define_type(scope, (struct type_def) { .name = "int", .sz = 4, }); scope_define_type(scope, (struct type_def) { .name = "float", .sz = 4, }); scope_define_type(scope, (struct type_def) { .name = "long", .sz = 8, }); scope_define_type(scope, (struct type_def) { .name = "double", .sz = 8, }); } bool scope_get_type( const struct scope* scope, struct type_def* p_entry, const char* name ) { for (; scope != NULL; scope = scope->next_out) { struct type_def** cell = type_cell(scope, name); if (cell == NULL || *cell == NULL) continue; if (p_entry != NULL) *p_entry = **cell; return true; } return false; } void scope_define_type(struct scope* scope, struct type_def type) { struct type_def** cell = type_cell(scope, type.name); while (cell == NULL) { rehash_types(scope); cell = type_cell(scope, type.name); } *cell = calloc(1, sizeof(struct type_def)); if (*cell == NULL) { fprintf(stderr, "ccc: out of memory\n"); exit(1); } **cell = type; } bool scope_get_var( const struct scope* scope, struct var_def* p_entry, const char* name ) { for (; scope != NULL; scope = scope->next_out) { struct var_def** cell = var_cell(scope, name); if (cell == NULL || *cell == NULL) continue; if (p_entry != NULL) *p_entry = **cell; return true; } return false; } void scope_define_var(struct scope* scope, struct var_def var) { struct var_def** cell = var_cell(scope, var.name); while (cell == NULL) { rehash_vars(scope); cell = var_cell(scope, var.name); } if (*cell == NULL) { *cell = calloc(1, sizeof(struct var_def)); if (*cell == NULL) { fprintf(stderr, "ccc: out of memory\n"); exit(1); } } /* technically C allows redefinition :/ */ **cell = var; }