Files
mguinhos 90d47bf8a0 Converte símbolos Unicode decorativos dos comentários para ASCII
Substitui barras de caixa (─ ═ │ ┌ ┐ └ ┘ ├ ┤ ┬ ┴), travessões (—),
setas (→ ←), reticências (…), bullets (•), multiplicação (×) e
guillemets (› ‹) por equivalentes ASCII. Acentos portugueses são
preservados.
2026-10-20 19:27:33 -03:00

236 lines
9.1 KiB
C

/*
* TinyBlocks - exemplo hello
*
* Demonstra:
* 1. Inicializar o runtime
* 2. Criar TypeBlocks (Math.Add, Math.Mul, IO.Print)
* 3. Criar blocos literais (IntegerBlock, TextBlock)
* 4. Conectar blocos e avaliar
* 5. Usar LibraryBlock como namespace
*/
#include <stdio.h>
#include <tinyblocks/tinyblocks.h>
/* -- TypeBlock: Math.Add --- */
/* inputs: a(int), b(int)
* outputs: result(int) */
static void eval_math_add(TinyBlocks_Block* self, TinyBlocks* rt)
{
TinyBlocks_Value va = tb_block_get_input(self, rt, 0);
TinyBlocks_Value vb = tb_block_get_input(self, rt, 1);
int64_t a = va.kind == TB_VALUE_INTEGER ? va.as.integer : 0;
int64_t b = vb.kind == TB_VALUE_INTEGER ? vb.as.integer : 0;
tb_block_set_output(self, 0, tb_value_integer(a + b));
}
/* -- TypeBlock: Math.Mul --- */
static void eval_math_mul(TinyBlocks_Block* self, TinyBlocks* rt)
{
TinyBlocks_Value va = tb_block_get_input(self, rt, 0);
TinyBlocks_Value vb = tb_block_get_input(self, rt, 1);
int64_t a = va.kind == TB_VALUE_INTEGER ? va.as.integer : 0;
int64_t b = vb.kind == TB_VALUE_INTEGER ? vb.as.integer : 0;
tb_block_set_output(self, 0, tb_value_integer(a * b));
}
/* -- TypeBlock: IO.Print --- */
/* inputs: label(text), value(int)
* outputs: (nenhum) */
static void eval_io_print(TinyBlocks_Block* self, TinyBlocks* rt)
{
TinyBlocks_Value vlabel = tb_block_get_input(self, rt, 0);
TinyBlocks_Value vvalue = tb_block_get_input(self, rt, 1);
const char* label = vlabel.kind == TB_VALUE_TEXT ? vlabel.as.text : "(sem rótulo)";
char valstr[64] = "?";
if (vvalue.kind == TB_VALUE_INTEGER)
snprintf(valstr, sizeof(valstr), "%lld", (long long)vvalue.as.integer);
else if (vvalue.kind == TB_VALUE_REAL)
snprintf(valstr, sizeof(valstr), "%g", vvalue.as.real);
else if (vvalue.kind == TB_VALUE_TEXT)
snprintf(valstr, sizeof(valstr), "%s", vvalue.as.text);
printf("[IO.Print] %s = %s\n", label, valstr);
}
/* -- TypeBlock: Math.Factorial (demonstra userdata por instância) --- */
/* inputs: n(int)
* outputs: result(int) */
typedef struct { int64_t memo; } FactorialData;
static void eval_math_factorial(TinyBlocks_Block* self, TinyBlocks* rt)
{
TinyBlocks_Value vn = tb_block_get_input(self, rt, 0);
int64_t n = vn.kind == TB_VALUE_INTEGER ? vn.as.integer : 0;
int64_t result = 1;
for (int64_t i = 2; i <= n; ++i) result *= i;
FactorialData* d = (FactorialData*)self->userdata;
if (d) d->memo = result; /* guarda no userdata por instância */
tb_block_set_output(self, 0, tb_value_integer(result));
}
/* ===
* main
* === */
int main(void)
{
printf("=== TinyBlocks - exemplo hello ===\n\n");
/* 1. Inicializar runtime */
TinyBlocks* rt = NULL;
if (!tinyblocks_init(&rt, NULL)) {
fprintf(stderr, "Falha ao inicializar TinyBlocks\n");
return 1;
}
printf("[runtime] inicializado id_counter=%llu\n\n",
(unsigned long long)rt->next_id);
/* 2. Criar library "Math" */
TinyBlocks_LibraryBlock* lib_math = tb_library_block_create(rt, "Math");
TinyBlocks_LibraryBlock* lib_io = tb_library_block_create(rt, "IO");
/* 3. Definir slots e TypeBlocks */
static const TinyBlocks_SlotDesc two_int_ins[] = {
{ "a", TB_VALUE_INTEGER }, { "b", TB_VALUE_INTEGER } };
static const TinyBlocks_SlotDesc int_out[] = {
{ "result", TB_VALUE_INTEGER } };
static const TinyBlocks_SlotDesc one_int_in[] = {
{ "n", TB_VALUE_INTEGER } };
static const TinyBlocks_SlotDesc print_ins[] = {
{ "label", TB_VALUE_TEXT }, { "value", TB_VALUE_NONE } };
TinyBlocks_TypeBlock* t_add = tb_type_block_create(rt, "Math.Add",
two_int_ins, 2, int_out, 1, eval_math_add);
TinyBlocks_TypeBlock* t_mul = tb_type_block_create(rt, "Math.Mul",
two_int_ins, 2, int_out, 1, eval_math_mul);
TinyBlocks_TypeBlock* t_fact = tb_type_block_create(rt, "Math.Factorial",
one_int_in, 1, int_out, 1, eval_math_factorial);
TinyBlocks_TypeBlock* t_print = tb_type_block_create(rt, "IO.Print",
print_ins, 2, NULL, 0, eval_io_print);
/* Factorial usa userdata por instância */
tb_type_block_set_instance_hooks(t_fact, sizeof(FactorialData), NULL, NULL);
/* Adiciona às libraries */
tb_library_add(lib_math, &t_add->base);
tb_library_add(lib_math, &t_mul->base);
tb_library_add(lib_math, &t_fact->base);
tb_library_add(lib_io, &t_print->base);
printf("[types] Math.Add, Math.Mul, Math.Factorial, IO.Print registrados\n");
printf("[libs] Math=%u blocos, IO=%u bloco(s)\n\n",
lib_math->block_count, lib_io->block_count);
/* -- Exemplo 1: 3 + 4 --- */
printf("--- Exemplo 1: 3 + 4 ---\n");
{
TinyBlocks_IntegerBlock* lit3 = tb_integer_block_create(rt, 3);
TinyBlocks_IntegerBlock* lit4 = tb_integer_block_create(rt, 4);
TinyBlocks_TextBlock* label = tb_text_block_create(rt, "3 + 4");
TinyBlocks_Block* add = tb_block_create(rt, t_add);
TinyBlocks_Block* print = tb_block_create(rt, t_print);
/* Conecta: add.in[0] <- lit3.out[0]
add.in[1] <- lit4.out[0] */
tb_block_connect(add, 0, &lit3->base, 0);
tb_block_connect(add, 1, &lit4->base, 0);
/* print.in[0] <- label.out[0]
print.in[1] <- add.out[0] */
tb_block_connect(print, 0, &label->base, 0);
tb_block_connect(print, 1, add, 0);
/* Avalia a cadeia inteira (lazy: avalia dependências primeiro) */
tb_block_eval(print, rt);
}
/* -- Exemplo 2: (2 + 3) * 5 --- */
printf("\n--- Exemplo 2: (2 + 3) * 5 ---\n");
{
TinyBlocks_IntegerBlock* lit2 = tb_integer_block_create(rt, 2);
TinyBlocks_IntegerBlock* lit3 = tb_integer_block_create(rt, 3);
TinyBlocks_IntegerBlock* lit5 = tb_integer_block_create(rt, 5);
TinyBlocks_TextBlock* label = tb_text_block_create(rt, "(2+3)*5");
TinyBlocks_Block* add = tb_block_create(rt, t_add);
TinyBlocks_Block* mul = tb_block_create(rt, t_mul);
TinyBlocks_Block* print = tb_block_create(rt, t_print);
tb_block_connect(add, 0, &lit2->base, 0);
tb_block_connect(add, 1, &lit3->base, 0);
tb_block_connect(mul, 0, add, 0);
tb_block_connect(mul, 1, &lit5->base, 0);
tb_block_connect(print, 0, &label->base, 0);
tb_block_connect(print, 1, mul, 0);
tb_block_eval(print, rt);
}
/* -- Exemplo 3: 7! (fatorial com userdata por instância) --- */
printf("\n--- Exemplo 3: 7! ---\n");
{
TinyBlocks_IntegerBlock* lit7 = tb_integer_block_create(rt, 7);
TinyBlocks_TextBlock* label = tb_text_block_create(rt, "7!");
TinyBlocks_Block* fact = tb_block_create(rt, t_fact);
TinyBlocks_Block* print = tb_block_create(rt, t_print);
tb_block_connect(fact, 0, &lit7->base, 0);
tb_block_connect(print, 0, &label->base, 0);
tb_block_connect(print, 1, fact, 0);
tb_block_eval(print, rt);
/* Lê o valor memoizado do userdata */
FactorialData* d = (FactorialData*)fact->userdata;
printf(" userdata.memo = %lld\n", (long long)d->memo);
}
/* -- Exemplo 4: busca de tipo por nome --- */
printf("\n--- Exemplo 4: busca de tipo por nome ---\n");
{
TinyBlocks_TypeBlock* found = tb_type_block_find(rt, "Math.Add");
printf("tb_type_block_find(\"Math.Add\") = %s\n",
found ? found->type_name : "NULL");
/* Busca dentro da library */
TinyBlocks_Block* b = tb_library_find(lib_math, "Math.Mul");
printf("tb_library_find(lib_math, \"Math.Mul\") kind=%d name=%s\n",
b ? (int)b->kind : -1,
b && b->kind == TB_KIND_TYPE
? ((TinyBlocks_TypeBlock*)b)->type_name : "?");
}
/* -- Exemplo 5: to_string --- */
printf("\n--- Exemplo 5: representação textual ---\n");
{
TinyBlocks_IntegerBlock* ib = tb_integer_block_create(rt, 42);
TinyBlocks_RealBlock* rb = tb_real_block_create(rt, 3.14);
TinyBlocks_TextBlock* tb2 = tb_text_block_create(rt, "olá");
char buf[256];
tb_block_eval(&ib->base, rt);
tb_block_eval(&rb->base, rt);
tb_block_eval(&tb2->base, rt);
tb_block_to_string(&ib->base, rt, buf, sizeof(buf));
printf("IntegerBlock: %s\n", buf);
tb_block_to_string(&rb->base, rt, buf, sizeof(buf));
printf("RealBlock: %s\n", buf);
tb_block_to_string(&tb2->base, rt, buf, sizeof(buf));
printf("TextBlock: %s\n", buf);
}
/* -- Resumo do runtime --- */
printf("\n--- Estado do runtime ---\n");
printf("blocks vivos: %u\n", rt->block_count);
printf("types: %u\n", rt->type_count);
printf("libraries: %u\n", rt->library_count);
/* 4. Desinicializar (libera tudo) */
tinyblocks_deinit(&rt);
printf("\n[runtime] deinicializado. rt = %p\n", (void*)rt);
return 0;
}