Result como tagged union e classes de valor (polimorfismo dos valores)

- TinyBlocks_Result (tb_result.h): Ok | Erro com tag (enum) e payload,
  ref-counted, que flui como TB_VALUE_RESULT. A library de erros passa a
  produzir/consumir Result em vez de objetos JSON {"ok"/"erro"}.
- TinyBlocks_ValueClass (tb_value_class.h): cada kind de valor tem uma
  "interface" (copy, release, to_text, to_number, truthy, equals,
  type_name) numa tabela; tb_value_clone/free e as operacoes genericas
  (tb_value_to_text, tb_value_to_number, tb_value_truthy, tb_value_equals,
  tb_value_type_name) despacham por ela. Sem libm no core.
- Editor e as conversoes "texto de" / "número de" usam o despacho.
- make test-value: classes de valor e Result.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mguinhosandClaude Opus 5.5 committed 2026-10-22 18:50:06 -03:00
1 parent 9f6a1a0eb0
commit 351e85bb14
13 files changed
+650 -290

No files matched your search

+8 -35
View File
@@ -19,7 +19,6 @@
#include <ctype.h>
#include <tinyblocks/tinyblocks.h>
#include <tinyblocks/tb_json_block.h>
#include "tb_number.h"
#ifndef M_PI
@@ -157,46 +156,20 @@ static void eval_is_int(TinyBlocks_Block* self, TinyBlocks* rt) {
/* -- Conversoes ----------------------------------------------------------- */
/* Texto -> numero. Aceita espacos nas pontas e virgula decimal ("3,5").
false se nao for numero. */
/* Texto -> numero: o mesmo do core (espacos nas pontas, virgula decimal) */
TB_API bool tb_number_parse(const char* s, double* out) {
if (!s) return false;
while (isspace((unsigned char)*s)) s++;
char buf[128];
size_t n = strlen(s);
while (n && isspace((unsigned char)s[n - 1])) n--;
if (!n || n >= sizeof(buf)) return false;
memcpy(buf, s, n); buf[n] = 0;
char* comma = strchr(buf, ',');
if (comma && !strchr(buf, '.') && !strchr(comma + 1, ',')) *comma = '.';
char* e;
double d = strtod(buf, &e);
if (e == buf || *e || !isfinite(d)) return false;
*out = d;
return true;
return tb_text_to_number(s, out);
}
/* "número de (x)": texto vira numero (0 se nao for), verdadeiro = 1 */
/* "número de (x)": cada tipo de valor sabe virar numero (classe do valor);
o que nao vira e 0, e "verdadeiro"/"true" e 1 */
static void eval_from(TinyBlocks_Block* self, TinyBlocks* rt) {
TinyBlocks_Value v = tb_block_get_input(self, rt, 0);
double d = 0;
switch (v.kind) {
case TB_VALUE_INTEGER: d = (double)v.as.integer; break;
case TB_VALUE_REAL: d = v.as.real; break;
case TB_VALUE_TEXT:
if (!tb_number_parse(v.as.text, &d)) {
const char* t = v.as.text ? v.as.text : "";
d = (!strcmp(t, "verdadeiro") || !strcmp(t, "true")) ? 1 : 0;
}
break;
case TB_VALUE_JSON: {
const char* s = tb_json_as_string(v.as.json);
if (s) { if (!tb_number_parse(s, &d)) d = 0; }
else if (tb_json_kind(v.as.json) == TB_JSON_BOOL) d = tb_json_as_bool(v.as.json, false);
else d = tb_json_as_number(v.as.json, 0);
break;
}
default: break;
if (!tb_value_to_number(v, &d)) {
char t[16];
tb_value_to_text(v, t, sizeof(t));
d = (!strcmp(t, "verdadeiro") || !strcmp(t, "true")) ? 1 : 0;
}
out_num(self, d);
}