/* * TinyBlocks - tb_vars.c * Variaveis com escopos (ver tb_vars.h). */ #include #include #include #include "tinyblocks/tb_vars.h" typedef struct { char* name; uint32_t hash; TinyBlocks_Value val; } Var; /* Vetor na ordem de criacao (o painel de variaveis mostra nessa ordem) + * indice hash (enderecamento aberto, guarda posicao+1; 0 = vazio). * Ler uma variavel e o que a VM mais faz: sem o indice, cada leitura * comparava o nome com todas as outras. */ typedef struct { Var* v; int n, cap; int* idx; int idx_cap; /* potencia de 2, >= 2 * cap */ } VarTable; struct TbVarScope { VarTable t; TbVarScope* parent; TbVarScope* next_live; /* lista de todos os escopos vivos */ }; static VarTable s_global; /* gen muda quando uma variavel e criada/apagada ou um escopo morre (TbVarRef); scope espelha s_cur. Exposto (tb_vars_epoch) para codigo compilado conferir sem chamar funcao. */ static TbVarsEpoch s_epoch = { 1, NULL }; #define s_gen (s_epoch.gen) static TbVarScope* s_cur = NULL; static TbVarScope* s_live = NULL; /* -- Tabela --- */ static uint32_t name_hash(const char* s) { uint32_t h = 2166136261u; /* FNV-1a */ for (; *s; s++) { h ^= (unsigned char)*s; h *= 16777619u; } return h; } static void index_rebuild(VarTable* t) { if (!t->idx) return; memset(t->idx, 0, (size_t)t->idx_cap * sizeof(int)); uint32_t mask = (uint32_t)t->idx_cap - 1; for (int i = 0; i < t->n; i++) { uint32_t k = t->v[i].hash & mask; while (t->idx[k]) k = (k + 1) & mask; t->idx[k] = i + 1; } } static int table_find_h(const VarTable* t, const char* name, uint32_t h) { if (!t->idx) return -1; uint32_t mask = (uint32_t)t->idx_cap - 1; for (uint32_t k = h & mask; t->idx[k]; k = (k + 1) & mask) { const Var* x = &t->v[t->idx[k] - 1]; if (x->hash == h && !strcmp(x->name, name)) return t->idx[k] - 1; } return -1; } static Var* table_put(VarTable* t, const char* name, TinyBlocks_Value v) { uint32_t h = name_hash(name); int i = table_find_h(t, name, h); if (i >= 0) { TinyBlocks_Value old = t->v[i].val; t->v[i].val = tb_value_clone(v); /* clona antes: v pode ser o proprio valor */ tb_value_free(&old); return &t->v[i]; } if (t->n == t->cap) { int nc = t->cap ? t->cap * 2 : 16; Var* nv = realloc(t->v, (size_t)nc * sizeof(Var)); if (!nv) return NULL; int* ni = calloc((size_t)nc * 2, sizeof(int)); if (!ni) { t->v = nv; t->cap = nc; return NULL; } free(t->idx); t->v = nv; t->cap = nc; t->idx = ni; t->idx_cap = nc * 2; index_rebuild(t); } size_t len = strlen(name); char* nm = malloc(len + 1); if (!nm) return NULL; memcpy(nm, name, len + 1); s_gen++; Var* x = &t->v[t->n++]; x->name = nm; x->hash = h; x->val = tb_value_clone(v); uint32_t mask = (uint32_t)t->idx_cap - 1, k = h & mask; while (t->idx[k]) k = (k + 1) & mask; t->idx[k] = t->n; return x; } static void table_remove(VarTable* t, int i) { free(t->v[i].name); tb_value_free(&t->v[i].val); memmove(&t->v[i], &t->v[i + 1], (size_t)(t->n - i - 1) * sizeof(Var)); t->n--; index_rebuild(t); /* posicoes mudaram (apagar e raro) */ s_gen++; } static void table_clear(VarTable* t) { s_gen++; for (int i = 0; i < t->n; i++) { free(t->v[i].name); tb_value_free(&t->v[i].val); } t->n = 0; free(t->v); free(t->idx); t->v = NULL; t->cap = 0; t->idx = NULL; t->idx_cap = 0; } /* Tabela onde `name` existe, do escopo atual para fora; NULL se nenhuma */ static VarTable* where(const char* name, int* idx) { uint32_t h = name_hash(name); for (TbVarScope* s = s_cur; s; s = s->parent) { int i = table_find_h(&s->t, name, h); if (i >= 0) { *idx = i; return &s->t; } } int i = table_find_h(&s_global, name, h); if (i >= 0) { *idx = i; return &s_global; } return NULL; } /* -- Escopos --- */ TbVarScope* tb_vars_scope_new(TbVarScope* parent) { TbVarScope* s = calloc(1, sizeof(TbVarScope)); if (!s) return NULL; s->parent = parent; s->next_live = s_live; s_live = s; return s; } void tb_vars_scope_free(TbVarScope* s) { if (!s) return; for (TbVarScope** pp = &s_live; *pp; pp = &(*pp)->next_live) if (*pp == s) { *pp = s->next_live; break; } for (TbVarScope* o = s_live; o; o = o->next_live) /* filhos perdem o pai */ if (o->parent == s) o->parent = NULL; if (s_cur == s) { s_cur = NULL; s_epoch.scope = NULL; } s_gen++; table_clear(&s->t); free(s); } TbVarScope* tb_vars_scope_current(void) { return s_cur; } void tb_vars_scope_set(TbVarScope* s) { s_cur = s; s_epoch.scope = s; } const TbVarsEpoch* tb_vars_epoch(void) { return &s_epoch; } /* -- Valores --- */ const TinyBlocks_Value* tb_vars_get(const char* name) { if (!name || !name[0]) return NULL; int i; VarTable* t = where(name, &i); return t ? &t->v[i].val : NULL; } /* Cache valido: mesma geracao e o nome confere (o nome do bloco pode mudar) */ static Var* ref_hit(const char* name, const TbVarRef* ref) { if (!ref || ref->gen != s_gen || ref->scope != s_cur || !ref->table) return NULL; Var* x = &((VarTable*)ref->table)->v[ref->idx]; if (ref->fixed && ref->name == name) return x; /* nome fixo (codigo compilado) */ return strcmp(x->name, name) == 0 ? x : NULL; } const TinyBlocks_Value* tb_vars_get_ref(const char* name, TbVarRef* ref) { if (!name || !name[0]) return NULL; Var* x = ref_hit(name, ref); if (x) return &x->val; int i; VarTable* t = where(name, &i); if (!t) return NULL; if (ref) { ref->gen = s_gen; ref->scope = s_cur; ref->table = t; ref->idx = i; ref->name = name; } return &t->v[i].val; } void tb_vars_set_ref(const char* name, TinyBlocks_Value v, TbVarRef* ref) { if (!name || !name[0]) return; Var* x = ref_hit(name, ref); if (!x) { tb_vars_set(name, v); int i; VarTable* t = where(name, &i); if (t && ref) { ref->gen = s_gen; ref->scope = s_cur; ref->table = t; ref->idx = i; ref->name = name; } return; } TinyBlocks_Value old = x->val; x->val = tb_value_clone(v); tb_value_free(&old); } bool tb_vars_exists(const char* name) { return tb_vars_get(name) != NULL; } void tb_vars_set(const char* name, TinyBlocks_Value v) { if (!name || !name[0]) return; int i; VarTable* t = where(name, &i); table_put(t ? t : &s_global, name, v); } void tb_vars_declare_local(const char* name, TinyBlocks_Value v) { if (!name || !name[0]) return; table_put(s_cur ? &s_cur->t : &s_global, name, v); } void tb_vars_declare_global(const char* name, TinyBlocks_Value v) { if (!name || !name[0]) return; table_put(&s_global, name, v); } bool tb_vars_delete(const char* name) { if (!name || !name[0]) return false; int i; VarTable* t = where(name, &i); if (!t) return false; table_remove(t, i); return true; } /* -- Globais --- */ int tb_vars_global_count(void) { return s_global.n; } const char* tb_vars_global_name(int i) { return (i >= 0 && i < s_global.n) ? s_global.v[i].name : ""; } const TinyBlocks_Value* tb_vars_global_value(int i) { return (i >= 0 && i < s_global.n) ? &s_global.v[i].val : NULL; } void tb_vars_clear(void) { table_clear(&s_global); } static void reset_blocks_in(VarTable* t) { for (int i = 0; i < t->n; i++) if (t->v[i].val.kind == TB_VALUE_BLOCK) { tb_value_free(&t->v[i].val); t->v[i].val = tb_value_integer(0); } } void tb_vars_reset_blocks(void) { reset_blocks_in(&s_global); for (TbVarScope* s = s_live; s; s = s->next_live) reset_blocks_in(&s->t); }