scratchblocks: rótulos com ( ) e vírgula ("nova imagem(96 x 72)", "em (x,y)")

Rótulos de library com pontuação literal não casavam: o "(" do rótulo
virava campo da notação. Agora, nos rótulos do editor e das libraries,
"( )" e "," são texto, e na importação um grupo ( ) com vários tokens
dentro é aberto quando nada casa. Aceita o que o exportador escreve
("nova imagem((96) x (72))") e a forma sem parênteses extras; a linha
"pinte a imagem ... em ((px),(py))" sumia sem aviso.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mguinhosandClaude Opus 5.5 committed 2026-10-23 16:12:39 -03:00
1 parent 63c8fb0c1c
commit feb20a2127
2 files changed
+77 -6

No files matched your search

@@ -264,12 +264,27 @@ static int slot_ref(const char* s, bool* list) {
return *p ? -1 : v - 1; return *p ? -1 : v - 1;
} }
static bool model_build(Model* m, const char* type, const char* text, char shape) { /* Rotulo de library/editor: "( )" e "," sao texto, nao campos da notacao
* ("nova imagem(%1 x %2)" -> "nova imagem %1 x %2", "em (%2,%3)" -> "em %2 , %3") */
static void loose_punct(char* buf, size_t cap) {
char out[512];
size_t n = 0;
for (const char* p = buf; *p && n + 3 < sizeof(out); p++) {
if (*p == '(' || *p == ')') out[n++] = ' ';
else if (*p == ',') { out[n++] = ' '; out[n++] = ','; out[n++] = ' '; }
else out[n++] = *p;
}
out[n] = 0;
snprintf(buf, cap, "%s", out);
}
static bool model_build(Model* m, const char* type, const char* text, char shape, bool loose) {
memset(m, 0, sizeof(*m)); memset(m, 0, sizeof(*m));
m->type = type; m->type = type;
m->shape = shape; m->shape = shape;
char buf[512]; char buf[512];
snprintf(buf, sizeof(buf), "%s", text); snprintf(buf, sizeof(buf), "%s", text);
if (loose) loose_punct(buf, sizeof(buf));
const char* cc = strstr(text, "::"); const char* cc = strstr(text, "::");
if (cc) { if (cc) {
cc += 2; cc += 2;
@@ -304,7 +319,7 @@ static Models models_load(const TbSbContext* cx) {
if (!M.m) return M; if (!M.m) return M;
for (int i = 0; i < BUILTIN_N; i++) { for (int i = 0; i < BUILTIN_N; i++) {
Model* m = &M.m[M.n]; Model* m = &M.m[M.n];
if (!model_build(m, s_builtin[i].type, s_builtin[i].text, s_builtin[i].shape)) continue; if (!model_build(m, s_builtin[i].type, s_builtin[i].text, s_builtin[i].shape, false)) continue;
m->builtin = true; m->builtin = true;
m->import_only = s_builtin[i].import_only; m->import_only = s_builtin[i].import_only;
M.n++; M.n++;
@@ -314,7 +329,7 @@ static Models models_load(const TbSbContext* cx) {
if (!L->type || !L->label) continue; if (!L->type || !L->label) continue;
int sh = cx_shape(cx, L->type); int sh = cx_shape(cx, L->type);
Model* m = &M.m[M.n]; Model* m = &M.m[M.n];
if (!model_build(m, L->type, L->label, (char)shape_char(sh < 0 ? TB_SB_STACK : sh))) continue; if (!model_build(m, L->type, L->label, (char)shape_char(sh < 0 ? TB_SB_STACK : sh), true)) continue;
/* "%1" sozinho (ex.: valor de variavel) casaria com qualquer coisa */ /* "%1" sozinho (ex.: valor de variavel) casaria com qualquer coisa */
if (m->n == 1 && m->slot[0] >= 0) continue; if (m->n == 1 && m->slot[0] >= 0) continue;
M.n++; M.n++;
@@ -604,6 +619,34 @@ static const Model* find_model(const Models* M, const Tok* t, int n, char cls) {
return NULL; return NULL;
} }
/* Nenhum modelo casou: abre os grupos ( ) que tem varios tokens dentro
* ("imagem((96) x (72))" -> imagem (96) x (72)) e tenta de novo. E como a
* pontuacao literal dos rotulos de library volta ao formato dos modelos. */
static const Model* find_model_loose(const Models* M, Tok* t, int* n, char cls) {
const Model* m = find_model(M, t, *n, cls);
if (m) return m;
Tok flat[TOK_MAX];
int fn = 0;
bool changed = false;
for (int i = 0; i < *n && fn < TOK_MAX; i++) {
Tok inner[TOK_MAX];
int k = t[i].kind == T_ROUND ? tokenize(t[i].text, inner, TOK_MAX) : -1;
if (k > 1) {
for (int j = 0; j < k && fn < TOK_MAX; j++) flat[fn++] = inner[j];
changed = true;
} else {
flat[fn++] = t[i];
}
}
if (!changed) return NULL;
m = find_model(M, flat, fn, cls);
if (m) {
memcpy(t, flat, (size_t)fn * sizeof(Tok));
*n = fn;
}
return m;
}
/* Conteudo de um ( ) ou < > -> repórter */ /* Conteudo de um ( ) ou < > -> repórter */
static int parse_inner(Imp* I, const char* text, char cls) { static int parse_inner(Imp* I, const char* text, char cls) {
char buf[TOK_TEXT]; char buf[TOK_TEXT];
@@ -612,7 +655,7 @@ static int parse_inner(Imp* I, const char* text, char cls) {
Tok t[TOK_MAX]; Tok t[TOK_MAX];
int n = tokenize(buf, t, TOK_MAX); int n = tokenize(buf, t, TOK_MAX);
if (n < 0) { imp_warn(I, "parenteses nao fecham", text); return -1; } if (n < 0) { imp_warn(I, "parenteses nao fecham", text); return -1; }
const Model* m = find_model(I->M, t, n, cls); const Model* m = find_model_loose(I->M, t, &n, cls);
if (m) return build_from(I, m, t, n); if (m) return build_from(I, m, t, n);
bool words = n > 0; bool words = n > 0;
for (int i = 0; i < n; i++) if (t[i].kind != T_WORD) words = false; for (int i = 0; i < n; i++) if (t[i].kind != T_WORD) words = false;
@@ -702,7 +745,7 @@ int tb_scratchblocks_import(const char* text, TbBpTab* out, const TbSbContext* c
Tok t[TOK_MAX]; Tok t[TOK_MAX];
int n = tokenize(body, t, TOK_MAX); int n = tokenize(body, t, TOK_MAX);
if (n < 0) { imp_warn(&I, "parenteses nao fecham", line); continue; } if (n < 0) { imp_warn(&I, "parenteses nao fecham", line); continue; }
m = find_model(&M, t, n, 's'); m = find_model_loose(&M, t, &n, 's');
if (!m) { imp_warn(&I, "bloco desconhecido", line); continue; } if (!m) { imp_warn(&I, "bloco desconhecido", line); continue; }
bi = build_from(&I, m, t, n); bi = build_from(&I, m, t, n);
} }
+29 -1
View File
@@ -15,11 +15,14 @@ static int fails = 0;
static const TbSbLabel s_labels[] = { static const TbSbLabel s_labels[] = {
{"Stage.Circle", "desenhe círculo em x %1 y %2 raio %3"}, {"Stage.Circle", "desenhe círculo em x %1 y %2 raio %3"},
{"FS.IsDir", "%1 é pasta?"}, {"FS.IsDir", "%1 é pasta?"},
{"Image.New", "nova imagem(%1 x %2) cor r %3 g %4 b %5"}, /* ( ) e , no rotulo */
{"Image.SetPixel", "pinte a imagem %1 em (%2,%3) com r %4 g %5 b %6"},
}; };
static int shape(const char* type, void* ud) { static int shape(const char* type, void* ud) {
(void)ud; (void)ud;
if (!strcmp(type, "FS.IsDir")) return TB_SB_BOOLEAN; if (!strcmp(type, "FS.IsDir")) return TB_SB_BOOLEAN;
if (!strcmp(type, "Image.New")) return TB_SB_REPORTER;
return -1; /* o resto: tabela interna */ return -1; /* o resto: tabela interna */
} }
static int slot_kind(const char* type, int slot, void* ud) { static int slot_kind(const char* type, int slot, void* ud) {
@@ -31,7 +34,7 @@ static const char* category(const char* type, void* ud) {
return !strncmp(type, "Stage.", 6) ? "pen" : "extension"; return !strncmp(type, "Stage.", 6) ? "pen" : "extension";
} }
static const TbSbContext CX = { s_labels, 2, shape, slot_kind, category, NULL }; static const TbSbContext CX = { s_labels, (int)(sizeof(s_labels) / sizeof(s_labels[0])), shape, slot_kind, category, NULL };
static TbBpTab* fresh(void) { return calloc(1, sizeof(TbBpTab)); } static TbBpTab* fresh(void) { return calloc(1, sizeof(TbBpTab)); }
@@ -159,6 +162,31 @@ int main(void) {
CHECK(txt && !strcmp(txt, "say [a \\] b]\n"), "exporta escapando o ]"); CHECK(txt && !strcmp(txt, "say [a \\] b]\n"), "exporta escapando o ]");
free(txt); drop(t); free(txt); drop(t);
/* -- rotulos com ( ) e , : com os parenteses do exportador e sem eles --- */
t = fresh();
warn[0] = 0;
n = tb_scratchblocks_import("set [tela v] to (nova imagem((96) x (72)) cor r (135) g (200) b (255))\n"
"pinte a imagem (tela) em ((px),(py)) com r (1) g (2) b (3)\n"
"pinte a imagem (tela) em (4) , (5) com r (1) g (2) b (3)\n", t, &CX, warn, sizeof(warn));
{
int set = t->top_n ? t->top[0] : -1, img = child(t, set, 1);
int p1 = set >= 0 ? t->blocks[set].next : -1, p2 = p1 >= 0 ? t->blocks[p1].next : -1;
CHECK(!warn[0] && !strcmp(ty(t, img), "Image.New") && !strcmp(t->blocks[img].in_text[0], "96") &&
!strcmp(t->blocks[img].in_text[4], "255"), "rótulo com ( ) no meio: nova imagem((96) x (72))");
CHECK(!strcmp(ty(t, p1), "Image.SetPixel") && !strcmp(ty(t, child(t, p1, 1)), "Variables.Get"),
"rótulo com (x,y): pinte ... em ((px),(py))");
CHECK(!strcmp(ty(t, p2), "Image.SetPixel") && !strcmp(t->blocks[p2].in_text[2], "5"),
"e sem os parênteses: em (4) , (5)");
txt = tb_scratchblocks_export(t, &CX);
TbBpTab* t3 = fresh();
int n3 = txt ? tb_scratchblocks_import(txt, t3, &CX, warn, sizeof(warn)) : 0;
char* txt3 = n3 > 0 ? tb_scratchblocks_export(t3, &CX) : NULL;
CHECK(txt && txt3 && !strcmp(txt, txt3), "rótulos com pontuação: exporta e importa de novo igual");
if (txt && txt3 && strcmp(txt, txt3)) printf("---- 1:\n%s---- 2:\n%s", txt, txt3);
free(txt); free(txt3); drop(t3);
}
drop(t);
if (fails) printf("\n%d FALHA(S)\n", fails); if (fails) printf("\n%d FALHA(S)\n", fails);
else printf("\nTUDO OK\n"); else printf("\nTUDO OK\n");
return fails != 0; return fails != 0;