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:
1 parent
a5e36b3eed
commit
5ffc1741c2
27 files changed
+217
-416
No files matched your search
@@ -11,41 +11,9 @@
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
#include "tb_operators.h"
|
||||
|
||||
/* == Conversões === */
|
||||
static bool value_num(const TinyBlocks_Value* v, double* d) {
|
||||
if (v->kind == TB_VALUE_INTEGER) { *d = (double)v->as.integer; return true; }
|
||||
if (v->kind == TB_VALUE_REAL) { *d = v->as.real; return true; }
|
||||
if (v->kind == TB_VALUE_TEXT && v->as.text && v->as.text[0]) {
|
||||
char* e; *d = strtod(v->as.text, &e);
|
||||
return *e == 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/* == Conversões: classes de valor do core (tb_value_to_number/_truthy/_to_text) === */
|
||||
|
||||
static bool value_truthy(const TinyBlocks_Value* v) {
|
||||
switch (v->kind) {
|
||||
case TB_VALUE_INTEGER: return v->as.integer != 0;
|
||||
case TB_VALUE_REAL: return v->as.real != 0.0;
|
||||
case TB_VALUE_TEXT: {
|
||||
const char* t = v->as.text ? v->as.text : "";
|
||||
if (!t[0] || strcmp(t, "0") == 0) return false;
|
||||
const char* f = "false";
|
||||
for (; *t && *f; t++, f++)
|
||||
if ((*t | 32) != *f) return true;
|
||||
return *t || *f;
|
||||
}
|
||||
default: return false;
|
||||
}
|
||||
}
|
||||
|
||||
static void value_to_str(const TinyBlocks_Value* v, char* buf, size_t cap) {
|
||||
switch (v->kind) {
|
||||
case TB_VALUE_INTEGER: snprintf(buf, cap, "%lld", (long long)v->as.integer); break;
|
||||
case TB_VALUE_REAL: snprintf(buf, cap, "%g", v->as.real); break;
|
||||
case TB_VALUE_TEXT: snprintf(buf, cap, "%s", v->as.text ? v->as.text : ""); break;
|
||||
default: buf[0] = 0; break;
|
||||
}
|
||||
}
|
||||
|
||||
static int str_icmp(const char* a, const char* b) {
|
||||
for (;; a++, b++) {
|
||||
@@ -58,10 +26,10 @@ static int cmp_inputs(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_Value a = tb_block_get_input(self, rt, 0);
|
||||
TinyBlocks_Value b = tb_block_get_input(self, rt, 1);
|
||||
double x, y;
|
||||
if (value_num(&a, &x) && value_num(&b, &y)) return (x > y) - (x < y);
|
||||
if (tb_value_to_number(a, &x) && tb_value_to_number(b, &y)) return (x > y) - (x < y);
|
||||
char sa[256], sb[256];
|
||||
value_to_str(&a, sa, sizeof(sa));
|
||||
value_to_str(&b, sb, sizeof(sb));
|
||||
tb_value_to_text(a, sa, sizeof(sa));
|
||||
tb_value_to_text(b, sb, sizeof(sb));
|
||||
return str_icmp(sa, sb);
|
||||
}
|
||||
|
||||
@@ -76,18 +44,18 @@ static void eval_op_gt(TinyBlocks_Block* self, TinyBlocks* rt)
|
||||
static void eval_op_and(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_Value a = tb_block_get_input(self, rt, 0);
|
||||
TinyBlocks_Value b = tb_block_get_input(self, rt, 1);
|
||||
tb_block_set_output(self, 0, tb_value_integer(value_truthy(&a) && value_truthy(&b)));
|
||||
tb_block_set_output(self, 0, tb_value_integer(tb_value_truthy(a) && tb_value_truthy(b)));
|
||||
self->dirty = false;
|
||||
}
|
||||
static void eval_op_or(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_Value a = tb_block_get_input(self, rt, 0);
|
||||
TinyBlocks_Value b = tb_block_get_input(self, rt, 1);
|
||||
tb_block_set_output(self, 0, tb_value_integer(value_truthy(&a) || value_truthy(&b)));
|
||||
tb_block_set_output(self, 0, tb_value_integer(tb_value_truthy(a) || tb_value_truthy(b)));
|
||||
self->dirty = false;
|
||||
}
|
||||
static void eval_op_not(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_Value a = tb_block_get_input(self, rt, 0);
|
||||
tb_block_set_output(self, 0, tb_value_integer(!value_truthy(&a)));
|
||||
tb_block_set_output(self, 0, tb_value_integer(!tb_value_truthy(a)));
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
@@ -95,7 +63,7 @@ static void eval_op_random(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_Value a = tb_block_get_input(self, rt, 0);
|
||||
TinyBlocks_Value b = tb_block_get_input(self, rt, 1);
|
||||
double lo = 0, hi = 0;
|
||||
value_num(&a, &lo); value_num(&b, &hi);
|
||||
tb_value_to_number(a, &lo); tb_value_to_number(b, &hi);
|
||||
if (lo > hi) { double t = lo; lo = hi; hi = t; }
|
||||
if (lo == floor(lo) && hi == floor(hi)) {
|
||||
long long span = (long long)(hi - lo) + 1;
|
||||
|
||||
Reference in new issue
Block a user