/* * TinyBlocks - tb_vector.c * Categoria "Listas": List.Empty / Of / Range / Add / Insert / Remove / * Replace / Item / Length / IndexOf / Slice / Reverse / Sort / Join / Sum e * o booleano List.Contains. Listas sao arrays JSON imutaveis: cada bloco * monta uma lista nova (ver tb_vector.h). * * Listas com nome (como as do Scratch): List.Var / Push / DeleteAt / Clear / * InsertAt / SetAt / Assign mudam uma lista guardada por esta library. * * Comparacao de itens como no Scratch: numeros pelo valor ("2" == 2.0) e * textos sem diferenciar maiusculas. */ #define _POSIX_C_SOURCE 200809L #include #include #include #include #include #include #include #include #include #include "tb_vector.h" #define LIST_MAX_RANGE 100000 /* -- Itens --- */ static bool text_number(const char* s, double* out) { while (isspace((unsigned char)*s)) s++; if (!*s) return false; char* e; double d = strtod(s, &e); if (e == s) return false; while (isspace((unsigned char)*e)) e++; if (*e) return false; *out = d; return true; } /* Item JSON a partir de um texto digitado: numero vira numero */ static TinyBlocks_JsonValue* item_from_text(const char* s) { double d; if (text_number(s, &d)) return tb_json_number(d); return tb_json_string(s); } /* Item JSON a partir de um valor de entrada (ref nova; NULL = sem valor) */ static TinyBlocks_JsonValue* item_from_value(TinyBlocks_Value v) { switch (v.kind) { case TB_VALUE_INTEGER: return tb_json_number((double)v.as.integer); case TB_VALUE_REAL: return tb_json_number(v.as.real); case TB_VALUE_TEXT: return tb_json_string(v.as.text ? v.as.text : ""); case TB_VALUE_JSON: if (v.as.json) tb_json_retain(v.as.json); return v.as.json; default: return NULL; } } /* Valor de saida a partir de um item JSON */ static TinyBlocks_Value value_from_item(const TinyBlocks_JsonValue* j) { if (!j) return tb_value_text(""); switch (tb_json_kind(j)) { case TB_JSON_NUMBER: { double d = tb_json_as_number(j, 0); if (d == floor(d) && fabs(d) < 1e15) return tb_value_integer((int64_t)d); return tb_value_real(d); } case TB_JSON_STRING: return tb_value_text(tb_json_as_string(j)); case TB_JSON_BOOL: return tb_value_integer(tb_json_as_bool(j, false) ? 1 : 0); case TB_JSON_NULL: return tb_value_text(""); default: { TinyBlocks_JsonValue* c = tb_json_clone(j); TinyBlocks_Value v = tb_value_json(c); tb_json_release(c); return v; } } } /* Texto do item (para juntar/comparar): pela classe de valor do JSON */ static void item_text(const TinyBlocks_JsonValue* j, char* out, size_t cap) { TinyBlocks_Value v = { .kind = TB_VALUE_JSON, .as.json = (TinyBlocks_JsonValue*)j }; /* emprestado */ tb_value_to_text(v, out, cap); } static bool item_number(const TinyBlocks_JsonValue* j, double* d) { if (!j) return false; if (tb_json_kind(j) == TB_JSON_NUMBER) { *d = tb_json_as_number(j, 0); return true; } if (tb_json_kind(j) == TB_JSON_STRING) return text_number(tb_json_as_string(j), d); return false; } static bool item_equal(const TinyBlocks_JsonValue* a, const TinyBlocks_JsonValue* b) { double x, y; if (item_number(a, &x) && item_number(b, &y)) return x == y; char sa[512], sb[512]; item_text(a, sa, sizeof(sa)); item_text(b, sb, sizeof(sb)); return strcasecmp(sa, sb) == 0; } /* -- Listas --- */ static void push_owned(TinyBlocks_JsonValue* arr, TinyBlocks_JsonValue* item) { if (!item) return; tb_json_array_push(arr, item); tb_json_release(item); } /* "maçã, banana" -> ["maçã", "banana"]; itens numericos viram numero */ static TinyBlocks_JsonValue* list_from_csv(const char* s) { TinyBlocks_JsonValue* arr = tb_json_array(); if (!arr) return NULL; while (*s) { const char* e = strchr(s, ','); size_t n = e ? (size_t)(e - s) : strlen(s); while (n && isspace((unsigned char)*s)) { s++; n--; } while (n && isspace((unsigned char)s[n - 1])) n--; char* piece = malloc(n + 1); if (piece) { memcpy(piece, s, n); piece[n] = 0; push_owned(arr, item_from_text(piece)); free(piece); } if (!e) break; s = e + 1; } return arr; } TB_API TinyBlocks_JsonValue* tb_vector_from_value(TinyBlocks_Value v) { if (v.kind == TB_VALUE_JSON && v.as.json && tb_json_kind(v.as.json) == TB_JSON_ARRAY) { tb_json_retain(v.as.json); return v.as.json; } if (v.kind == TB_VALUE_TEXT && v.as.text) { const char* s = v.as.text; while (isspace((unsigned char)*s)) s++; if (!*s) return tb_json_array(); if (*s == '[') { TinyBlocks_JsonValue* j = tb_json_parse(s, NULL, 0); if (j && tb_json_kind(j) == TB_JSON_ARRAY) return j; if (j) tb_json_release(j); } return list_from_csv(s); } TinyBlocks_JsonValue* arr = tb_json_array(); if (arr && (v.kind == TB_VALUE_INTEGER || v.kind == TB_VALUE_REAL || (v.kind == TB_VALUE_JSON && v.as.json))) push_owned(arr, item_from_value(v)); return arr; } /* Copia os itens [from, to) de src para uma lista nova */ static TinyBlocks_JsonValue* list_copy(const TinyBlocks_JsonValue* src, uint32_t from, uint32_t to) { TinyBlocks_JsonValue* arr = tb_json_array(); uint32_t n = src ? tb_json_array_length(src) : 0; if (to > n) to = n; for (uint32_t i = from; arr && i < to; i++) push_owned(arr, tb_json_array_get(src, i)); return arr; } /* -- Entradas/saidas --- */ static TinyBlocks_JsonValue* in_list(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { return tb_vector_from_value(tb_block_get_input(self, rt, i)); } static TinyBlocks_JsonValue* in_item(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) { TinyBlocks_JsonValue* it = item_from_value(tb_block_get_input(self, rt, i)); return it ? it : tb_json_string(""); } /* Toma a referencia de arr */ static void out_list(TinyBlocks_Block* self, TinyBlocks_JsonValue* arr) { if (!arr) arr = tb_json_array(); tb_block_set_output(self, 0, tb_value_json(arr)); tb_json_release(arr); self->dirty = false; } static void out_int(TinyBlocks_Block* self, long long n) { tb_block_set_output(self, 0, tb_value_integer(n)); self->dirty = false; } /* -- Blocos --- */ static void eval_empty(TinyBlocks_Block* self, TinyBlocks* rt) { (void)rt; out_list(self, tb_json_array()); } /* "lista com A B C": campos vazios ficam de fora */ static void eval_of(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_JsonValue* arr = tb_json_array(); for (uint32_t i = 0; arr && i < 3; i++) { TinyBlocks_Value v = tb_block_get_input(self, rt, i); if (v.kind == TB_VALUE_NONE || (v.kind == TB_VALUE_TEXT && (!v.as.text || !v.as.text[0]))) continue; push_owned(arr, item_from_value(v)); } out_list(self, arr); } /* "números de A até B" (inclusivo, passo 1 ou -1) */ static void eval_range(TinyBlocks_Block* self, TinyBlocks* rt) { long long a = tb_block_input_integer(self, rt, 0, 0), b = tb_block_input_integer(self, rt, 1, 0); long long step = a <= b ? 1 : -1; long long n = (b - a) * step + 1; if (n > LIST_MAX_RANGE) n = LIST_MAX_RANGE; TinyBlocks_JsonValue* arr = tb_json_array(); for (long long i = 0; arr && i < n; i++) push_owned(arr, tb_json_number((double)(a + i * step))); out_list(self, arr); } static void eval_add(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_JsonValue* src = in_list(self, rt, 0); TinyBlocks_JsonValue* arr = list_copy(src, 0, UINT32_MAX); if (arr) push_owned(arr, in_item(self, rt, 1)); tb_json_release(src); out_list(self, arr); } /* "insira X na posição N de L": N fora da faixa encaixa no comeco/fim */ static void eval_insert(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_JsonValue* src = in_list(self, rt, 0); uint32_t n = src ? tb_json_array_length(src) : 0; long long pos = tb_block_input_integer(self, rt, 2, 0); if (pos < 1) pos = 1; if (pos > (long long)n + 1) pos = (long long)n + 1; TinyBlocks_JsonValue* arr = list_copy(src, 0, (uint32_t)(pos - 1)); if (arr) { push_owned(arr, in_item(self, rt, 1)); for (uint32_t i = (uint32_t)(pos - 1); i < n; i++) push_owned(arr, tb_json_array_get(src, i)); } tb_json_release(src); out_list(self, arr); } static void eval_remove(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_JsonValue* src = in_list(self, rt, 0); uint32_t n = src ? tb_json_array_length(src) : 0; long long k = tb_block_input_integer(self, rt, 1, 0); TinyBlocks_JsonValue* arr = tb_json_array(); for (uint32_t i = 0; arr && i < n; i++) if ((long long)i + 1 != k) push_owned(arr, tb_json_array_get(src, i)); tb_json_release(src); out_list(self, arr); } static void eval_replace(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_JsonValue* src = in_list(self, rt, 0); uint32_t n = src ? tb_json_array_length(src) : 0; long long k = tb_block_input_integer(self, rt, 1, 0); TinyBlocks_JsonValue* arr = tb_json_array(); for (uint32_t i = 0; arr && i < n; i++) { if ((long long)i + 1 == k) push_owned(arr, in_item(self, rt, 2)); else push_owned(arr, tb_json_array_get(src, i)); } tb_json_release(src); out_list(self, arr); } static void eval_item(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_JsonValue* src = in_list(self, rt, 0); long long k = tb_block_input_integer(self, rt, 1, 0); uint32_t n = src ? tb_json_array_length(src) : 0; TinyBlocks_JsonValue* it = (k >= 1 && k <= (long long)n) ? tb_json_array_get(src, (uint32_t)(k - 1)) : NULL; tb_block_set_output(self, 0, value_from_item(it)); if (it) tb_json_release(it); tb_json_release(src); self->dirty = false; } static void eval_length(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_JsonValue* src = in_list(self, rt, 0); out_int(self, src ? tb_json_array_length(src) : 0); tb_json_release(src); } /* posicao do item (1..n), 0 se nao estiver na lista */ static long long find_item(const TinyBlocks_JsonValue* src, const TinyBlocks_JsonValue* want) { uint32_t n = src ? tb_json_array_length(src) : 0; for (uint32_t i = 0; i < n; i++) { TinyBlocks_JsonValue* it = tb_json_array_get(src, i); bool eq = item_equal(it, want); if (it) tb_json_release(it); if (eq) return (long long)i + 1; } return 0; } static void eval_index_of(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_JsonValue* src = in_list(self, rt, 0); TinyBlocks_JsonValue* want = in_item(self, rt, 1); out_int(self, find_item(src, want)); tb_json_release(want); tb_json_release(src); } static void eval_contains(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_JsonValue* src = in_list(self, rt, 0); TinyBlocks_JsonValue* want = in_item(self, rt, 1); out_int(self, find_item(src, want) > 0 ? 1 : 0); tb_json_release(want); tb_json_release(src); } /* "itens A a B de L" (inclusivo, cortado aos limites) */ static void eval_slice(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_JsonValue* src = in_list(self, rt, 0); long long a = tb_block_input_integer(self, rt, 1, 0), b = tb_block_input_integer(self, rt, 2, 0); if (a < 1) a = 1; TinyBlocks_JsonValue* arr = (b >= a) ? list_copy(src, (uint32_t)(a - 1), (uint32_t)(b > UINT32_MAX ? UINT32_MAX : b)) : tb_json_array(); tb_json_release(src); out_list(self, arr); } static void eval_reverse(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_JsonValue* src = in_list(self, rt, 0); uint32_t n = src ? tb_json_array_length(src) : 0; TinyBlocks_JsonValue* arr = tb_json_array(); for (uint32_t i = n; arr && i > 0; i--) push_owned(arr, tb_json_array_get(src, i - 1)); tb_json_release(src); out_list(self, arr); } /* Ordem: numeros (pelo valor) antes de textos (sem diferenciar maiusculas) */ static int cmp_items(const void* pa, const void* pb) { const TinyBlocks_JsonValue* a = *(TinyBlocks_JsonValue* const*)pa; const TinyBlocks_JsonValue* b = *(TinyBlocks_JsonValue* const*)pb; double x, y; bool na = item_number(a, &x), nb = item_number(b, &y); if (na && nb) return x < y ? -1 : x > y ? 1 : 0; if (na != nb) return na ? -1 : 1; char sa[512], sb[512]; item_text(a, sa, sizeof(sa)); item_text(b, sb, sizeof(sb)); return strcasecmp(sa, sb); } static void eval_sort(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_JsonValue* src = in_list(self, rt, 0); uint32_t n = src ? tb_json_array_length(src) : 0; TinyBlocks_JsonValue** items = n ? malloc(n * sizeof(*items)) : NULL; TinyBlocks_JsonValue* arr = tb_json_array(); if (items) { for (uint32_t i = 0; i < n; i++) items[i] = tb_json_array_get(src, i); qsort(items, n, sizeof(*items), cmp_items); for (uint32_t i = 0; i < n; i++) { if (arr) push_owned(arr, items[i]); else if (items[i]) tb_json_release(items[i]); } free(items); } tb_json_release(src); out_list(self, arr); } static void eval_join(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_JsonValue* src = in_list(self, rt, 0); const char* sep = tb_block_input_text(self, rt, 1); /* (0) como separador vira "0" */ uint32_t n = src ? tb_json_array_length(src) : 0; size_t cap = 256, len = 0, sl = strlen(sep); char* out = malloc(cap); if (out) out[0] = 0; for (uint32_t i = 0; out && i < n; i++) { char t[512]; TinyBlocks_JsonValue* it = tb_json_array_get(src, i); item_text(it, t, sizeof(t)); if (it) tb_json_release(it); size_t tl = strlen(t), need = len + (i ? sl : 0) + tl + 1; if (need > cap) { while (cap < need) cap *= 2; char* no = realloc(out, cap); if (!no) break; out = no; } if (i) { memcpy(out + len, sep, sl); len += sl; } memcpy(out + len, t, tl + 1); len += tl; } tb_block_set_output(self, 0, tb_value_text(out ? out : "")); free(out); tb_json_release(src); self->dirty = false; } static void eval_sum(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_JsonValue* src = in_list(self, rt, 0); uint32_t n = src ? tb_json_array_length(src) : 0; double s = 0; for (uint32_t i = 0; i < n; i++) { TinyBlocks_JsonValue* it = tb_json_array_get(src, i); double d; if (item_number(it, &d)) s += d; if (it) tb_json_release(it); } tb_json_release(src); if (s == floor(s) && fabs(s) < 1e15) tb_block_set_output(self, 0, tb_value_integer((int64_t)s)); else tb_block_set_output(self, 0, tb_value_real(s)); self->dirty = false; } /* -- 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 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); if (v.kind == TB_VALUE_TEXT && v.as.text) snprintf(buf, cap, "%s", v.as.text); else if (v.kind == TB_VALUE_INTEGER) snprintf(buf, cap, "%lld", (long long)v.as.integer); else buf[0] = 0; return buf; } /* Lista com esse nome (criada vazia na primeira vez); NULL se o nome e vazio */ 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; NamedList* L = &s_named[s_named_n++]; snprintf(L->name, sizeof(L->name), "%s", name); L->arr = tb_json_array(); return L; } /* Troca o conteudo (toma a referencia de arr) */ static void named_set(NamedList* L, TinyBlocks_JsonValue* arr) { if (!L || !arr) { if (arr) tb_json_release(arr); return; } if (L->arr) tb_json_release(L->arr); L->arr = arr; } /* Posicao N (1..n); "ultimo"/"último"/"last" = n; fora da faixa = 0 */ static long long in_pos(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i, uint32_t n, bool allow_end) { TinyBlocks_Value v = tb_block_get_input(self, rt, i); if (v.kind == TB_VALUE_TEXT && v.as.text && (!strcasecmp(v.as.text, "ultimo") || !strcasecmp(v.as.text, "último") || !strcasecmp(v.as.text, "last"))) return allow_end ? (long long)n + 1 : (long long)n; long long k = tb_block_input_integer(self, rt, i, 0); long long hi = allow_end ? (long long)n + 1 : (long long)n; return (k >= 1 && k <= hi) ? k : 0; } /* Listas com nome sao objetos mutaveis, como no Scratch: "lista [nome]" e a * propria lista (sem copia) e adicionar / inserir / apagar / substituir mudam * a lista no lugar, em tempo constante (inserir/apagar: linear no que vem * depois). Ler um item de uma lista de 10 mil nao copia 10 mil itens. */ static void eval_var(TinyBlocks_Block* self, TinyBlocks* rt) { char nm[64]; NamedList* L = named_get(in_name(self, rt, 0, nm, sizeof(nm))); if (L) tb_json_retain(L->arr); out_list(self, L ? L->arr : tb_json_array()); } static void eval_push(TinyBlocks_Block* self, TinyBlocks* rt) { 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)); 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)); } 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); 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); if (k > 0) { TinyBlocks_JsonValue* it = in_item(self, rt, 0); tb_json_array_insert(L->arr, (uint32_t)(k - 1), it); tb_json_release(it); } } 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); if (k > 0) { TinyBlocks_JsonValue* it = in_item(self, rt, 2); tb_json_array_set(L->arr, (uint32_t)(k - 1), it); tb_json_release(it); } } self->dirty = false; } /* "mude a lista [nome] para [lista]": guarda o resultado de outro bloco */ static void eval_assign(TinyBlocks_Block* self, TinyBlocks* rt) { char nm[64]; 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; } /* "lista de (x)": texto "a, b" ou "[1,2]" vira lista, numero vira [n], objeto JSON vira a lista dos valores, lista continua lista */ static void eval_from(TinyBlocks_Block* self, TinyBlocks* rt) { TinyBlocks_Value v = tb_block_get_input(self, rt, 0); if (v.kind == TB_VALUE_JSON && v.as.json && tb_json_kind(v.as.json) == TB_JSON_OBJECT) { TinyBlocks_JsonValue* arr = tb_json_array(); for (uint32_t i = 0; arr && i < tb_json_object_length(v.as.json); i++) push_owned(arr, tb_json_object_get(v.as.json, tb_json_object_key_at(v.as.json, i))); out_list(self, arr); return; } out_list(self, tb_vector_from_value(v)); } /* -- Registro --- */ static bool reg(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib, const char* name, const TinyBlocks_SlotDesc* in, uint32_t ni, const TinyBlocks_SlotDesc* out, uint32_t no, void (*fn)(TinyBlocks_Block*, TinyBlocks*)) { TinyBlocks_TypeBlock* t = tb_type_block_create(rt, name, in, ni, out, no, fn); return t && tb_library_add(lib, &t->base); } static bool vector_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) { static const TinyBlocks_Category cat = { "Listas", {255/255.f, 102/255.f, 26/255.f, 1.f}, {230/255.f, 77/255.f, 0/255.f, 1.f}, }; tb_library_set_category(lib, &cat); TB_BLOCK_META("List.Empty", "eval_empty", "Uma lista sem itens"); TB_BLOCK_META("List.Of", "eval_of", "Lista com ate 3 itens (campos vazios ficam de fora)"); TB_BLOCK_META("List.Range", "eval_range", "Lista com os numeros de A ate B"); TB_BLOCK_META("List.Add", "eval_add", "Nova lista com o item no fim"); TB_BLOCK_META("List.Insert", "eval_insert", "Nova lista com o item na posicao N"); TB_BLOCK_META("List.Remove", "eval_remove", "Nova lista sem o item N"); TB_BLOCK_META("List.Replace", "eval_replace", "Nova lista com o item N trocado"); TB_BLOCK_META("List.Item", "eval_item", "O item N da lista (1 = primeiro)"); TB_BLOCK_META("List.Length", "eval_length", "Quantos itens a lista tem"); TB_BLOCK_META("List.IndexOf", "eval_index_of", "Posicao do item na lista (0 = nao tem)"); TB_BLOCK_META("List.Contains", "eval_contains", "Verdadeiro se a lista tiver o item"); TB_BLOCK_META("List.Slice", "eval_slice", "Pedaco da lista do item A ao item B"); TB_BLOCK_META("List.Reverse", "eval_reverse", "Lista de tras para frente"); TB_BLOCK_META("List.Sort", "eval_sort", "Lista em ordem (numeros, depois textos)"); TB_BLOCK_META("List.Join", "eval_join", "Texto com os itens separados pelo separador"); TB_BLOCK_META("List.Sum", "eval_sum", "Soma dos itens numericos"); TB_BLOCK_META("List.Var", "eval_var", "Conteudo da lista com esse nome"); TB_BLOCK_META("List.Push", "eval_push", "Adiciona o item no fim da lista com esse nome"); TB_BLOCK_META("List.DeleteAt", "eval_delete_at", "Apaga o item N (ou o ultimo) da lista com esse nome"); TB_BLOCK_META("List.Clear", "eval_clear", "Apaga todos os itens da lista com esse nome"); TB_BLOCK_META("List.InsertAt", "eval_insert_at", "Insere o item na posicao N da lista com esse nome"); TB_BLOCK_META("List.SetAt", "eval_set_at", "Troca o item N da lista com esse nome"); TB_BLOCK_META("List.Assign", "eval_assign", "Guarda uma lista (de outro bloco) com esse nome"); /* listas e itens chegam como "qualquer" (TB_VALUE_NONE): o campo digitado vira numero ou texto, e o texto pode ser "[1,2]" ou "a, b" */ static const TinyBlocks_SlotDesc l1[] = {{"lista", TB_VALUE_NONE}}; static const TinyBlocks_SlotDesc li[] = {{"lista", TB_VALUE_NONE}, {"item", TB_VALUE_NONE}}; static const TinyBlocks_SlotDesc ln[] = {{"lista", TB_VALUE_NONE}, {"n", TB_VALUE_INTEGER}}; static const TinyBlocks_SlotDesc lin[] = {{"lista", TB_VALUE_NONE}, {"item", TB_VALUE_NONE}, {"n", TB_VALUE_INTEGER}}; static const TinyBlocks_SlotDesc lni[] = {{"lista", TB_VALUE_NONE}, {"n", TB_VALUE_INTEGER}, {"item", TB_VALUE_NONE}}; static const TinyBlocks_SlotDesc lab[] = {{"lista", TB_VALUE_NONE}, {"de", TB_VALUE_INTEGER}, {"ate", TB_VALUE_INTEGER}}; static const TinyBlocks_SlotDesc lsep[] = {{"lista", TB_VALUE_NONE}, {"separador", TB_VALUE_TEXT}}; static const TinyBlocks_SlotDesc of_in[] = {{"a", TB_VALUE_NONE}, {"b", TB_VALUE_NONE}, {"c", TB_VALUE_NONE}}; static const TinyBlocks_SlotDesc rng[] = {{"de", TB_VALUE_INTEGER}, {"ate", TB_VALUE_INTEGER}}; static const TinyBlocks_SlotDesc list_out[] = {{"lista", TB_VALUE_JSON}}; /* listas com nome: posicao e TEXTO para aceitar "último" */ static const TinyBlocks_SlotDesc nm1[] = {{"nome", TB_VALUE_TEXT}}; static const TinyBlocks_SlotDesc it_nm[] = {{"item", TB_VALUE_NONE}, {"nome", TB_VALUE_TEXT}}; static const TinyBlocks_SlotDesc pos_nm[] = {{"n", TB_VALUE_NONE}, {"nome", TB_VALUE_TEXT}}; static const TinyBlocks_SlotDesc it_pos_nm[] = {{"item", TB_VALUE_NONE}, {"n", TB_VALUE_NONE}, {"nome", TB_VALUE_TEXT}}; static const TinyBlocks_SlotDesc pos_nm_it[] = {{"n", TB_VALUE_NONE}, {"nome", TB_VALUE_TEXT}, {"item", TB_VALUE_NONE}}; static const TinyBlocks_SlotDesc nm_list[] = {{"nome", TB_VALUE_TEXT}, {"lista", TB_VALUE_NONE}}; static const TinyBlocks_SlotDesc item_out[] = {{"item", TB_VALUE_NONE}}; static const TinyBlocks_SlotDesc int_out[] = {{"numero", TB_VALUE_INTEGER}}; static const TinyBlocks_SlotDesc num_out[] = {{"numero", TB_VALUE_NONE}}; static const TinyBlocks_SlotDesc text_out[] = {{"texto", TB_VALUE_TEXT}}; /* saida terminada em '?' = bloco booleano (hexagonal no editor) */ static const TinyBlocks_SlotDesc has_out[] = {{"contém?", TB_VALUE_INTEGER}}; return reg(rt, lib, "List.From", l1, 1, list_out, 1, eval_from) && reg(rt, lib, "List.Empty", NULL, 0, list_out, 1, eval_empty) && reg(rt, lib, "List.Of", of_in, 3, list_out, 1, eval_of) && reg(rt, lib, "List.Range", rng, 2, list_out, 1, eval_range) && reg(rt, lib, "List.Add", li, 2, list_out, 1, eval_add) && reg(rt, lib, "List.Insert", lin, 3, list_out, 1, eval_insert) && reg(rt, lib, "List.Remove", ln, 2, list_out, 1, eval_remove) && reg(rt, lib, "List.Replace", lni, 3, list_out, 1, eval_replace) && reg(rt, lib, "List.Item", ln, 2, item_out, 1, eval_item) && reg(rt, lib, "List.Length", l1, 1, int_out, 1, eval_length) && reg(rt, lib, "List.IndexOf", li, 2, int_out, 1, eval_index_of) && reg(rt, lib, "List.Contains", li, 2, has_out, 1, eval_contains) && reg(rt, lib, "List.Slice", lab, 3, list_out, 1, eval_slice) && reg(rt, lib, "List.Reverse", l1, 1, list_out, 1, eval_reverse) && reg(rt, lib, "List.Sort", l1, 1, list_out, 1, eval_sort) && reg(rt, lib, "List.Join", lsep, 2, text_out, 1, eval_join) && reg(rt, lib, "List.Sum", l1, 1, num_out, 1, eval_sum) && reg(rt, lib, "List.Var", nm1, 1, list_out, 1, eval_var) && reg(rt, lib, "List.Push", it_nm, 2, NULL, 0, eval_push) && reg(rt, lib, "List.DeleteAt", pos_nm, 2, NULL, 0, eval_delete_at) && reg(rt, lib, "List.Clear", nm1, 1, NULL, 0, eval_clear) && reg(rt, lib, "List.InsertAt", it_pos_nm, 3, NULL, 0, eval_insert_at) && reg(rt, lib, "List.SetAt", pos_nm_it, 3, NULL, 0, eval_set_at) && reg(rt, lib, "List.Assign", nm_list, 2, NULL, 0, eval_assign); } 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; } TINYBLOCKS_DEFINE_LIBRARY(vector, "tinyblocks-vector", "1.0.0", vector_init, vector_deinit) const TinyBlocks_LibraryDescriptor* tb_vector_get_library(void) { return &_tb_lib_desc_vector; }