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:
mguinhosandClaude Opus 5.5 committed 2026-10-26 23:38:20 -03:00
1 parent 756540eb76
commit 25bb30d12f
4 files changed
+248 -12

No files matched your search

+45 -1
View File
@@ -34,7 +34,7 @@ static const char* category(const char* type, void* ud) {
return !strncmp(type, "Stage.", 6) ? "pen" : "extension";
}
static const TbSbContext CX = { s_labels, (int)(sizeof(s_labels) / sizeof(s_labels[0])), shape, slot_kind, category, NULL };
static const TbSbContext CX = { s_labels, (int)(sizeof(s_labels) / sizeof(s_labels[0])), shape, slot_kind, category, NULL, NULL, NULL };
static TbBpTab* fresh(void) { return calloc(1, sizeof(TbBpTab)); }
@@ -73,6 +73,15 @@ static const char* EXPECTED =
"add [maçã] to [frutas v]\n"
"say (item (1) of [frutas v])\n";
static int s_def_n, s_def_parts;
static bool s_def_rep;
static char s_def_name[48];
static void on_define(const char* name, const TbBpParam* parts, int n, bool reporter, void* ud) {
(void)parts; (void)ud;
s_def_n++; s_def_parts = n; s_def_rep = reporter;
snprintf(s_def_name, sizeof(s_def_name), "%s", name);
}
static int child(const TbBpTab* t, int bi, int s) { return bi >= 0 ? t->blocks[bi].args[s] : -1; }
static const char* ty(const TbBpTab* t, int bi) { return bi >= 0 ? t->blocks[bi].type : "(nada)"; }
@@ -198,6 +207,41 @@ int main(void) {
}
drop(t);
/* -- Meus Blocos: define, parametros e chamadas -------------------------- */
{
TbSbContext cx = CX;
cx.define = on_define;
t = fresh();
warn[0] = 0;
int n = tb_scratchblocks_import(
"when flag clicked\n"
"dobro (21) e <(1) = (1)> :: custom\n"
"\n"
"define dobro (n) e <ok>\n"
"if <ok> then\n"
" say ((n) * (2))\n"
"fim\n", t, &cx, warn, sizeof(warn));
if (warn[0]) printf(" aviso: %s\n", warn);
CHECK(n == 2 && !warn[0], "define + chamada: 2 pilhas, sem aviso (fim em português)");
CHECK(s_def_n == 1 && !strcmp(s_def_name, "dobro") && s_def_parts == 3 && !s_def_rep,
"define avisa o chamador: nome e 3 partes (entrada, rótulo, booleana)");
int call = t->top_n ? t->blocks[t->top[0]].next : -1;
CHECK(!strcmp(ty(t, call), "User.dobro") && !strcmp(t->blocks[call].in_text[0], "21"),
"a chamada antes do define já vira User.dobro (21)");
int hat = t->top_n > 1 ? t->top[1] : -1;
int iff = hat >= 0 ? t->blocks[hat].next : -1;
int ret = iff >= 0 ? t->blocks[iff].sub[0] : -1;
int mul = child(t, ret, 0);
CHECK(!strcmp(ty(t, hat), "Port.Define") && !strcmp(t->blocks[hat].in_text[0], "dobro") &&
!strcmp(ty(t, child(t, iff, 0)), "Port.In") && !strcmp(ty(t, child(t, mul, 0)), "Port.In"),
"no corpo, (n) e <ok> viram parâmetros (Port.In), não variáveis");
drop(t);
t = fresh();
tb_scratchblocks_import("say (n)\n", t, &cx, warn, sizeof(warn));
CHECK(!strcmp(ty(t, child(t, t->top_n ? t->top[0] : -1, 0)), "Variables.Get"), "fora do define, (n) é variável");
drop(t);
}
if (fails) printf("\n%d FALHA(S)\n", fails);
else printf("\nTUDO OK\n");
return fails != 0;