Map redesign, removing just flags as unoccupied, when getting value we dont check occupancy but key, avoids hanging values due to the nature of linear probing

This commit is contained in:
Krzosa Karol
2022-05-26 23:46:26 +02:00
parent 7c4b01cd92
commit 01acad47ec

View File

@@ -44,28 +44,17 @@ sym_insert(Sym *sym){
Sym *is_sym = (Sym *)map_get(&pctx->syms, hash); Sym *is_sym = (Sym *)map_get(&pctx->syms, hash);
if(is_sym){ if(is_sym){
parsing_error(sym->ast->pos, "Symbol with name: [%s] defined multiple times", sym->name.s.str); parsing_error(sym->ast->pos, "Symbol with name: [%s] defined multiple times", sym->name.s.str);
return;
} }
if(pctx->scope > 0){ if(pctx->scope > 0){
For(pctx->local_syms){
if(it[0]->name.str == sym->name.str) parsing_error(sym->ast->pos, "Symbol with name: [%s] defined multiple times", sym->name.s.str);
return;
}
pctx->local_syms.add(sym); pctx->local_syms.add(sym);
} }
else{
map_insert(&pctx->syms, hash, sym); map_insert(&pctx->syms, hash, sym);
} }
}
function Sym * function Sym *
sym_get(Intern_String name){ sym_get(Intern_String name){
Sym *result = (Sym *)map_get(&pctx->syms, hash_string(name.s)); Sym *result = (Sym *)map_get(&pctx->syms, hash_string(name.s));
if(!result){
For(pctx->local_syms){
if(it[0]->name.str == name.str) return it[0];
}
}
return result; return result;
} }
@@ -80,6 +69,11 @@ function void
scope_close(S64 local_sym_count){ scope_close(S64 local_sym_count){
pctx->scope--; pctx->scope--;
assert(pctx->scope >= 0); assert(pctx->scope >= 0);
for(S64 i = local_sym_count; i < pctx->local_syms.len; i++){
Sym *it = pctx->local_syms.data[i];
void *removed = map_remove(&pctx->syms, hash_string(it->name.s));
assert(removed);
}
pctx->local_syms.len = local_sym_count; pctx->local_syms.len = local_sym_count;
} }