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];
|
||||
if (m->slot[i] >= 0) {
|
||||
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;
|
||||
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; }
|
||||
const Model* m = find_model_loose(I->M, t, &n, cls);
|
||||
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;
|
||||
for (int i = 0; i < n; i++) if (t[i].kind != T_WORD) words = false;
|
||||
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) --- */
|
||||
|
||||
#define NAMED_MAX 64
|
||||
typedef struct { char name[64]; TinyBlocks_JsonValue* arr; } NamedList;
|
||||
static NamedList s_named[NAMED_MAX];
|
||||
static int s_named_n = 0;
|
||||
static NamedList* s_named = NULL; /* cresce: quantas listas o programa quiser */
|
||||
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) {
|
||||
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;
|
||||
for (int i = 0; i < s_named_n; 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++];
|
||||
snprintf(L->name, sizeof(L->name), "%s", name);
|
||||
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());
|
||||
}
|
||||
|
||||
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];
|
||||
NamedList* L = named_get(in_name(self, rt, 1, nm, sizeof(nm)));
|
||||
if (L) push_owned(L->arr, in_item(self, rt, 0));
|
||||
NamedList* L = named_get(in_name(self, rt, i, nm, sizeof(nm)));
|
||||
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;
|
||||
}
|
||||
|
||||
static void eval_delete_at(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
char nm[64];
|
||||
NamedList* L = named_get(in_name(self, rt, 1, nm, sizeof(nm)));
|
||||
if (L) {
|
||||
long long k = in_pos(self, rt, 0, tb_json_array_length(L->arr), false);
|
||||
if (k > 0) tb_json_array_remove(L->arr, (uint32_t)(k - 1));
|
||||
TinyBlocks_JsonValue* arr = named_arr(self, rt, 1);
|
||||
if (arr) {
|
||||
long long k = in_pos(self, rt, 0, tb_json_array_length(arr), false);
|
||||
if (k > 0) tb_json_array_remove(arr, (uint32_t)(k - 1));
|
||||
tb_json_release(arr);
|
||||
}
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void eval_clear(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
char nm[64];
|
||||
NamedList* L = named_get(in_name(self, rt, 0, nm, sizeof(nm)));
|
||||
if (L) tb_json_array_clear(L->arr);
|
||||
TinyBlocks_JsonValue* arr = named_arr(self, rt, 0);
|
||||
if (arr) { tb_json_array_clear(arr); tb_json_release(arr); }
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void eval_insert_at(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
char nm[64];
|
||||
NamedList* L = named_get(in_name(self, rt, 2, nm, sizeof(nm)));
|
||||
if (L) {
|
||||
long long k = in_pos(self, rt, 1, tb_json_array_length(L->arr), true);
|
||||
TinyBlocks_JsonValue* arr = named_arr(self, rt, 2);
|
||||
if (arr) {
|
||||
long long k = in_pos(self, rt, 1, tb_json_array_length(arr), true);
|
||||
if (k > 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(arr);
|
||||
}
|
||||
self->dirty = false;
|
||||
}
|
||||
|
||||
static void eval_set_at(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
char nm[64];
|
||||
NamedList* L = named_get(in_name(self, rt, 1, nm, sizeof(nm)));
|
||||
if (L) {
|
||||
long long k = in_pos(self, rt, 0, tb_json_array_length(L->arr), false);
|
||||
TinyBlocks_JsonValue* arr = named_arr(self, rt, 1);
|
||||
if (arr) {
|
||||
long long k = in_pos(self, rt, 0, tb_json_array_length(arr), false);
|
||||
if (k > 0) {
|
||||
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(arr);
|
||||
}
|
||||
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 */
|
||||
static void eval_assign(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||
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)));
|
||||
TinyBlocks_JsonValue* src = in_list(self, rt, 1);
|
||||
if (L && src) named_set(L, tb_json_clone(src));
|
||||
if (src) tb_json_release(src);
|
||||
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) {
|
||||
(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);
|
||||
s_named_n = 0;
|
||||
free(s_named);
|
||||
s_named = NULL;
|
||||
s_named_n = s_named_cap = 0;
|
||||
}
|
||||
|
||||
TINYBLOCKS_DEFINE_LIBRARY(vector,
|
||||
|
||||
Reference in new issue
Block a user