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

+2 -18
View File
@@ -315,25 +315,9 @@ static void eval_isempty(TinyBlocks_Block* self, TinyBlocks* rt) {
/* -- Conversoes ----------------------------------------------------------- */
/* "texto de (x)": numero sem ".0", lista/JSON como JSON, imagem descrita */
/* "texto de (x)": cada tipo de valor sabe virar texto (classe do valor) */
static void eval_from(TinyBlocks_Block* self, TinyBlocks* rt) {
TinyBlocks_Value v = tb_block_get_input(self, rt, 0);
char buf[64];
switch (v.kind) {
case TB_VALUE_TEXT: out_owned(self, dup_n(v.as.text ? v.as.text : "", strlen(v.as.text ? v.as.text : ""))); return;
case TB_VALUE_INTEGER: snprintf(buf, sizeof(buf), "%lld", (long long)v.as.integer); break;
case TB_VALUE_REAL: snprintf(buf, sizeof(buf), "%.15g", v.as.real); break;
case TB_VALUE_JSON: {
const char* s = tb_json_as_string(v.as.json); /* texto JSON sai sem aspas */
out_owned(self, s ? dup_n(s, strlen(s)) : tb_json_stringify(v.as.json));
return;
}
case TB_VALUE_IMAGE:
snprintf(buf, sizeof(buf), "imagem %dx%d", tb_image_width(v.as.image), tb_image_height(v.as.image));
break;
default: buf[0] = 0; break;
}
out_owned(self, dup_n(buf, strlen(buf)));
out_owned(self, tb_value_to_string(tb_block_get_input(self, rt, 0)));
}
/* Ponto de codigo da primeira letra (UTF-8); 0 se vazio */