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

@@ -1,32 +1,22 @@
/*
* TinyBlocks - tb_variables.c
* Categoria "Variáveis": mude / adicione / valor de <variável>.
* A library é dona do armazenamento de variáveis e o expõe para monitores.
* Categoria "Variáveis": mude / adicione / valor de <variável>, crie
* variável local / global e apague a variável.
*
* O armazenamento fica no core (tinyblocks/tb_vars.h), com escopos: assim
* existe uma copia so mesmo com esta library carregada como .so, e quem
* executa (o editor) pode dar a cada script e a cada chamada de "Meus
* Blocos" as suas variaveis locais. A API tb_variables_* (monitores do
* editor, salvar projeto) olha as globais.
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <tinyblocks/tinyblocks.h>
#include <tinyblocks/tb_vars.h>
#include "tb_variables.h"
#define MAX_VARS 64
typedef struct { char name[32]; TinyBlocks_Value val; } Var;
static Var s_vars[MAX_VARS];
static int s_n = 0;
static int var_find(const char* n, bool create) {
for (int i = 0; i < s_n; i++)
if (strcmp(s_vars[i].name, n) == 0) return i;
if (!create || s_n >= MAX_VARS) return -1;
int i = s_n++;
strncpy(s_vars[i].name, n, 31); s_vars[i].name[31] = 0;
s_vars[i].val = tb_value_integer(0);
return i;
}
static void input_name(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i,
char* out, size_t cap) {
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
@@ -41,81 +31,81 @@ static void input_name(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i,
static void eval_var_get(TinyBlocks_Block* self, TinyBlocks* rt) {
char name[64]; input_name(self, rt, 0, name, sizeof(name));
int i = var_find(name, false);
tb_block_set_output(self, 0, i >= 0 ? tb_value_clone(s_vars[i].val)
: tb_value_integer(0));
const TinyBlocks_Value* v = tb_vars_get(name);
tb_block_set_output(self, 0, v ? tb_value_clone(*v) : tb_value_integer(0));
self->dirty = false;
}
static void eval_var_set(TinyBlocks_Block* self, TinyBlocks* rt) {
char name[64]; input_name(self, rt, 0, name, sizeof(name));
int i = var_find(name, true);
if (i >= 0) {
TinyBlocks_Value v = tb_block_get_input(self, rt, 1);
tb_value_free(&s_vars[i].val);
s_vars[i].val = tb_value_clone(v);
}
tb_vars_set(name, tb_block_get_input(self, rt, 1));
self->dirty = false;
}
static double as_num(const TinyBlocks_Value* v) {
if (!v) return 0;
if (v->kind == TB_VALUE_INTEGER) return (double)v->as.integer;
if (v->kind == TB_VALUE_REAL) return v->as.real;
if (v->kind == TB_VALUE_TEXT && v->as.text) return strtod(v->as.text, NULL);
return 0;
}
static void eval_var_change(TinyBlocks_Block* self, TinyBlocks* rt) {
char name[64]; input_name(self, rt, 0, name, sizeof(name));
int i = var_find(name, true);
if (i >= 0) {
TinyBlocks_Value by = tb_block_get_input(self, rt, 1);
TinyBlocks_Value* cur = &s_vars[i].val;
double a = 0, d = 0;
if (cur->kind == TB_VALUE_INTEGER) a = (double)cur->as.integer;
else if (cur->kind == TB_VALUE_REAL) a = cur->as.real;
else if (cur->kind == TB_VALUE_TEXT && cur->as.text) a = strtod(cur->as.text, NULL);
if (by.kind == TB_VALUE_INTEGER) d = (double)by.as.integer;
else if (by.kind == TB_VALUE_REAL) d = by.as.real;
else if (by.kind == TB_VALUE_TEXT && by.as.text) d = strtod(by.as.text, NULL);
TinyBlocks_Value nv = (a == floor(a) && d == floor(d))
? tb_value_integer((long long)llround(a + d))
: tb_value_real(a + d);
tb_value_free(cur);
*cur = nv;
}
TinyBlocks_Value by = tb_block_get_input(self, rt, 1);
double a = as_num(tb_vars_get(name)), d = as_num(&by);
TinyBlocks_Value nv = (a == floor(a) && d == floor(d))
? tb_value_integer((long long)llround(a + d))
: tb_value_real(a + d);
tb_vars_set(name, nv);
tb_value_free(&nv);
self->dirty = false;
}
/* == API de enumeração === */
int tb_variables_count(void) { return s_n; }
const char* tb_variables_name(int i) {
return (i >= 0 && i < s_n) ? s_vars[i].name : "";
/* "crie variável local [x] com valor (v)": so deste script/chamada */
static void eval_var_local(TinyBlocks_Block* self, TinyBlocks* rt) {
char name[64]; input_name(self, rt, 0, name, sizeof(name));
TinyBlocks_Value v = tb_block_get_input(self, rt, 1);
tb_vars_declare_local(name, v.kind == TB_VALUE_NONE ? tb_value_integer(0) : v);
self->dirty = false;
}
const TinyBlocks_Value* tb_variables_value(int i) {
return (i >= 0 && i < s_n) ? &s_vars[i].val : NULL;
/* "crie variável global [x] com valor (v)": visivel em todo lugar */
static void eval_var_global(TinyBlocks_Block* self, TinyBlocks* rt) {
char name[64]; input_name(self, rt, 0, name, sizeof(name));
TinyBlocks_Value v = tb_block_get_input(self, rt, 1);
tb_vars_declare_global(name, v.kind == TB_VALUE_NONE ? tb_value_integer(0) : v);
self->dirty = false;
}
void tb_variables_reset_blocks(void) {
for (int v = 0; v < s_n; v++)
if (s_vars[v].val.kind == TB_VALUE_BLOCK) {
tb_value_free(&s_vars[v].val);
s_vars[v].val = tb_value_integer(0);
}
/* "apague a variável [x]": a mais interna com esse nome */
static void eval_var_delete(TinyBlocks_Block* self, TinyBlocks* rt) {
char name[64]; input_name(self, rt, 0, name, sizeof(name));
if (!tb_vars_delete(name) && name[0])
tinyblocks_set_error(rt, "variável '%s' não existe", name);
self->dirty = false;
}
void tb_variables_clear(void) {
for (int i = 0; i < s_n; i++) tb_value_free(&s_vars[i].val);
s_n = 0;
}
/* == API (globais) === */
int tb_variables_count(void) { return tb_vars_global_count(); }
const char* tb_variables_name(int i) { return tb_vars_global_name(i); }
const TinyBlocks_Value* tb_variables_value(int i) { return tb_vars_global_value(i); }
void tb_variables_reset_blocks(void) { tb_vars_reset_blocks(); }
void tb_variables_clear(void) { tb_vars_clear(); }
void tb_variables_set_text(const char* name, const char* value) {
if (!name || !name[0]) return;
int i = var_find(name, true);
if (i < 0) return;
tb_value_free(&s_vars[i].val);
const char* s = value ? value : "";
char* end = NULL;
TinyBlocks_Value v;
long long iv = strtoll(s, &end, 10);
if (end && *end == 0 && end != s) { s_vars[i].val = tb_value_integer(iv); return; }
double dv = strtod(s, &end);
if (end && *end == 0 && end != s) { s_vars[i].val = tb_value_real(dv); return; }
s_vars[i].val = tb_value_text(s);
if (end && *end == 0 && end != s) v = tb_value_integer(iv);
else {
double dv = strtod(s, &end);
v = (end && *end == 0 && end != s) ? tb_value_real(dv) : tb_value_text(s);
}
tb_vars_declare_global(name, v);
tb_value_free(&v);
}
/* == Registro === */
@@ -134,6 +124,12 @@ static bool variables_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
"Altera (incrementa) o valor de uma variavel");
TB_BLOCK_META("Variables.Get", "eval_var_get",
"Le o valor de uma variavel");
TB_BLOCK_META("Variables.DeclareLocal", "eval_var_local",
"Cria uma variavel que so existe neste script (ou nesta chamada de Meus Blocos)");
TB_BLOCK_META("Variables.DeclareGlobal", "eval_var_global",
"Cria uma variavel visivel em todos os scripts");
TB_BLOCK_META("Variables.Delete", "eval_var_delete",
"Apaga a variavel (a local, se houver; senao a global)");
static const TinyBlocks_SlotDesc var_in[] = {{"variável", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc var_set[] = {{"variável", TB_VALUE_TEXT}, {"valor", TB_VALUE_NONE}};
@@ -146,13 +142,18 @@ static bool variables_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
if (!t || !tb_library_add(lib, &t->base)) return false;
t = tb_type_block_create(rt, "Variables.Get", var_in, 1, any_out, 1, eval_var_get);
if (!t || !tb_library_add(lib, &t->base)) return false;
t = tb_type_block_create(rt, "Variables.DeclareLocal", var_set, 2, NULL, 0, eval_var_local);
if (!t || !tb_library_add(lib, &t->base)) return false;
t = tb_type_block_create(rt, "Variables.DeclareGlobal", var_set, 2, NULL, 0, eval_var_global);
if (!t || !tb_library_add(lib, &t->base)) return false;
t = tb_type_block_create(rt, "Variables.Delete", var_in, 1, NULL, 0, eval_var_delete);
if (!t || !tb_library_add(lib, &t->base)) return false;
return true;
}
static void variables_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
(void)rt; (void)lib;
for (int i = 0; i < s_n; i++) tb_value_free(&s_vars[i].val);
s_n = 0;
tb_vars_clear();
}
TINYBLOCKS_DEFINE_LIBRARY(variables,