Estado atual do TinyBlocks: editor estilo Scratch + graphics como interface
Snapshot do projeto antes de implementar o diálogo "Criar um Bloco": - runtime C block-based (src/, include/) - libraries: graphics (interface), sdl2 (backend), editor (estilo Scratch), python, mcp - exemplos (hello, sdl2, editor, vector, json, customappearance, python, mcp) - Makefile com alvo help e build do editor Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
commit
4950ad6006
49 files changed
+10596
No files matched your search
@@ -0,0 +1,260 @@
|
||||
/*
|
||||
* json.c — Library de JSON para o TinyBlocks Editor.
|
||||
*
|
||||
* Blocos incluídos:
|
||||
* Json.Parse — texto -> json (null em caso de erro de sintaxe)
|
||||
* Json.Stringify — json -> texto
|
||||
* Json.Null — constrói json null
|
||||
* Json.Bool — inteiro (0/1) -> json bool
|
||||
* Json.Number — real -> json number
|
||||
* Json.String — texto -> json string
|
||||
* Json.Array — constrói array vazio
|
||||
* Json.Object — constrói objeto vazio
|
||||
* Json.Push — (array, valor) -> novo array com valor anexado
|
||||
* Json.Set — (objeto, chave, valor) -> novo objeto com campo definido
|
||||
* Json.Get — (objeto, chave) -> valor do campo, ou Json.Null
|
||||
* Json.Index — (array, índice) -> valor na posição, ou Json.Null
|
||||
* Json.Length — (json) -> nº de elementos (array/objeto) ou 0
|
||||
* Json.TypeOf — (json) -> "null"|"bool"|"number"|"string"|"array"|"object"
|
||||
* Json.ToNumber — (json, default) -> número (ou default se não for number/bool)
|
||||
* Json.ToText — (json) -> string contida, ou "" se não for string
|
||||
* Json.ToBool — (json, default) -> 0/1 (ou default se não for bool/number)
|
||||
*
|
||||
* Json.Push/Json.Set não mutam o valor recebido: clonam e devolvem uma nova
|
||||
* árvore, mantendo o fluxo de dados imutável como o resto do TinyBlocks.
|
||||
*
|
||||
* Para compilar:
|
||||
* make json
|
||||
*
|
||||
* Para carregar no editor:
|
||||
* Ctrl+O → build/json.so ou arraste build/json.so para a janela
|
||||
*/
|
||||
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
|
||||
/* ── Helpers de leitura de inputs ────────────────────────────────────────── */
|
||||
static const char* gt(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 : "";
|
||||
}
|
||||
|
||||
static double gn(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 int64_t gi(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t idx) {
|
||||
TinyBlocks_Value v = tb_block_get_input(self, rt, idx);
|
||||
if (v.kind == TB_VALUE_INTEGER) return v.as.integer;
|
||||
if (v.kind == TB_VALUE_REAL) return (int64_t)v.as.real;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static TinyBlocks_JsonValue* gj(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t idx) {
|
||||
TinyBlocks_Value v = tb_block_get_input(self, rt, idx);
|
||||
return tb_value_as_json(v);
|
||||
}
|
||||
|
||||
/* Macro: produz output json/texto/integer/real e libera self->dirty */
|
||||
#define EMIT_JSON(self, j) do { \
|
||||
tb_block_set_output((self), 0, tb_value_json(j)); \
|
||||
tb_json_release(j); \
|
||||
(self)->dirty = false; \
|
||||
} while (0)
|
||||
|
||||
/* ══════════════════════════════════════════════════════════════════════════
|
||||
* Implementações dos blocos
|
||||
* ══════════════════════════════════════════════════════════════════════════ */
|
||||
|
||||
static void eval_parse(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
char err[128] = {0};
|
||||
TinyBlocks_JsonValue* j = tb_json_parse(gt(self, rt, 0), err, sizeof(err));
|
||||
if (!j) j = tb_json_null();
|
||||
EMIT_JSON(self, j);
|
||||
}
|
||||
|
||||
static void eval_stringify(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_JsonValue* j = gj(self, rt, 0);
|
||||
char* s = tb_json_stringify(j);
|
||||
tb_block_set_output(self, 0, tb_value_text(s ? s : "null"));
|
||||
free(s);
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void eval_null(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
(void)rt;
|
||||
EMIT_JSON(self, tb_json_null());
|
||||
}
|
||||
|
||||
static void eval_bool(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
EMIT_JSON(self, tb_json_bool(gi(self, rt, 0) != 0));
|
||||
}
|
||||
|
||||
static void eval_number(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
EMIT_JSON(self, tb_json_number(gn(self, rt, 0)));
|
||||
}
|
||||
|
||||
static void eval_string(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
EMIT_JSON(self, tb_json_string(gt(self, rt, 0)));
|
||||
}
|
||||
|
||||
static void eval_array(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
(void)rt;
|
||||
EMIT_JSON(self, tb_json_array());
|
||||
}
|
||||
|
||||
static void eval_object(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
(void)rt;
|
||||
EMIT_JSON(self, tb_json_object());
|
||||
}
|
||||
|
||||
static void eval_push(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_JsonValue* arr = tb_json_clone(gj(self, rt, 0));
|
||||
if (!arr || tb_json_kind(arr) != TB_JSON_ARRAY) {
|
||||
tb_json_release(arr);
|
||||
arr = tb_json_array();
|
||||
}
|
||||
TinyBlocks_JsonValue* item = gj(self, rt, 1);
|
||||
tb_json_array_push(arr, item ? item : tb_json_null());
|
||||
EMIT_JSON(self, arr);
|
||||
}
|
||||
|
||||
static void eval_set(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_JsonValue* obj = tb_json_clone(gj(self, rt, 0));
|
||||
if (!obj || tb_json_kind(obj) != TB_JSON_OBJECT) {
|
||||
tb_json_release(obj);
|
||||
obj = tb_json_object();
|
||||
}
|
||||
TinyBlocks_JsonValue* val = gj(self, rt, 2);
|
||||
tb_json_object_set(obj, gt(self, rt, 1), val ? val : tb_json_null());
|
||||
EMIT_JSON(self, obj);
|
||||
}
|
||||
|
||||
static void eval_get(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_JsonValue* obj = gj(self, rt, 0);
|
||||
TinyBlocks_JsonValue* val = tb_json_object_get(obj, gt(self, rt, 1));
|
||||
if (!val) val = tb_json_null();
|
||||
EMIT_JSON(self, val);
|
||||
}
|
||||
|
||||
static void eval_index(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_JsonValue* arr = gj(self, rt, 0);
|
||||
int64_t idx = gi(self, rt, 1);
|
||||
TinyBlocks_JsonValue* val = (idx >= 0) ? tb_json_array_get(arr, (uint32_t)idx) : NULL;
|
||||
if (!val) val = tb_json_null();
|
||||
EMIT_JSON(self, val);
|
||||
}
|
||||
|
||||
static void eval_length(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_JsonValue* j = gj(self, rt, 0);
|
||||
uint32_t n = 0;
|
||||
switch (tb_json_kind(j)) {
|
||||
case TB_JSON_ARRAY: n = tb_json_array_length(j); break;
|
||||
case TB_JSON_OBJECT: n = tb_json_object_length(j); break;
|
||||
case TB_JSON_STRING: n = (uint32_t)strlen(tb_json_as_string(j)); break;
|
||||
default: break;
|
||||
}
|
||||
tb_block_set_output(self, 0, tb_value_integer(n));
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void eval_typeof(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_JsonValue* j = gj(self, rt, 0);
|
||||
tb_block_set_output(self, 0, tb_value_text(tb_json_kind_name(j)));
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void eval_to_number(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_JsonValue* j = gj(self, rt, 0);
|
||||
double def = gn(self, rt, 1);
|
||||
tb_block_set_output(self, 0, tb_value_real(tb_json_as_number(j, def)));
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void eval_to_text(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_JsonValue* j = gj(self, rt, 0);
|
||||
const char* s = tb_json_as_string(j);
|
||||
tb_block_set_output(self, 0, tb_value_text(s ? s : ""));
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void eval_to_bool(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
TinyBlocks_JsonValue* j = gj(self, rt, 0);
|
||||
bool def = gi(self, rt, 1) != 0;
|
||||
tb_block_set_output(self, 0, tb_value_integer(tb_json_as_bool(j, def) ? 1 : 0));
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
/* ══════════════════════════════════════════════════════════════════════════
|
||||
* Descritores de slots
|
||||
* ══════════════════════════════════════════════════════════════════════════ */
|
||||
static const TinyBlocks_SlotDesc out_json[] = {{"json", TB_VALUE_JSON}};
|
||||
static const TinyBlocks_SlotDesc out_text[] = {{"text", TB_VALUE_TEXT}};
|
||||
static const TinyBlocks_SlotDesc out_integer[] = {{"n", TB_VALUE_INTEGER}};
|
||||
static const TinyBlocks_SlotDesc out_real[] = {{"n", TB_VALUE_REAL}};
|
||||
|
||||
static const TinyBlocks_SlotDesc in_text[] = {{"text", TB_VALUE_TEXT}};
|
||||
static const TinyBlocks_SlotDesc in_json[] = {{"json", TB_VALUE_JSON}};
|
||||
static const TinyBlocks_SlotDesc in_integer[] = {{"n", TB_VALUE_INTEGER}};
|
||||
static const TinyBlocks_SlotDesc in_real[] = {{"n", TB_VALUE_REAL}};
|
||||
static const TinyBlocks_SlotDesc in_json_json[] = {{"a", TB_VALUE_JSON},
|
||||
{"b", TB_VALUE_JSON}};
|
||||
static const TinyBlocks_SlotDesc in_json_text_json[] = {{"obj", TB_VALUE_JSON},
|
||||
{"key", TB_VALUE_TEXT},
|
||||
{"val", TB_VALUE_JSON}};
|
||||
static const TinyBlocks_SlotDesc in_json_text[] = {{"obj", TB_VALUE_JSON},
|
||||
{"key", TB_VALUE_TEXT}};
|
||||
static const TinyBlocks_SlotDesc in_json_int[] = {{"arr", TB_VALUE_JSON},
|
||||
{"idx", TB_VALUE_INTEGER}};
|
||||
static const TinyBlocks_SlotDesc in_json_real[] = {{"json", TB_VALUE_JSON},
|
||||
{"def", TB_VALUE_REAL}};
|
||||
static const TinyBlocks_SlotDesc in_json_int_def[] = {{"json", TB_VALUE_JSON},
|
||||
{"def", TB_VALUE_INTEGER}};
|
||||
|
||||
/* ══════════════════════════════════════════════════════════════════════════
|
||||
* Registro da library
|
||||
* ══════════════════════════════════════════════════════════════════════════ */
|
||||
static bool json_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("Json.Parse", in_text, 1, out_json, 1, eval_parse);
|
||||
REG("Json.Stringify", in_json, 1, out_text, 1, eval_stringify);
|
||||
REG("Json.Null", NULL, 0, out_json, 1, eval_null);
|
||||
REG("Json.Bool", in_integer, 1, out_json, 1, eval_bool);
|
||||
REG("Json.Number", in_real, 1, out_json, 1, eval_number);
|
||||
REG("Json.String", in_text, 1, out_json, 1, eval_string);
|
||||
REG("Json.Array", NULL, 0, out_json, 1, eval_array);
|
||||
REG("Json.Object", NULL, 0, out_json, 1, eval_object);
|
||||
REG("Json.Push", in_json_json, 2, out_json, 1, eval_push);
|
||||
REG("Json.Set", in_json_text_json, 3, out_json, 1, eval_set);
|
||||
REG("Json.Get", in_json_text, 2, out_json, 1, eval_get);
|
||||
REG("Json.Index", in_json_int, 2, out_json, 1, eval_index);
|
||||
REG("Json.Length", in_json, 1, out_integer, 1, eval_length);
|
||||
REG("Json.TypeOf", in_json, 1, out_text, 1, eval_typeof);
|
||||
REG("Json.ToNumber", in_json_real, 2, out_real, 1, eval_to_number);
|
||||
REG("Json.ToText", in_json, 1, out_text, 1, eval_to_text);
|
||||
REG("Json.ToBool", in_json_int_def, 2, out_integer, 1, eval_to_bool);
|
||||
|
||||
#undef REG
|
||||
return true;
|
||||
}
|
||||
|
||||
static void json_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
(void)rt; (void)lib;
|
||||
}
|
||||
|
||||
TINYBLOCKS_DEFINE_LIBRARY(json,
|
||||
"tinyblocks-json",
|
||||
"1.0.0",
|
||||
json_init,
|
||||
json_deinit)
|
||||
Reference in new issue
Block a user