Encaixes de bloco: o bloco encaixado vai como codigo (JSON)

Uma entrada TB_SLOT_CODE recebe o CODIGO do bloco encaixado, sem
executa-lo; TB_SLOT_STACK recebe uma pilha de comandos. No editor o
encaixe e uma cavidade mais escura com a forma do bloco que cabe nela
(pilula, hexagono ou comando, como a sombra do prototipo no "defina"); o
de pilha fica numa faixa abaixo do rotulo e cresce com a pilha, e os
comandos encaixam no topo dele ao arrastar.

Formato (tb_defs.h): {"type", "inputs": [...], "mouths": [...]}; uma
pilha e uma lista de blocos; campos digitados viram texto ou numero.

Controle ganha "codigo de (...)" e "codigo da pilha (...)". O MCP poe
comandos num encaixe de pilha (into + input). Na notacao do forum o
encaixe de pilha de um comando vem nas linhas de baixo, indentado e
fechado com "end", como um bloco C.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mguinhosandClaude Opus 5.5 committed 2026-10-27 02:47:26 -03:00
1 parent 5bae31edf3
commit a3bae660fb
8 files changed
+311 -18

No files matched your search

@@ -40,7 +40,9 @@ enum {
};
/* Tipo de um campo de entrada (escolhe ( ), [ ] ou < >) */
enum { TB_SB_SLOT_TEXT = 0, TB_SB_SLOT_NUM, TB_SB_SLOT_BOOL };
enum { TB_SB_SLOT_TEXT = 0, TB_SB_SLOT_NUM, TB_SB_SLOT_BOOL,
TB_SB_SLOT_STACK }; /* encaixe de pilha: a pilha vem nas linhas de baixo,
indentada e fechada com "end" (como um bloco C) */
/* Rotulo extra: "%1".."%9" sao os campos (ex.: "mude %1 para %2") */
typedef struct {
@@ -237,6 +237,7 @@ typedef struct {
Tok tok[TOK_MAX];
int slot[TOK_MAX]; /* campo (0-based) do token; -1 = fixo/palavra */
bool listslot[TOK_MAX]; /* %LN */
int stack_slot; /* encaixe de pilha (fora da linha); -1 = nenhum */
char cat[24]; /* "list" de "... :: list" (modelos internos) */
} Model;
@@ -280,6 +281,7 @@ static void loose_punct(char* buf, size_t cap) {
static bool model_build(Model* m, const char* type, const char* text, char shape, bool loose) {
memset(m, 0, sizeof(*m));
m->stack_slot = -1;
m->type = type;
m->shape = shape;
char buf[512];
@@ -308,6 +310,27 @@ typedef struct {
const TbSbContext* cx;
} Models;
/* O encaixe de pilha nao fica na linha do bloco: tira o "%k" do modelo e
lembra qual e (a pilha vem nas linhas de baixo, ate o "end") */
static void model_take_stack(Model* m, const TbSbContext* cx) {
if (!cx || !cx->slot_kind) return;
for (int i = 0; i < m->n; i++) {
if (m->slot[i] < 0 || cx->slot_kind(m->type, m->slot[i], cx->ud) != TB_SB_SLOT_STACK) continue;
if (m->stack_slot < 0) m->stack_slot = m->slot[i];
memmove(&m->tok[i], &m->tok[i + 1], (size_t)(m->n - i - 1) * sizeof(m->tok[0]));
memmove(&m->slot[i], &m->slot[i + 1], (size_t)(m->n - i - 1) * sizeof(m->slot[0]));
memmove(&m->listslot[i], &m->listslot[i + 1], (size_t)(m->n - i - 1) * sizeof(m->listslot[0]));
m->n--; i--;
}
}
/* O modelo do tipo que tem encaixe de pilha (NULL se o tipo nao tem) */
static const Model* model_of(const Models* M, const char* type) {
for (int i = 0; type && i < M->n; i++)
if (M->m[i].stack_slot >= 0 && !strcmp(M->m[i].type, type)) return &M->m[i];
return NULL;
}
/* "define nome (a) rotulo <b>": um bloco do usuario achado no texto */
#define PROTO_MAX 64
typedef struct {
@@ -385,6 +408,7 @@ static Models models_load_with(const TbSbContext* cx, const Proto* protos, int n
int sh = cx_shape(cx, L->type);
Model* m = &M.m[M.n];
if (!model_build(m, L->type, L->label, (char)shape_char(sh < 0 ? TB_SB_STACK : sh), true)) continue;
model_take_stack(m, cx);
/* "%1" sozinho (ex.: valor de variavel) casaria com qualquer coisa */
if (m->n == 1 && m->slot[0] >= 0) continue;
M.n++;
@@ -457,6 +481,7 @@ static bool model_fits(const Model* m, const TbBpTab* tab, int bi) {
if (m->import_only) return false;
const TbBpBlock* b = &tab->blocks[bi];
bool covered[TB_BP_MAX_INPUTS] = {0};
if (m->stack_slot >= 0 && m->stack_slot < TB_BP_MAX_INPUTS) covered[m->stack_slot] = true;
for (int i = 0; i < m->n; i++) {
int s = m->slot[i];
if (s < 0 || s >= TB_BP_MAX_INPUTS) continue;
@@ -584,6 +609,11 @@ static void render_chain(const Models* M, const TbBpTab* tab, int bi, int depth,
indent(out, depth);
render_block(M, tab, c, out, true);
sb_str(out, "\n");
const Model* md = model_of(M, b->type);
if (md && md->stack_slot >= 0 && md->stack_slot < TB_BP_MAX_INPUTS) {
render_chain(M, tab, b->args[md->stack_slot], depth + 1, out);
indent(out, depth); sb_str(out, "end\n");
}
if (sh == TB_SB_C1 || sh == TB_SB_C2 || sh == TB_SB_CAP) {
render_chain(M, tab, b->sub[0], depth + 1, out);
if (sh == TB_SB_C2) {
@@ -886,10 +916,15 @@ int tb_scratchblocks_import(const char* text, TbBpTab* out, const TbSbContext* c
} else {
Level* L = &lv[depth];
if (L->last >= 0) tb_blueprint_link(out, L->last, TB_BP_REL_NEXT, bi);
else if (depth > 0 && L->sub < 0) tb_blueprint_link(out, L->cblock, TB_BP_REL_ARG0 + (-1 - L->sub), bi);
else if (depth > 0) tb_blueprint_link(out, L->cblock, TB_BP_REL_SUB0 + L->sub, bi);
L->last = bi;
}
if (!outer && (sh == TB_SB_C1 || sh == TB_SB_C2 || sh == TB_SB_CAP) && depth + 1 < LEVEL_MAX) {
const Model* md = outer ? NULL : model_of(&M, out->blocks[bi].type);
if (md && md->stack_slot >= 0 && depth + 1 < LEVEL_MAX) { /* encaixe de pilha: sub < 0 */
depth++;
lv[depth].cblock = bi; lv[depth].sub = -1 - md->stack_slot; lv[depth].last = -1;
} else if (!outer && (sh == TB_SB_C1 || sh == TB_SB_C2 || sh == TB_SB_CAP) && depth + 1 < LEVEL_MAX) {
depth++;
lv[depth].cblock = bi; lv[depth].sub = 0; lv[depth].last = -1;
}