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

+15 -77
View File
@@ -119,56 +119,8 @@ static void draw_bump(TinyBlocks_RenderableBlock* rb, float x, float y, Col c, C
/* ===
* Valores
* === */
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;
case TB_VALUE_BLOCK: snprintf(buf, cap, "[bloco]"); break;
case TB_VALUE_VECTOR: snprintf(buf, cap, "[vetor]"); break;
case TB_VALUE_JSON: {
char* js = v->as.json ? tb_json_stringify(v->as.json) : NULL;
snprintf(buf, cap, "%s", js ? js : "[json]");
free(js);
break;
}
case TB_VALUE_IMAGE: snprintf(buf, cap, "[imagem %dx%d]",
tb_image_width(v->as.image), tb_image_height(v->as.image)); break;
default: buf[0] = 0; break;
}
}
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;
}
static long long value_to_ll(const TinyBlocks_Value* v) {
double d;
if (v->kind == TB_VALUE_INTEGER) return v->as.integer;
return value_num(v, &d) ? llround(d) : 0;
}
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 (tolower((unsigned char)*t) != *f) return true;
return *t || *f;
}
case TB_VALUE_BLOCK: return v->as.block != NULL;
default: return false;
}
}
/* Texto, numero, verdade e nome do tipo vem das classes de valor do core
(tb_value_to_text, tb_value_as_integer, tb_value_truthy, tb_value_type_name). */
/* Converte o texto de um campo no valor esperado pelo slot.
* Slots sem tipo (NONE) adivinham: inteiro, real ou texto. */
@@ -1144,13 +1096,13 @@ static void run_chain(Ed* ed, Tab* T, int head) {
if (strcmp(tn, "Control.Repeat") == 0) {
TinyBlocks_Value v = eval_arg(ed, T, bi, 0);
long long n = value_to_ll(&v);
long long n = tb_value_as_integer(v, 0);
tb_value_free(&v);
for (long long k = 0; k < n && !err_pending(ed) && step(ed); k++) run_chain(ed, T, b->sub[0]);
} else if (strcmp(tn, "Control.RepeatUntil") == 0) {
for (;;) {
TinyBlocks_Value v = eval_arg(ed, T, bi, 0);
bool done = value_truthy(&v);
bool done = tb_value_truthy(v);
tb_value_free(&v);
if (done || err_pending(ed) || !step(ed)) break;
run_chain(ed, T, b->sub[0]);
@@ -1160,7 +1112,7 @@ static void run_chain(Ed* ed, Tab* T, int head) {
while (!err_pending(ed) && step(ed)) run_chain(ed, T, b->sub[0]);
} else if (strcmp(tn, "Control.If") == 0 || strcmp(tn, "Control.IfElse") == 0) {
TinyBlocks_Value v = eval_arg(ed, T, bi, 0);
bool cond = value_truthy(&v);
bool cond = tb_value_truthy(v);
tb_value_free(&v);
if (err_pending(ed)) return;
if (cond) run_chain(ed, T, b->sub[0]);
@@ -1275,7 +1227,7 @@ static bool vm_step(Ed* ed, VmThread* th) {
f->n--;
} else if (f->kind == VF_UNTIL) {
TinyBlocks_Value v = eval_arg(ed, T, f->bi, 0);
bool done = value_truthy(&v);
bool done = tb_value_truthy(v);
tb_value_free(&v);
if (done) { th->sp--; continue; }
}
@@ -1295,7 +1247,7 @@ static bool vm_step(Ed* ed, VmThread* th) {
if (sh == SH_HAT || shape_is_value(sh)) continue;
if (strcmp(tn, "Control.Repeat") == 0) {
TinyBlocks_Value v = eval_arg(ed, T, bi, 0);
long long n = value_to_ll(&v);
long long n = tb_value_as_integer(v, 0);
tb_value_free(&v);
if (n > 0) vm_push(ed, th, T, bi, VF_REPEAT, n);
} else if (strcmp(tn, "Control.RepeatUntil") == 0) {
@@ -1304,7 +1256,7 @@ static bool vm_step(Ed* ed, VmThread* th) {
vm_push(ed, th, T, bi, VF_FOREVER, 0);
} else if (strcmp(tn, "Control.If") == 0 || strcmp(tn, "Control.IfElse") == 0) {
TinyBlocks_Value v = eval_arg(ed, T, bi, 0);
bool cond = value_truthy(&v);
bool cond = tb_value_truthy(v);
tb_value_free(&v);
if (err_pending(ed)) continue;
if (cond) vm_push(ed, th, T, b->sub[0], VF_CHAIN, 0);
@@ -1485,25 +1437,11 @@ static bool json_point_at(const TinyBlocks_JsonValue* arr, int i, double* x, dou
return ok;
}
/* Nome do tipo do valor, para o balão de resultado */
/* Nome do tipo do valor, para o balão de resultado. "lista de pontos" é
coisa do editor (desenha os pontos); o resto vem da classe do valor. */
static const char* value_type_name(const TinyBlocks_Value* v) {
switch (v->kind) {
case TB_VALUE_INTEGER: return "número inteiro";
case TB_VALUE_REAL: return "número real";
case TB_VALUE_TEXT: return "texto";
case TB_VALUE_BLOCK: return "bloco";
case TB_VALUE_VECTOR: return "desenho vetorial";
case TB_VALUE_IMAGE: return "imagem";
case TB_VALUE_JSON:
if (!v->as.json) return "JSON";
if (json_is_points(v->as.json)) return "lista de pontos";
switch (tb_json_kind(v->as.json)) {
case TB_JSON_ARRAY: return "lista";
case TB_JSON_OBJECT: return "objeto JSON";
default: return "JSON";
}
default: return "nada";
}
if (v->kind == TB_VALUE_JSON && v->as.json && json_is_points(v->as.json)) return "lista de pontos";
return tb_value_type_name(*v);
}
/* Mostra o resultado de um repórter no balão (estilo Scratch) */
@@ -1533,7 +1471,7 @@ static void bubble_set(Ed* ed, int root, TinyBlocks_Value* v) {
tb_image_width(v->as.image), tb_image_height(v->as.image));
return;
}
value_to_str(v, ed->bubble_text, sizeof(ed->bubble_text));
tb_value_to_text(*v, ed->bubble_text, sizeof(ed->bubble_text));
if (!ed->bubble_text[0]) strcpy(ed->bubble_text, "(vazio)");
}
@@ -1567,7 +1505,7 @@ static unsigned now_ms(void) {
* === */
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(&v, out, cap);
tb_value_to_text(v, out, cap);
}
/* == Blocos compostos ("Meus Blocos") ===
@@ -4709,7 +4647,7 @@ static void render_output(Ed* ed, TinyBlocks_RenderableBlock* rb) {
const char* vname = tb_variables_name(v);
const TinyBlocks_Value* vv = tb_variables_value(v);
char val[64];
if (vv) value_to_str(vv, val, sizeof(val)); else val[0] = 0;
if (vv) tb_value_to_text(*vv, val, sizeof(val)); else val[0] = 0;
float nw = tw(vname, FS), vw = fmaxf(30, tw(val, FS) + 14);
float mw = nw + vw + 26;
rb_fill_rrect(rb, bx+10, y, mw, 26, 5, COL_MON_BORDER);