Core: registro de metadados de blocos (tb_meta) + testes; metadados declarados nas libraries

This commit is contained in:
mguinhos committed 2026-10-20 22:07:40 -03:00
1 parent 22ae18f508
commit 036162280d
14 files changed
+397 -2

No files matched your search

+12 -2
View File
@@ -67,7 +67,8 @@ SRCS := src/tinyblocks.c \
src/tb_renderable_block.c \ src/tb_renderable_block.c \
src/tb_stage.c \ src/tb_stage.c \
src/tb_json_block.c \ src/tb_json_block.c \
src/tb_image.c src/tb_image.c \
src/tb_meta.c
OBJS := $(patsubst src/%.c,$(BUILD)/%.o,$(SRCS)) OBJS := $(patsubst src/%.c,$(BUILD)/%.o,$(SRCS))
OBJS_DBG := $(patsubst src/%.c,$(BUILD)/%-dbg.o,$(SRCS)) OBJS_DBG := $(patsubst src/%.c,$(BUILD)/%-dbg.o,$(SRCS))
@@ -92,7 +93,7 @@ LIB_LDFLAGS := -L$(ED_DIR)/build -L$(GX_DIR)/build -L$(SD_DIR)/build \
.PHONY: all help debug release static shared example sdl2 clean run python-lib mcp-lib json \ .PHONY: all help debug release static shared example sdl2 clean run python-lib mcp-lib json \
editor run-editor run-sdl2 mylib vector customappearance graphics sdl2-backend test-editor \ editor run-editor run-sdl2 mylib vector customappearance graphics sdl2-backend test-editor \
appdir appimage run-appimage icon \ appdir appimage run-appimage icon \
test-toml test-plugin test-blueprint test-base64 test-curves test-ppm test-bmp test-png test-image test-vg test-svg test-jpg \ test-toml test-plugin test-blueprint test-base64 test-curves test-meta test-ppm test-bmp test-png test-image test-vg test-svg test-jpg \
test-syntax test-regex test-markdown test-opentype \ test-syntax test-regex test-markdown test-opentype \
category-libs category-sos libs \ category-libs category-sos libs \
lib-events lib-control lib-operators lib-variables lib-appearance lib-pen lib-fs \ lib-events lib-control lib-operators lib-variables lib-appearance lib-pen lib-fs \
@@ -494,6 +495,15 @@ $(BUILD)/base64_test: tests/base64_test.c $(B64_DIR)/src/tb_base64.c static
test-base64: $(BUILD)/base64_test ## Roda o teste da base64 (ASan/UBSan) test-base64: $(BUILD)/base64_test ## Roda o teste da base64 (ASan/UBSan)
./$(BUILD)/base64_test ./$(BUILD)/base64_test
# -- Teste dos metadados de blocos (core tb_meta) ---
$(BUILD)/meta_test: tests/meta_test.c $(OP_DIR)/src/tb_operators.c static
@mkdir -p $(BUILD)
$(CC) $(CFLAGS_DBG) -I$(OP_DIR)/include $(OP_DIR)/src/tb_operators.c \
tests/meta_test.c -o $@ -Wl,--start-group $(LIB_A) -Wl,--end-group -lm
test-meta: $(BUILD)/meta_test ## Roda o teste dos metadados de blocos (ASan/UBSan)
./$(BUILD)/meta_test
# -- Teste da library curves --- # -- Teste da library curves ---
$(BUILD)/curves_test: tests/curves_test.c $(CV_DIR)/src/tb_curves.c static $(BUILD)/curves_test: tests/curves_test.c $(CV_DIR)/src/tb_curves.c static
@mkdir -p $(BUILD) @mkdir -p $(BUILD)
+47
View File
@@ -0,0 +1,47 @@
/*
* TinyBlocks - tb_meta.h
* Registro de metadados de blocos: opcionalmente uma library declara, para
* cada bloco, o nome da funcao C que o implementa, o arquivo-fonte e uma
* descricao. O editor usa isso no tooltip (Alt + hover) e para abrir o
* codigo-fonte na linha da funcao.
*/
#ifndef TINYBLOCKS_TB_META_H
#define TINYBLOCKS_TB_META_H
#include <stdbool.h>
#include <stddef.h>
#include <tinyblocks/tb_defs.h>
typedef struct {
const char* type_name; /* nome do bloco, ex.: "Mover" */
const char* func_name; /* funcao C que implementa o bloco */
const char* file; /* arquivo-fonte (__FILE__) */
const char* doc; /* descricao de uma linha (ou NULL) */
} TinyBlocks_BlockMeta;
/* Declara metadados (chame em <lib>_init()). `file` costuma ser __FILE__. */
TB_API void tb_meta_declare(const char* type_name, const char* func_name,
const char* file, const char* doc);
TB_API const TinyBlocks_BlockMeta* tb_meta_find(const char* type_name);
TB_API int tb_meta_count(void);
TB_API const TinyBlocks_BlockMeta* tb_meta_at(int i);
TB_API void tb_meta_clear(void);
/* Resolve o caminho real do arquivo-fonte (procura em libraries/ etc.).
* Devolve true se conseguiu abrir/ler o arquivo em `out`. */
TB_API bool tb_meta_resolve_file(const char* file, char* out, size_t cap);
/* Linha (1-based) da definicao de `m->func_name` em `m->file`, ou -1. */
TB_API int tb_meta_line(const TinyBlocks_BlockMeta* m);
/* Le `max_lines` linhas a partir de `line` (1-based) do arquivo resolvido.
* Devolve o numero de linhas lidas. `buf` recebe as linhas unidas por '\n'. */
TB_API int tb_meta_read_lines(const TinyBlocks_BlockMeta* m, int line,
int max_lines, char* buf, size_t cap);
/* Declara os metadados de um bloco. Coloque em <lib>_init(). */
#define TB_BLOCK_META(type_name, func_name, doc_str) \
tb_meta_declare((type_name), (func_name), __FILE__, (doc_str))
#endif /* TINYBLOCKS_TB_META_H */
+1
View File
@@ -36,6 +36,7 @@
#include "tb_vector_block.h" #include "tb_vector_block.h"
#include "tb_json_block.h" #include "tb_json_block.h"
#include "tb_image.h" #include "tb_image.h"
#include "tb_meta.h"
/* -- Runtime --- */ /* -- Runtime --- */
struct TinyBlocks { struct TinyBlocks {
@@ -36,6 +36,12 @@ static bool appearance_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
}; };
tb_library_set_category(lib, &cat); tb_library_set_category(lib, &cat);
/* Metadados dos blocos (tooltip do editor: Alt + hover). */
TB_BLOCK_META("IO.Print", "eval_print",
"Mostra um texto e um valor na saida");
TB_BLOCK_META("IO.PrintInt", "eval_print_int",
"Mostra um numero inteiro na saida");
static const TinyBlocks_SlotDesc print_in[] = {{"label", TB_VALUE_TEXT}, {"value", TB_VALUE_NONE}}; static const TinyBlocks_SlotDesc print_in[] = {{"label", TB_VALUE_TEXT}, {"value", TB_VALUE_NONE}};
static const TinyBlocks_SlotDesc one_int[] = {{"n", TB_VALUE_INTEGER}}; static const TinyBlocks_SlotDesc one_int[] = {{"n", TB_VALUE_INTEGER}};
@@ -26,6 +26,16 @@ static bool control_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
}; };
tb_library_set_category(lib, &cat); tb_library_set_category(lib, &cat);
/* Metadados dos blocos (tooltip do editor: Alt + hover). */
TB_BLOCK_META("Control.Repeat", "eval_noop",
"Repete o corpo do bloco o numero de vezes indicado");
TB_BLOCK_META("Control.RepeatUntil", "eval_noop",
"Repete o corpo enquanto a condicao for falsa");
TB_BLOCK_META("Control.If", "eval_noop",
"Executa o corpo se a condicao for verdadeira");
TB_BLOCK_META("Control.IfElse", "eval_noop",
"Executa o corpo 'entao' ou 'senao' conforme a condicao");
static const TinyBlocks_SlotDesc times_in[] = {{"vezes", TB_VALUE_INTEGER}}; static const TinyBlocks_SlotDesc times_in[] = {{"vezes", TB_VALUE_INTEGER}};
static const TinyBlocks_SlotDesc cond_in[] = {{"condição", TB_VALUE_INTEGER}}; static const TinyBlocks_SlotDesc cond_in[] = {{"condição", TB_VALUE_INTEGER}};
return reg(rt, lib, "Control.Repeat", times_in, 1) return reg(rt, lib, "Control.Repeat", times_in, 1)
@@ -503,6 +503,14 @@ static bool markdown_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
{ 55/255.f, 71/255.f, 79/255.f, 1.f}, { 55/255.f, 71/255.f, 79/255.f, 1.f},
}; };
tb_library_set_category(lib, &cat); tb_library_set_category(lib, &cat);
/* Metadados dos blocos (tooltip do editor: Alt + hover). */
TB_BLOCK_META("Markdown.ToHtml", "eval_html",
"Converte Markdown para HTML");
TB_BLOCK_META("Markdown.ToXml", "eval_xml",
"Converte Markdown para XML");
TB_BLOCK_META("Markdown.ToSvg", "eval_svg",
"Converte Markdown para uma imagem SVG");
static const TinyBlocks_SlotDesc in_md[] = {{"markdown", TB_VALUE_TEXT}}; static const TinyBlocks_SlotDesc in_md[] = {{"markdown", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc out_text[] = {{"texto", TB_VALUE_TEXT}}; static const TinyBlocks_SlotDesc out_text[] = {{"texto", TB_VALUE_TEXT}};
return reg(rt, lib, "Markdown.ToHtml", in_md, 1, out_text, 1, eval_html) return reg(rt, lib, "Markdown.ToHtml", in_md, 1, out_text, 1, eval_html)
@@ -363,6 +363,20 @@ static bool opentype_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
}; };
tb_library_set_category(lib, &cat); tb_library_set_category(lib, &cat);
/* Metadados dos blocos (tooltip do editor: Alt + hover). */
TB_BLOCK_META("OpenType.Load", "eval_load",
"Carrega uma fonte OpenType/TrueType");
TB_BLOCK_META("OpenType.UnitsPerEm", "eval_upem",
"Unidades por em da fonte");
TB_BLOCK_META("OpenType.GlyphCount", "eval_glyphs",
"Numero de glifos da fonte");
TB_BLOCK_META("OpenType.Advance", "eval_advance",
"Largura em pixels de um texto");
TB_BLOCK_META("OpenType.Outline", "eval_outline",
"Contornos de um caractere (pontos JSON)");
TB_BLOCK_META("OpenType.Render", "eval_render",
"Rasteriza um texto numa imagem");
static const TinyBlocks_SlotDesc load_in[] = {{"caminho", TB_VALUE_TEXT}}; static const TinyBlocks_SlotDesc load_in[] = {{"caminho", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc font_in[] = {{"fonte", TB_VALUE_REAL}}; static const TinyBlocks_SlotDesc font_in[] = {{"fonte", TB_VALUE_REAL}};
static const TinyBlocks_SlotDesc adv_in[] = {{"fonte", TB_VALUE_REAL}, {"texto", TB_VALUE_TEXT}, {"tamanho", TB_VALUE_REAL}}; static const TinyBlocks_SlotDesc adv_in[] = {{"fonte", TB_VALUE_REAL}, {"texto", TB_VALUE_TEXT}, {"tamanho", TB_VALUE_REAL}};
@@ -179,6 +179,48 @@ static bool operators_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
}; };
tb_library_set_category(lib, &cat); tb_library_set_category(lib, &cat);
/* Metadados dos blocos (tooltip do editor: Alt + hover). */
TB_BLOCK_META("Operators.Lt", "eval_op_lt",
"Verdadeiro se A for menor que B");
TB_BLOCK_META("Operators.Eq", "eval_op_eq",
"Verdadeiro se A for igual a B");
TB_BLOCK_META("Operators.Gt", "eval_op_gt",
"Verdadeiro se A for maior que B");
TB_BLOCK_META("Operators.And", "eval_op_and",
"Verdadeiro se ambos os lados forem verdadeiros");
TB_BLOCK_META("Operators.Or", "eval_op_or",
"Verdadeiro se ao menos um lado for verdadeiro");
TB_BLOCK_META("Operators.Not", "eval_op_not",
"Inverte o valor logico");
TB_BLOCK_META("Operators.Random", "eval_op_random",
"Numero inteiro aleatorio entre A e B");
TB_BLOCK_META("Math.Add", "eval_add",
"Soma dois numeros");
TB_BLOCK_META("Math.Sub", "eval_sub",
"Subtrai B de A");
TB_BLOCK_META("Math.Mul", "eval_mul",
"Multiplica dois numeros");
TB_BLOCK_META("Math.Div", "eval_div",
"Divide A por B");
TB_BLOCK_META("Math.Mod", "eval_mod",
"Resto da divisao de A por B");
TB_BLOCK_META("Math.Abs", "eval_abs",
"Valor absoluto de um numero");
TB_BLOCK_META("Math.Max", "eval_max",
"Maior entre dois numeros");
TB_BLOCK_META("Math.Min", "eval_min",
"Menor entre dois numeros");
TB_BLOCK_META("Text.Concat", "eval_concat",
"Junta dois textos");
TB_BLOCK_META("Text.Length", "eval_len",
"Numero de caracteres de um texto");
TB_BLOCK_META("Literal.Integer", "eval_lit_int",
"Valor inteiro digitado");
TB_BLOCK_META("Literal.Real", "eval_lit_real",
"Valor real (decimal) digitado");
TB_BLOCK_META("Literal.Text", "eval_lit_text",
"Valor de texto digitado");
static const TinyBlocks_SlotDesc any2[] = {{"a", TB_VALUE_NONE}, {"b", TB_VALUE_NONE}}; static const TinyBlocks_SlotDesc any2[] = {{"a", TB_VALUE_NONE}, {"b", TB_VALUE_NONE}};
static const TinyBlocks_SlotDesc bool2[] = {{"a", TB_VALUE_INTEGER}, {"b", TB_VALUE_INTEGER}}; static const TinyBlocks_SlotDesc bool2[] = {{"a", TB_VALUE_INTEGER}, {"b", TB_VALUE_INTEGER}};
static const TinyBlocks_SlotDesc bool1[] = {{"a", TB_VALUE_INTEGER}}; static const TinyBlocks_SlotDesc bool1[] = {{"a", TB_VALUE_INTEGER}};
+18
View File
@@ -70,6 +70,24 @@ static bool pen_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
}; };
tb_library_set_category(lib, &cat); tb_library_set_category(lib, &cat);
/* Metadados dos blocos (tooltip do editor: Alt + hover). */
TB_BLOCK_META("Stage.Clear", "eval_clear",
"Limpa o palco");
TB_BLOCK_META("Stage.Background", "eval_background",
"Define a cor de fundo do palco");
TB_BLOCK_META("Stage.PenColor", "eval_pen_color",
"Define a cor da caneta");
TB_BLOCK_META("Stage.PenSize", "eval_pen_size",
"Define a espessura da caneta");
TB_BLOCK_META("Stage.Line", "eval_line",
"Desenha uma linha entre dois pontos");
TB_BLOCK_META("Stage.Rect", "eval_rect",
"Desenha um retangulo");
TB_BLOCK_META("Stage.Circle", "eval_circle",
"Desenha um circulo");
TB_BLOCK_META("Stage.Text", "eval_text",
"Escreve um texto no palco");
static const TinyBlocks_SlotDesc rgb[] = {{"r", TB_VALUE_REAL}, {"g", TB_VALUE_REAL}, {"b", TB_VALUE_REAL}}; static const TinyBlocks_SlotDesc rgb[] = {{"r", TB_VALUE_REAL}, {"g", TB_VALUE_REAL}, {"b", TB_VALUE_REAL}};
static const TinyBlocks_SlotDesc size[] = {{"tamanho", TB_VALUE_REAL}}; static const TinyBlocks_SlotDesc size[] = {{"tamanho", TB_VALUE_REAL}};
static const TinyBlocks_SlotDesc line[] = {{"x1", TB_VALUE_REAL}, {"y1", TB_VALUE_REAL}, static const TinyBlocks_SlotDesc line[] = {{"x1", TB_VALUE_REAL}, {"y1", TB_VALUE_REAL},
+18
View File
@@ -292,6 +292,24 @@ static bool regex_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
}; };
tb_library_set_category(lib, &cat); tb_library_set_category(lib, &cat);
/* Metadados dos blocos (tooltip do editor: Alt + hover). */
TB_BLOCK_META("Regex.Test", "eval_test",
"Verdadeiro se o padrao ocorre no texto");
TB_BLOCK_META("Regex.Match", "eval_match",
"Verdadeiro se o padrao casa o texto inteiro");
TB_BLOCK_META("Regex.Find", "eval_find",
"Primeiro trecho do texto que casa o padrao");
TB_BLOCK_META("Regex.Group", "eval_group",
"Conteudo do grupo N do primeiro casamento");
TB_BLOCK_META("Regex.Count", "eval_count",
"Quantas vezes o padrao ocorre no texto");
TB_BLOCK_META("Regex.Groups", "eval_groups",
"Numero de grupos do padrao");
TB_BLOCK_META("Regex.Replace", "eval_replace",
"Substitui as ocorrencias ($1..$9 como retorno)");
TB_BLOCK_META("Regex.Split", "eval_split",
"Divide o texto pelo padrao (lista JSON)");
static const TinyBlocks_SlotDesc two[] = {{"texto", TB_VALUE_TEXT}, {"padrao", TB_VALUE_TEXT}}; static const TinyBlocks_SlotDesc two[] = {{"texto", TB_VALUE_TEXT}, {"padrao", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc three[] = {{"texto", TB_VALUE_TEXT}, {"padrao", TB_VALUE_TEXT}, {"com", TB_VALUE_TEXT}}; static const TinyBlocks_SlotDesc three[] = {{"texto", TB_VALUE_TEXT}, {"padrao", TB_VALUE_TEXT}, {"com", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc grp[] = {{"texto", TB_VALUE_TEXT}, {"padrao", TB_VALUE_TEXT}, {"grupo", TB_VALUE_REAL}}; static const TinyBlocks_SlotDesc grp[] = {{"texto", TB_VALUE_TEXT}, {"padrao", TB_VALUE_TEXT}, {"grupo", TB_VALUE_REAL}};
@@ -483,6 +483,14 @@ static bool syntax_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
}; };
tb_library_set_category(lib, &cat); tb_library_set_category(lib, &cat);
/* Metadados dos blocos (tooltip do editor: Alt + hover). */
TB_BLOCK_META("Syntax.Highlight", "eval_highlight",
"Realca a sintaxe de um texto (XML)");
TB_BLOCK_META("Syntax.Inline", "eval_inline",
"Realca uma unica linha de codigo (XML)");
TB_BLOCK_META("Syntax.Languages", "eval_languages",
"Lista as linguagens suportadas");
static const TinyBlocks_SlotDesc hl_in[] = {{"texto", TB_VALUE_TEXT}, {"linguagem", TB_VALUE_TEXT}}; static const TinyBlocks_SlotDesc hl_in[] = {{"texto", TB_VALUE_TEXT}, {"linguagem", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc out_text_s[] = {{"xml", TB_VALUE_TEXT}}; static const TinyBlocks_SlotDesc out_text_s[] = {{"xml", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc out_list_s[] = {{"linguagens", TB_VALUE_TEXT}}; static const TinyBlocks_SlotDesc out_list_s[] = {{"linguagens", TB_VALUE_TEXT}};
@@ -127,6 +127,14 @@ static bool variables_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
}; };
tb_library_set_category(lib, &cat); tb_library_set_category(lib, &cat);
/* Metadados dos blocos (tooltip do editor: Alt + hover). */
TB_BLOCK_META("Variables.Set", "eval_var_set",
"Define o valor de uma variavel");
TB_BLOCK_META("Variables.Change", "eval_var_change",
"Altera (incrementa) o valor de uma variavel");
TB_BLOCK_META("Variables.Get", "eval_var_get",
"Le o valor de uma variavel");
static const TinyBlocks_SlotDesc var_in[] = {{"variável", TB_VALUE_TEXT}}; static const TinyBlocks_SlotDesc var_in[] = {{"variável", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc var_set[] = {{"variável", TB_VALUE_TEXT}, {"valor", TB_VALUE_NONE}}; static const TinyBlocks_SlotDesc var_set[] = {{"variável", TB_VALUE_TEXT}, {"valor", TB_VALUE_NONE}};
static const TinyBlocks_SlotDesc any_out[] = {{"valor", TB_VALUE_NONE}}; static const TinyBlocks_SlotDesc any_out[] = {{"valor", TB_VALUE_NONE}};
+154
View File
@@ -0,0 +1,154 @@
/*
* TinyBlocks - tb_meta.c
* Registro global de metadados de blocos (arquivo/linha/funcao/doc) e
* utilitarios para localizar e ler o codigo-fonte correspondente.
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <ctype.h>
#include <tinyblocks/tinyblocks.h>
#include <tinyblocks/tb_meta.h>
static TinyBlocks_BlockMeta* s_meta = NULL;
static int s_n = 0, s_cap = 0;
void tb_meta_declare(const char* type_name, const char* func_name,
const char* file, const char* doc) {
if (!type_name) return;
for (int i = 0; i < s_n; i++) {
if (strcmp(s_meta[i].type_name, type_name) == 0) {
/* atualiza mantendo os ponteiros mais completos */
if (func_name) s_meta[i].func_name = func_name;
if (file) s_meta[i].file = file;
if (doc) s_meta[i].doc = doc;
return;
}
}
if (s_n == s_cap) {
int cap = s_cap ? s_cap * 2 : 64;
TinyBlocks_BlockMeta* p = (TinyBlocks_BlockMeta*)realloc(s_meta, (size_t)cap * sizeof(*p));
if (!p) return;
s_meta = p; s_cap = cap;
}
TinyBlocks_BlockMeta* m = &s_meta[s_n++];
m->type_name = type_name;
m->func_name = func_name;
m->file = file;
m->doc = doc;
}
const TinyBlocks_BlockMeta* tb_meta_find(const char* type_name) {
if (!type_name) return NULL;
for (int i = 0; i < s_n; i++)
if (strcmp(s_meta[i].type_name, type_name) == 0) return &s_meta[i];
return NULL;
}
int tb_meta_count(void) { return s_n; }
const TinyBlocks_BlockMeta* tb_meta_at(int i) {
if (i < 0 || i >= s_n) return NULL;
return &s_meta[i];
}
void tb_meta_clear(void) { s_n = 0; }
static bool try_file(const char* path, char* out, size_t cap) {
FILE* f = fopen(path, "r");
if (!f) return false;
fclose(f);
snprintf(out, cap, "%s", path);
return true;
}
bool tb_meta_resolve_file(const char* file, char* out, size_t cap) {
if (!file || !*file) return false;
if (try_file(file, out, cap)) return true;
/* tenta a partir da raiz do repositorio (../../) */
char cand[1200];
snprintf(cand, sizeof(cand), "../../%s", file);
if (try_file(cand, out, cap)) return true;
/* procura em libraries/<dir>/file */
const char* bases[] = { "libraries", "../libraries", "../../libraries", NULL };
for (int b = 0; bases[b]; b++) {
DIR* d = opendir(bases[b]);
if (!d) continue;
struct dirent* e;
while ((e = readdir(d))) {
if (e->d_name[0] == '.') continue;
snprintf(cand, sizeof(cand), "%s/%s/%s", bases[b], e->d_name, file);
if (try_file(cand, out, cap)) { closedir(d); return true; }
}
closedir(d);
}
return false;
}
/* Verifica se `line` contem `name` como identificador seguido de '(' ou de
* espaco+'('. Retorna true e o indice do inicio do nome. */
static bool line_has_func(const char* line, const char* name, const char** where) {
size_t nl = strlen(name);
const char* p = line;
while ((p = strstr(p, name))) {
bool ok_before = (p == line) || !(isalnum((unsigned char)p[-1]) || p[-1] == '_');
bool ok_after = !(isalnum((unsigned char)p[nl]) || p[nl] == '_');
if (ok_before && ok_after) {
const char* q = p + nl;
while (*q == ' ' || *q == '\t') q++;
if (*q == '(') { if (where) *where = p; return true; }
}
p += nl;
}
return false;
}
int tb_meta_line(const TinyBlocks_BlockMeta* m) {
if (!m || !m->file || !m->func_name) return -1;
char path[1200];
if (!tb_meta_resolve_file(m->file, path, sizeof(path))) return -1;
FILE* f = fopen(path, "r");
if (!f) return -1;
char line[2048];
int ln = 0, found = -1;
while (fgets(line, sizeof(line), f)) {
ln++;
const char* w = NULL;
if (line_has_func(line, m->func_name, &w)) {
found = ln;
/* definicao vs. declaracao: para na primeira ocorrencia */
break;
}
}
fclose(f);
return found;
}
int tb_meta_read_lines(const TinyBlocks_BlockMeta* m, int line,
int max_lines, char* buf, size_t cap) {
if (buf && cap) buf[0] = 0;
if (!m || !m->file || line < 1 || max_lines < 1) return 0;
char path[1200];
if (!tb_meta_resolve_file(m->file, path, sizeof(path))) return 0;
FILE* f = fopen(path, "r");
if (!f) return 0;
char tmp[2048];
int ln = 0, got = 0;
size_t used = 0;
while (fgets(tmp, sizeof(tmp), f)) {
ln++;
if (ln < line) continue;
if (got >= max_lines) break;
size_t tl = strlen(tmp);
while (tl && (tmp[tl-1] == '\n' || tmp[tl-1] == '\r')) tmp[--tl] = 0;
size_t need = tl + (got ? 1 : 0);
if (used + need + 1 >= cap) break;
if (got) buf[used++] = '\n';
memcpy(buf + used, tmp, tl);
used += tl; buf[used] = 0;
got++;
}
fclose(f);
return got;
}
+51
View File
@@ -0,0 +1,51 @@
/*
* meta_test.c - testa o registro de metadados de blocos (core tb_meta).
*/
#include <stdio.h>
#include <string.h>
#include <tinyblocks/tinyblocks.h>
#include <tb_operators.h>
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)
int main(void) {
TinyBlocks* rt;
tinyblocks_init(&rt, NULL);
tb_library_register(rt, tb_operators_get_library());
CHECK(tb_meta_count() > 10, "metadados declarados pela operators");
const TinyBlocks_BlockMeta* m = tb_meta_find("Math.Add");
CHECK(m != NULL, "acha Math.Add");
if (m) {
CHECK(m->func_name && !strcmp(m->func_name, "eval_add"), "funcao = eval_add");
CHECK(m->doc && strstr(m->doc, "Soma"), "doc menciona 'Soma'");
CHECK(m->file && strstr(m->file, "tb_operators.c"), "arquivo tb_operators.c");
}
const TinyBlocks_BlockMeta* lt = tb_meta_find("Operators.Lt");
CHECK(lt && lt->func_name && !strcmp(lt->func_name, "eval_op_lt"), "funcao de Operators.Lt");
CHECK(tb_meta_find("Nao.Existe") == NULL, "bloco desconhecido -> NULL");
CHECK(tb_meta_at(0) != NULL, "tb_meta_at(0)");
CHECK(tb_meta_at(99999) == NULL, "tb_meta_at fora do range -> NULL");
if (m) {
char path[1200];
CHECK(tb_meta_resolve_file(m->file, path, sizeof(path)), "resolve o arquivo-fonte");
int line = tb_meta_line(m);
CHECK(line > 0, "localiza a linha de eval_add");
if (line > 0) {
char buf[1024];
int got = tb_meta_read_lines(m, line, 1, buf, sizeof(buf));
CHECK(got == 1 && strstr(buf, "eval_add"), "le a linha da funcao");
}
}
tinyblocks_deinit(&rt);
if (fails) printf("\n%d FALHA(S)\n", fails);
else printf("\nTUDO OK\n");
return fails != 0;
}