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:
1 parent
d9449a568e
commit
0c73e81c1a
8 files changed
+428
-79
No files matched your search
@@ -20,6 +20,7 @@
|
||||
#include <tb_graphics.h>
|
||||
#include <tinyblocks/tb_stage.h>
|
||||
#include <tinyblocks/tb_input.h>
|
||||
#include <tinyblocks/tb_vars.h>
|
||||
#include <tb_variables.h>
|
||||
#include <tb_blueprint.h>
|
||||
#include <tb_api.h>
|
||||
@@ -318,6 +319,9 @@ static const BlockSpec s_specs[] = {
|
||||
{"Variables.Set", "mude %1 para %2", "x|0"},
|
||||
{"Variables.Change", "adicione %2 a %1", "x|1"},
|
||||
{"Variables.Get", "%1", "x"},
|
||||
{"Variables.DeclareLocal", "crie variável local %1 com valor %2", "contador|0"},
|
||||
{"Variables.DeclareGlobal", "crie variável global %1 com valor %2", "placar|0"},
|
||||
{"Variables.Delete", "apague a variável %1", "contador"},
|
||||
{"IO.Print", "escreva %1 %2", "Olá!|"},
|
||||
{"IO.PrintInt", "escreva o número %1", NULL},
|
||||
{"Port.In", "parâmetro %1", "x"},
|
||||
@@ -648,8 +652,9 @@ typedef struct {
|
||||
#define VM_STACK 64
|
||||
#define VM_THREADS 32
|
||||
typedef struct {
|
||||
VmFrame st[VM_STACK];
|
||||
int sp;
|
||||
VmFrame st[VM_STACK];
|
||||
int sp;
|
||||
TbVarScope* scope; /* variaveis locais deste script */
|
||||
} VmThread;
|
||||
|
||||
/* Cache das arvores do runtime por (aba, bloco, profundidade de chamada):
|
||||
|
||||
@@ -816,7 +816,10 @@ static bool slot_is_var_name(const TinyBlocks_TypeBlock* t, int s) {
|
||||
const char* n = t->type_name;
|
||||
return strcmp(n, "Variables.Get") == 0 ||
|
||||
strcmp(n, "Variables.Set") == 0 ||
|
||||
strcmp(n, "Variables.Change") == 0;
|
||||
strcmp(n, "Variables.Change") == 0 ||
|
||||
strcmp(n, "Variables.DeclareLocal") == 0 ||
|
||||
strcmp(n, "Variables.DeclareGlobal") == 0 ||
|
||||
strcmp(n, "Variables.Delete") == 0;
|
||||
}
|
||||
|
||||
static void draw_slot(TinyBlocks_RenderableBlock* rb, const EdBlock* b, int s,
|
||||
@@ -974,6 +977,8 @@ static void clear_live(Tab* T) {
|
||||
|
||||
/* Destrói as instâncias do runtime da execução anterior (e para os scripts) */
|
||||
static void run_reset(Ed* ed) {
|
||||
for (int i = 0; i < ed->vm_n; i++) { tb_vars_scope_free(ed->vm[i].scope); ed->vm[i].scope = NULL; }
|
||||
tb_vars_scope_set(NULL);
|
||||
ed->vm_n = 0;
|
||||
ed->vm_running = false;
|
||||
ed->rt_cache_n = 0;
|
||||
@@ -1147,6 +1152,7 @@ static void vm_start(Ed* ed, Tab* T, int head) {
|
||||
if (ed->vm_n >= VM_THREADS) return;
|
||||
VmThread* th = &ed->vm[ed->vm_n++];
|
||||
th->sp = 0;
|
||||
th->scope = tb_vars_scope_new(NULL); /* variaveis locais deste script */
|
||||
vm_push(ed, th, T, head, VF_CHAIN, 0);
|
||||
ed->vm_running = true;
|
||||
}
|
||||
@@ -1277,8 +1283,14 @@ static void vm_frame(Ed* ed) {
|
||||
bool alive;
|
||||
do {
|
||||
alive = false;
|
||||
for (int i = 0; i < ed->vm_n && !ed->abort; i++)
|
||||
if (ed->vm[i].sp > 0 && vm_step(ed, &ed->vm[i])) alive = true;
|
||||
for (int i = 0; i < ed->vm_n && !ed->abort; i++) {
|
||||
VmThread* th = &ed->vm[i];
|
||||
if (th->sp <= 0) continue;
|
||||
tb_vars_scope_set(th->scope);
|
||||
if (vm_step(ed, th)) alive = true;
|
||||
else { tb_vars_scope_set(NULL); tb_vars_scope_free(th->scope); th->scope = NULL; }
|
||||
tb_vars_scope_set(NULL);
|
||||
}
|
||||
} while (alive && !ed->abort && !ed->redraw_req && now_ms() - t0 < VM_FRAME_MS);
|
||||
cap_end(ed);
|
||||
if (!alive || ed->abort) {
|
||||
@@ -1493,6 +1505,9 @@ static void eval_composite(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
CallFrame* f = &ed->frames[ed->frame_n++];
|
||||
memset(f, 0, sizeof(*f));
|
||||
f->def = def;
|
||||
TbVarScope* outer = tb_vars_scope_current();
|
||||
TbVarScope* local = tb_vars_scope_new(NULL); /* locais da chamada: nao veem as de quem chamou */
|
||||
tb_vars_scope_set(local);
|
||||
for (int i = 0; i < def->input_count; i++)
|
||||
f->in[i] = tb_value_clone(tb_block_get_input(self, rt, (uint32_t)i));
|
||||
|
||||
@@ -1513,6 +1528,8 @@ static void eval_composite(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
f->out[i] = tb_value_none();
|
||||
}
|
||||
for (int i = 0; i < def->input_count; i++) tb_value_free(&f->in[i]);
|
||||
tb_vars_scope_set(outer);
|
||||
tb_vars_scope_free(local);
|
||||
ed->frame_n--;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
/*
|
||||
* TinyBlocks - tb_variables.h
|
||||
* Library da categoria "Variáveis". Ela é dona do armazenamento (nome=valor);
|
||||
* UIs, como o editor, consultam a lista para desenhar monitores.
|
||||
* Library da categoria "Variáveis" (blocos). O armazenamento, com escopos
|
||||
* locais, fica no core (tinyblocks/tb_vars.h); esta API olha as globais,
|
||||
* que UIs como o editor usam para desenhar monitores e salvar o projeto.
|
||||
*/
|
||||
#ifndef TINYBLOCKS_VARIABLES_H
|
||||
#define TINYBLOCKS_VARIABLES_H
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user