/* * TinyBlocks - tb_vars.c * Variaveis com escopos (ver tb_vars.h). */ #include #include #include "tinyblocks/tb_vars.h" typedef struct { char* name; TinyBlocks_Value val; } Var; typedef struct { Var* v; int n, cap; } VarTable; struct TbVarScope { VarTable t; TbVarScope* parent; TbVarScope* next_live; /* lista de todos os escopos vivos */ }; static VarTable s_global; static TbVarScope* s_cur = NULL; static TbVarScope* s_live = NULL; /* -- Tabela ---------------------------------------------------------------- */ static int table_find(const VarTable* t, const char* name) { for (int i = 0; i < t->n; i++) if (!strcmp(t->v[i].name, name)) return i; return -1; } static Var* table_put(VarTable* t, const char* name, TinyBlocks_Value v) { int i = table_find(t, name); if (i >= 0) { tb_value_free(&t->v[i].val); t->v[i].val = tb_value_clone(v); 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; t->v = nv; t->cap = nc; } size_t len = strlen(name); char* nm = malloc(len + 1); if (!nm) return NULL; memcpy(nm, name, len + 1); Var* x = &t->v[t->n++]; x->name = nm; x->val = tb_value_clone(v); 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--; } static void table_clear(VarTable* t) { while (t->n > 0) table_remove(t, t->n - 1); free(t->v); t->v = NULL; t->cap = 0; } /* Tabela onde `name` existe, do escopo atual para fora; NULL se nenhuma */ static VarTable* where(const char* name, int* idx) { for (TbVarScope* s = s_cur; s; s = s->parent) { int i = table_find(&s->t, name); if (i >= 0) { *idx = i; return &s->t; } } int i = table_find(&s_global, name); 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; 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; } /* -- 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; } 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); }