74 lines
1.4 KiB
C
74 lines
1.4 KiB
C
#include "var-map.h"
|
|
|
|
#include <blue/ds/hashmap.h>
|
|
#include <blue/ds/string.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
enum ivy_status codegen_var_map_init(struct codegen_var_map *map)
|
|
{
|
|
memset(map, 0x0, sizeof *map);
|
|
map->m_map = b_hashmap_create(free, free);
|
|
|
|
return IVY_OK;
|
|
}
|
|
|
|
enum ivy_status codegen_var_map_fini(struct codegen_var_map *map)
|
|
{
|
|
b_hashmap_unref(map->m_map);
|
|
return IVY_OK;
|
|
}
|
|
|
|
enum ivy_status codegen_var_map_get(
|
|
struct codegen_var_map *map, const char *ident, struct codegen_var **out)
|
|
{
|
|
b_hashmap_key key = {
|
|
.key_data = ident,
|
|
.key_size = strlen(ident),
|
|
};
|
|
|
|
const b_hashmap_value *value = b_hashmap_get(map->m_map, &key);
|
|
|
|
if (!value) {
|
|
return IVY_ERR_NO_ENTRY;
|
|
}
|
|
|
|
*out = value->value_data;
|
|
|
|
return IVY_OK;
|
|
}
|
|
|
|
enum ivy_status codegen_var_map_put(
|
|
struct codegen_var_map *map, const char *ident,
|
|
const struct codegen_var *var)
|
|
{
|
|
b_hashmap_key key = {
|
|
.key_data = b_strdup(ident),
|
|
.key_size = strlen(ident),
|
|
};
|
|
|
|
if (!key.key_data) {
|
|
return IVY_ERR_NO_MEMORY;
|
|
}
|
|
|
|
b_hashmap_value value = {
|
|
.value_data = malloc(sizeof *var),
|
|
.value_size = sizeof *var,
|
|
};
|
|
|
|
if (!value.value_data) {
|
|
free((void *)key.key_data);
|
|
return IVY_ERR_NO_MEMORY;
|
|
}
|
|
|
|
memcpy(value.value_data, var, sizeof *var);
|
|
|
|
b_status status = b_hashmap_put(map->m_map, &key, &value);
|
|
if (!B_OK(status)) {
|
|
free((void *)key.key_data);
|
|
free(value.value_data);
|
|
}
|
|
|
|
return ivy_status_from_b_status(status);
|
|
}
|