Executor compila expressões puras; texto = TEXTO não lia além do fim

Traço novo no core: tb_type_block_set_pure (TB_PURE_NUM/LT/EQ/GT/AND/
OR/NOT/VAR). A library declara que o bloco é puro e qual a semântica;
number e operators definem cada conta como uma função de doubles, que
é ao mesmo tempo o eval do bloco e o que um executor compila.

tb_editor_jit.c: ao montar a árvore do runtime, uma expressão de
blocos puros vira um bloco só (Editor.Compiled), com nós nativos
(double, variável resolvida para o slot), sem marcar/avaliar/copiar
valor em cada nó. O que não é puro (item de lista, Meus Blocos...)
entra como nó que avalia a árvore normal. TINYBLOCKS_NO_JIT=1 desliga.

Render do Minecraft: 18 ms -> 12 ms, com a mesma imagem (checksum).

O "=" e o "<" entre textos passavam do fim da string: o | 32 vinha
antes do teste do fim, e "abc" = "ABC" dava falso.

tests/vm_bench.c (make bench-vm BENCH=projeto.json) mede a VM sem
janela; TINYBLOCKS_VM_MS troca a fatia de tempo da VM por quadro.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mguinhosandClaude Opus 5.5 committed 2026-10-23 17:08:13 -03:00
1 parent 146d58b7af
commit 630a1686e5
13 files changed
+694 -152

No files matched your search

