Result como tagged union e classes de valor (polimorfismo dos valores)
- TinyBlocks_Result (tb_result.h): Ok | Erro com tag (enum) e payload,
ref-counted, que flui como TB_VALUE_RESULT. A library de erros passa a
produzir/consumir Result em vez de objetos JSON {"ok"/"erro"}.
- TinyBlocks_ValueClass (tb_value_class.h): cada kind de valor tem uma
"interface" (copy, release, to_text, to_number, truthy, equals,
type_name) numa tabela; tb_value_clone/free e as operacoes genericas
(tb_value_to_text, tb_value_to_number, tb_value_truthy, tb_value_equals,
tb_value_type_name) despacham por ela. Sem libm no core.
- Editor e as conversoes "texto de" / "número de" usam o despacho.
- make test-value: classes de valor e Result.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9f6a1a0eb0
commit
351e85bb14
13 files changed
+650
-290
No files matched your search
@@ -29,6 +29,7 @@ typedef struct TinyBlocks_VectorData TinyBlocks_VectorData;
|
||||
typedef struct TinyBlocks_RenderableBlock TinyBlocks_RenderableBlock;
|
||||
typedef struct TinyBlocks_JsonValue TinyBlocks_JsonValue;
|
||||
typedef struct TinyBlocks_Image TinyBlocks_Image;
|
||||
typedef struct TinyBlocks_Result TinyBlocks_Result;
|
||||
|
||||
/* -- Kinds de bloco --- */
|
||||
typedef enum TinyBlocks_BlockKind {
|
||||
@@ -51,6 +52,8 @@ typedef enum TinyBlocks_ValueKind {
|
||||
TB_VALUE_VECTOR = 5, /* dados de vetor 2D (ref-counted) */
|
||||
TB_VALUE_JSON = 6, /* valor JSON (ref-counted) */
|
||||
TB_VALUE_IMAGE = 7, /* imagem RGBA/cinza (ref-counted) */
|
||||
TB_VALUE_RESULT = 8, /* Ok(valor) | Erro(valor) (ref-counted) */
|
||||
TB_VALUE_KIND_COUNT /* quantidade de kinds (tabela de classes) */
|
||||
} TinyBlocks_ValueKind;
|
||||
|
||||
/* -- Valor com tag (flui entre slots) --- */
|
||||
@@ -64,6 +67,7 @@ typedef struct TinyBlocks_Value {
|
||||
TinyBlocks_VectorData* vector; /* ref-counted (retain/release) */
|
||||
TinyBlocks_JsonValue* json; /* ref-counted (retain/release) */
|
||||
TinyBlocks_Image* image; /* ref-counted (retain/release) */
|
||||
TinyBlocks_Result* result; /* ref-counted (retain/release) */
|
||||
} as;
|
||||
} TinyBlocks_Value;
|
||||
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* TinyBlocks - tb_result.h
|
||||
* Resultado no estilo do Result do Rust: um tagged union Ok(valor) |
|
||||
* Erro(valor), ref-counted, que flui entre blocos como TB_VALUE_RESULT.
|
||||
*
|
||||
* TinyBlocks_Result* r = tb_result_err_text("arquivo não existe");
|
||||
* TinyBlocks_Value v = tb_value_result(r); // retem
|
||||
* tb_result_release(r);
|
||||
* if (tb_value_is_err(v)) ...
|
||||
*/
|
||||
#ifndef TINYBLOCKS_TB_RESULT_H
|
||||
#define TINYBLOCKS_TB_RESULT_H
|
||||
|
||||
#include "tb_defs.h"
|
||||
|
||||
typedef enum TinyBlocks_ResultTag {
|
||||
TB_RESULT_OK = 0,
|
||||
TB_RESULT_ERR = 1,
|
||||
} TinyBlocks_ResultTag;
|
||||
|
||||
struct TinyBlocks_Result {
|
||||
TinyBlocks_ResultTag tag; /* qual variante */
|
||||
TinyBlocks_Value payload; /* Ok: o valor; Erro: a mensagem/dado */
|
||||
int32_t ref;
|
||||
};
|
||||
|
||||
/* Criacao (ref = 1; o payload e clonado) */
|
||||
TB_API TinyBlocks_Result* tb_result_ok(TinyBlocks_Value v);
|
||||
TB_API TinyBlocks_Result* tb_result_err(TinyBlocks_Value e);
|
||||
TB_API TinyBlocks_Result* tb_result_err_text(const char* msg);
|
||||
|
||||
TB_API void tb_result_retain (TinyBlocks_Result* r);
|
||||
TB_API void tb_result_release(TinyBlocks_Result* r);
|
||||
|
||||
/* Valor <-> resultado */
|
||||
TB_API TinyBlocks_Value tb_value_result(TinyBlocks_Result* r); /* retem */
|
||||
TB_API TinyBlocks_Result* tb_value_as_result(TinyBlocks_Value v); /* emprestado ou NULL */
|
||||
TB_API bool tb_value_is_ok (TinyBlocks_Value v);
|
||||
TB_API bool tb_value_is_err(TinyBlocks_Value v);
|
||||
|
||||
#endif /* TINYBLOCKS_TB_RESULT_H */
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* TinyBlocks - tb_value_class.h
|
||||
* Polimorfismo dos valores: cada kind de TinyBlocks_Value tem uma "classe"
|
||||
* (uma interface feita de ponteiros de funcao) e as operacoes genericas
|
||||
* despacham por ela, em vez de cada library repetir um switch no kind.
|
||||
*
|
||||
* tb_value_to_text(v, buf, cap) -> "42", "olá", "[1,2]", "Ok(3)"...
|
||||
* tb_value_to_number(v, &d) -> true se v vira numero
|
||||
* tb_value_truthy(v) -> verdadeiro/falso (condicoes)
|
||||
* tb_value_equals(a, b) -> igualdade (numeros pelo valor)
|
||||
* tb_value_type_name(v) -> "número inteiro", "lista", "resultado (erro)"
|
||||
*
|
||||
* tb_value_clone / tb_value_free tambem despacham pela classe.
|
||||
*/
|
||||
#ifndef TINYBLOCKS_TB_VALUE_CLASS_H
|
||||
#define TINYBLOCKS_TB_VALUE_CLASS_H
|
||||
|
||||
#include "tb_defs.h"
|
||||
|
||||
typedef struct TinyBlocks_ValueClass {
|
||||
TinyBlocks_ValueKind kind;
|
||||
const char* name; /* nome para pessoas ("texto") */
|
||||
|
||||
/* Ciclo de vida. copy: referencia nova (ou copia profunda). release:
|
||||
solta o que for do valor. NULL = valor simples (nada a fazer). */
|
||||
TinyBlocks_Value (*copy)(TinyBlocks_Value v);
|
||||
void (*release)(TinyBlocks_Value* v);
|
||||
|
||||
/* Conversoes. to_text tem a semantica do snprintf: escreve ate cap-1
|
||||
bytes e devolve o tamanho completo. to_number NULL = nunca e numero. */
|
||||
size_t (*to_text)(TinyBlocks_Value v, char* out, size_t cap);
|
||||
bool (*to_number)(TinyBlocks_Value v, double* out);
|
||||
bool (*truthy)(TinyBlocks_Value v);
|
||||
|
||||
/* a e b do mesmo kind. NULL = compara pelo texto. */
|
||||
bool (*equals)(TinyBlocks_Value a, TinyBlocks_Value b);
|
||||
|
||||
/* Nome que depende do conteudo ("lista" x "objeto JSON"); NULL = name */
|
||||
const char* (*type_name)(TinyBlocks_Value v);
|
||||
} TinyBlocks_ValueClass;
|
||||
|
||||
/* Classe do kind (nunca NULL: kinds desconhecidos usam a de "nada") */
|
||||
TB_API const TinyBlocks_ValueClass* tb_value_class(TinyBlocks_ValueKind kind);
|
||||
|
||||
/* -- Operacoes genericas (despacham pela classe) -- */
|
||||
TB_API size_t tb_value_to_text(TinyBlocks_Value v, char* out, size_t cap);
|
||||
TB_API char* tb_value_to_string(TinyBlocks_Value v); /* malloc */
|
||||
TB_API bool tb_value_to_number(TinyBlocks_Value v, double* out);
|
||||
TB_API double tb_value_as_number(TinyBlocks_Value v, double def);
|
||||
TB_API int64_t tb_value_as_integer(TinyBlocks_Value v, int64_t def); /* arredonda */
|
||||
TB_API bool tb_value_truthy(TinyBlocks_Value v);
|
||||
TB_API bool tb_value_equals(TinyBlocks_Value a, TinyBlocks_Value b);
|
||||
TB_API const char* tb_value_type_name(TinyBlocks_Value v);
|
||||
|
||||
/* Numero escrito em texto: espacos nas pontas e virgula decimal ("3,5") */
|
||||
TB_API bool tb_text_to_number(const char* s, double* out);
|
||||
|
||||
/* Numero sem ".0" quando inteiro (o que os blocos mostram) */
|
||||
TB_API TinyBlocks_Value tb_value_number(double d);
|
||||
|
||||
#endif /* TINYBLOCKS_TB_VALUE_CLASS_H */
|
||||
@@ -36,6 +36,8 @@
|
||||
#include "tb_vector_block.h"
|
||||
#include "tb_json_block.h"
|
||||
#include "tb_image.h"
|
||||
#include "tb_result.h"
|
||||
#include "tb_value_class.h"
|
||||
#include "tb_meta.h"
|
||||
|
||||
/* -- Runtime --- */
|
||||
|
||||
Reference in new issue
Block a user