larpcc/syms.c
2026-10-02 00:32:31 +02:00

54 lines
995 B
C

#include "syms.h"
#include "collection.h"
#include <assert.h>
#include <stdlib.h>
struct Syms {
ARRAY_FIELD(Hashmap, maps);
};
Syms* syms_new()
{
Syms* s = malloc(sizeof(Syms));
*s = (Syms) {
ARRAY_FIELD_INIT(maps),
};
ARRAY_FIELD_PUSH(s, maps, &(Hashmap) { });
hashmap_init(&s->maps[0], sizeof(Sym*));
return s;
}
void syms_free(Syms* s)
{
free(s->maps);
free(s);
}
void syms_push_scope(Syms* s)
{
ARRAY_FIELD_PUSH(s, maps, &(Hashmap) { });
hashmap_init(&s->maps[s->maps_size - 1], sizeof(Sym*));
}
void syms_pop_scope(Syms* s)
{
assert(s->maps_size > 0);
hashmap_deinit(&s->maps[s->maps_size - 1]);
s->maps_size -= 1;
}
void syms_define(Syms* s, const char* ident, Sym* sym)
{
Hashmap* m = &s->maps[s->maps_size - 1];
hashmap_set(m, ident, &sym);
}
Sym* syms_get(Syms* s, const char* ident)
{
Hashmap* m = &s->maps[s->maps_size - 1];
Sym** res = hashmap_get(m, ident);
return res ? *res : nullptr;
}