- 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.
159 lines
5.6 KiB
C
159 lines
5.6 KiB
C
/*
|
|
* TinyBlocks - tb_variables.c
|
|
* Categoria "Variáveis": mude / adicione / valor de <variável>.
|
|
* A library é dona do armazenamento de variáveis e o expõe para monitores.
|
|
*/
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <math.h>
|
|
#include <tinyblocks/tinyblocks.h>
|
|
#include "tb_variables.h"
|
|
|
|
#define MAX_VARS 64
|
|
|
|
typedef struct { char name[32]; TinyBlocks_Value val; } Var;
|
|
|
|
static Var s_vars[MAX_VARS];
|
|
static int s_n = 0;
|
|
|
|
static int var_find(const char* n, bool create) {
|
|
for (int i = 0; i < s_n; i++)
|
|
if (strcmp(s_vars[i].name, n) == 0) return i;
|
|
if (!create || s_n >= MAX_VARS) return -1;
|
|
int i = s_n++;
|
|
strncpy(s_vars[i].name, n, 31); s_vars[i].name[31] = 0;
|
|
s_vars[i].val = tb_value_integer(0);
|
|
return i;
|
|
}
|
|
|
|
static void input_name(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i,
|
|
char* out, size_t cap) {
|
|
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
|
|
/* value_to_str reduzido */
|
|
switch (v.kind) {
|
|
case TB_VALUE_INTEGER: snprintf(out, cap, "%lld", (long long)v.as.integer); break;
|
|
case TB_VALUE_REAL: snprintf(out, cap, "%g", v.as.real); break;
|
|
case TB_VALUE_TEXT: snprintf(out, cap, "%s", v.as.text ? v.as.text : ""); break;
|
|
default: out[0] = 0; break;
|
|
}
|
|
}
|
|
|
|
static void eval_var_get(TinyBlocks_Block* self, TinyBlocks* rt) {
|
|
char name[64]; input_name(self, rt, 0, name, sizeof(name));
|
|
int i = var_find(name, false);
|
|
tb_block_set_output(self, 0, i >= 0 ? tb_value_clone(s_vars[i].val)
|
|
: tb_value_integer(0));
|
|
self->dirty = false;
|
|
}
|
|
|
|
static void eval_var_set(TinyBlocks_Block* self, TinyBlocks* rt) {
|
|
char name[64]; input_name(self, rt, 0, name, sizeof(name));
|
|
int i = var_find(name, true);
|
|
if (i >= 0) {
|
|
TinyBlocks_Value v = tb_block_get_input(self, rt, 1);
|
|
tb_value_free(&s_vars[i].val);
|
|
s_vars[i].val = tb_value_clone(v);
|
|
}
|
|
self->dirty = false;
|
|
}
|
|
|
|
static void eval_var_change(TinyBlocks_Block* self, TinyBlocks* rt) {
|
|
char name[64]; input_name(self, rt, 0, name, sizeof(name));
|
|
int i = var_find(name, true);
|
|
if (i >= 0) {
|
|
TinyBlocks_Value by = tb_block_get_input(self, rt, 1);
|
|
TinyBlocks_Value* cur = &s_vars[i].val;
|
|
double a = 0, d = 0;
|
|
if (cur->kind == TB_VALUE_INTEGER) a = (double)cur->as.integer;
|
|
else if (cur->kind == TB_VALUE_REAL) a = cur->as.real;
|
|
else if (cur->kind == TB_VALUE_TEXT && cur->as.text) a = strtod(cur->as.text, NULL);
|
|
if (by.kind == TB_VALUE_INTEGER) d = (double)by.as.integer;
|
|
else if (by.kind == TB_VALUE_REAL) d = by.as.real;
|
|
else if (by.kind == TB_VALUE_TEXT && by.as.text) d = strtod(by.as.text, NULL);
|
|
TinyBlocks_Value nv = (a == floor(a) && d == floor(d))
|
|
? tb_value_integer((long long)llround(a + d))
|
|
: tb_value_real(a + d);
|
|
tb_value_free(cur);
|
|
*cur = nv;
|
|
}
|
|
self->dirty = false;
|
|
}
|
|
|
|
/* == API de enumeração === */
|
|
int tb_variables_count(void) { return s_n; }
|
|
|
|
const char* tb_variables_name(int i) {
|
|
return (i >= 0 && i < s_n) ? s_vars[i].name : "";
|
|
}
|
|
|
|
const TinyBlocks_Value* tb_variables_value(int i) {
|
|
return (i >= 0 && i < s_n) ? &s_vars[i].val : NULL;
|
|
}
|
|
|
|
void tb_variables_reset_blocks(void) {
|
|
for (int v = 0; v < s_n; v++)
|
|
if (s_vars[v].val.kind == TB_VALUE_BLOCK) {
|
|
tb_value_free(&s_vars[v].val);
|
|
s_vars[v].val = tb_value_integer(0);
|
|
}
|
|
}
|
|
|
|
void tb_variables_clear(void) {
|
|
for (int i = 0; i < s_n; i++) tb_value_free(&s_vars[i].val);
|
|
s_n = 0;
|
|
}
|
|
|
|
void tb_variables_set_text(const char* name, const char* value) {
|
|
if (!name || !name[0]) return;
|
|
int i = var_find(name, true);
|
|
if (i < 0) return;
|
|
tb_value_free(&s_vars[i].val);
|
|
const char* s = value ? value : "";
|
|
char* end = NULL;
|
|
long long iv = strtoll(s, &end, 10);
|
|
if (end && *end == 0 && end != s) { s_vars[i].val = tb_value_integer(iv); return; }
|
|
double dv = strtod(s, &end);
|
|
if (end && *end == 0 && end != s) { s_vars[i].val = tb_value_real(dv); return; }
|
|
s_vars[i].val = tb_value_text(s);
|
|
}
|
|
|
|
/* == Registro === */
|
|
static bool variables_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
|
static const TinyBlocks_Category cat = {
|
|
"Variáveis",
|
|
{255/255.f, 140/255.f, 26/255.f, 1.f},
|
|
{219/255.f, 110/255.f, 0/255.f, 1.f},
|
|
};
|
|
tb_library_set_category(lib, &cat);
|
|
|
|
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 any_out[] = {{"valor", TB_VALUE_NONE}};
|
|
|
|
TinyBlocks_TypeBlock* t;
|
|
t = tb_type_block_create(rt, "Variables.Set", var_set, 2, NULL, 0, eval_var_set);
|
|
if (!t || !tb_library_add(lib, &t->base)) return false;
|
|
t = tb_type_block_create(rt, "Variables.Change", var_set, 2, NULL, 0, eval_var_change);
|
|
if (!t || !tb_library_add(lib, &t->base)) return false;
|
|
t = tb_type_block_create(rt, "Variables.Get", var_in, 1, any_out, 1, eval_var_get);
|
|
if (!t || !tb_library_add(lib, &t->base)) return false;
|
|
return true;
|
|
}
|
|
|
|
static void variables_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
|
(void)rt; (void)lib;
|
|
for (int i = 0; i < s_n; i++) tb_value_free(&s_vars[i].val);
|
|
s_n = 0;
|
|
}
|
|
|
|
TINYBLOCKS_DEFINE_LIBRARY(variables,
|
|
"tinyblocks-variables",
|
|
"1.0.0",
|
|
variables_init,
|
|
variables_deinit)
|
|
|
|
const TinyBlocks_LibraryDescriptor* tb_variables_get_library(void) {
|
|
return &_tb_lib_desc_variables;
|
|
}
|