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:
1 parent
146d58b7af
commit
630a1686e5
13 files changed
+694
-152
No files matched your search
@@ -5,7 +5,7 @@ CC := gcc
|
||||
BUILD := build
|
||||
CORE := ../../build
|
||||
|
||||
CFLAGS := -std=c11 -Wall -Wextra -pedantic -fvisibility=hidden \
|
||||
CFLAGS := -std=c11 -O2 -Wall -Wextra -pedantic -fvisibility=hidden \
|
||||
-D_POSIX_C_SOURCE=200809L -DTB_EDITOR_PRIVATE_API \
|
||||
-I../../include -Iinclude \
|
||||
-I../tinyblocks-graphics/include \
|
||||
@@ -29,7 +29,7 @@ IMG_A := $(IMG_DIR)/build/libtinyblocks-editor-image.a
|
||||
FILES_DIR := libraries/tinyblocks-editor-files
|
||||
FILES_A := $(FILES_DIR)/build/libtinyblocks-editor-files.a
|
||||
|
||||
OBJ := $(BUILD)/tb_editor.o $(BUILD)/tb_stage_widget.o $(BUILD)/tb_editor_modules.o $(BUILD)/tb_editor_mcp.o
|
||||
OBJ := $(BUILD)/tb_editor.o $(BUILD)/tb_stage_widget.o $(BUILD)/tb_editor_modules.o $(BUILD)/tb_editor_mcp.o $(BUILD)/tb_editor_jit.o
|
||||
LIB_A := $(BUILD)/libtinyblocks-editor.a
|
||||
LIB_SO := $(BUILD)/libtinyblocks-editor.so
|
||||
|
||||
@@ -60,6 +60,10 @@ $(BUILD)/tb_editor_mcp.o: src/tb_editor_mcp.c include/tb_editor_internal.h \
|
||||
@mkdir -p $(BUILD)
|
||||
$(CC) $(CFLAGS) -fPIC -c $< -o $@
|
||||
|
||||
$(BUILD)/tb_editor_jit.o: src/tb_editor_jit.c include/tb_editor_internal.h ../../include/tinyblocks/tb_type_block.h
|
||||
@mkdir -p $(BUILD)
|
||||
$(CC) $(CFLAGS) -fPIC -c $< -o $@
|
||||
|
||||
$(BUILD)/tb_stage_widget.o: src/tb_stage_widget.c include/tb_stage_widget.h \
|
||||
../tinyblocks-graphics/include/tb_graphics.h
|
||||
@mkdir -p $(BUILD)
|
||||
|
||||
@@ -1072,6 +1072,15 @@ const char* mouth_label(const TinyBlocks_TypeBlock* t, int k);
|
||||
int input_count(const TinyBlocks_TypeBlock* t);
|
||||
bool slot_is_bool(const TinyBlocks_TypeBlock* t, int i);
|
||||
const char* slot_src(const EdBlock* b, int s);
|
||||
|
||||
/* -- Executor: arvore do runtime e compilador de expressoes (tb_editor_jit.c) -- */
|
||||
TinyBlocks_Value value_from_text(const char* raw, TinyBlocks_ValueKind k);
|
||||
void run_track(Ed* ed, TinyBlocks_Block* b);
|
||||
TinyBlocks_TypeBlock* blk_type(Ed* ed, EdBlock* b);
|
||||
TinyBlocks_Block* build_rt(Ed* ed, Tab* T, int bi);
|
||||
void rt_mark_dirty(TinyBlocks_Block* r, int depth);
|
||||
/* Compila a expressao pura que comeca em bi num bloco so (NULL = nao deu) */
|
||||
TinyBlocks_Block* jit_build(Ed* ed, Tab* T, int bi);
|
||||
int cat_of(Ed* ed, const char* type_name);
|
||||
const BlockSpec* spec_of_type(const TinyBlocks_TypeBlock* t);
|
||||
void ax_block_label(Tab* T, int bi, char* out, size_t cap, int depth); /* "repita (10) vezes" */
|
||||
|
||||
@@ -126,7 +126,7 @@ static void draw_bump(TinyBlocks_RenderableBlock* rb, float x, float y, Col c, C
|
||||
|
||||
/* Converte o texto de um campo no valor esperado pelo slot.
|
||||
* Slots sem tipo (NONE) adivinham: inteiro, real ou texto. */
|
||||
static TinyBlocks_Value value_from_text(const char* raw, TinyBlocks_ValueKind k) {
|
||||
TinyBlocks_Value value_from_text(const char* raw, TinyBlocks_ValueKind k) {
|
||||
char* e;
|
||||
switch (k) {
|
||||
case TB_VALUE_INTEGER: {
|
||||
@@ -1262,7 +1262,7 @@ static void run_reset(Ed* ed) {
|
||||
tb_variables_reset_blocks();
|
||||
}
|
||||
|
||||
static void run_track(Ed* ed, TinyBlocks_Block* b) {
|
||||
void run_track(Ed* ed, TinyBlocks_Block* b) {
|
||||
if (ed->run_n >= ed->run_cap) {
|
||||
int nc = ed->run_cap ? ed->run_cap*2 : 256;
|
||||
TinyBlocks_Block** p = realloc(ed->run_blocks, (size_t)nc * sizeof(*p));
|
||||
@@ -1272,16 +1272,20 @@ static void run_track(Ed* ed, TinyBlocks_Block* b) {
|
||||
ed->run_blocks[ed->run_n++] = b;
|
||||
}
|
||||
|
||||
static TinyBlocks_TypeBlock* blk_type(Ed* ed, EdBlock* b) {
|
||||
TinyBlocks_TypeBlock* blk_type(Ed* ed, EdBlock* b) {
|
||||
if (!b->type && b->type_name[0]) b->type = tb_type_block_find(ed->rt, b->type_name);
|
||||
return b->type;
|
||||
}
|
||||
|
||||
/* Converte o bloco bi (e seus repórteres) em uma árvore de blocos do runtime */
|
||||
static TinyBlocks_Block* build_rt(Ed* ed, Tab* T, int bi) {
|
||||
TinyBlocks_Block* build_rt(Ed* ed, Tab* T, int bi) {
|
||||
EdBlock* b = &T->ws[bi];
|
||||
TinyBlocks_TypeBlock* t = blk_type(ed, b);
|
||||
if (!t) return NULL;
|
||||
if (t->pure && t->pure != TB_PURE_VAR) { /* expressao pura: compila */
|
||||
TinyBlocks_Block* c = jit_build(ed, T, bi);
|
||||
if (c) return c;
|
||||
}
|
||||
if (ed->run_n >= MAX_RUN_BLOCKS) { run_abort(ed, "limite de blocos em execução atingido"); return NULL; }
|
||||
TinyBlocks_Block* r = tb_block_create(ed->rt, t);
|
||||
if (!r) return NULL;
|
||||
@@ -1315,7 +1319,7 @@ static void rt_cache_clear(Ed* ed) {
|
||||
}
|
||||
|
||||
/* Marca a arvore inteira como "suja" para ser reavaliada */
|
||||
static void rt_mark_dirty(TinyBlocks_Block* r, int depth) {
|
||||
void rt_mark_dirty(TinyBlocks_Block* r, int depth) {
|
||||
if (!r || depth > 64) return;
|
||||
r->dirty = true;
|
||||
for (uint32_t i = 0; i < r->input_count; i++)
|
||||
@@ -1393,6 +1397,15 @@ static bool step(Ed* ed) {
|
||||
* avanca as threads varias vezes no mesmo quadro ate todas pausarem ou
|
||||
* terminarem. O corpo de Meus Blocos roda no mesmo executor, sem pausas. */
|
||||
#define VM_FRAME_MS 12u
|
||||
/* TINYBLOCKS_VM_MS troca a fatia (profiling: uma fatia grande roda tudo num quadro) */
|
||||
static unsigned vm_frame_ms(void) {
|
||||
static unsigned ms = 0;
|
||||
if (!ms) {
|
||||
const char* e = getenv("TINYBLOCKS_VM_MS");
|
||||
ms = e && atoi(e) > 0 ? (unsigned)atoi(e) : VM_FRAME_MS;
|
||||
}
|
||||
return ms;
|
||||
}
|
||||
static unsigned now_ms(void);
|
||||
|
||||
static const TinyBlocks_FlowClass* frame_flow(Ed* ed, VmFrame* f) {
|
||||
@@ -1577,7 +1590,7 @@ static void vm_frame(Ed* ed) {
|
||||
else { tb_vars_scope_set(NULL); tb_vars_scope_free(th->scope); th->scope = NULL; }
|
||||
tb_vars_scope_set(NULL);
|
||||
}
|
||||
} while (alive && !ed->abort && !ed->redraw_req && now_ms() - t0 < VM_FRAME_MS);
|
||||
} while (alive && !ed->abort && !ed->redraw_req && now_ms() - t0 < vm_frame_ms());
|
||||
cap_end(ed);
|
||||
if (!alive || ed->abort) {
|
||||
run_end(ed);
|
||||
|
||||
@@ -0,0 +1,285 @@
|
||||
/*
|
||||
* tb_editor_jit.c - compilador de expressoes do executor do editor.
|
||||
*
|
||||
* Uma expressao feita de blocos puros (os que a library declara com
|
||||
* tb_type_block_set_pure: contas, comparacoes, logica, "valor de variavel")
|
||||
* vira um bloco so, "Editor.Compiled", com a expressao numa arvore compacta
|
||||
* de nos nativos: numeros em double, variaveis resolvidas uma vez para o
|
||||
* slot (TbVarRef), sem marcar/avaliar/copiar valor em cada no.
|
||||
*
|
||||
* O que nao e puro (item de lista, pergunta, "Meus Blocos"...) entra como no
|
||||
* GEN: a arvore normal do runtime daquele bloco, avaliada como antes. A
|
||||
* semantica de cada no e a declarada pelo traco TB_PURE_* (tb_type_block.h).
|
||||
*/
|
||||
#include <math.h>
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
#include <tinyblocks/tb_vars.h>
|
||||
#include <tinyblocks/tb_value_class.h>
|
||||
#include "tb_editor_internal.h"
|
||||
|
||||
enum { J_CONST, J_VAR, J_GEN, J_NUM, J_CMP, J_AND, J_OR, J_NOT };
|
||||
|
||||
#define JIT_MAX_NODES 256
|
||||
#define JIT_MAX_ARGS 4
|
||||
|
||||
typedef struct {
|
||||
int op;
|
||||
int pure; /* J_CMP: TB_PURE_LT/EQ/GT */
|
||||
int arg[JIT_MAX_ARGS];
|
||||
int argc;
|
||||
double (*fn)(const double* x); /* J_NUM */
|
||||
double k; /* J_CONST: como numero */
|
||||
bool k_num; /* J_CONST: o valor e numero? */
|
||||
TinyBlocks_Value cval; /* J_CONST: valor original (owned) */
|
||||
TinyBlocks_Block* gen; /* J_GEN (dono: run_blocks do editor) */
|
||||
TbVarRef ref; /* J_VAR */
|
||||
char name[64]; /* J_VAR */
|
||||
} JNode;
|
||||
|
||||
typedef struct {
|
||||
TinyBlocks* rt;
|
||||
int n, root;
|
||||
JNode node[];
|
||||
} JProg;
|
||||
|
||||
typedef struct { /* compilacao em andamento */
|
||||
Ed* ed;
|
||||
Tab* T;
|
||||
JNode node[JIT_MAX_NODES];
|
||||
int n;
|
||||
bool fail;
|
||||
} JCtx;
|
||||
|
||||
/* -- Compilacao --- */
|
||||
|
||||
static int add_node(JCtx* c) {
|
||||
if (c->n >= JIT_MAX_NODES) { c->fail = true; return -1; }
|
||||
JNode* x = &c->node[c->n];
|
||||
memset(x, 0, sizeof(*x));
|
||||
return c->n++;
|
||||
}
|
||||
|
||||
static int compile_block(JCtx* c, int bi);
|
||||
|
||||
/* Entrada s do bloco bi: o bloco encaixado ou a constante do campo */
|
||||
static int compile_slot(JCtx* c, int bi, int s) {
|
||||
EdBlock* b = &c->T->ws[bi];
|
||||
if (b->args[s] >= 0) return compile_block(c, b->args[s]);
|
||||
int i = add_node(c);
|
||||
if (i < 0) return -1;
|
||||
JNode* x = &c->node[i];
|
||||
x->op = J_CONST;
|
||||
const char* raw = slot_src(b, s);
|
||||
x->cval = raw[0] ? value_from_text(raw, b->type->input_descs[s].expected_kind) : tb_value_none();
|
||||
double d;
|
||||
x->k_num = tb_value_to_number(x->cval, &d);
|
||||
x->k = x->k_num ? d : 0.0;
|
||||
return i;
|
||||
}
|
||||
|
||||
static int compile_gen(JCtx* c, int bi) {
|
||||
TinyBlocks_Block* r = build_rt(c->ed, c->T, bi);
|
||||
if (!r || r->output_count == 0) { c->fail = true; return -1; }
|
||||
int i = add_node(c);
|
||||
if (i < 0) return -1;
|
||||
c->node[i].op = J_GEN;
|
||||
c->node[i].gen = r;
|
||||
return i;
|
||||
}
|
||||
|
||||
static int compile_block(JCtx* c, int bi) {
|
||||
if (c->fail) return -1;
|
||||
EdBlock* b = &c->T->ws[bi];
|
||||
TinyBlocks_TypeBlock* t = blk_type(c->ed, b);
|
||||
if (!t) { c->fail = true; return -1; }
|
||||
int nin = (int)t->input_desc_count;
|
||||
switch (t->pure) {
|
||||
case TB_PURE_VAR: {
|
||||
if (nin < 1 || b->args[0] >= 0) return compile_gen(c, bi); /* nome calculado */
|
||||
const char* nm = slot_src(b, 0);
|
||||
if (!nm[0] || strlen(nm) >= sizeof(c->node[0].name)) return compile_gen(c, bi);
|
||||
int i = add_node(c);
|
||||
if (i < 0) return -1;
|
||||
c->node[i].op = J_VAR;
|
||||
strcpy(c->node[i].name, nm);
|
||||
c->node[i].ref.fixed = true; /* o nome fica no no: nunca muda */
|
||||
return i;
|
||||
}
|
||||
case TB_PURE_NUM:
|
||||
case TB_PURE_LT: case TB_PURE_EQ: case TB_PURE_GT:
|
||||
case TB_PURE_AND: case TB_PURE_OR: case TB_PURE_NOT: {
|
||||
int argc = t->pure == TB_PURE_NUM ? (int)t->pure_arity : t->pure == TB_PURE_NOT ? 1 : 2;
|
||||
if (argc > JIT_MAX_ARGS || argc > nin || (t->pure == TB_PURE_NUM && !t->pure_num))
|
||||
return compile_gen(c, bi);
|
||||
int i = add_node(c);
|
||||
if (i < 0) return -1;
|
||||
int a[JIT_MAX_ARGS];
|
||||
for (int k = 0; k < argc; k++) a[k] = compile_slot(c, bi, k);
|
||||
if (c->fail) return -1;
|
||||
JNode* x = &c->node[i];
|
||||
x->argc = argc;
|
||||
for (int k = 0; k < argc; k++) x->arg[k] = a[k];
|
||||
x->pure = t->pure;
|
||||
x->fn = t->pure_num;
|
||||
x->op = t->pure == TB_PURE_NUM ? J_NUM : t->pure == TB_PURE_AND ? J_AND
|
||||
: t->pure == TB_PURE_OR ? J_OR : t->pure == TB_PURE_NOT ? J_NOT : J_CMP;
|
||||
return i;
|
||||
}
|
||||
default:
|
||||
return compile_gen(c, bi);
|
||||
}
|
||||
}
|
||||
|
||||
/* -- Execucao --- */
|
||||
|
||||
static double jnum(JProg* P, int i);
|
||||
|
||||
/* Bloco nao compilado: reavalia a arvore dele (como o executor faria) */
|
||||
static TinyBlocks_Value gen_eval(JProg* P, JNode* x) {
|
||||
rt_mark_dirty(x->gen, 0);
|
||||
tb_block_eval(x->gen, P->rt);
|
||||
return x->gen->outputs[0];
|
||||
}
|
||||
|
||||
/* Valor (emprestado) de um no; contas viram numero em *tmp */
|
||||
static TinyBlocks_Value jval(JProg* P, int i, TinyBlocks_Value* tmp) {
|
||||
JNode* x = &P->node[i];
|
||||
switch (x->op) {
|
||||
case J_CONST: return x->cval;
|
||||
case J_VAR: {
|
||||
const TinyBlocks_Value* v = tb_vars_get_ref(x->name, &x->ref);
|
||||
return v ? *v : tb_value_integer(0);
|
||||
}
|
||||
case J_GEN: return gen_eval(P, x);
|
||||
default: {
|
||||
double d = jnum(P, i);
|
||||
*tmp = (d == floor(d) && fabs(d) < 9e15) ? tb_value_integer((int64_t)d) : tb_value_real(d);
|
||||
return *tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Texto sem diferenciar maiusculas (ASCII), como o "=" do Scratch */
|
||||
static int icmp(const char* a, const char* b) {
|
||||
for (;; a++, b++) {
|
||||
int ca = tolower((unsigned char)*a), cb = tolower((unsigned char)*b);
|
||||
if (ca != cb || !ca) return (ca > cb) - (ca < cb);
|
||||
}
|
||||
}
|
||||
|
||||
static int jcmp(JProg* P, JNode* x) {
|
||||
TinyBlocks_Value ta, tb2;
|
||||
TinyBlocks_Value a = jval(P, x->arg[0], &ta);
|
||||
double p, q;
|
||||
bool an = tb_value_to_number(a, &p);
|
||||
char sa[256] = "";
|
||||
if (!an) tb_value_to_text(a, sa, sizeof(sa)); /* antes de avaliar b (pode reusar a saida) */
|
||||
TinyBlocks_Value b = jval(P, x->arg[1], &tb2);
|
||||
bool bn = tb_value_to_number(b, &q);
|
||||
if (an && bn) return (p > q) - (p < q);
|
||||
char sb[256];
|
||||
if (an) tb_value_to_text(a, sa, sizeof(sa));
|
||||
tb_value_to_text(b, sb, sizeof(sb));
|
||||
return icmp(sa, sb);
|
||||
}
|
||||
|
||||
static bool jtruth(JProg* P, int i) {
|
||||
JNode* x = &P->node[i];
|
||||
if (x->op == J_CONST || x->op == J_VAR || x->op == J_GEN) {
|
||||
TinyBlocks_Value t;
|
||||
return tb_value_truthy(jval(P, i, &t));
|
||||
}
|
||||
return jnum(P, i) != 0.0;
|
||||
}
|
||||
|
||||
static double jnum(JProg* P, int i) {
|
||||
JNode* x = &P->node[i];
|
||||
switch (x->op) {
|
||||
case J_CONST: return x->k;
|
||||
case J_VAR: {
|
||||
const TinyBlocks_Value* v = tb_vars_get_ref(x->name, &x->ref);
|
||||
if (!v) return 0.0;
|
||||
if (v->kind == TB_VALUE_INTEGER) return (double)v->as.integer;
|
||||
if (v->kind == TB_VALUE_REAL) return v->as.real;
|
||||
return tb_value_as_number(*v, 0);
|
||||
}
|
||||
case J_GEN: return tb_value_as_number(gen_eval(P, x), 0);
|
||||
case J_NUM: {
|
||||
double a[JIT_MAX_ARGS];
|
||||
for (int k = 0; k < x->argc; k++) a[k] = jnum(P, x->arg[k]);
|
||||
return x->fn(a);
|
||||
}
|
||||
case J_CMP: {
|
||||
int r = jcmp(P, x);
|
||||
return (x->pure == TB_PURE_LT ? r < 0 : x->pure == TB_PURE_EQ ? r == 0 : r > 0) ? 1.0 : 0.0;
|
||||
}
|
||||
case J_AND: { bool a = jtruth(P, x->arg[0]), b = jtruth(P, x->arg[1]); return a && b; }
|
||||
case J_OR: { bool a = jtruth(P, x->arg[0]), b = jtruth(P, x->arg[1]); return a || b; }
|
||||
case J_NOT: return !jtruth(P, x->arg[0]);
|
||||
}
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
/* -- Bloco "Editor.Compiled" --- */
|
||||
|
||||
static void eval_compiled(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
(void)rt;
|
||||
JProg* P = self->userdata;
|
||||
double d = P ? jnum(P, P->root) : 0.0;
|
||||
if (isfinite(d) && d == floor(d) && fabs(d) < 9e15) tb_block_set_output(self, 0, tb_value_integer((int64_t)d));
|
||||
else tb_block_set_output(self, 0, tb_value_real(isfinite(d) ? d : 0.0));
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void deinit_compiled(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
(void)rt;
|
||||
JProg* P = self->userdata;
|
||||
if (!P) return;
|
||||
for (int i = 0; i < P->n; i++)
|
||||
if (P->node[i].op == J_CONST) tb_value_free(&P->node[i].cval);
|
||||
}
|
||||
|
||||
static TinyBlocks_TypeBlock* compiled_type(TinyBlocks* rt) {
|
||||
TinyBlocks_TypeBlock* t = tb_type_block_find(rt, "Editor.Compiled");
|
||||
if (t) return t;
|
||||
static const TinyBlocks_SlotDesc out[] = {{"valor", TB_VALUE_NONE}};
|
||||
t = tb_type_block_create(rt, "Editor.Compiled", NULL, 0, out, 1, eval_compiled);
|
||||
if (!t) return NULL;
|
||||
tb_type_block_set_flags(t, TB_TYPE_HIDDEN);
|
||||
tb_type_block_set_instance_hooks(t, 0, NULL, deinit_compiled);
|
||||
return t;
|
||||
}
|
||||
|
||||
TinyBlocks_Block* jit_build(Ed* ed, Tab* T, int bi) {
|
||||
const char* off = getenv("TINYBLOCKS_NO_JIT");
|
||||
if (off && off[0] && off[0] != '0') return NULL;
|
||||
TinyBlocks_TypeBlock* ct = compiled_type(ed->rt);
|
||||
if (!ct) return NULL;
|
||||
JCtx* c = calloc(1, sizeof(JCtx));
|
||||
if (!c) return NULL;
|
||||
c->ed = ed; c->T = T;
|
||||
int root = compile_block(c, bi);
|
||||
TinyBlocks_Block* r = NULL;
|
||||
if (!c->fail && root >= 0 && c->node[root].op != J_GEN && c->node[root].op != J_CONST) {
|
||||
r = tb_block_create(ed->rt, ct);
|
||||
JProg* P = r ? tb_alloc(ed->rt, sizeof(JProg) + (size_t)c->n * sizeof(JNode)) : NULL;
|
||||
if (P) {
|
||||
P->rt = ed->rt;
|
||||
P->n = c->n;
|
||||
P->root = root;
|
||||
memcpy(P->node, c->node, (size_t)c->n * sizeof(JNode));
|
||||
r->userdata = P;
|
||||
run_track(ed, r);
|
||||
c->n = 0; /* os valores agora sao do P */
|
||||
} else if (r) {
|
||||
tb_block_destroy(r, ed->rt);
|
||||
r = NULL;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < c->n; i++) if (c->node[i].op == J_CONST) tb_value_free(&c->node[i].cval);
|
||||
free(c);
|
||||
return r;
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <ctype.h>
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
#include "tb_operators.h"
|
||||
|
||||
@@ -15,9 +16,10 @@
|
||||
|
||||
|
||||
|
||||
/* Texto sem diferenciar maiusculas (ASCII), como o "=" do Scratch */
|
||||
static int str_icmp(const char* a, const char* b) {
|
||||
for (;; a++, b++) {
|
||||
int ca = (*a | 32), cb = (*b | 32);
|
||||
int ca = tolower((unsigned char)*a), cb = tolower((unsigned char)*b);
|
||||
if (ca != cb || !ca) return (ca > cb) - (ca < cb);
|
||||
}
|
||||
}
|
||||
@@ -74,60 +76,38 @@ static void eval_op_random(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
/* == Aritmética === */
|
||||
/* Inteiro com inteiro dá inteiro (conta exata); se um lado tiver casas
|
||||
/* == Aritmética ===
|
||||
* Inteiro com inteiro dá inteiro (conta exata); se um lado tiver casas
|
||||
* decimais, a conta é real, como no Scratch: (134) * (0.8) = 107.2.
|
||||
* O campo digitado chega como real (slot REAL), então 7 vale como inteiro. */
|
||||
typedef struct { bool ints; int64_t ia, ib; double a, b; } Num2;
|
||||
|
||||
* Cada bloco é a função de doubles (TB_PURE_NUM): o eval e um executor que
|
||||
* compile o bloco usam a mesma. */
|
||||
static bool integral(double x) { return isfinite(x) && x == floor(x) && fabs(x) < 9e15; }
|
||||
|
||||
static Num2 num2(TinyBlocks_Block* s, TinyBlocks* rt, uint32_t n) {
|
||||
Num2 r = { true, 0, 0, 0, 0 };
|
||||
r.a = tb_block_input_number(s, rt, 0, 0);
|
||||
r.b = n > 1 ? tb_block_input_number(s, rt, 1, 0) : 0;
|
||||
r.ints = integral(r.a) && integral(r.b);
|
||||
r.ia = r.ints ? (int64_t)r.a : 0;
|
||||
r.ib = r.ints ? (int64_t)r.b : 0;
|
||||
return r;
|
||||
static double f_add(const double* x) { return x[0] + x[1]; }
|
||||
static double f_sub(const double* x) { return x[0] - x[1]; }
|
||||
static double f_mul(const double* x) { return x[0] * x[1]; }
|
||||
/* inteiros: divisão inteira; com real: divisão real (por 0 dá 0) */
|
||||
static double f_div(const double* x) {
|
||||
if (integral(x[0]) && integral(x[1]))
|
||||
return x[1] != 0.0 ? (double)((int64_t)x[0] / (int64_t)x[1]) : 0.0;
|
||||
return x[1] != 0.0 ? x[0] / x[1] : 0.0;
|
||||
}
|
||||
static void out_real(TinyBlocks_Block* s, double x) {
|
||||
tb_block_set_output(s, 0, tb_value_real(isfinite(x) ? x : 0.0));
|
||||
static double f_mod(const double* x) {
|
||||
if (integral(x[0]) && integral(x[1]))
|
||||
return x[1] != 0.0 ? (double)((int64_t)x[0] % (int64_t)x[1]) : 0.0;
|
||||
return x[1] != 0.0 ? fmod(x[0], x[1]) : 0.0;
|
||||
}
|
||||
static void eval_add(TinyBlocks_Block* s, TinyBlocks* rt) {
|
||||
Num2 n = num2(s, rt, 2);
|
||||
if (n.ints) tb_block_set_output(s, 0, tb_value_integer(n.ia + n.ib)); else out_real(s, n.a + n.b);
|
||||
}
|
||||
static void eval_sub(TinyBlocks_Block* s, TinyBlocks* rt) {
|
||||
Num2 n = num2(s, rt, 2);
|
||||
if (n.ints) tb_block_set_output(s, 0, tb_value_integer(n.ia - n.ib)); else out_real(s, n.a - n.b);
|
||||
}
|
||||
static void eval_mul(TinyBlocks_Block* s, TinyBlocks* rt) {
|
||||
Num2 n = num2(s, rt, 2);
|
||||
if (n.ints) tb_block_set_output(s, 0, tb_value_integer(n.ia * n.ib)); else out_real(s, n.a * n.b);
|
||||
}
|
||||
/* inteiros: divisão inteira (como antes); com real: divisão real */
|
||||
static void eval_div(TinyBlocks_Block* s, TinyBlocks* rt) {
|
||||
Num2 n = num2(s, rt, 2);
|
||||
if (n.ints) tb_block_set_output(s, 0, tb_value_integer(n.ib ? n.ia / n.ib : 0));
|
||||
else out_real(s, n.b != 0.0 ? n.a / n.b : 0.0);
|
||||
}
|
||||
static void eval_mod(TinyBlocks_Block* s, TinyBlocks* rt) {
|
||||
Num2 n = num2(s, rt, 2);
|
||||
if (n.ints) tb_block_set_output(s, 0, tb_value_integer(n.ib ? n.ia % n.ib : 0));
|
||||
else out_real(s, n.b != 0.0 ? fmod(n.a, n.b) : 0.0);
|
||||
}
|
||||
static void eval_abs(TinyBlocks_Block* s, TinyBlocks* rt) {
|
||||
Num2 n = num2(s, rt, 1);
|
||||
if (n.ints) tb_block_set_output(s, 0, tb_value_integer(n.ia < 0 ? -n.ia : n.ia)); else out_real(s, fabs(n.a));
|
||||
}
|
||||
static void eval_max(TinyBlocks_Block* s, TinyBlocks* rt) {
|
||||
Num2 n = num2(s, rt, 2);
|
||||
if (n.ints) tb_block_set_output(s, 0, tb_value_integer(n.ia > n.ib ? n.ia : n.ib)); else out_real(s, n.a > n.b ? n.a : n.b);
|
||||
}
|
||||
static void eval_min(TinyBlocks_Block* s, TinyBlocks* rt) {
|
||||
Num2 n = num2(s, rt, 2);
|
||||
if (n.ints) tb_block_set_output(s, 0, tb_value_integer(n.ia < n.ib ? n.ia : n.ib)); else out_real(s, n.a < n.b ? n.a : n.b);
|
||||
static double f_abs(const double* x) { return fabs(x[0]); }
|
||||
static double f_max(const double* x) { return x[0] > x[1] ? x[0] : x[1]; }
|
||||
static double f_min(const double* x) { return x[0] < x[1] ? x[0] : x[1]; }
|
||||
|
||||
static void eval_pure(TinyBlocks_Block* s, TinyBlocks* rt) {
|
||||
double x[2] = {0, 0};
|
||||
for (uint32_t i = 0; i < s->type->pure_arity && i < 2; i++) x[i] = tb_block_input_number(s, rt, i, 0);
|
||||
double r = s->type->pure_num(x);
|
||||
if (integral(r)) tb_block_set_output(s, 0, tb_value_integer((int64_t)r));
|
||||
else tb_block_set_output(s, 0, tb_value_real(isfinite(r) ? r : 0.0));
|
||||
s->dirty = false;
|
||||
}
|
||||
|
||||
/* == Texto === */
|
||||
@@ -169,6 +149,26 @@ static bool reg(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib, const char* name,
|
||||
return t && tb_library_add(lib, &t->base);
|
||||
}
|
||||
|
||||
static bool pure(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib, const char* name,
|
||||
const TinyBlocks_SlotDesc* in, uint32_t ni, double (*f)(const double*)) {
|
||||
static const TinyBlocks_SlotDesc out[] = {{"result", TB_VALUE_NONE}};
|
||||
TinyBlocks_TypeBlock* t = tb_type_block_create(rt, name, in, ni, 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);
|
||||
}
|
||||
|
||||
/* Registra e declara a semantica pura (comparacoes e logica) */
|
||||
static bool reg_p(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib, const char* name,
|
||||
const TinyBlocks_SlotDesc* in, uint32_t ni,
|
||||
const TinyBlocks_SlotDesc* out, uint32_t no,
|
||||
void (*fn)(TinyBlocks_Block*, TinyBlocks*), int pure_kind) {
|
||||
TinyBlocks_TypeBlock* t = tb_type_block_create(rt, name, in, ni, out, no, fn);
|
||||
if (!t) return false;
|
||||
tb_type_block_set_pure(t, pure_kind, NULL, ni);
|
||||
return tb_library_add(lib, &t->base);
|
||||
}
|
||||
|
||||
static bool operators_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
srand((unsigned)time(NULL));
|
||||
static const TinyBlocks_Category cat = {
|
||||
@@ -193,21 +193,21 @@ static bool operators_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
"Inverte o valor logico");
|
||||
TB_BLOCK_META("Operators.Random", "eval_op_random",
|
||||
"Numero inteiro aleatorio entre A e B");
|
||||
TB_BLOCK_META("Math.Add", "eval_add",
|
||||
TB_BLOCK_META("Math.Add", "f_add",
|
||||
"Soma dois numeros");
|
||||
TB_BLOCK_META("Math.Sub", "eval_sub",
|
||||
TB_BLOCK_META("Math.Sub", "f_sub",
|
||||
"Subtrai B de A");
|
||||
TB_BLOCK_META("Math.Mul", "eval_mul",
|
||||
TB_BLOCK_META("Math.Mul", "f_mul",
|
||||
"Multiplica dois numeros");
|
||||
TB_BLOCK_META("Math.Div", "eval_div",
|
||||
TB_BLOCK_META("Math.Div", "f_div",
|
||||
"Divide A por B");
|
||||
TB_BLOCK_META("Math.Mod", "eval_mod",
|
||||
TB_BLOCK_META("Math.Mod", "f_mod",
|
||||
"Resto da divisao de A por B");
|
||||
TB_BLOCK_META("Math.Abs", "eval_abs",
|
||||
TB_BLOCK_META("Math.Abs", "f_abs",
|
||||
"Valor absoluto de um numero");
|
||||
TB_BLOCK_META("Math.Max", "eval_max",
|
||||
TB_BLOCK_META("Math.Max", "f_max",
|
||||
"Maior entre dois numeros");
|
||||
TB_BLOCK_META("Math.Min", "eval_min",
|
||||
TB_BLOCK_META("Math.Min", "f_min",
|
||||
"Menor entre dois numeros");
|
||||
TB_BLOCK_META("Text.Concat", "eval_concat",
|
||||
"Junta dois textos");
|
||||
@@ -241,21 +241,21 @@ static bool operators_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
static const TinyBlocks_SlotDesc lit_txt_in[] = {{"value", TB_VALUE_TEXT}};
|
||||
static const TinyBlocks_SlotDesc lit_txt_out[] = {{"out", TB_VALUE_TEXT}};
|
||||
|
||||
if (!(reg(rt, lib, "Operators.Lt", any2, 2, bool_out, 1, eval_op_lt)
|
||||
&& reg(rt, lib, "Operators.Eq", any2, 2, bool_out, 1, eval_op_eq)
|
||||
&& reg(rt, lib, "Operators.Gt", any2, 2, bool_out, 1, eval_op_gt)
|
||||
&& reg(rt, lib, "Operators.And", bool2, 2, bool_out, 1, eval_op_and)
|
||||
&& reg(rt, lib, "Operators.Or", bool2, 2, bool_out, 1, eval_op_or)
|
||||
&& reg(rt, lib, "Operators.Not", bool1, 1, bool_out, 1, eval_op_not)
|
||||
if (!(reg_p(rt, lib, "Operators.Lt", any2, 2, bool_out, 1, eval_op_lt, TB_PURE_LT)
|
||||
&& reg_p(rt, lib, "Operators.Eq", any2, 2, bool_out, 1, eval_op_eq, TB_PURE_EQ)
|
||||
&& reg_p(rt, lib, "Operators.Gt", any2, 2, bool_out, 1, eval_op_gt, TB_PURE_GT)
|
||||
&& reg_p(rt, lib, "Operators.And", bool2, 2, bool_out, 1, eval_op_and, TB_PURE_AND)
|
||||
&& reg_p(rt, lib, "Operators.Or", bool2, 2, bool_out, 1, eval_op_or, TB_PURE_OR)
|
||||
&& reg_p(rt, lib, "Operators.Not", bool1, 1, bool_out, 1, eval_op_not, TB_PURE_NOT)
|
||||
&& reg(rt, lib, "Operators.Random", rnd_in, 2, any_out, 1, eval_op_random)
|
||||
&& reg(rt, lib, "Math.Add", two_int, 2, int_out, 1, eval_add)
|
||||
&& reg(rt, lib, "Math.Sub", two_int, 2, int_out, 1, eval_sub)
|
||||
&& reg(rt, lib, "Math.Mul", two_int, 2, int_out, 1, eval_mul)
|
||||
&& reg(rt, lib, "Math.Div", two_int, 2, int_out, 1, eval_div)
|
||||
&& reg(rt, lib, "Math.Mod", two_int, 2, int_out, 1, eval_mod)
|
||||
&& reg(rt, lib, "Math.Abs", one_int, 1, int_out, 1, eval_abs)
|
||||
&& reg(rt, lib, "Math.Max", two_int, 2, int_out, 1, eval_max)
|
||||
&& reg(rt, lib, "Math.Min", two_int, 2, int_out, 1, eval_min)
|
||||
&& pure(rt, lib, "Math.Add", two_int, 2, f_add)
|
||||
&& pure(rt, lib, "Math.Sub", two_int, 2, f_sub)
|
||||
&& pure(rt, lib, "Math.Mul", two_int, 2, f_mul)
|
||||
&& pure(rt, lib, "Math.Div", two_int, 2, f_div)
|
||||
&& pure(rt, lib, "Math.Mod", two_int, 2, f_mod)
|
||||
&& pure(rt, lib, "Math.Abs", one_int, 1, f_abs)
|
||||
&& pure(rt, lib, "Math.Max", two_int, 2, f_max)
|
||||
&& pure(rt, lib, "Math.Min", two_int, 2, f_min)
|
||||
&& reg(rt, lib, "Text.Concat", two_text, 2, text_out, 1, eval_concat)
|
||||
&& reg(rt, lib, "Text.Length", text_in, 1, int_out, 1, eval_len)
|
||||
&& reg(rt, lib, "Literal.Integer", lit_int_in, 1, lit_int_out, 1, eval_lit_int)
|
||||
|
||||
Reference in new issue
Block a user