Adiciona tinyblocks-blueprint e libraries json/xml/csv; abrir/salvar/exportar projeto
- tinyblocks-blueprint: dona do estado de edicao (abas, pool de blocos, vinculos parent/prel/next/sub/args, blocos compostos e variaveis) e da serializacao. JSON nativo (to/from) usando a AST JSON do core e export JSON/XML/CSV. Testado com round-trip (test-blueprint). - tinyblocks-json: lexer + parser + emissor JSON independentes. - tinyblocks-xml: lexer + parser de XML -> AST JSON do core + emissor. - tinyblocks-csv: divisor de linhas/campos + parser/emissor sobre a AST JSON. - Editor: botoes Abrir / Salvar / Exportar JSON XML CSV no menu; converte Ed <-> TbBlueprint; barra de caminho com modo; .json pode ser arrastado. - Variaveis: tb_variables_clear e tb_variables_set_text. - Harness: teste de salvar/abrir projeto; tests/blueprint_test.c.
This commit is contained in:
1 parent
dc23b92d15
commit
1199d2d7c7
39 files changed
+3462
-24
No files matched your search
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* Teste da tinyblocks-blueprint: modelo + serializacao JSON nativa e export
|
||||
* XML/CSV.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <blueprint.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 TbBlueprint* make_example(void) {
|
||||
TbBlueprint* bp = tb_blueprint_new();
|
||||
int t0 = tb_blueprint_add_tab(bp, "Codigo", false);
|
||||
TbBpTab* T = tb_blueprint_tab(bp, t0);
|
||||
|
||||
int hat = tb_blueprint_alloc_block(T);
|
||||
snprintf(T->blocks[hat].type, sizeof(T->blocks[hat].type), "Events.WhenRun");
|
||||
T->blocks[hat].x = 40; T->blocks[hat].y = 40;
|
||||
tb_blueprint_top_add(T, hat);
|
||||
|
||||
int set = tb_blueprint_alloc_block(T);
|
||||
snprintf(T->blocks[set].type, sizeof(T->blocks[set].type), "Variables.Set");
|
||||
snprintf(T->blocks[set].in_text[0], 64, "x");
|
||||
snprintf(T->blocks[set].in_text[1], 64, "10");
|
||||
tb_blueprint_link(T, hat, TB_BP_REL_NEXT, set);
|
||||
|
||||
int rep = tb_blueprint_alloc_block(T);
|
||||
snprintf(T->blocks[rep].type, sizeof(T->blocks[rep].type), "Control.Repeat");
|
||||
snprintf(T->blocks[rep].in_text[0], 64, "5");
|
||||
tb_blueprint_link(T, set, TB_BP_REL_NEXT, rep);
|
||||
|
||||
int chg = tb_blueprint_alloc_block(T);
|
||||
snprintf(T->blocks[chg].type, sizeof(T->blocks[chg].type), "Variables.Change");
|
||||
snprintf(T->blocks[chg].in_text[0], 64, "x");
|
||||
snprintf(T->blocks[chg].in_text[1], 64, "1");
|
||||
tb_blueprint_link(T, rep, TB_BP_REL_SUB0, chg);
|
||||
|
||||
/* bloco composto */
|
||||
TbBpUserBlock* d = &bp->user_blocks[bp->user_block_n++];
|
||||
memset(d, 0, sizeof(*d));
|
||||
snprintf(d->name, sizeof(d->name), "dobro");
|
||||
d->color_idx = 2;
|
||||
d->input_count = 1;
|
||||
snprintf(d->input_names[0], 32, "n");
|
||||
d->output_count = 1;
|
||||
snprintf(d->output_names[0], 32, "resultado");
|
||||
d->part_n = 2;
|
||||
d->parts[0].kind = TB_BP_PARAM_NUM; snprintf(d->parts[0].name, 32, "n");
|
||||
d->parts[1].kind = TB_BP_PARAM_LABEL; snprintf(d->parts[1].name, 32, "vezes 2");
|
||||
d->tab_idx = -1;
|
||||
|
||||
tb_blueprint_var_set_text(bp, "x", "42");
|
||||
tb_blueprint_var_set_text(bp, "nome", "mundo");
|
||||
return bp;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
TbBlueprint* bp = make_example();
|
||||
|
||||
/* JSON nativo round-trip */
|
||||
char* json = tb_blueprint_serialize(bp, TB_BP_FMT_JSON, true);
|
||||
CHECK(json && strstr(json, "\"format\": \"tinyblocks-project\""), "JSON contem o formato");
|
||||
CHECK(json && strstr(json, "Events.WhenRun"), "JSON contem o tipo do bloco");
|
||||
|
||||
char err[160] = "";
|
||||
TbBlueprint* bp2 = tb_blueprint_parse(json, TB_BP_FMT_JSON, err, sizeof(err));
|
||||
CHECK(bp2 != NULL, "JSON e carregavel");
|
||||
if (bp2) {
|
||||
CHECK(bp2->tab_count == 1, "uma aba preservada");
|
||||
TbBpTab* T = &bp2->tabs[0];
|
||||
CHECK(T->top_n == 1 && T->top[0] == 0, "topo da pilha preservado");
|
||||
CHECK(strcmp(T->blocks[0].type, "Events.WhenRun") == 0, "tipo do bloco 0");
|
||||
CHECK(T->blocks[0].next == 1, "vinculo next preservado");
|
||||
CHECK(T->blocks[2].sub[0] == 3, "boca do repita preservada");
|
||||
CHECK(strcmp(T->blocks[3].in_text[1], "1") == 0, "texto de campo preservado");
|
||||
CHECK(bp2->user_block_n == 1 && strcmp(bp2->user_blocks[0].name, "dobro") == 0,
|
||||
"bloco composto preservado");
|
||||
CHECK(bp2->user_blocks[0].part_n == 2 &&
|
||||
bp2->user_blocks[0].parts[1].kind == TB_BP_PARAM_LABEL, "partes do prototipo");
|
||||
CHECK(bp2->var_n == 2 && strcmp(bp2->vars[0].name, "x") == 0 &&
|
||||
bp2->vars[0].kind == TB_BP_VAL_INT && bp2->vars[0].num == 42, "variavel inteira");
|
||||
CHECK(bp2->vars[1].kind == TB_BP_VAL_TEXT && strcmp(bp2->vars[1].text, "mundo") == 0,
|
||||
"variavel de texto");
|
||||
tb_blueprint_free(bp2);
|
||||
}
|
||||
|
||||
/* export XML */
|
||||
char* xml = tb_blueprint_serialize(bp, TB_BP_FMT_XML, true);
|
||||
CHECK(xml && strstr(xml, "<?xml"), "XML tem cabecalho");
|
||||
CHECK(xml && strstr(xml, "<tab"), "XML tem <tab>");
|
||||
CHECK(xml && strstr(xml, "Events.WhenRun"), "XML contem o tipo do bloco");
|
||||
|
||||
/* export CSV (tabela de blocos) */
|
||||
char* csv = tb_blueprint_serialize(bp, TB_BP_FMT_CSV, false);
|
||||
CHECK(csv && strstr(csv, "aba,tipo,x,y,texto"), "CSV tem cabecalho de colunas");
|
||||
CHECK(csv && strstr(csv, "Events.WhenRun"), "CSV lista o bloco");
|
||||
CHECK(csv && strstr(csv, ",40,40,"), "CSV preserva as coordenadas");
|
||||
|
||||
free(json);
|
||||
free(xml);
|
||||
free(csv);
|
||||
tb_blueprint_free(bp);
|
||||
|
||||
printf(fails ? "\n%d FALHA(S)\n" : "\nTUDO OK\n", fails);
|
||||
return fails != 0;
|
||||
}
|
||||
Reference in new issue
Block a user