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--;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user