Adiciona libraries toml/fs, metadados .toml e build por library
- tinyblocks-toml: lexer + parser TOML (include/lexeme.h, include/ast.h, src/lexer.c, src/parser.c) e bloco Toml.Get. - tinyblocks-fs: categoria "Arquivos" com FS.Open/Read/Write/Append/Tell/Close. - library.toml em cada library com metadados (categoria, cor, descricao) e receita de build, sem precisar embutir isso no .so. - Makefiles proprios por library com build/ local; Makefile raiz delega. - Palco (tb_stage) movido para o core; pen deixa de depender do editor, o que torna os .so arrastaveis. - Editor linka com -rdynamic para resolver simbolos do core em .so via dlopen. - Testes: test-toml (parser) e test-plugin (dlopen de .so standalone).
This commit is contained in:
1 parent
90d47bf8a0
commit
dc23b92d15
48 files changed
+2405
-744
No files matched your search
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* Teste de plugin: prova que um library .so e standalone e cargavel via
|
||||
* dlopen, resolvendo os simbolos do core exportados pelo binario (-rdynamic).
|
||||
*
|
||||
* Uso: plugin_test <caminho.so>
|
||||
*/
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <dlfcn.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <tinyblocks/tinyblocks.h>
|
||||
#include <tinyblocks/tb_block.h>
|
||||
#include <tinyblocks/tb_type_block.h>
|
||||
#include <tinyblocks/tb_library_block.h>
|
||||
|
||||
typedef const TinyBlocks_LibraryDescriptor* (*GetLibFn)(void);
|
||||
|
||||
static int fails = 0;
|
||||
#define CHECK(cond, msg) do { if (cond) printf("ok %s\n", msg); else { printf("FAIL %s\n", msg); fails++; } } while (0)
|
||||
|
||||
static TinyBlocks_Block* mk(TinyBlocks* rt, const char* type) {
|
||||
TinyBlocks_TypeBlock* t = tb_type_block_find(rt, type);
|
||||
if (!t) { printf("FAIL tipo %s nao encontrado\n", type); fails++; return NULL; }
|
||||
return tb_block_create(rt, t);
|
||||
}
|
||||
|
||||
static void set_text(TinyBlocks_Block* b, uint32_t i, const char* s) {
|
||||
TinyBlocks_Value v = tb_value_text(s);
|
||||
tb_block_set_default(b, i, v);
|
||||
tb_value_free(&v);
|
||||
}
|
||||
static void set_int(TinyBlocks_Block* b, uint32_t i, int64_t n) {
|
||||
TinyBlocks_Value v = tb_value_integer(n);
|
||||
tb_block_set_default(b, i, v);
|
||||
tb_value_free(&v);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
const char* so = argc > 1 ? argv[1] : "libraries/tinyblocks-fs/build/libtinyblocks-fs.so";
|
||||
char path[256];
|
||||
snprintf(path, sizeof(path), "/tmp/tb_plugin_%d.txt", (int)getpid());
|
||||
unlink(path);
|
||||
|
||||
TinyBlocks* rt = NULL;
|
||||
CHECK(tinyblocks_init(&rt, NULL), "runtime inicializa");
|
||||
|
||||
void* dl = dlopen(so, RTLD_NOW | RTLD_LOCAL);
|
||||
if (!dl) { printf("FAIL dlopen %s: %s\n", so, dlerror()); return 1; }
|
||||
printf("ok dlopen %s\n", so);
|
||||
|
||||
GetLibFn get = (GetLibFn)(uintptr_t)dlsym(dl, "tinyblocks_get_library");
|
||||
CHECK(get != NULL, "simbolo tinyblocks_get_library presente");
|
||||
if (!get) return 1;
|
||||
|
||||
const TinyBlocks_LibraryDescriptor* desc = get();
|
||||
CHECK(desc && desc->name && strstr(desc->name, "fs") != NULL, "descriptor da library");
|
||||
CHECK(tb_library_register(rt, desc) != NULL, "library registra no runtime");
|
||||
|
||||
/* Anexa duas linhas e le de volta. */
|
||||
TinyBlocks_Block* app = mk(rt, "FS.Append");
|
||||
if (app) {
|
||||
set_text(app, 0, path);
|
||||
set_text(app, 1, "linha-1\n");
|
||||
tb_block_eval(app, rt);
|
||||
set_text(app, 1, "linha-2\n");
|
||||
tb_block_eval(app, rt);
|
||||
}
|
||||
|
||||
TinyBlocks_Block* op = mk(rt, "FS.Open");
|
||||
int64_t handle = 0;
|
||||
if (op) {
|
||||
set_text(op, 0, path);
|
||||
tb_block_eval(op, rt);
|
||||
handle = op->outputs[0].as.integer;
|
||||
}
|
||||
CHECK(handle >= 1, "FS.Open devolve handle");
|
||||
|
||||
TinyBlocks_Block* rd = mk(rt, "FS.Read");
|
||||
if (rd) {
|
||||
set_int(rd, 0, handle);
|
||||
set_int(rd, 1, 0);
|
||||
tb_block_eval(rd, rt);
|
||||
const char* got = rd->outputs[0].as.text;
|
||||
CHECK(got && strcmp(got, "linha-1\nlinha-2\n") == 0, "FS.Read le o conteudo");
|
||||
if (got && strcmp(got, "linha-1\nlinha-2\n") != 0) printf(" obtido=[%s]\n", got);
|
||||
}
|
||||
|
||||
TinyBlocks_Block* tl = mk(rt, "FS.Tell");
|
||||
if (tl) {
|
||||
set_int(tl, 0, handle);
|
||||
tb_block_eval(tl, rt);
|
||||
CHECK(tl->outputs[0].as.integer == 16, "FS.Tell reporta 16 bytes");
|
||||
}
|
||||
|
||||
TinyBlocks_Block* cl = mk(rt, "FS.Close");
|
||||
if (cl) { set_int(cl, 0, handle); tb_block_eval(cl, rt); }
|
||||
|
||||
unlink(path);
|
||||
dlclose(dl);
|
||||
tinyblocks_deinit(&rt);
|
||||
printf(fails ? "\n%d FALHA(S)\n" : "\nTUDO OK\n", fails);
|
||||
return fails != 0;
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* Teste do parser TOML (tinyblocks-toml): valida contra os library.toml do
|
||||
* repo e alguns casos sintaticos.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <glob.h>
|
||||
#include <ast.h>
|
||||
|
||||
static int fails = 0;
|
||||
#define CHECK(cond, msg) do { if (cond) printf("ok %s\n", msg); else { printf("FAIL %s\n", msg); fails++; } } while (0)
|
||||
|
||||
static void test_library_files(void) {
|
||||
glob_t g;
|
||||
if (glob("libraries/*/library.toml", 0, NULL, &g) != 0) {
|
||||
printf("FAIL nenhum library.toml encontrado\n"); fails++; return;
|
||||
}
|
||||
int n = 0;
|
||||
for (size_t i = 0; i < g.gl_pathc; i++) {
|
||||
TomlLexError err;
|
||||
TomlDoc* doc = toml_parse_file(g.gl_pathv[i], &err);
|
||||
if (!doc) {
|
||||
printf("FAIL parse %s: %s (%d:%d)\n", g.gl_pathv[i], err.msg, err.line, err.col);
|
||||
fails++; continue;
|
||||
}
|
||||
const char* name = toml_as_string(toml_path_get(doc->root, "library.name"), NULL);
|
||||
const char* cat = toml_as_string(toml_path_get(doc->root, "library.category"), NULL);
|
||||
const char* so = toml_as_string(toml_path_get(doc->root, "build.shared"), NULL);
|
||||
if (!name || !cat || !so) {
|
||||
printf("FAIL metadados incompletos em %s\n", g.gl_pathv[i]);
|
||||
fails++;
|
||||
} else {
|
||||
n++;
|
||||
}
|
||||
toml_free(doc);
|
||||
}
|
||||
globfree(&g);
|
||||
CHECK(n >= 13, "todos os library.toml tem name/category/build.shared");
|
||||
printf(" %d arquivos .toml validados\n", n);
|
||||
}
|
||||
|
||||
static void test_syntax(void) {
|
||||
const char* src =
|
||||
"# comentario\n"
|
||||
"title = \"TOML \\u00e9 \\\"legal\\\"\"\n"
|
||||
"lit = 'C:\\Users\\x'\n"
|
||||
"int = 1_000\n"
|
||||
"hex = 0xDEAD_BEEF\n"
|
||||
"flt = 3.14e2\n"
|
||||
"b = true\n"
|
||||
"arr = [1, 2, 3, \"quatro\"]\n"
|
||||
"date = 1979-05-27T07:32:00Z\n"
|
||||
"dotted.key = \"v\"\n"
|
||||
"inline = { x = 1, y = 2 }\n"
|
||||
"\n"
|
||||
"[servidor]\n"
|
||||
"host = \"localhost\"\n"
|
||||
"porta = 8080\n"
|
||||
"\n"
|
||||
"[[itens]]\n"
|
||||
"nome = \"a\"\n"
|
||||
"[[itens]]\n"
|
||||
"nome = \"b\"\n";
|
||||
TomlLexError err;
|
||||
TomlDoc* doc = toml_parse(src, &err);
|
||||
CHECK(doc != NULL, "parser aceita TOML variado");
|
||||
if (!doc) { printf(" erro: %s (%d:%d)\n", err.msg, err.line, err.col); return; }
|
||||
|
||||
CHECK(strcmp(toml_as_string(toml_path_get(doc->root, "title"), ""), "TOML \xc3\xa9 \"legal\"") == 0,
|
||||
"string basica com escapes/unicode");
|
||||
CHECK(strcmp(toml_as_string(toml_path_get(doc->root, "lit"), ""), "C:\\Users\\x") == 0,
|
||||
"string literal");
|
||||
CHECK(toml_as_int(toml_path_get(doc->root, "int"), 0) == 1000, "inteiro com underline");
|
||||
CHECK(toml_as_int(toml_path_get(doc->root, "hex"), 0) == 0xDEADBEEF, "inteiro hexadecimal");
|
||||
CHECK(toml_as_float(toml_path_get(doc->root, "flt"), 0) == 314.0, "float com expoente");
|
||||
CHECK(toml_as_bool(toml_path_get(doc->root, "b"), false) == true, "booleano");
|
||||
CHECK(toml_array_count(toml_path_get(doc->root, "arr")) == 4, "array com 4 itens");
|
||||
CHECK(strcmp(toml_as_string(toml_array_get(toml_path_get(doc->root, "arr"), 3), ""), "quatro") == 0,
|
||||
"item de array");
|
||||
CHECK(strcmp(toml_as_string(toml_path_get(doc->root, "date"), ""), "1979-05-27T07:32:00Z") == 0,
|
||||
"data/hora");
|
||||
CHECK(strcmp(toml_as_string(toml_path_get(doc->root, "dotted.key"), ""), "v") == 0,
|
||||
"chave pontuada");
|
||||
CHECK(strcmp(toml_as_string(toml_path_get(doc->root, "servidor.host"), ""), "localhost") == 0,
|
||||
"tabela [servidor]");
|
||||
CHECK(toml_as_int(toml_path_get(doc->root, "servidor.porta"), 0) == 8080, "valor de tabela");
|
||||
CHECK(toml_array_count(toml_path_get(doc->root, "itens")) == 2, "array de tabelas");
|
||||
CHECK(strcmp(toml_as_string(toml_path_get(doc->root, "itens.1.nome"), ""), "b") == 0,
|
||||
"segundo item do array de tabelas");
|
||||
CHECK(toml_as_int(toml_path_get(doc->root, "inline.x"), 0) == 1 &&
|
||||
toml_as_int(toml_path_get(doc->root, "inline.y"), 0) == 2, "tabela inline");
|
||||
toml_free(doc);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
test_syntax();
|
||||
test_library_files();
|
||||
printf(fails ? "\n%d FALHA(S)\n" : "\nTUDO OK\n", fails);
|
||||
return fails != 0;
|
||||
}
|
||||
Reference in new issue
Block a user