Notação do fórum do Scratch (scratchblocks): exportar e importar scripts
- Nova library tinyblocks-scratchblocks: converte uma aba (TbBpTab da
blueprint) para o texto do fórum e de volta. Blocos com equivalente no
Scratch saem em inglês ("when flag clicked", "repeat (10)", "set [x v]
to (0)", "if <> then ... else ... end", listas, operadores, sensores);
os do TinyBlocks usam o rótulo em português com "::categoria". A
importação aceita as duas formas, comentários "//", "::categoria",
escapes e as tags [scratchblocks]. Forma, tipo dos campos e categoria
vêm por callbacks (a library não conhece o editor).
- Editor: "Exportar scratchblocks" no menu Exportar (aba atual -> .txt);
"Abrir" aceita .txt/.sb e cria uma aba com os scripts.
- test-scratchblocks + cenário de ida e volta no harness.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
649ae14fb3
commit
d842890b0b
11 files changed
+1255
-38
No files matched your search
+40
-3
@@ -460,7 +460,7 @@ int main(void) {
|
||||
mkdir(fdir, 0755);
|
||||
snprintf(fp, sizeof(fp), "%s/sub", fdir);
|
||||
mkdir(fp, 0755);
|
||||
snprintf(fp, sizeof(fp), "%s/nota.txt", fdir);
|
||||
snprintf(fp, sizeof(fp), "%s/nota.md", fdir);
|
||||
FILE* tf = fopen(fp, "wb");
|
||||
if (tf) { fputs("oi", tf); fclose(tf); }
|
||||
snprintf(fp, sizeof(fp), "%s/proj.json", fdir);
|
||||
@@ -477,7 +477,7 @@ int main(void) {
|
||||
type(fdir);
|
||||
key(TB_KEY_RETURN, 0, 0);
|
||||
CHECK(find("proj.json", R_ANY, 0, &x, &y) && find("sub", R_ANY, 0, &x, &y)
|
||||
&& !find("nota.txt", R_ANY, 0, &x, &y),
|
||||
&& !find("nota.md", R_ANY, 0, &x, &y),
|
||||
"explorador lista pastas + .json e filtra o resto");
|
||||
if (need("proj.json", R_ANY, 0, &x, &y)) { click(x, y); click(x, y); }
|
||||
frame(0, 0, 0, 0, 0, 0, NULL);
|
||||
@@ -501,7 +501,7 @@ int main(void) {
|
||||
CHECK(!find("Abrir projeto", R_ANY, 0, &x, &y), "Esc fecha o explorador");
|
||||
|
||||
remove(fp);
|
||||
snprintf(fp, sizeof(fp), "%s/nota.txt", fdir); remove(fp);
|
||||
snprintf(fp, sizeof(fp), "%s/nota.md", fdir); remove(fp);
|
||||
snprintf(fp, sizeof(fp), "%s/sub", fdir); rmdir(fp);
|
||||
rmdir(fdir);
|
||||
}
|
||||
@@ -950,6 +950,43 @@ int main(void) {
|
||||
}
|
||||
}
|
||||
|
||||
/* 24. Notação do fórum (scratchblocks): Abrir .txt cria aba; Exportar gera o texto */
|
||||
{
|
||||
const char* src = "/tmp/tb_harness_sb.txt";
|
||||
const char* dst = "/tmp/tb_harness_sb_out.txt";
|
||||
const char* script =
|
||||
"when flag clicked\n"
|
||||
"set [x v] to (0)\n"
|
||||
"repeat (3)\n"
|
||||
" change [x v] by (1)\n"
|
||||
"end\n"
|
||||
"say (x)\n";
|
||||
FILE* f = fopen(src, "wb");
|
||||
if (f) { fputs(script, f); fclose(f); }
|
||||
remove(dst);
|
||||
frame(0, 0, 0, 0, 0, 0, NULL);
|
||||
if (need("Abrir", R_FLY, 0, &x, &y)) click(x, y);
|
||||
for (int i = 0; i < 40; i++) key(TB_KEY_BACKSPACE, 0, 0);
|
||||
type(src);
|
||||
key(TB_KEY_RETURN, 0, 0);
|
||||
frame(0, 0, 0, 0, 0, 0, NULL);
|
||||
CHECK(any_has("Importado: 1 script"), "Abrir um .txt em scratchblocks cria aba com o script");
|
||||
CHECK(any_has("de tb_harness_sb.txt"), "aba nova recebe o nome do arquivo");
|
||||
CHECK(find("repita", R_WS, 0, &x, &y) || ws_has("repita"), "blocos importados aparecem (repita)");
|
||||
if (need("Exportar", R_FLY, 0, &x, &y)) click(x, y);
|
||||
if (need("Exportar scratchblocks", R_ANY, 0, &x, &y)) click(x, y);
|
||||
for (int i = 0; i < 40; i++) key(TB_KEY_BACKSPACE, 0, 0);
|
||||
type(dst);
|
||||
key(TB_KEY_RETURN, 0, 0);
|
||||
frame(0, 0, 0, 0, 0, 0, NULL);
|
||||
char got[512] = "";
|
||||
f = fopen(dst, "rb");
|
||||
if (f) { size_t n = fread(got, 1, sizeof(got) - 1, f); got[n] = 0; fclose(f); }
|
||||
CHECK(!strcmp(got, script), "Exportar scratchblocks gera o mesmo texto");
|
||||
if (strcmp(got, script)) printf("---- exportado:\n%s----\n", got);
|
||||
remove(src); remove(dst);
|
||||
}
|
||||
|
||||
printf("comandos de desenho no último frame: %u\n", ncmd);
|
||||
tinyblocks_deinit(&rt);
|
||||
printf(fails ? "\n%d FALHA(S)\n" : "\nTUDO OK\n", fails);
|
||||
|
||||
@@ -0,0 +1,162 @@
|
||||
/*
|
||||
* scratchblocks_test.c - testa tinyblocks-scratchblocks: notacao de texto do
|
||||
* forum do Scratch <-> aba de blocos (TbBpTab).
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <blueprint.h>
|
||||
#include <tb_scratchblocks.h>
|
||||
|
||||
static int fails = 0;
|
||||
#define CHECK(c, m) do { if (c) printf("ok %s\n", m); else { printf("FAIL %s\n", m); fails++; } } while (0)
|
||||
|
||||
/* rotulos "do editor" para blocos que so o TinyBlocks tem */
|
||||
static const TbSbLabel s_labels[] = {
|
||||
{"Stage.Circle", "desenhe círculo em x %1 y %2 raio %3"},
|
||||
{"FS.IsDir", "%1 é pasta?"},
|
||||
};
|
||||
|
||||
static int shape(const char* type, void* ud) {
|
||||
(void)ud;
|
||||
if (!strcmp(type, "FS.IsDir")) return TB_SB_BOOLEAN;
|
||||
return -1; /* o resto: tabela interna */
|
||||
}
|
||||
static int slot_kind(const char* type, int slot, void* ud) {
|
||||
(void)ud; (void)slot;
|
||||
return !strcmp(type, "Stage.Circle") ? TB_SB_SLOT_NUM : TB_SB_SLOT_TEXT;
|
||||
}
|
||||
static const char* category(const char* type, void* ud) {
|
||||
(void)ud;
|
||||
return !strncmp(type, "Stage.", 6) ? "pen" : "extension";
|
||||
}
|
||||
|
||||
static const TbSbContext CX = { s_labels, 2, shape, slot_kind, category, NULL };
|
||||
|
||||
static TbBpTab* fresh(void) { return calloc(1, sizeof(TbBpTab)); }
|
||||
|
||||
static const char* SCRIPT =
|
||||
"[scratchblocks]\n"
|
||||
"when flag clicked\n"
|
||||
"set [x v] to (0)\n"
|
||||
"repeat (10)\n"
|
||||
" change [x v] by (1) // conta\n"
|
||||
" if <(x) > (5)> then\n"
|
||||
" say [grande]\n"
|
||||
" else\n"
|
||||
" say (join [x = ] (x))\n"
|
||||
" end\n"
|
||||
"end\n"
|
||||
"delete all of [frutas v]\n"
|
||||
"add [maçã] to [frutas v]\n"
|
||||
"say (item (1) of [frutas v])\n"
|
||||
"[/scratchblocks]\n";
|
||||
|
||||
static const char* EXPECTED =
|
||||
"when flag clicked\n"
|
||||
"set [x v] to (0)\n"
|
||||
"repeat (10)\n"
|
||||
" change [x v] by (1)\n"
|
||||
" if <(x) > (5)> then\n"
|
||||
" say [grande]\n"
|
||||
" else\n"
|
||||
" say (join [x = ] (x))\n"
|
||||
" end\n"
|
||||
"end\n"
|
||||
"delete all of [frutas v]\n"
|
||||
"add [maçã] to [frutas v]\n"
|
||||
"say (item (1) of [frutas v])\n";
|
||||
|
||||
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)"; }
|
||||
|
||||
int main(void) {
|
||||
char warn[256];
|
||||
|
||||
/* -- importar ---------------------------------------------------------- */
|
||||
TbBpTab* t = fresh();
|
||||
int n = tb_scratchblocks_import(SCRIPT, t, &CX, warn, sizeof(warn));
|
||||
CHECK(n == 1 && !warn[0], "importa um script (uma pilha, sem avisos)");
|
||||
int hat = t->top_n ? t->top[0] : -1;
|
||||
CHECK(!strcmp(ty(t, hat), "Events.WhenRun"), "chapéu: quando a bandeira for clicada");
|
||||
int set = t->blocks[hat].next;
|
||||
CHECK(!strcmp(ty(t, set), "Variables.Set") && !strcmp(t->blocks[set].in_text[0], "x") &&
|
||||
!strcmp(t->blocks[set].in_text[1], "0"), "set [x v] to (0)");
|
||||
int rep = t->blocks[set].next;
|
||||
CHECK(!strcmp(ty(t, rep), "Control.Repeat") && !strcmp(t->blocks[rep].in_text[0], "10"), "repeat (10)");
|
||||
int chg = t->blocks[rep].sub[0];
|
||||
CHECK(!strcmp(ty(t, chg), "Variables.Change"), "corpo do repeat começa com change (comentário ignorado)");
|
||||
int iff = t->blocks[chg].next;
|
||||
CHECK(!strcmp(ty(t, iff), "Control.IfElse"), "if ... else ... end vira se/senão");
|
||||
int cond = child(t, iff, 0);
|
||||
CHECK(!strcmp(ty(t, cond), "Operators.Gt") && !strcmp(ty(t, child(t, cond, 0)), "Variables.Get") &&
|
||||
!strcmp(t->blocks[cond].in_text[1], "5"), "condição <(x) > (5)> com variável e número");
|
||||
int say2 = t->blocks[iff].sub[1];
|
||||
int join = child(t, say2, 0);
|
||||
CHECK(!strcmp(ty(t, join), "Text.Concat") && !strcmp(t->blocks[join].in_text[0], "x = "),
|
||||
"senão: say (join [x = ] (x))");
|
||||
int after = t->blocks[rep].next;
|
||||
CHECK(!strcmp(ty(t, after), "List.Clear") && !strcmp(t->blocks[after].in_text[0], "frutas"),
|
||||
"depois do end: delete all of [frutas v]");
|
||||
int add = t->blocks[after].next;
|
||||
CHECK(!strcmp(ty(t, add), "List.Push") && !strcmp(t->blocks[add].in_text[0], "maçã"), "add [maçã] to [frutas v]");
|
||||
int say3 = t->blocks[add].next;
|
||||
int item = child(t, say3, 0);
|
||||
CHECK(!strcmp(ty(t, item), "List.Item") && !strcmp(ty(t, child(t, item, 0)), "List.Var") &&
|
||||
!strcmp(t->blocks[child(t, item, 0)].in_text[0], "frutas"),
|
||||
"item (1) of [frutas v] usa a lista com nome");
|
||||
|
||||
/* -- exportar ---------------------------------------------------------- */
|
||||
char* txt = tb_scratchblocks_export(t, &CX);
|
||||
CHECK(txt && !strcmp(txt, EXPECTED), "exporta igual ao texto do fórum");
|
||||
if (txt && strcmp(txt, EXPECTED)) printf("---- obtido:\n%s----\n", txt);
|
||||
|
||||
/* ida e volta */
|
||||
TbBpTab* t2 = fresh();
|
||||
tb_scratchblocks_import(txt ? txt : "", t2, &CX, warn, sizeof(warn));
|
||||
char* txt2 = tb_scratchblocks_export(t2, &CX);
|
||||
CHECK(txt2 && txt && !strcmp(txt, txt2), "ida e volta (exportar(importar(texto))) é estável");
|
||||
free(txt); free(txt2); free(t2); free(t);
|
||||
|
||||
/* -- blocos só do TinyBlocks: rótulo em português + ::categoria ------- */
|
||||
t = fresh();
|
||||
n = tb_scratchblocks_import(
|
||||
"when flag clicked\n"
|
||||
"desenhe círculo em x (10) y (-20) raio (30) :: pen\n"
|
||||
"if <[/tmp] é pasta? :: extension> then\n"
|
||||
" erase all\n"
|
||||
"end\n"
|
||||
"\n"
|
||||
"forever\n"
|
||||
" set pen size to (4)\n"
|
||||
"end\n"
|
||||
"say [isto vira outra pilha]\n", t, &CX, warn, sizeof(warn));
|
||||
CHECK(n == 3 && !warn[0], "rótulos em português, chapéu, \"sempre\" e linha em branco separam pilhas");
|
||||
if (!(n == 3 && !warn[0])) printf(" n=%d aviso=%s\n", n, warn);
|
||||
txt = tb_scratchblocks_export(t, &CX);
|
||||
CHECK(txt && strstr(txt, "desenhe círculo em x (10) y (-20) raio (30) :: pen\n"),
|
||||
"exporta bloco do TinyBlocks com rótulo em português e ::pen");
|
||||
CHECK(txt && strstr(txt, "if <[/tmp] é pasta? :: extension> then\n"), "booleano do TinyBlocks dentro do if");
|
||||
if (txt && !strstr(txt, "if <[/tmp] é pasta? :: extension> then\n")) printf("---- obtido:\n%s----\n", txt);
|
||||
CHECK(txt && strstr(txt, "forever\n set pen size to (4)\nend\n"), "sempre (tampa) com corpo");
|
||||
free(txt); free(t);
|
||||
|
||||
/* -- linha desconhecida: avisa e segue -------------------------------- */
|
||||
t = fresh();
|
||||
n = tb_scratchblocks_import("when flag clicked\nvoe para a lua\nerase all\n", t, &CX, warn, sizeof(warn));
|
||||
CHECK(n == 1 && strstr(warn, "linha 2"), "linha desconhecida gera aviso com o número da linha");
|
||||
CHECK(!strcmp(ty(t, t->blocks[t->top[0]].next), "Stage.Clear"), "e o resto continua sendo importado");
|
||||
free(t);
|
||||
|
||||
/* -- texto com colchetes é escapado ----------------------------------- */
|
||||
t = fresh();
|
||||
tb_scratchblocks_import("say [a \\] b]\n", t, &CX, warn, sizeof(warn));
|
||||
CHECK(!strcmp(t->blocks[t->top[0]].in_text[0], "a ] b"), "\\] dentro de [ ] é texto");
|
||||
txt = tb_scratchblocks_export(t, &CX);
|
||||
CHECK(txt && !strcmp(txt, "say [a \\] b]\n"), "exporta escapando o ]");
|
||||
free(txt); free(t);
|
||||
|
||||
if (fails) printf("\n%d FALHA(S)\n", fails);
|
||||
else printf("\nTUDO OK\n");
|
||||
return fails != 0;
|
||||
}
|
||||
Reference in new issue
Block a user