Libraries os, string, number e vector (listas); pastas e booleanos no fs
- tinyblocks-fs: itens da pasta (lista), quantidade/item N da pasta e os booleanos "é arquivo?", "é pasta?" e "existe?". - tinyblocks-os (categoria Sistema): sistema operacional, distribuição, versão do kernel, arquitetura, computador, usuário, pastas, CPUs, memória, tempo ligado e "o sistema é [ ]?". - tinyblocks-string (Texto): maiúsculas/minúsculas, trecho, letra N, posição, troca, apara, repete, inverte, divide em lista e testes; conta letras UTF-8. - tinyblocks-number (Números): reais, potência, raiz, arredondamentos, trigonometria em graus, logaritmos, pi, aleatório, limite, formatação, par/primo/inteiro. - tinyblocks-vector (Listas, blocos List.*): listas JSON imutáveis com adicionar, inserir, remover, trocar, item N, posição, fatia, ordenar, juntar, soma; campos aceitam "[1,2]" ou "a, b". O exemplo de desenho vetorial passa a se chamar tinyblocks-vector-draw. - Editor: saída única cujo nome termina em '?' vira bloco booleano (hexágono); listas JSON aparecem como texto no "escreva"; rótulos PT. - Testes: make test-os e make test-stdlib. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f0abc7593a
commit
33705b2903
25 files changed
+2262
-9
No files matched your search
@@ -0,0 +1,516 @@
|
||||
/*
|
||||
* 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).
|
||||
*
|
||||
* Comparacao de itens como no Scratch: numeros pelo valor ("2" == 2.0) e
|
||||
* textos sem diferenciar maiusculas.
|
||||
*/
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include <stdint.h>
|
||||
#include <ctype.h>
|
||||
#include <math.h>
|
||||
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
#include <tinyblocks/tb_json_block.h>
|
||||
#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) */
|
||||
static void item_text(const TinyBlocks_JsonValue* j, char* out, size_t cap) {
|
||||
out[0] = 0;
|
||||
if (!j) return;
|
||||
switch (tb_json_kind(j)) {
|
||||
case TB_JSON_NUMBER: {
|
||||
double d = tb_json_as_number(j, 0);
|
||||
if (d == floor(d) && fabs(d) < 1e15) snprintf(out, cap, "%lld", (long long)d);
|
||||
else snprintf(out, cap, "%g", d);
|
||||
break;
|
||||
}
|
||||
case TB_JSON_STRING: snprintf(out, cap, "%s", tb_json_as_string(j)); break;
|
||||
case TB_JSON_BOOL: snprintf(out, cap, "%s", tb_json_as_bool(j, false) ? "1" : "0"); break;
|
||||
case TB_JSON_NULL: break;
|
||||
default: {
|
||||
char* s = tb_json_stringify(j);
|
||||
snprintf(out, cap, "%s", s ? s : "");
|
||||
free(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 long long in_int(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) {
|
||||
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
|
||||
if (v.kind == TB_VALUE_INTEGER) return v.as.integer;
|
||||
if (v.kind == TB_VALUE_REAL) return (long long)llround(v.as.real);
|
||||
if (v.kind == TB_VALUE_TEXT && v.as.text) return strtoll(v.as.text, NULL, 10);
|
||||
return 0;
|
||||
}
|
||||
|
||||
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 = in_int(self, rt, 0), b = in_int(self, rt, 1);
|
||||
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 = in_int(self, rt, 2);
|
||||
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 = in_int(self, rt, 1);
|
||||
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 = in_int(self, rt, 1);
|
||||
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 = in_int(self, rt, 1);
|
||||
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 = in_int(self, rt, 1), b = in_int(self, rt, 2);
|
||||
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);
|
||||
TinyBlocks_Value sv = tb_block_get_input(self, rt, 1);
|
||||
const char* sep = (sv.kind == TB_VALUE_TEXT && sv.as.text) ? sv.as.text : "";
|
||||
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;
|
||||
}
|
||||
|
||||
/* -- 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");
|
||||
|
||||
/* 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}};
|
||||
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.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);
|
||||
}
|
||||
|
||||
static void vector_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
(void)rt; (void)lib;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
Reference in new issue
Block a user