+74 -67
View File
@@ -84,34 +84,37 @@ static double trig(double x) { return tb_number_round_to(x, 10); }
#define N(i) tb_block_input_number(self, rt, (i), 0)
static void eval_div(TinyBlocks_Block* self, TinyBlocks* rt) {
double b = N(1);
out_num(self, b == 0.0 ? 0.0 : N(0) / b);
/* Blocos puros: a funcao de doubles e a definicao do bloco. O eval le as
* entradas e chama a mesma funcao que um executor compila (TB_PURE_NUM). */
static double f_div(const double* x) { return x[1] == 0.0 ? 0.0 : x[0] / x[1]; }
static double f_pow(const double* x) { return pow(x[0], x[1]); }
static double f_sqrt(const double* x) { return x[0] < 0 ? 0.0 : sqrt(x[0]); }
static double f_round(const double* x) { return round(x[0]); }
static double f_floor(const double* x) { return floor(x[0]); }
static double f_ceil(const double* x) { return ceil(x[0]); }
static double f_round_to(const double* x) { return tb_number_round_to(x[0], (int)x[1]); }
static double f_sin(const double* x) { return trig(sin(x[0] * DEG)); }
static double f_cos(const double* x) { return trig(cos(x[0] * DEG)); }
static double f_tan(const double* x) { return trig(tan(x[0] * DEG)); }
static double f_asin(const double* x) { return (x[0] < -1 || x[0] > 1) ? 0.0 : trig(asin(x[0]) / DEG); }
static double f_acos(const double* x) { return (x[0] < -1 || x[0] > 1) ? 0.0 : trig(acos(x[0]) / DEG); }
static double f_atan(const double* x) { return trig(atan(x[0]) / DEG); }
static double f_log10(const double* x) { return x[0] > 0 ? log10(x[0]) : 0.0; }
static double f_ln(const double* x) { return x[0] > 0 ? log(x[0]) : 0.0; }
static double f_exp(const double* x) { return exp(x[0]); }
static double f_pi(const double* x) { (void)x; return M_PI; }
static double f_clamp(const double* x) {
double lo = x[1], hi = x[2];
if (lo > hi) { double t = lo; lo = hi; hi = t; }
return x[0] < lo ? lo : x[0] > hi ? hi : x[0];
}
static void eval_pow(TinyBlocks_Block* self, TinyBlocks* rt) { out_num(self, pow(N(0), N(1))); }
static void eval_sqrt(TinyBlocks_Block* self, TinyBlocks* rt) { double x = N(0); out_num(self, x < 0 ? 0.0 : sqrt(x)); }
static void eval_round(TinyBlocks_Block* self, TinyBlocks* rt) { out_num(self, round(N(0))); }
static void eval_floor(TinyBlocks_Block* self, TinyBlocks* rt) { out_num(self, floor(N(0))); }
static void eval_ceil(TinyBlocks_Block* self, TinyBlocks* rt) { out_num(self, ceil(N(0))); }
static void eval_round_to(TinyBlocks_Block* self, TinyBlocks* rt) {
out_num(self, tb_number_round_to(N(0), (int)N(1)));
static void eval_pure(TinyBlocks_Block* self, TinyBlocks* rt) {
double x[4] = {0};
uint32_t n = self->type->pure_arity;
for (uint32_t i = 0; i < n && i < 4; i++) x[i] = N(i);
out_num(self, self->type->pure_num(x));
}
static void eval_sin(TinyBlocks_Block* self, TinyBlocks* rt) { out_num(self, trig(sin(N(0) * DEG))); }
static void eval_cos(TinyBlocks_Block* self, TinyBlocks* rt) { out_num(self, trig(cos(N(0) * DEG))); }
static void eval_tan(TinyBlocks_Block* self, TinyBlocks* rt) { out_num(self, trig(tan(N(0) * DEG))); }
static void eval_asin(TinyBlocks_Block* self, TinyBlocks* rt) {
double x = N(0);
out_num(self, (x < -1 || x > 1) ? 0.0 : trig(asin(x) / DEG));
}
static void eval_acos(TinyBlocks_Block* self, TinyBlocks* rt) {
double x = N(0);
out_num(self, (x < -1 || x > 1) ? 0.0 : trig(acos(x) / DEG));
}
static void eval_atan(TinyBlocks_Block* self, TinyBlocks* rt) { out_num(self, trig(atan(N(0)) / DEG)); }
static void eval_log10(TinyBlocks_Block* self, TinyBlocks* rt) { double x = N(0); out_num(self, x > 0 ? log10(x) : 0.0); }
static void eval_ln(TinyBlocks_Block* self, TinyBlocks* rt) { double x = N(0); out_num(self, x > 0 ? log(x) : 0.0); }
static void eval_exp(TinyBlocks_Block* self, TinyBlocks* rt) { out_num(self, exp(N(0))); }
static void eval_pi(TinyBlocks_Block* self, TinyBlocks* rt) { (void)rt; out_num(self, M_PI); }
static void eval_random(TinyBlocks_Block* self, TinyBlocks* rt) {
double a = N(0), b = N(1);
@@ -121,12 +124,6 @@ static void eval_random(TinyBlocks_Block* self, TinyBlocks* rt) {
self->dirty = false;
}
static void eval_clamp(TinyBlocks_Block* self, TinyBlocks* rt) {
double x = N(0), lo = N(1), hi = N(2);
if (lo > hi) { double t = lo; lo = hi; hi = t; }
out_num(self, x < lo ? lo : x > hi ? hi : x);
}
static void eval_format(TinyBlocks_Block* self, TinyBlocks* rt) {
char buf[64];
tb_number_format(N(0), (int)N(1), buf, sizeof(buf));
@@ -203,6 +200,16 @@ static void eval_from_base(TinyBlocks_Block* self, TinyBlocks* rt) {
/* -- Registro --- */
static const TinyBlocks_SlotDesc s_num_out[] = {{"numero", TB_VALUE_NONE}};
static bool pure(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib, const char* name,
const TinyBlocks_SlotDesc* in, uint32_t ni, double (*f)(const double*)) {
TinyBlocks_TypeBlock* t = tb_type_block_create(rt, name, in, ni, s_num_out, 1, eval_pure);
if (!t) return false;
tb_type_block_set_pure(t, TB_PURE_NUM, f, ni);
return tb_library_add(lib, &t->base);
}
static bool reg(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib, const char* name,
const TinyBlocks_SlotDesc* in, uint32_t ni,
const TinyBlocks_SlotDesc* out, uint32_t no,
@@ -220,25 +227,25 @@ static bool number_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
};
tb_library_set_category(lib, &cat);
TB_BLOCK_META("Number.Div", "eval_div", "Divisao com casas decimais (por 0 da 0)");
TB_BLOCK_META("Number.Pow", "eval_pow", "A elevado a B");
TB_BLOCK_META("Number.Sqrt", "eval_sqrt", "Raiz quadrada");
TB_BLOCK_META("Number.Round", "eval_round", "Arredonda para o inteiro mais proximo");
TB_BLOCK_META("Number.Floor", "eval_floor", "Arredonda para baixo");
TB_BLOCK_META("Number.Ceil", "eval_ceil", "Arredonda para cima");
TB_BLOCK_META("Number.Div", "f_div", "Divisao com casas decimais (por 0 da 0)");
TB_BLOCK_META("Number.Pow", "f_pow", "A elevado a B");
TB_BLOCK_META("Number.Sqrt", "f_sqrt", "Raiz quadrada");
TB_BLOCK_META("Number.Round", "f_round", "Arredonda para o inteiro mais proximo");
TB_BLOCK_META("Number.Floor", "f_floor", "Arredonda para baixo");
TB_BLOCK_META("Number.Ceil", "f_ceil", "Arredonda para cima");
TB_BLOCK_META("Number.RoundTo", "tb_number_round_to", "Arredonda mantendo N casas decimais");
TB_BLOCK_META("Number.Sin", "eval_sin", "Seno (angulo em graus)");
TB_BLOCK_META("Number.Cos", "eval_cos", "Cosseno (angulo em graus)");
TB_BLOCK_META("Number.Tan", "eval_tan", "Tangente (angulo em graus)");
TB_BLOCK_META("Number.Asin", "eval_asin", "Arco-seno, em graus");
TB_BLOCK_META("Number.Acos", "eval_acos", "Arco-cosseno, em graus");
TB_BLOCK_META("Number.Atan", "eval_atan", "Arco-tangente, em graus");
TB_BLOCK_META("Number.Log10", "eval_log10", "Logaritmo na base 10");
TB_BLOCK_META("Number.Ln", "eval_ln", "Logaritmo natural (base e)");
TB_BLOCK_META("Number.Exp", "eval_exp", "e elevado a X");
TB_BLOCK_META("Number.Pi", "eval_pi", "O numero pi (3,14159...)");
TB_BLOCK_META("Number.Sin", "f_sin", "Seno (angulo em graus)");
TB_BLOCK_META("Number.Cos", "f_cos", "Cosseno (angulo em graus)");
TB_BLOCK_META("Number.Tan", "f_tan", "Tangente (angulo em graus)");
TB_BLOCK_META("Number.Asin", "f_asin", "Arco-seno, em graus");
TB_BLOCK_META("Number.Acos", "f_acos", "Arco-cosseno, em graus");
TB_BLOCK_META("Number.Atan", "f_atan", "Arco-tangente, em graus");
TB_BLOCK_META("Number.Log10", "f_log10", "Logaritmo na base 10");
TB_BLOCK_META("Number.Ln", "f_ln", "Logaritmo natural (base e)");
TB_BLOCK_META("Number.Exp", "f_exp", "e elevado a X");
TB_BLOCK_META("Number.Pi", "f_pi", "O numero pi (3,14159...)");
TB_BLOCK_META("Number.Random", "eval_random", "Numero real aleatorio entre A e B");
TB_BLOCK_META("Number.Clamp", "eval_clamp", "Prende o numero entre o minimo e o maximo");
TB_BLOCK_META("Number.Clamp", "f_clamp", "Prende o numero entre o minimo e o maximo");
TB_BLOCK_META("Number.Format", "tb_number_format", "Texto do numero com N casas decimais");
TB_BLOCK_META("Number.IsEven", "eval_is_even", "Verdadeiro se o numero for par");
TB_BLOCK_META("Number.IsPrime", "tb_number_is_prime", "Verdadeiro se o numero for primo");
@@ -262,25 +269,25 @@ static bool number_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
static const TinyBlocks_SlotDesc prime_out[] = {{"é primo?", TB_VALUE_INTEGER}};
static const TinyBlocks_SlotDesc int_out[] = {{"é inteiro?", TB_VALUE_INTEGER}};
return reg(rt, lib, "Number.Div", n2, 2, out, 1, eval_div)
&& reg(rt, lib, "Number.Pow", n2, 2, out, 1, eval_pow)
&& reg(rt, lib, "Number.Sqrt", n1, 1, out, 1, eval_sqrt)
&& reg(rt, lib, "Number.Round", n1, 1, out, 1, eval_round)
&& reg(rt, lib, "Number.Floor", n1, 1, out, 1, eval_floor)
&& reg(rt, lib, "Number.Ceil", n1, 1, out, 1, eval_ceil)
&& reg(rt, lib, "Number.RoundTo", nk, 2, out, 1, eval_round_to)
&& reg(rt, lib, "Number.Sin", n1, 1, out, 1, eval_sin)
&& reg(rt, lib, "Number.Cos", n1, 1, out, 1, eval_cos)
&& reg(rt, lib, "Number.Tan", n1, 1, out, 1, eval_tan)
&& reg(rt, lib, "Number.Asin", n1, 1, out, 1, eval_asin)
&& reg(rt, lib, "Number.Acos", n1, 1, out, 1, eval_acos)
&& reg(rt, lib, "Number.Atan", n1, 1, out, 1, eval_atan)
&& reg(rt, lib, "Number.Log10", n1, 1, out, 1, eval_log10)
&& reg(rt, lib, "Number.Ln", n1, 1, out, 1, eval_ln)
&& reg(rt, lib, "Number.Exp", n1, 1, out, 1, eval_exp)
&& reg(rt, lib, "Number.Pi", NULL, 0, out, 1, eval_pi)
return pure(rt, lib, "Number.Div", n2, 2, f_div)
&& pure(rt, lib, "Number.Pow", n2, 2, f_pow)
&& pure(rt, lib, "Number.Sqrt", n1, 1, f_sqrt)
&& pure(rt, lib, "Number.Round", n1, 1, f_round)
&& pure(rt, lib, "Number.Floor", n1, 1, f_floor)
&& pure(rt, lib, "Number.Ceil", n1, 1, f_ceil)
&& pure(rt, lib, "Number.RoundTo", nk, 2, f_round_to)
&& pure(rt, lib, "Number.Sin", n1, 1, f_sin)
&& pure(rt, lib, "Number.Cos", n1, 1, f_cos)
&& pure(rt, lib, "Number.Tan", n1, 1, f_tan)
&& pure(rt, lib, "Number.Asin", n1, 1, f_asin)
&& pure(rt, lib, "Number.Acos", n1, 1, f_acos)
&& pure(rt, lib, "Number.Atan", n1, 1, f_atan)
&& pure(rt, lib, "Number.Log10", n1, 1, f_log10)
&& pure(rt, lib, "Number.Ln", n1, 1, f_ln)
&& pure(rt, lib, "Number.Exp", n1, 1, f_exp)
&& pure(rt, lib, "Number.Pi", NULL, 0, f_pi)
&& reg(rt, lib, "Number.Random", n2, 2, out, 1, eval_random)
&& reg(rt, lib, "Number.Clamp", n3, 3, out, 1, eval_clamp)
&& pure(rt, lib, "Number.Clamp", n3, 3, f_clamp)
&& reg(rt, lib, "Number.Format", nk, 2, text_out, 1, eval_format)
&& reg(rt, lib, "Number.IsEven", n1, 1, even_out, 1, eval_is_even)
&& reg(rt, lib, "Number.IsPrime", n1, 1, prime_out, 1, eval_is_prime)