Variáveis locais e globais: crie local, crie global e apague a variável

- Blocos novos em Variáveis: "crie variável local [] com valor ()",
  "crie variável global [] com valor ()" e "apague a variável []".
- O armazenamento das variáveis foi para o core (tinyblocks/tb_vars.h),
  com escopos: uma cópia só, mesmo com a library carregada como .so.
- Editor: cada script tem as suas locais (somem quando ele termina) e cada
  chamada de "Meus Blocos" também (vê as globais, não as de quem chamou).
  mude/adicione alteram onde a variável existir; se não existir, criam global.
- A API tb_variables_* (monitores, salvar projeto) olha as globais.
- make test-vars: escopos e os blocos novos.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mguinhosandClaude Opus 5.5 committed 2026-10-22 18:13:07 -03:00
1 parent d9449a568e
commit 0c73e81c1a
8 files changed
+428 -79

No files matched your search

+107
View File
@@ -0,0 +1,107 @@
/*
* vars_test.c - testa as variaveis com escopos do core (tb_vars) e os blocos
* da tinyblocks-variables (crie local/global, apague, mude, adicione).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tinyblocks/tinyblocks.h>
#include <tinyblocks/tb_vars.h>
#include <tb_variables.h>
static int fails = 0;
#define CHECK(c, m) do { if (c) printf("ok %s\n", m); else { printf("FAIL %s\n", m); fails++; } } while (0)
static TinyBlocks* rt;
static void run(const char* type, const char* name, long long v, bool with_v) {
TinyBlocks_Block* b = tb_block_create(rt, tb_rt_find_type(rt, type));
TinyBlocks_Value n = tb_value_text(name);
tb_block_set_default(b, 0, n);
tb_value_free(&n);
if (with_v) { TinyBlocks_Value x = tb_value_integer(v); tb_block_set_default(b, 1, x); }
tb_block_eval(b, rt);
tb_block_destroy(b, rt);
}
static long long get(const char* name) {
TinyBlocks_Block* b = tb_block_create(rt, tb_rt_find_type(rt, "Variables.Get"));
TinyBlocks_Value n = tb_value_text(name);
tb_block_set_default(b, 0, n);
tb_value_free(&n);
tb_block_eval(b, rt);
long long r = b->outputs[0].kind == TB_VALUE_INTEGER ? b->outputs[0].as.integer : -999;
tb_block_destroy(b, rt);
return r;
}
int main(void) {
tinyblocks_init(&rt, NULL);
tb_library_register(rt, tb_variables_get_library());
CHECK(tb_rt_find_type(rt, "Variables.DeclareLocal") && tb_rt_find_type(rt, "Variables.DeclareGlobal") &&
tb_rt_find_type(rt, "Variables.Delete"), "blocos crie local / crie global / apague registrados");
run("Variables.DeclareGlobal", "placar", 10, true);
CHECK(get("placar") == 10 && tb_variables_count() == 1, "crie variável global aparece nas globais");
/* script A com uma local */
TbVarScope* a = tb_vars_scope_new(NULL);
tb_vars_scope_set(a);
run("Variables.DeclareLocal", "c", 1, true);
run("Variables.Change", "c", 5, true);
run("Variables.Change", "placar", 1, true);
CHECK(get("c") == 6, "local criada e alterada dentro do script");
CHECK(tb_variables_count() == 1, "local não entra nas globais (monitores)");
/* script B: não vê a local de A; a dele tem o mesmo nome */
TbVarScope* b = tb_vars_scope_new(NULL);
tb_vars_scope_set(b);
CHECK(get("c") == 0 && get("placar") == 11, "outro script não vê a local, mas vê a global");
run("Variables.DeclareLocal", "c", 100, true);
tb_vars_scope_set(a);
CHECK(get("c") == 6, "locais de mesmo nome em scripts diferentes são independentes");
/* local com o mesmo nome de uma global a esconde; mude escreve na local */
run("Variables.DeclareLocal", "placar", 0, true);
run("Variables.Set", "placar", 3, true);
CHECK(get("placar") == 3, "local esconde a global de mesmo nome");
tb_vars_scope_set(NULL);
CHECK(get("placar") == 11, "a global continua com o valor dela");
/* apague: a mais interna primeiro */
tb_vars_scope_set(a);
run("Variables.Delete", "placar", 0, false);
CHECK(get("placar") == 11, "apagar remove a local e a global volta a aparecer");
run("Variables.Delete", "c", 0, false);
CHECK(!tb_vars_exists("c"), "apague a variável local");
/* mude numa variável que não existe cria global */
run("Variables.Set", "nova", 7, true);
tb_vars_scope_set(NULL);
CHECK(get("nova") == 7, "mude em variável inexistente cria global");
/* escopo acaba: as locais somem */
tb_vars_scope_set(b);
tb_vars_scope_free(b);
CHECK(tb_vars_scope_current() == NULL, "liberar o escopo atual volta para as globais");
CHECK(get("c") == 0, "locais do script somem quando ele termina");
/* chamada de Meus Blocos: escopo sem pai não vê as locais de quem chamou */
tb_vars_scope_set(a);
run("Variables.DeclareLocal", "segredo", 42, true);
TbVarScope* call = tb_vars_scope_new(NULL);
tb_vars_scope_set(call);
CHECK(get("segredo") == 0 && get("placar") == 11, "chamada não vê as locais de quem chamou");
tb_vars_scope_set(a);
tb_vars_scope_free(call);
CHECK(get("segredo") == 42, "de volta ao script, a local continua lá");
tb_vars_scope_set(NULL);
tb_vars_scope_free(a);
run("Variables.Delete", "placar", 0, false);
CHECK(!tb_vars_exists("placar"), "apague a variável global");
tinyblocks_deinit(&rt);
if (fails) printf("\n%d FALHA(S)\n", fails);
else printf("\nTUDO OK\n");
return fails != 0;
}