Files
tinyblocks/examples/python/python.c
T
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

112 lines
3.4 KiB
C

/*
* python.c - Plugin TinyBlocks com blocos Python.*.
*
* Blocos:
* Python.Eval - avalia expressão Python com variáveis x0..x3
* Python.Run - executa script Python com variáveis x0..x3, captura stdout
*
* Compilar:
* make python-lib
*
* Carregar no editor:
* ./build/editor build/python.so ou Ctrl+O -> build/python.so
*
* Exemplos de expressões:
* x0 ** 2 + x1 ** 2
* __import__('math').sqrt(x0)
* [x*x for x in range(int(x0))]
*/
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <string.h>
#include <tinyblocks/tinyblocks.h>
#include <tinyblocks-python/tb_python.h>
/* -- Helper: lê float de slot numérico --- */
static double gd(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t idx) {
TinyBlocks_Value v = tb_block_get_input(self, rt, idx);
if (v.kind == TB_VALUE_REAL) return v.as.real;
if (v.kind == TB_VALUE_INTEGER) return (double)v.as.integer;
return 0.0;
}
static const char* gs(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t idx) {
TinyBlocks_Value v = tb_block_get_input(self, rt, idx);
return (v.kind == TB_VALUE_TEXT && v.as.text) ? v.as.text : "";
}
/* -- Python.Eval --- */
/*
* Inputs: expr (text), x0..x3 (real)
* Output: result (text)
*/
static void eval_python_eval(TinyBlocks_Block* self, TinyBlocks* rt) {
const char* expr = gs(self, rt, 0);
double vals[4] = { gd(self,rt,1), gd(self,rt,2), gd(self,rt,3), gd(self,rt,4) };
static const char* names[] = {"x0","x1","x2","x3"};
char* result = tb_python_eval(expr, names, vals, 4);
tb_block_set_output(self, 0, tb_value_text(result ? result : ""));
free(result);
self->dirty = false;
}
/* -- Python.Run --- */
/*
* Inputs: code (text), x0..x3 (real)
* Output: stdout (text)
*/
static void eval_python_run(TinyBlocks_Block* self, TinyBlocks* rt) {
const char* code = gs(self, rt, 0);
double vals[4] = { gd(self,rt,1), gd(self,rt,2), gd(self,rt,3), gd(self,rt,4) };
static const char* names[] = {"x0","x1","x2","x3"};
char* out = tb_python_run_with_vars(code, names, vals, 4);
tb_block_set_output(self, 0, tb_value_text(out ? out : ""));
free(out);
self->dirty = false;
}
/* -- Slots --- */
static const TinyBlocks_SlotDesc eval_in[] = {
{"expr", TB_VALUE_TEXT},
{"x0", TB_VALUE_REAL}, {"x1", TB_VALUE_REAL},
{"x2", TB_VALUE_REAL}, {"x3", TB_VALUE_REAL},
};
static const TinyBlocks_SlotDesc run_in[] = {
{"code", TB_VALUE_TEXT},
{"x0", TB_VALUE_REAL}, {"x1", TB_VALUE_REAL},
{"x2", TB_VALUE_REAL}, {"x3", TB_VALUE_REAL},
};
static const TinyBlocks_SlotDesc text_out[] = {{"result", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc stdout_out[]= {{"stdout", TB_VALUE_TEXT}};
/* -- Registro --- */
static bool py_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
if (!tb_python_init()) return false;
TinyBlocks_TypeBlock* t;
t = tb_type_block_create(rt, "Python.Eval",
eval_in, 5, text_out, 1, eval_python_eval);
if (t) tb_library_add(lib, &t->base);
t = tb_type_block_create(rt, "Python.Run",
run_in, 5, stdout_out, 1, eval_python_run);
if (t) tb_library_add(lib, &t->base);
return true;
}
static void py_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
(void)rt; (void)lib;
tb_python_deinit();
}
TINYBLOCKS_DEFINE_LIBRARY(python,
"tinyblocks-python",
"1.0.0",
py_init,
py_deinit)