Core: - TinyBlocks_FlowClass (tb_flow.h): um bloco C diz como roda (bocas, rótulos das faixas, tampa, next/error). next escolhe a boca, sai ou pede pausa (TB_FLOW_YIELD); error trata um erro que subiu de dentro. - TypeBlock: shape (chapéu...), flags (TB_TYPE_HIDDEN, TB_TYPE_REDRAW), flow, event (TB_EVENT_FLAG) e traços por campo (TB_SLOT_BOOLEAN, TB_SLOT_VARIABLE) via tb_type_block_set_input/output_flags. Libraries declaram o próprio comportamento: - control: repita / repita até que / sempre / se / se-senão como fluxos; errors: tente / se der erro / finalmente como fluxos. - events: chapéu da bandeira; operators: booleanos e campos hexagonais, literais numéricos ocultos; variables: campo de nome de variável; pen: redesenha o palco; graphics/editor: tipos de infraestrutura ocultos. Editor: - Forma, bocas, rótulos, tampa, campos booleanos/de variável, ocultos e "redesenhe" vêm dos traços (sem strcmp em nomes de outras libraries). - run_chain e vm_step viraram um executor só, que segue a classe de fluxo de cada bloco C; o corpo de Meus Blocos roda nele sem pausas. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
174 lines
7.4 KiB
C
174 lines
7.4 KiB
C
/*
|
|
* TinyBlocks - tb_variables.c
|
|
* 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"
|
|
|
|
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);
|
|
/* value_to_str reduzido */
|
|
switch (v.kind) {
|
|
case TB_VALUE_INTEGER: snprintf(out, cap, "%lld", (long long)v.as.integer); break;
|
|
case TB_VALUE_REAL: snprintf(out, cap, "%g", v.as.real); break;
|
|
case TB_VALUE_TEXT: snprintf(out, cap, "%s", v.as.text ? v.as.text : ""); break;
|
|
default: out[0] = 0; break;
|
|
}
|
|
}
|
|
|
|
static void eval_var_get(TinyBlocks_Block* self, TinyBlocks* rt) {
|
|
char name[64]; input_name(self, rt, 0, name, sizeof(name));
|
|
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));
|
|
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));
|
|
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;
|
|
}
|
|
|
|
/* "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;
|
|
}
|
|
|
|
/* "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;
|
|
}
|
|
|
|
/* "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;
|
|
}
|
|
|
|
/* == 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;
|
|
const char* s = value ? value : "";
|
|
char* end = NULL;
|
|
TinyBlocks_Value v;
|
|
long long iv = strtoll(s, &end, 10);
|
|
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 === */
|
|
static bool variables_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
|
static const TinyBlocks_Category cat = {
|
|
"Variáveis",
|
|
{255/255.f, 140/255.f, 26/255.f, 1.f},
|
|
{219/255.f, 110/255.f, 0/255.f, 1.f},
|
|
};
|
|
tb_library_set_category(lib, &cat);
|
|
|
|
/* Metadados dos blocos (tooltip do editor: Alt + hover). */
|
|
TB_BLOCK_META("Variables.Set", "eval_var_set",
|
|
"Define o valor de uma variavel");
|
|
TB_BLOCK_META("Variables.Change", "eval_var_change",
|
|
"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}};
|
|
static const TinyBlocks_SlotDesc any_out[] = {{"valor", TB_VALUE_NONE}};
|
|
|
|
TinyBlocks_TypeBlock* t;
|
|
t = tb_type_block_create(rt, "Variables.Set", var_set, 2, NULL, 0, eval_var_set);
|
|
if (!t || !tb_library_add(lib, &t->base)) return false;
|
|
t = tb_type_block_create(rt, "Variables.Change", var_set, 2, NULL, 0, eval_var_change);
|
|
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;
|
|
|
|
/* o 1o campo de todos e o NOME de uma variavel (campo laranja no editor) */
|
|
static const char* const named[] = { "Variables.Set", "Variables.Change", "Variables.Get",
|
|
"Variables.DeclareLocal", "Variables.DeclareGlobal", "Variables.Delete" };
|
|
for (size_t k = 0; k < sizeof(named) / sizeof(named[0]); k++)
|
|
tb_type_block_set_input_flags(tb_type_block_find(rt, named[k]), 0, TB_SLOT_VARIABLE);
|
|
return true;
|
|
}
|
|
|
|
static void variables_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
|
(void)rt; (void)lib;
|
|
tb_vars_clear();
|
|
}
|
|
|
|
TINYBLOCKS_DEFINE_LIBRARY(variables,
|
|
"tinyblocks-variables",
|
|
"1.0.0",
|
|
variables_init,
|
|
variables_deinit)
|
|
|
|
const TinyBlocks_LibraryDescriptor* tb_variables_get_library(void) {
|
|
return &_tb_lib_desc_variables;
|
|
}
|