Listas com nome sem limite de 64; scratchblocks com parênteses a mais
Da 65a lista com nome em diante, adicionar e ler não faziam nada, sem aviso. Agora o vetor cresce; os comandos seguram o array (retain) em vez do ponteiro para a entrada, que pode mudar de lugar. No scratchblocks, "((seno de (x)))" casava com o modelo "[%1 v]" de lista e criava uma lista com o nome do resultado (a cada quadro, uma nova). Um modelo que é só um campo exige o mesmo tipo de grupo, e parênteses a mais em volta de uma expressão são abertos. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9cff7d8ca5
commit
a94d45059e
4 files changed
+67
-27
No files matched your search
@@ -348,6 +348,9 @@ static bool model_match(const Model* m, const Tok* t, int n) {
|
|||||||
const Tok* mt = &m->tok[i];
|
const Tok* mt = &m->tok[i];
|
||||||
if (m->slot[i] >= 0) {
|
if (m->slot[i] >= 0) {
|
||||||
if (t[i].kind == T_WORD) return false;
|
if (t[i].kind == T_WORD) return false;
|
||||||
|
/* modelo que e so um campo ("[%1 v]" da lista): o grupo tem que ser
|
||||||
|
o mesmo, senao "((seno de (x)))" vira uma lista chamada "seno..." */
|
||||||
|
if (n == 1 && (mt->kind != t[i].kind || mt->dropdown != t[i].dropdown)) return false;
|
||||||
if (m->listslot[i] && !(t[i].kind == T_SQUARE && t[i].dropdown)) return false;
|
if (m->listslot[i] && !(t[i].kind == T_SQUARE && t[i].dropdown)) return false;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -657,6 +660,11 @@ static int parse_inner(Imp* I, const char* text, char cls) {
|
|||||||
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_loose(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);
|
||||||
|
if (n == 1 && t[0].kind == T_ROUND) { /* ((expr)): parenteses a mais */
|
||||||
|
Tok in[TOK_MAX];
|
||||||
|
int k = tokenize(t[0].text, in, TOK_MAX);
|
||||||
|
if (k > 1 || (k == 1 && in[0].kind != T_WORD)) return parse_inner(I, t[0].text, cls);
|
||||||
|
}
|
||||||
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;
|
||||||
if (words && cls == 'r') { /* (x): variavel */
|
if (words && cls == 'r') { /* (x): variavel */
|
||||||
|
|||||||
@@ -410,10 +410,9 @@ static void eval_sum(TinyBlocks_Block* self, TinyBlocks* rt) {
|
|||||||
|
|
||||||
/* -- Listas com nome (comandos que mudam a lista, como no Scratch) --- */
|
/* -- Listas com nome (comandos que mudam a lista, como no Scratch) --- */
|
||||||
|
|
||||||
#define NAMED_MAX 64
|
|
||||||
typedef struct { char name[64]; TinyBlocks_JsonValue* arr; } NamedList;
|
typedef struct { char name[64]; TinyBlocks_JsonValue* arr; } NamedList;
|
||||||
static NamedList s_named[NAMED_MAX];
|
static NamedList* s_named = NULL; /* cresce: quantas listas o programa quiser */
|
||||||
static int s_named_n = 0;
|
static int s_named_n = 0, s_named_cap = 0;
|
||||||
|
|
||||||
static const char* in_name(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i, char* buf, size_t cap) {
|
static const char* in_name(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i, char* buf, size_t cap) {
|
||||||
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
|
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
|
||||||
@@ -428,7 +427,12 @@ static NamedList* named_get(const char* name) {
|
|||||||
if (!name[0]) return NULL;
|
if (!name[0]) return NULL;
|
||||||
for (int i = 0; i < s_named_n; i++)
|
for (int i = 0; i < s_named_n; i++)
|
||||||
if (!strcmp(s_named[i].name, name)) return &s_named[i];
|
if (!strcmp(s_named[i].name, name)) return &s_named[i];
|
||||||
if (s_named_n >= NAMED_MAX) return NULL;
|
if (s_named_n == s_named_cap) {
|
||||||
|
int nc = s_named_cap ? s_named_cap * 2 : 32;
|
||||||
|
NamedList* np = realloc(s_named, (size_t)nc * sizeof(NamedList));
|
||||||
|
if (!np) return NULL;
|
||||||
|
s_named = np; s_named_cap = nc;
|
||||||
|
}
|
||||||
NamedList* L = &s_named[s_named_n++];
|
NamedList* L = &s_named[s_named_n++];
|
||||||
snprintf(L->name, sizeof(L->name), "%s", name);
|
snprintf(L->name, sizeof(L->name), "%s", name);
|
||||||
L->arr = tb_json_array();
|
L->arr = tb_json_array();
|
||||||
@@ -464,54 +468,62 @@ static void eval_var(TinyBlocks_Block* self, TinyBlocks* rt) {
|
|||||||
out_list(self, L ? L->arr : tb_json_array());
|
out_list(self, L ? L->arr : tb_json_array());
|
||||||
}
|
}
|
||||||
|
|
||||||
static void eval_push(TinyBlocks_Block* self, TinyBlocks* rt) {
|
/* Array da lista (com referencia: avaliar outro campo pode criar listas e
|
||||||
|
* mover s_named, mas o array continua o mesmo); NULL se o nome e vazio */
|
||||||
|
static TinyBlocks_JsonValue* named_arr(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) {
|
||||||
char nm[64];
|
char nm[64];
|
||||||
NamedList* L = named_get(in_name(self, rt, 1, nm, sizeof(nm)));
|
NamedList* L = named_get(in_name(self, rt, i, nm, sizeof(nm)));
|
||||||
if (L) push_owned(L->arr, in_item(self, rt, 0));
|
if (!L) return NULL;
|
||||||
|
tb_json_retain(L->arr);
|
||||||
|
return L->arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void eval_push(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||||
|
TinyBlocks_JsonValue* arr = named_arr(self, rt, 1);
|
||||||
|
if (arr) { push_owned(arr, in_item(self, rt, 0)); tb_json_release(arr); }
|
||||||
self->dirty = false;
|
self->dirty = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void eval_delete_at(TinyBlocks_Block* self, TinyBlocks* rt) {
|
static void eval_delete_at(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||||
char nm[64];
|
TinyBlocks_JsonValue* arr = named_arr(self, rt, 1);
|
||||||
NamedList* L = named_get(in_name(self, rt, 1, nm, sizeof(nm)));
|
if (arr) {
|
||||||
if (L) {
|
long long k = in_pos(self, rt, 0, tb_json_array_length(arr), false);
|
||||||
long long k = in_pos(self, rt, 0, tb_json_array_length(L->arr), false);
|
if (k > 0) tb_json_array_remove(arr, (uint32_t)(k - 1));
|
||||||
if (k > 0) tb_json_array_remove(L->arr, (uint32_t)(k - 1));
|
tb_json_release(arr);
|
||||||
}
|
}
|
||||||
self->dirty = false;
|
self->dirty = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void eval_clear(TinyBlocks_Block* self, TinyBlocks* rt) {
|
static void eval_clear(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||||
char nm[64];
|
TinyBlocks_JsonValue* arr = named_arr(self, rt, 0);
|
||||||
NamedList* L = named_get(in_name(self, rt, 0, nm, sizeof(nm)));
|
if (arr) { tb_json_array_clear(arr); tb_json_release(arr); }
|
||||||
if (L) tb_json_array_clear(L->arr);
|
|
||||||
self->dirty = false;
|
self->dirty = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void eval_insert_at(TinyBlocks_Block* self, TinyBlocks* rt) {
|
static void eval_insert_at(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||||
char nm[64];
|
TinyBlocks_JsonValue* arr = named_arr(self, rt, 2);
|
||||||
NamedList* L = named_get(in_name(self, rt, 2, nm, sizeof(nm)));
|
if (arr) {
|
||||||
if (L) {
|
long long k = in_pos(self, rt, 1, tb_json_array_length(arr), true);
|
||||||
long long k = in_pos(self, rt, 1, tb_json_array_length(L->arr), true);
|
|
||||||
if (k > 0) {
|
if (k > 0) {
|
||||||
TinyBlocks_JsonValue* it = in_item(self, rt, 0);
|
TinyBlocks_JsonValue* it = in_item(self, rt, 0);
|
||||||
tb_json_array_insert(L->arr, (uint32_t)(k - 1), it);
|
tb_json_array_insert(arr, (uint32_t)(k - 1), it);
|
||||||
tb_json_release(it);
|
tb_json_release(it);
|
||||||
}
|
}
|
||||||
|
tb_json_release(arr);
|
||||||
}
|
}
|
||||||
self->dirty = false;
|
self->dirty = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void eval_set_at(TinyBlocks_Block* self, TinyBlocks* rt) {
|
static void eval_set_at(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||||
char nm[64];
|
TinyBlocks_JsonValue* arr = named_arr(self, rt, 1);
|
||||||
NamedList* L = named_get(in_name(self, rt, 1, nm, sizeof(nm)));
|
if (arr) {
|
||||||
if (L) {
|
long long k = in_pos(self, rt, 0, tb_json_array_length(arr), false);
|
||||||
long long k = in_pos(self, rt, 0, tb_json_array_length(L->arr), false);
|
|
||||||
if (k > 0) {
|
if (k > 0) {
|
||||||
TinyBlocks_JsonValue* it = in_item(self, rt, 2);
|
TinyBlocks_JsonValue* it = in_item(self, rt, 2);
|
||||||
tb_json_array_set(L->arr, (uint32_t)(k - 1), it);
|
tb_json_array_set(arr, (uint32_t)(k - 1), it);
|
||||||
tb_json_release(it);
|
tb_json_release(it);
|
||||||
}
|
}
|
||||||
|
tb_json_release(arr);
|
||||||
}
|
}
|
||||||
self->dirty = false;
|
self->dirty = false;
|
||||||
}
|
}
|
||||||
@@ -519,8 +531,8 @@ static void eval_set_at(TinyBlocks_Block* self, TinyBlocks* rt) {
|
|||||||
/* "mude a lista [nome] para [lista]": guarda o resultado de outro bloco */
|
/* "mude a lista [nome] para [lista]": guarda o resultado de outro bloco */
|
||||||
static void eval_assign(TinyBlocks_Block* self, TinyBlocks* rt) {
|
static void eval_assign(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||||
char nm[64];
|
char nm[64];
|
||||||
|
TinyBlocks_JsonValue* src = in_list(self, rt, 1); /* antes: pode criar listas */
|
||||||
NamedList* L = named_get(in_name(self, rt, 0, nm, sizeof(nm)));
|
NamedList* L = named_get(in_name(self, rt, 0, nm, sizeof(nm)));
|
||||||
TinyBlocks_JsonValue* src = in_list(self, rt, 1);
|
|
||||||
if (L && src) named_set(L, tb_json_clone(src));
|
if (L && src) named_set(L, tb_json_clone(src));
|
||||||
if (src) tb_json_release(src);
|
if (src) tb_json_release(src);
|
||||||
self->dirty = false;
|
self->dirty = false;
|
||||||
@@ -637,7 +649,9 @@ static bool vector_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
|||||||
static void vector_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
static void vector_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||||
(void)rt; (void)lib;
|
(void)rt; (void)lib;
|
||||||
for (int i = 0; i < s_named_n; i++) if (s_named[i].arr) tb_json_release(s_named[i].arr);
|
for (int i = 0; i < s_named_n; i++) if (s_named[i].arr) tb_json_release(s_named[i].arr);
|
||||||
s_named_n = 0;
|
free(s_named);
|
||||||
|
s_named = NULL;
|
||||||
|
s_named_n = s_named_cap = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
TINYBLOCKS_DEFINE_LIBRARY(vector,
|
TINYBLOCKS_DEFINE_LIBRARY(vector,
|
||||||
|
|||||||
@@ -187,6 +187,17 @@ int main(void) {
|
|||||||
}
|
}
|
||||||
drop(t);
|
drop(t);
|
||||||
|
|
||||||
|
/* -- parenteses a mais: ((seno)) nao vira uma lista chamada "seno..." --- */
|
||||||
|
t = fresh();
|
||||||
|
warn[0] = 0;
|
||||||
|
tb_scratchblocks_import("change [cx v] by (((([sin v] of (yaw)))) * (0.4))\n", t, &CX, warn, sizeof(warn));
|
||||||
|
{
|
||||||
|
int mul = child(t, t->top_n ? t->top[0] : -1, 1);
|
||||||
|
CHECK(!warn[0] && !strcmp(ty(t, mul), "Math.Mul") && !strcmp(ty(t, child(t, mul, 0)), "Number.Sin"),
|
||||||
|
"((expressão)) com parênteses a mais");
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
|||||||
@@ -280,6 +280,13 @@ int main(void) {
|
|||||||
run("List.Clear", "nl", NULL, NULL);
|
run("List.Clear", "nl", NULL, NULL);
|
||||||
CHECK(is_list(run("List.Var", "nl", NULL, NULL), "[]"), "apague todos os itens");
|
CHECK(is_list(run("List.Var", "nl", NULL, NULL), "[]"), "apague todos os itens");
|
||||||
|
|
||||||
|
/* mais de 64 listas (antes: da 65a em diante nada entrava) */
|
||||||
|
char nm2[16];
|
||||||
|
for (int i = 0; i < 100; i++) { snprintf(nm2, sizeof(nm2), "l%d", i); run("List.Push", "1", nm2, NULL); }
|
||||||
|
run("List.Push", "2", "l99", NULL);
|
||||||
|
CHECK(is_list(run("List.Var", "l99", NULL, NULL), "[1,2]") && is_list(run("List.Var", "nl", NULL, NULL), "[]"),
|
||||||
|
"100 listas com nome");
|
||||||
|
|
||||||
/* 10 mil itens: ler e trocar um item nao pode custar a lista inteira */
|
/* 10 mil itens: ler e trocar um item nao pode custar a lista inteira */
|
||||||
run("List.Clear", "big", NULL, NULL);
|
run("List.Clear", "big", NULL, NULL);
|
||||||
TinyBlocks_Block* push = tb_block_create(rt, tb_rt_find_type(rt, "List.Push"));
|
TinyBlocks_Block* push = tb_block_create(rt, tb_rt_find_type(rt, "List.Push"));
|
||||||
|
|||||||
Reference in new issue
Block a user