Divide as seções da paleta em libraries de categoria
Cada seção (Eventos, Controle, Operadores, Variáveis, Aparência, Caneta) virou uma library própria, com seus blocos e eval. Foi adicionado um metadado opcional de categoria (TinyBlocks_Category) para a UI agrupar a paleta a partir das libraries registradas, sem hardcode no editor. - núcleo: tb_library_set_category() e campo category em LibraryBlock - editor: cats_rebuild/cat_of usam a library dona de cada TypeBlock; "Meus Blocos" (User./Port.) continua no editor - variáveis: armazenamento movido para tinyblocks-variables, com API de enumeração (tb_variables_count/name/value/reset_blocks) - palco: Editor.Stage continua no editor; a API de desenho (tb_stage_*) é usada pela library tinyblocks-pen - host/tests/Makefile: registram e linkam as novas libraries
This commit is contained in:
1 parent
278d3f4473
commit
1cc0ff9d02
20 files changed
+1063
-597
No files matched your search
@@ -0,0 +1,237 @@
|
||||
/*
|
||||
* TinyBlocks - tb_operators.c
|
||||
* Categoria "Operadores": comparações, lógica booleana, número aleatório,
|
||||
* aritmética, texto e literais.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
#include "tb_operators.h"
|
||||
|
||||
/* == Conversões === */
|
||||
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 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 ((*t | 32) != *f) return true;
|
||||
return *t || *f;
|
||||
}
|
||||
default: return false;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
default: buf[0] = 0; break;
|
||||
}
|
||||
}
|
||||
|
||||
static int str_icmp(const char* a, const char* b) {
|
||||
for (;; a++, b++) {
|
||||
int ca = (*a | 32), cb = (*b | 32);
|
||||
if (ca != cb || !ca) return (ca > cb) - (ca < cb);
|
||||
}
|
||||
}
|
||||
|
||||
static int cmp_inputs(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_Value a = tb_block_get_input(self, rt, 0);
|
||||
TinyBlocks_Value b = tb_block_get_input(self, rt, 1);
|
||||
double x, y;
|
||||
if (value_num(&a, &x) && value_num(&b, &y)) return (x > y) - (x < y);
|
||||
char sa[256], sb[256];
|
||||
value_to_str(&a, sa, sizeof(sa));
|
||||
value_to_str(&b, sb, sizeof(sb));
|
||||
return str_icmp(sa, sb);
|
||||
}
|
||||
|
||||
/* == Lógica e comparação === */
|
||||
static void eval_op_lt(TinyBlocks_Block* self, TinyBlocks* rt)
|
||||
{ tb_block_set_output(self, 0, tb_value_integer(cmp_inputs(self, rt) < 0)); self->dirty = false; }
|
||||
static void eval_op_eq(TinyBlocks_Block* self, TinyBlocks* rt)
|
||||
{ tb_block_set_output(self, 0, tb_value_integer(cmp_inputs(self, rt) == 0)); self->dirty = false; }
|
||||
static void eval_op_gt(TinyBlocks_Block* self, TinyBlocks* rt)
|
||||
{ tb_block_set_output(self, 0, tb_value_integer(cmp_inputs(self, rt) > 0)); self->dirty = false; }
|
||||
|
||||
static void eval_op_and(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_Value a = tb_block_get_input(self, rt, 0);
|
||||
TinyBlocks_Value b = tb_block_get_input(self, rt, 1);
|
||||
tb_block_set_output(self, 0, tb_value_integer(value_truthy(&a) && value_truthy(&b)));
|
||||
self->dirty = false;
|
||||
}
|
||||
static void eval_op_or(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_Value a = tb_block_get_input(self, rt, 0);
|
||||
TinyBlocks_Value b = tb_block_get_input(self, rt, 1);
|
||||
tb_block_set_output(self, 0, tb_value_integer(value_truthy(&a) || value_truthy(&b)));
|
||||
self->dirty = false;
|
||||
}
|
||||
static void eval_op_not(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_Value a = tb_block_get_input(self, rt, 0);
|
||||
tb_block_set_output(self, 0, tb_value_integer(!value_truthy(&a)));
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void eval_op_random(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_Value a = tb_block_get_input(self, rt, 0);
|
||||
TinyBlocks_Value b = tb_block_get_input(self, rt, 1);
|
||||
double lo = 0, hi = 0;
|
||||
value_num(&a, &lo); value_num(&b, &hi);
|
||||
if (lo > hi) { double t = lo; lo = hi; hi = t; }
|
||||
if (lo == floor(lo) && hi == floor(hi)) {
|
||||
long long span = (long long)(hi - lo) + 1;
|
||||
tb_block_set_output(self, 0, tb_value_integer((long long)lo + (span > 0 ? rand() % span : 0)));
|
||||
} else {
|
||||
tb_block_set_output(self, 0, tb_value_real(lo + (hi - lo) * rand() / (double)RAND_MAX));
|
||||
}
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
/* == Aritmética (inteiros) === */
|
||||
static int64_t ai(TinyBlocks_Block* s, TinyBlocks* rt, uint32_t i) {
|
||||
TinyBlocks_Value v = tb_block_get_input(s, rt, i);
|
||||
return v.kind == TB_VALUE_INTEGER ? v.as.integer : 0;
|
||||
}
|
||||
static void eval_add(TinyBlocks_Block* s, TinyBlocks* rt)
|
||||
{ tb_block_set_output(s, 0, tb_value_integer(ai(s,rt,0) + ai(s,rt,1))); }
|
||||
static void eval_sub(TinyBlocks_Block* s, TinyBlocks* rt)
|
||||
{ tb_block_set_output(s, 0, tb_value_integer(ai(s,rt,0) - ai(s,rt,1))); }
|
||||
static void eval_mul(TinyBlocks_Block* s, TinyBlocks* rt)
|
||||
{ tb_block_set_output(s, 0, tb_value_integer(ai(s,rt,0) * ai(s,rt,1))); }
|
||||
static void eval_div(TinyBlocks_Block* s, TinyBlocks* rt)
|
||||
{ int64_t b = ai(s,rt,1); tb_block_set_output(s, 0, tb_value_integer(b ? ai(s,rt,0)/b : 0)); }
|
||||
static void eval_mod(TinyBlocks_Block* s, TinyBlocks* rt)
|
||||
{ int64_t b = ai(s,rt,1); tb_block_set_output(s, 0, tb_value_integer(b ? ai(s,rt,0)%b : 0)); }
|
||||
static void eval_abs(TinyBlocks_Block* s, TinyBlocks* rt)
|
||||
{ int64_t a = ai(s,rt,0); tb_block_set_output(s, 0, tb_value_integer(a < 0 ? -a : a)); }
|
||||
static void eval_max(TinyBlocks_Block* s, TinyBlocks* rt)
|
||||
{ int64_t a = ai(s,rt,0), b = ai(s,rt,1); tb_block_set_output(s, 0, tb_value_integer(a > b ? a : b)); }
|
||||
static void eval_min(TinyBlocks_Block* s, TinyBlocks* rt)
|
||||
{ int64_t a = ai(s,rt,0), b = ai(s,rt,1); tb_block_set_output(s, 0, tb_value_integer(a < b ? a : b)); }
|
||||
|
||||
/* == Texto === */
|
||||
static void eval_concat(TinyBlocks_Block* s, TinyBlocks* rt) {
|
||||
TinyBlocks_Value a = tb_block_get_input(s, rt, 0);
|
||||
TinyBlocks_Value b = tb_block_get_input(s, rt, 1);
|
||||
const char* sa = a.kind == TB_VALUE_TEXT ? a.as.text : "";
|
||||
const char* sb = b.kind == TB_VALUE_TEXT ? b.as.text : "";
|
||||
if (!sa) sa = "";
|
||||
if (!sb) sb = "";
|
||||
size_t n = strlen(sa) + strlen(sb) + 1;
|
||||
char* buf = (char*)malloc(n);
|
||||
if (!buf) return;
|
||||
memcpy(buf, sa, strlen(sa));
|
||||
memcpy(buf + strlen(sa), sb, strlen(sb) + 1);
|
||||
TinyBlocks_Value v; v.kind = TB_VALUE_TEXT; v.as.text = buf;
|
||||
tb_block_set_output(s, 0, v); /* set_output toma ownership */
|
||||
}
|
||||
static void eval_len(TinyBlocks_Block* s, TinyBlocks* rt) {
|
||||
TinyBlocks_Value a = tb_block_get_input(s, rt, 0);
|
||||
int64_t n = (a.kind == TB_VALUE_TEXT && a.as.text) ? (int64_t)strlen(a.as.text) : 0;
|
||||
tb_block_set_output(s, 0, tb_value_integer(n));
|
||||
}
|
||||
|
||||
/* == Literais === */
|
||||
static void eval_lit_int(TinyBlocks_Block* s, TinyBlocks* rt)
|
||||
{ tb_block_set_output(s, 0, tb_value_clone(tb_block_get_input(s, rt, 0))); }
|
||||
static void eval_lit_real(TinyBlocks_Block* s, TinyBlocks* rt)
|
||||
{ tb_block_set_output(s, 0, tb_value_clone(tb_block_get_input(s, rt, 0))); }
|
||||
static void eval_lit_text(TinyBlocks_Block* s, TinyBlocks* rt) {
|
||||
TinyBlocks_Value v = tb_block_get_input(s, rt, 0);
|
||||
const char* t = (v.kind == TB_VALUE_TEXT && v.as.text) ? v.as.text : "";
|
||||
tb_block_set_output(s, 0, tb_value_text(t));
|
||||
}
|
||||
|
||||
/* == Registro === */
|
||||
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,
|
||||
void (*fn)(TinyBlocks_Block*, TinyBlocks*)) {
|
||||
TinyBlocks_TypeBlock* t = tb_type_block_create(rt, name, in, ni, out, no, fn);
|
||||
return t && tb_library_add(lib, &t->base);
|
||||
}
|
||||
|
||||
static bool operators_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
srand((unsigned)time(NULL));
|
||||
static const TinyBlocks_Category cat = {
|
||||
"Operadores",
|
||||
{ 89/255.f, 192/255.f, 89/255.f, 1.f},
|
||||
{ 56/255.f, 148/255.f, 56/255.f, 1.f},
|
||||
};
|
||||
tb_library_set_category(lib, &cat);
|
||||
|
||||
static const TinyBlocks_SlotDesc any2[] = {{"a", TB_VALUE_NONE}, {"b", TB_VALUE_NONE}};
|
||||
static const TinyBlocks_SlotDesc bool2[] = {{"a", TB_VALUE_INTEGER}, {"b", TB_VALUE_INTEGER}};
|
||||
static const TinyBlocks_SlotDesc bool1[] = {{"a", TB_VALUE_INTEGER}};
|
||||
static const TinyBlocks_SlotDesc bool_out[] = {{"resultado", TB_VALUE_INTEGER}};
|
||||
static const TinyBlocks_SlotDesc rnd_in[] = {{"de", TB_VALUE_NONE}, {"até", TB_VALUE_NONE}};
|
||||
static const TinyBlocks_SlotDesc any_out[] = {{"valor", TB_VALUE_NONE}};
|
||||
|
||||
static const TinyBlocks_SlotDesc two_int[] = {{"a", TB_VALUE_INTEGER}, {"b", TB_VALUE_INTEGER}};
|
||||
static const TinyBlocks_SlotDesc one_int[] = {{"n", TB_VALUE_INTEGER}};
|
||||
static const TinyBlocks_SlotDesc int_out[] = {{"result", TB_VALUE_INTEGER}};
|
||||
static const TinyBlocks_SlotDesc two_text[] = {{"a", TB_VALUE_TEXT}, {"b", TB_VALUE_TEXT}};
|
||||
static const TinyBlocks_SlotDesc text_out[] = {{"result", TB_VALUE_TEXT}};
|
||||
static const TinyBlocks_SlotDesc text_in[] = {{"text", TB_VALUE_TEXT}};
|
||||
|
||||
static const TinyBlocks_SlotDesc lit_int_in[] = {{"value", TB_VALUE_INTEGER}};
|
||||
static const TinyBlocks_SlotDesc lit_int_out[] = {{"out", TB_VALUE_INTEGER}};
|
||||
static const TinyBlocks_SlotDesc lit_real_in[] = {{"value", TB_VALUE_REAL}};
|
||||
static const TinyBlocks_SlotDesc lit_real_out[]= {{"out", TB_VALUE_REAL}};
|
||||
static const TinyBlocks_SlotDesc lit_txt_in[] = {{"value", TB_VALUE_TEXT}};
|
||||
static const TinyBlocks_SlotDesc lit_txt_out[] = {{"out", TB_VALUE_TEXT}};
|
||||
|
||||
return 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)
|
||||
&& 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)
|
||||
&& 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)
|
||||
&& reg(rt, lib, "Literal.Real", lit_real_in, 1, lit_real_out, 1, eval_lit_real)
|
||||
&& reg(rt, lib, "Literal.Text", lit_txt_in, 1, lit_txt_out, 1, eval_lit_text);
|
||||
}
|
||||
|
||||
static void operators_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
(void)rt; (void)lib;
|
||||
}
|
||||
|
||||
TINYBLOCKS_DEFINE_LIBRARY(operators,
|
||||
"tinyblocks-operators",
|
||||
"1.0.0",
|
||||
operators_init,
|
||||
operators_deinit)
|
||||
|
||||
const TinyBlocks_LibraryDescriptor* tb_operators_get_library(void) {
|
||||
return &_tb_lib_desc_operators;
|
||||
}
|
||||
Reference in new issue
Block a user