/* * mylib.c - Library de exemplo para testar hot reload no TinyBlocks Editor. * * Blocos incluídos: * Extra.Square - n^2 de um inteiro * Extra.Cube - n^3 de um inteiro * Extra.Clamp - limita valor entre min e max * Extra.Fibonacci - n-ésimo número de Fibonacci * Extra.IsEven - 1 se par, 0 se ímpar * Extra.IntToText - converte inteiro em texto * Extra.Pow - base elevado a exp (inteiros) * * Para compilar: * make mylib * * Para carregar no editor: * Ctrl+O -> build/mylib.so ou arraste build/mylib.so para a janela */ #define _POSIX_C_SOURCE 200809L #include #include #include #include /* -- Slots reutilizáveis --- */ static const TinyBlocks_SlotDesc in_n[] = {{"n", TB_VALUE_INTEGER}}; static const TinyBlocks_SlotDesc in_ab[] = {{"a", TB_VALUE_INTEGER}, {"b", TB_VALUE_INTEGER}}; static const TinyBlocks_SlotDesc in_clamp[]= {{"val", TB_VALUE_INTEGER}, {"min", TB_VALUE_INTEGER}, {"max", TB_VALUE_INTEGER}}; static const TinyBlocks_SlotDesc out_int[] = {{"out", TB_VALUE_INTEGER}}; static const TinyBlocks_SlotDesc out_txt[] = {{"out", TB_VALUE_TEXT}}; /* -- Implementações --- */ static void eval_square(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_Value v = tb_block_get_input(self, rt, 0); int64_t n = v.kind==TB_VALUE_INTEGER ? v.as.integer : 0; tb_block_set_output(self, 0, tb_value_integer(n * n)); } static void eval_cube(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_Value v = tb_block_get_input(self, rt, 0); int64_t n = v.kind==TB_VALUE_INTEGER ? v.as.integer : 0; tb_block_set_output(self, 0, tb_value_integer(n * n * n)); } static void eval_pow(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); int64_t base = a.kind==TB_VALUE_INTEGER ? a.as.integer : 0; int64_t exp = b.kind==TB_VALUE_INTEGER ? b.as.integer : 0; if (exp < 0) { tb_block_set_output(self, 0, tb_value_integer(0)); return; } int64_t result = 1; for (int64_t i = 0; i < exp; i++) result *= base; tb_block_set_output(self, 0, tb_value_integer(result)); } static void eval_clamp(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_Value val = tb_block_get_input(self, rt, 0); TinyBlocks_Value mn = tb_block_get_input(self, rt, 1); TinyBlocks_Value mx = tb_block_get_input(self, rt, 2); int64_t v = val.kind==TB_VALUE_INTEGER ? val.as.integer : 0; int64_t lo = mn.kind ==TB_VALUE_INTEGER ? mn.as.integer : 0; int64_t hi = mx.kind ==TB_VALUE_INTEGER ? mx.as.integer : 100; if (v < lo) v = lo; if (v > hi) v = hi; tb_block_set_output(self, 0, tb_value_integer(v)); } static void eval_fibonacci(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_Value v = tb_block_get_input(self, rt, 0); int64_t n = v.kind==TB_VALUE_INTEGER ? v.as.integer : 0; if (n <= 0) { tb_block_set_output(self, 0, tb_value_integer(0)); return; } if (n == 1) { tb_block_set_output(self, 0, tb_value_integer(1)); return; } int64_t a=0, b=1; for (int64_t i=2; i<=n && i<94; i++) { int64_t c=a+b; a=b; b=c; } tb_block_set_output(self, 0, tb_value_integer(b)); } static void eval_iseven(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_Value v = tb_block_get_input(self, rt, 0); int64_t n = v.kind==TB_VALUE_INTEGER ? v.as.integer : 0; tb_block_set_output(self, 0, tb_value_integer((n % 2 == 0) ? 1 : 0)); } static void eval_int_to_text(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_Value v = tb_block_get_input(self, rt, 0); int64_t n = v.kind==TB_VALUE_INTEGER ? v.as.integer : 0; char buf[32]; snprintf(buf, sizeof(buf), "%lld", (long long)n); tb_block_set_output(self, 0, tb_value_text(buf)); } /* -- Registro da library --- */ static bool mylib_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) { TinyBlocks_TypeBlock* t; #define REG(name, ins, ni, outs, no, fn) \ t = tb_type_block_create(rt, name, ins, ni, outs, no, fn); \ if (t) tb_library_add(lib, &t->base) REG("Extra.Square", in_n, 1, out_int, 1, eval_square); REG("Extra.Cube", in_n, 1, out_int, 1, eval_cube); REG("Extra.Pow", in_ab, 2, out_int, 1, eval_pow); REG("Extra.Clamp", in_clamp, 3, out_int, 1, eval_clamp); REG("Extra.Fibonacci", in_n, 1, out_int, 1, eval_fibonacci); REG("Extra.IsEven", in_n, 1, out_int, 1, eval_iseven); REG("Extra.IntToText", in_n, 1, out_txt, 1, eval_int_to_text); #undef REG return true; } static void mylib_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) { (void)rt; (void)lib; } TINYBLOCKS_DEFINE_LIBRARY(mylib, "mylib", "1.0.0", mylib_init, mylib_deinit)