Entradas convertidas no core: fim dos helpers de conversão repetidos

- tb_block_input_text / _number / _integer / _truthy: a entrada do bloco
  já convertida, despachando pela classe do valor. O texto de um valor
  que não é texto fica em cache na conexão (sem buffers nos blocos).
- Saída com nome terminado em '?' vira o traço TB_SLOT_BOOLEAN na criação
  do tipo (a convenção mora num lugar só; o editor só lê traços).
- Removidas ~35 funções locais (in_text, in_real, in_int, num, as_num,
  input_name, value_num, value_truthy, value_to_str, item_text) em 22
  libraries e no editor. Inteiros que truncavam (coordenadas de pixel)
  continuam truncando; um número ligado a um campo de texto agora vira
  texto ("42") em vez de "".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mguinhosandClaude Opus 5.5 committed 2026-10-22 18:59:14 -03:00
1 parent a5e36b3eed
commit 5ffc1741c2
27 files changed
+217 -416

No files matched your search

@@ -17,43 +17,29 @@
#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 char* name = tb_block_input_text(self, rt, 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));
const char* name = tb_block_input_text(self, rt, 0);
tb_vars_set(name, tb_block_get_input(self, rt, 1));
self->dirty = false;
}
/* Numero de uma variavel (NULL = nao existe = 0) */
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;
return v ? tb_value_as_number(*v, 0) : 0;
}
static void eval_var_change(TinyBlocks_Block* self, TinyBlocks* rt) {
char name[64]; input_name(self, rt, 0, name, sizeof(name));
const char* name = tb_block_input_text(self, rt, 0);
TinyBlocks_Value by = tb_block_get_input(self, rt, 1);
double a = as_num(tb_vars_get(name)), d = as_num(&by);
double a = as_num(tb_vars_get(name)), d = tb_value_as_number(by, 0);
TinyBlocks_Value nv = (a == floor(a) && d == floor(d))
? tb_value_integer((long long)llround(a + d))
: tb_value_real(a + d);
@@ -64,7 +50,7 @@ static void eval_var_change(TinyBlocks_Block* self, TinyBlocks* rt) {
/* "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));
const char* name = tb_block_input_text(self, rt, 0);
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;
@@ -72,7 +58,7 @@ static void eval_var_local(TinyBlocks_Block* self, TinyBlocks* rt) {
/* "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));
const char* name = tb_block_input_text(self, rt, 0);
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;
@@ -80,7 +66,7 @@ static void eval_var_global(TinyBlocks_Block* self, TinyBlocks* rt) {
/* "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));
const char* name = tb_block_input_text(self, rt, 0);
if (!tb_vars_delete(name) && name[0])
tinyblocks_set_error(rt, "variável '%s' não existe", name);
self->dirty = false;