Files
tinyblocks/libraries/tinyblocks-appearance/src/tb_appearance.c
T
mguinhosandClaude Opus 5.5 520cb10f08 tinyblocks-tensor: tensores com gradiente automatico como blocos
Tensores de float (ate 6 eixos) viajam como bloco-handle; cada bloco
produtor e dono de um handle e troca o tensor a cada avaliacao. Contas
com broadcasting, matmul em lote (GEMM com AVX2 escolhido em execucao e
pool de threads), forma, fatias, juncao, indices, reducoes, imagem,
bytes de arquivo e .tbt. Autograd por grafo com contagem de referencias;
parametros por dono e por grupo, salvar/carregar. Outras libraries usam
a tabela TbTensorApi do lib_userdata, sem linkar.

test-tensor confere cada gradiente por diferenca finita. O "escreva"
passa a usar as classes de valor (tensor aparecia como "resultado").

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 23:03:07 -03:00

74 lines
2.7 KiB
C

/*
* TinyBlocks - tb_appearance.c
* Categoria "Aparência": blocos que produzem saída visível (console do editor).
*/
#include <stdio.h>
#include <stdlib.h>
#include <tinyblocks/tinyblocks.h>
#include <tinyblocks/tb_json_block.h>
#include "tb_appearance.h"
/* "escreva [rotulo] (valor)": o texto de cada um vem da classe do valor
(numero, texto, lista, tensor...). So o rotulo: escreve so ele. */
static void eval_print(TinyBlocks_Block* self, TinyBlocks* rt) {
TinyBlocks_Value label = tb_block_get_input(self, rt, 0);
TinyBlocks_Value value = tb_block_get_input(self, rt, 1);
char* lbl = label.kind == TB_VALUE_NONE ? NULL : tb_value_to_string(label);
const char* l = lbl && lbl[0] ? lbl : "resultado";
if (value.kind == TB_VALUE_NONE || (value.kind == TB_VALUE_TEXT && (!value.as.text || !value.as.text[0]))) {
printf("%s\n", l);
} else {
char* v = tb_value_to_string(value);
printf("%s = %s\n", l, v ? v : "");
free(v);
}
free(lbl);
self->dirty = false;
}
static void eval_print_int(TinyBlocks_Block* self, TinyBlocks* rt) {
TinyBlocks_Value v = tb_block_get_input(self, rt, 0);
int64_t n = v.kind == TB_VALUE_INTEGER ? v.as.integer : 0;
printf("%lld\n", (long long)n);
self->dirty = false;
}
static bool appearance_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
static const TinyBlocks_Category cat = {
"Aparência",
{153/255.f, 102/255.f, 255/255.f, 1.f},
{119/255.f, 77/255.f, 203/255.f, 1.f},
};
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 one_int[] = {{"n", TB_VALUE_INTEGER}};
TinyBlocks_TypeBlock* t;
t = tb_type_block_create(rt, "IO.Print", print_in, 2, NULL, 0, eval_print);
if (!t || !tb_library_add(lib, &t->base)) return false;
t = tb_type_block_create(rt, "IO.PrintInt", one_int, 1, NULL, 0, eval_print_int);
if (!t || !tb_library_add(lib, &t->base)) return false;
return true;
}
static void appearance_deinit(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
(void)rt; (void)lib;
}
TINYBLOCKS_DEFINE_LIBRARY(appearance,
"tinyblocks-appearance",
"1.0.0",
appearance_init,
appearance_deinit)
const TinyBlocks_LibraryDescriptor* tb_appearance_get_library(void) {
return &_tb_lib_desc_appearance;
}