"junte", "tamanho de" e o separador da lista aceitam qualquer valor
"junte [n=] (n)" saía "n=": o bloco só lia entradas de texto e tratava um número como vazio (o mesmo no "tamanho de", no literal de texto e no separador do "junte os itens"). Agora convertem como o resto (tb_block_input_text). "tamanho de" conta letras, não bytes (maçã = 4). Achado testando o servidor MCP do editor; test-stdlib cobre. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
11d6e5317b
commit
10a8099c46
4 files changed
+36
-19
No files matched your search
@@ -682,7 +682,7 @@ $(BUILD)/input_test: tests/input_test.c $(LIBDIR)/tinyblocks-input/src/tb_input.
|
|||||||
test-input: $(BUILD)/input_test ## Roda o teste de tinyblocks-input + estado de entrada do core (ASan/UBSan)
|
test-input: $(BUILD)/input_test ## Roda o teste de tinyblocks-input + estado de entrada do core (ASan/UBSan)
|
||||||
./$(BUILD)/input_test
|
./$(BUILD)/input_test
|
||||||
|
|
||||||
STD_DIRS := $(LIBDIR)/tinyblocks-string $(LIBDIR)/tinyblocks-number $(LIBDIR)/tinyblocks-vector
|
STD_DIRS := $(LIBDIR)/tinyblocks-string $(LIBDIR)/tinyblocks-number $(LIBDIR)/tinyblocks-vector $(LIBDIR)/tinyblocks-operators
|
||||||
STD_SRCS := $(foreach d,$(STD_DIRS),$(d)/src/$(patsubst tinyblocks-%,tb_%,$(notdir $(d))).c)
|
STD_SRCS := $(foreach d,$(STD_DIRS),$(d)/src/$(patsubst tinyblocks-%,tb_%,$(notdir $(d))).c)
|
||||||
|
|
||||||
$(BUILD)/stdlib_test: tests/stdlib_test.c $(STD_SRCS) static
|
$(BUILD)/stdlib_test: tests/stdlib_test.c $(STD_SRCS) static
|
||||||
|
|||||||
@@ -97,24 +97,23 @@ static void eval_min(TinyBlocks_Block* s, TinyBlocks* rt)
|
|||||||
{ int64_t a = ai(s,rt,0), b = ai(s,rt,1); tb_block_set_output(s, 0, tb_value_integer(a < b ? a : b)); }
|
{ int64_t a = ai(s,rt,0), b = ai(s,rt,1); tb_block_set_output(s, 0, tb_value_integer(a < b ? a : b)); }
|
||||||
|
|
||||||
/* == Texto === */
|
/* == Texto === */
|
||||||
|
/* Qualquer valor vira texto (junte [n=] (4) = "n=4") */
|
||||||
static void eval_concat(TinyBlocks_Block* s, TinyBlocks* rt) {
|
static void eval_concat(TinyBlocks_Block* s, TinyBlocks* rt) {
|
||||||
TinyBlocks_Value a = tb_block_get_input(s, rt, 0);
|
char* sa = strdup(tb_block_input_text(s, rt, 0)); /* a 2a entrada pode reavaliar a 1a */
|
||||||
TinyBlocks_Value b = tb_block_get_input(s, rt, 1);
|
const char* sb = tb_block_input_text(s, rt, 1);
|
||||||
const char* sa = a.kind == TB_VALUE_TEXT ? a.as.text : "";
|
size_t la = sa ? strlen(sa) : 0, lb = strlen(sb);
|
||||||
const char* sb = b.kind == TB_VALUE_TEXT ? b.as.text : "";
|
char* buf = (char*)malloc(la + lb + 1);
|
||||||
if (!sa) sa = "";
|
if (!buf) { free(sa); return; }
|
||||||
if (!sb) sb = "";
|
if (sa) memcpy(buf, sa, la);
|
||||||
size_t n = strlen(sa) + strlen(sb) + 1;
|
memcpy(buf + la, sb, lb + 1);
|
||||||
char* buf = (char*)malloc(n);
|
free(sa);
|
||||||
if (!buf) return;
|
|
||||||
memcpy(buf, sa, strlen(sa));
|
|
||||||
memcpy(buf + strlen(sa), sb, strlen(sb) + 1);
|
|
||||||
TinyBlocks_Value v; v.kind = TB_VALUE_TEXT; v.as.text = buf;
|
TinyBlocks_Value v; v.kind = TB_VALUE_TEXT; v.as.text = buf;
|
||||||
tb_block_set_output(s, 0, v); /* set_output toma ownership */
|
tb_block_set_output(s, 0, v); /* set_output toma ownership */
|
||||||
}
|
}
|
||||||
|
/* Letras, nao bytes ("maçã" tem 4), como no Scratch */
|
||||||
static void eval_len(TinyBlocks_Block* s, TinyBlocks* rt) {
|
static void eval_len(TinyBlocks_Block* s, TinyBlocks* rt) {
|
||||||
TinyBlocks_Value a = tb_block_get_input(s, rt, 0);
|
int64_t n = 0;
|
||||||
int64_t n = (a.kind == TB_VALUE_TEXT && a.as.text) ? (int64_t)strlen(a.as.text) : 0;
|
for (const char* p = tb_block_input_text(s, rt, 0); *p; p++) if ((*p & 0xC0) != 0x80) n++;
|
||||||
tb_block_set_output(s, 0, tb_value_integer(n));
|
tb_block_set_output(s, 0, tb_value_integer(n));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,9 +123,7 @@ static void eval_lit_int(TinyBlocks_Block* s, TinyBlocks* rt)
|
|||||||
static void eval_lit_real(TinyBlocks_Block* s, TinyBlocks* rt)
|
static void eval_lit_real(TinyBlocks_Block* s, TinyBlocks* rt)
|
||||||
{ tb_block_set_output(s, 0, tb_value_clone(tb_block_get_input(s, rt, 0))); }
|
{ tb_block_set_output(s, 0, tb_value_clone(tb_block_get_input(s, rt, 0))); }
|
||||||
static void eval_lit_text(TinyBlocks_Block* s, TinyBlocks* rt) {
|
static void eval_lit_text(TinyBlocks_Block* s, TinyBlocks* rt) {
|
||||||
TinyBlocks_Value v = tb_block_get_input(s, rt, 0);
|
tb_block_set_output(s, 0, tb_value_text(tb_block_input_text(s, rt, 0)));
|
||||||
const char* t = (v.kind == TB_VALUE_TEXT && v.as.text) ? v.as.text : "";
|
|
||||||
tb_block_set_output(s, 0, tb_value_text(t));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* == Registro === */
|
/* == Registro === */
|
||||||
|
|||||||
@@ -365,8 +365,7 @@ static void eval_sort(TinyBlocks_Block* self, TinyBlocks* rt) {
|
|||||||
|
|
||||||
static void eval_join(TinyBlocks_Block* self, TinyBlocks* rt) {
|
static void eval_join(TinyBlocks_Block* self, TinyBlocks* rt) {
|
||||||
TinyBlocks_JsonValue* src = in_list(self, rt, 0);
|
TinyBlocks_JsonValue* src = in_list(self, rt, 0);
|
||||||
TinyBlocks_Value sv = tb_block_get_input(self, rt, 1);
|
const char* sep = tb_block_input_text(self, rt, 1); /* (0) como separador vira "0" */
|
||||||
const char* sep = (sv.kind == TB_VALUE_TEXT && sv.as.text) ? sv.as.text : "";
|
|
||||||
uint32_t n = src ? tb_json_array_length(src) : 0;
|
uint32_t n = src ? tb_json_array_length(src) : 0;
|
||||||
size_t cap = 256, len = 0, sl = strlen(sep);
|
size_t cap = 256, len = 0, sl = strlen(sep);
|
||||||
char* out = malloc(cap);
|
char* out = malloc(cap);
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
#include <tb_string.h>
|
#include <tb_string.h>
|
||||||
#include <tb_number.h>
|
#include <tb_number.h>
|
||||||
#include <tb_vector.h>
|
#include <tb_vector.h>
|
||||||
|
#include <tb_operators.h>
|
||||||
|
|
||||||
static int fails = 0;
|
static int fails = 0;
|
||||||
#define CHECK(c, m) do { if (c) printf("ok %s\n", m); else { printf("FAIL %s\n", m); fails++; } } while (0)
|
#define CHECK(c, m) do { if (c) printf("ok %s\n", m); else { printf("FAIL %s\n", m); fails++; } } while (0)
|
||||||
@@ -77,6 +78,26 @@ int main(void) {
|
|||||||
TinyBlocks_LibraryBlock* ls = tb_library_register(rt, tb_string_get_library());
|
TinyBlocks_LibraryBlock* ls = tb_library_register(rt, tb_string_get_library());
|
||||||
TinyBlocks_LibraryBlock* ln = tb_library_register(rt, tb_number_get_library());
|
TinyBlocks_LibraryBlock* ln = tb_library_register(rt, tb_number_get_library());
|
||||||
TinyBlocks_LibraryBlock* lv = tb_library_register(rt, tb_vector_get_library());
|
TinyBlocks_LibraryBlock* lv = tb_library_register(rt, tb_vector_get_library());
|
||||||
|
tb_library_register(rt, tb_operators_get_library());
|
||||||
|
|
||||||
|
/* entradas que nao sao texto viram texto (junte [n=] (4) = "n=4") */
|
||||||
|
{
|
||||||
|
TinyBlocks_Block* j = tb_block_create(rt, tb_rt_find_type(rt, "Text.Concat"));
|
||||||
|
TinyBlocks_Value a = tb_value_text("n="), b = tb_value_integer(4);
|
||||||
|
tb_block_set_default(j, 0, a);
|
||||||
|
tb_block_set_default(j, 1, b);
|
||||||
|
tb_value_free(&a);
|
||||||
|
tb_block_eval(j, rt);
|
||||||
|
CHECK(is_text(&j->outputs[0], "n=4"), "junte texto com número");
|
||||||
|
tb_block_destroy(j, rt);
|
||||||
|
TinyBlocks_Block* l = tb_block_create(rt, tb_rt_find_type(rt, "Text.Length"));
|
||||||
|
TinyBlocks_Value n = tb_value_integer(1234);
|
||||||
|
tb_block_set_default(l, 0, n);
|
||||||
|
tb_block_eval(l, rt);
|
||||||
|
CHECK(is_int(&l->outputs[0], 4), "tamanho de um número (1234 tem 4)");
|
||||||
|
CHECK(is_int(run("Text.Length", "maçã", NULL, NULL), 4), "tamanho de conta letras (maçã = 4)");
|
||||||
|
tb_block_destroy(l, rt);
|
||||||
|
}
|
||||||
CHECK(ls && ln && lv, "libraries string, number e vector registradas");
|
CHECK(ls && ln && lv, "libraries string, number e vector registradas");
|
||||||
CHECK(!strcmp(tb_library_category(ls)->name, "Texto") &&
|
CHECK(!strcmp(tb_library_category(ls)->name, "Texto") &&
|
||||||
!strcmp(tb_library_category(ln)->name, "Números") &&
|
!strcmp(tb_library_category(ln)->name, "Números") &&
|
||||||
|
|||||||
Reference in new issue
Block a user