scratchblocks: Meus Blocos (define, parametros e chamadas)
Importar: uma pre-passada acha os "define nome (a) rotulo <b>" e cria um modelo User.<nome> para as chamadas casarem mesmo antes da definicao; o chamador e avisado (TbSbContext.define). No corpo, (x) com o nome de uma entrada vira Port.In. "fim" e "senao" valem como end e else. Exportar: o defina sai como "define <prototipo>" (TbSbContext.proto), o parametro como (x :: custom-arg) ou <x :: custom-arg> e as chamadas pelo rotulo do prototipo, com entradas ( ). No editor, a definicao importada registra o tipo User.<nome> com as saidas dos "devolva" do corpo. Agentes (MCP) e o forum agora criam e usam blocos proprios. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
756540eb76
commit
25bb30d12f
4 files changed
+248
-12
No files matched your search
@@ -3233,6 +3233,7 @@ static int sb_slot_kind(const char* type, int slot, void* ud) {
|
||||
TinyBlocks_TypeBlock* t = tb_type_block_find(ed->rt, type);
|
||||
if (!t || slot < 0 || slot >= input_count(t)) return TB_SB_SLOT_TEXT;
|
||||
if (slot_is_bool(t, slot)) return TB_SB_SLOT_BOOL;
|
||||
if (!strncmp(type, "User.", 5)) return TB_SB_SLOT_NUM; /* entrada de Meus Blocos: ( ) */
|
||||
TinyBlocks_ValueKind k = t->input_descs[slot].expected_kind;
|
||||
return (k == TB_VALUE_INTEGER || k == TB_VALUE_REAL) ? TB_SB_SLOT_NUM : TB_SB_SLOT_TEXT;
|
||||
}
|
||||
@@ -3253,11 +3254,59 @@ static const char* sb_category(const char* type, void* ud) {
|
||||
return "extension";
|
||||
}
|
||||
|
||||
/* == Meus Blocos no scratchblocks ===
|
||||
* Rotulo de chamada ("dobro %1") e prototipo ("dobro (n)") de cada bloco do
|
||||
* usuario, e os que a importacao definiu (para registrar os tipos depois). */
|
||||
static char s_sb_call[MAX_USER_BLOCKS][256];
|
||||
static bool s_sb_touched[MAX_USER_BLOCKS];
|
||||
|
||||
static void user_label(const UserBlockDef* def, char* out, size_t cap, bool proto) {
|
||||
size_t n = (size_t)snprintf(out, cap, "%s", def->name);
|
||||
int k = 0;
|
||||
for (int i = 0; i < def->part_n && n < cap; i++) {
|
||||
const UserParam* p = &def->parts[i];
|
||||
if (p->kind == PARAM_LABEL) n += (size_t)snprintf(out + n, cap - n, " %s", p->name);
|
||||
else if (!proto) n += (size_t)snprintf(out + n, cap - n, " %%%d", ++k);
|
||||
else n += (size_t)snprintf(out + n, cap - n, p->kind == PARAM_BOOL ? " <%s>" : " (%s)",
|
||||
p->name[0] ? p->name : "entrada");
|
||||
}
|
||||
}
|
||||
|
||||
static const char* sb_proto(const char* name, void* ud) {
|
||||
static char buf[256];
|
||||
UserBlockDef* def = user_def_find((Ed*)ud, name);
|
||||
if (!def) return NULL;
|
||||
user_label(def, buf, sizeof(buf), true);
|
||||
return buf;
|
||||
}
|
||||
|
||||
/* "define ..." no texto: cria ou atualiza a definicao (o tipo vem depois) */
|
||||
static void sb_define(const char* name, const TbBpParam* parts, int n, bool reporter, void* ud) {
|
||||
(void)reporter; /* as saidas vem dos "devolva" do corpo */
|
||||
Ed* ed = (Ed*)ud;
|
||||
UserBlockDef* def = user_def_find(ed, name);
|
||||
if (!def) {
|
||||
if (ed->user_block_n >= MAX_USER_BLOCKS) return;
|
||||
def = &ed->user_blocks[ed->user_block_n++];
|
||||
memset(def, 0, sizeof(*def));
|
||||
snprintf(def->name, sizeof(def->name), "%s", name);
|
||||
def->tab_idx = -1;
|
||||
}
|
||||
def->part_n = 0;
|
||||
for (int i = 0; i < n && def->part_n < MAX_UBLK_PARTS; i++) {
|
||||
UserParam* p = &def->parts[def->part_n++];
|
||||
p->kind = (ParamKind)parts[i].kind;
|
||||
snprintf(p->name, sizeof(p->name), "%s", parts[i].name);
|
||||
}
|
||||
comp_build_input_names(def);
|
||||
s_sb_touched[def - ed->user_blocks] = true;
|
||||
}
|
||||
|
||||
/* Contexto: rotulos em portugues do editor + callbacks */
|
||||
static TbSbContext sb_context(Ed* ed, TbSbLabel** labels_out) {
|
||||
size_t n = sizeof(s_specs) / sizeof(s_specs[0]);
|
||||
uint32_t nt = tb_rt_type_count(ed->rt);
|
||||
TbSbLabel* L = calloc(n + nt, sizeof(TbSbLabel));
|
||||
TbSbLabel* L = calloc(n + nt + MAX_USER_BLOCKS, sizeof(TbSbLabel));
|
||||
int k = 0;
|
||||
for (size_t i = 0; L && i < n; i++) {
|
||||
L[k].type = s_specs[i].type;
|
||||
@@ -3271,8 +3320,16 @@ static TbSbContext sb_context(Ed* ed, TbSbLabel** labels_out) {
|
||||
L[k].label = t->label;
|
||||
k++;
|
||||
}
|
||||
for (int u = 0; L && u < ed->user_block_n; u++) { /* chamadas de Meus Blocos */
|
||||
UserBlockDef* def = &ed->user_blocks[u];
|
||||
if (!def->reg_type) continue;
|
||||
user_label(def, s_sb_call[u], sizeof(s_sb_call[u]), false);
|
||||
L[k].type = def->reg_type->type_name;
|
||||
L[k].label = s_sb_call[u];
|
||||
k++;
|
||||
}
|
||||
*labels_out = L;
|
||||
TbSbContext cx = { L, L ? k : 0, sb_shape, sb_slot_kind, sb_category, ed };
|
||||
TbSbContext cx = { L, L ? k : 0, sb_shape, sb_slot_kind, sb_category, ed, sb_define, sb_proto };
|
||||
return cx;
|
||||
}
|
||||
|
||||
@@ -3306,6 +3363,7 @@ int ed_scratchblocks_import(Ed* ed, Tab* T, const char* text, float x, float y,
|
||||
if (!B) return -1;
|
||||
TbSbLabel* labels = NULL;
|
||||
TbSbContext cx = sb_context(ed, &labels);
|
||||
memset(s_sb_touched, 0, sizeof(s_sb_touched));
|
||||
int n = tb_scratchblocks_import(text, B, &cx, warn, warn_cap);
|
||||
free(labels);
|
||||
if (n <= 0) { bp_tab_dispose(B); return -1; }
|
||||
@@ -3351,6 +3409,16 @@ int ed_scratchblocks_import(Ed* ed, Tab* T, const char* text, float x, float y,
|
||||
}
|
||||
free(map);
|
||||
bp_tab_dispose(B);
|
||||
/* "define" do texto: registra (ou refaz) o tipo User.<nome> com as
|
||||
entradas do prototipo e as saidas dos "devolva" do corpo */
|
||||
for (int u = 0; u < ed->user_block_n; u++) {
|
||||
if (!s_sb_touched[u]) continue;
|
||||
UserBlockDef* def = &ed->user_blocks[u];
|
||||
int t, hat = define_find(ed, def->name, &t);
|
||||
def->output_count = 0;
|
||||
if (hat >= 0) define_outputs(&ed->tabs[t], ed->tabs[t].ws[hat].next, def->output_names, &def->output_count);
|
||||
comp_make_type(ed, def);
|
||||
}
|
||||
layout_tab(T); /* empilha uma embaixo da outra */
|
||||
for (int k = 1; k < made && k < max && heads; k++) {
|
||||
float x0, y0, x1, y1;
|
||||
|
||||
Reference in new issue
Block a user