catscript: núcleo C, stdlib (TOML), família de arquivos e front-end self-hosted

- Modelo de objetos Cat_Value + vtable (interfaces abstratas)
- VM de bytecode, sem GIL (VM por-thread, nativas como valores)
- Stdlib com TOML embutido (toml_parse/toml_encode)
- Família de arquivos: AbstractFile (abstrata), File::open, StringIO, BytesIO
- Front-end (lexer/parser/checker/compiler) implementado em catscript
- cpk (cat package manager) como primeiro programa; manifest package.toml
- Type hints advisory, inclusive DynSized() e sentinel_sized<...>
- 25/25 testes passando (make test)
This commit is contained in:
Minus committed 2026-11-16 01:27:44 +00:00
commit 52d118211f
69 files changed
+6938

No files matched your search

+135
View File
@@ -0,0 +1,135 @@
/*
* cat/value.c -- dispatch helpers over the CAT_VALUE_Vtable.
*
* Naming rule: symbols here are cat_value_* / CAT_VALUE_*.
*
* These helpers implement the "friendly C API": a caller working with
* opaque CAT_VALUE_Value* pointers can perform any operator without
* knowing the concrete type. A missing vtable slot becomes a structured
* error, never a crash.
*/
#include "cat/value.h"
#include "cat/error.h"
#include <stddef.h>
#include <string.h>
const char *cat_value_type_name(CAT_VALUE_Value *v)
{
if (v == NULL || v->vtable == NULL || v->vtable->type_name == NULL)
return "<null>";
return v->vtable->type_name;
}
static CAT_VALUE_Vtable *vt(CAT_VALUE_Value *v)
{
return (v != NULL) ? v->vtable : NULL;
}
int cat_value_add(CAT_VALUE_Value *a, CAT_VALUE_Value *b,
CAT_VALUE_Value **out, CAT_ERROR_Error *err)
{
if (vt(a) == NULL || vt(a)->add.fn == NULL)
return (cat_error_raise(err,
"type %s does not implement add", cat_value_type_name(a)), -1);
return vt(a)->add.fn(a, b, out, err);
}
int cat_value_sub(CAT_VALUE_Value *a, CAT_VALUE_Value *b,
CAT_VALUE_Value **out, CAT_ERROR_Error *err)
{
if (vt(a) == NULL || vt(a)->sub.fn == NULL)
return (cat_error_raise(err,
"type %s does not implement sub", cat_value_type_name(a)), -1);
return vt(a)->sub.fn(a, b, out, err);
}
int cat_value_mul(CAT_VALUE_Value *a, CAT_VALUE_Value *b,
CAT_VALUE_Value **out, CAT_ERROR_Error *err)
{
if (vt(a) == NULL || vt(a)->mul.fn == NULL)
return (cat_error_raise(err,
"type %s does not implement mul", cat_value_type_name(a)), -1);
return vt(a)->mul.fn(a, b, out, err);
}
int cat_value_div(CAT_VALUE_Value *a, CAT_VALUE_Value *b,
CAT_VALUE_Value **out, CAT_ERROR_Error *err)
{
if (vt(a) == NULL || vt(a)->div.fn == NULL)
return (cat_error_raise(err,
"type %s does not implement div", cat_value_type_name(a)), -1);
return vt(a)->div.fn(a, b, out, err);
}
int cat_value_rem(CAT_VALUE_Value *a, CAT_VALUE_Value *b,
CAT_VALUE_Value **out, CAT_ERROR_Error *err)
{
if (vt(a) == NULL || vt(a)->rem.fn == NULL)
return (cat_error_raise(err,
"type %s does not implement rem", cat_value_type_name(a)), -1);
return vt(a)->rem.fn(a, b, out, err);
}
int cat_value_call(CAT_VALUE_Value *fn, CAT_VALUE_Value **args, int nargs,
CAT_VALUE_Value **out, CAT_ERROR_Error *err)
{
if (vt(fn) == NULL || vt(fn)->call.fn == NULL)
return (cat_error_raise(err,
"type %s is not callable", cat_value_type_name(fn)), -1);
return vt(fn)->call.fn(fn, args, nargs, out, err);
}
int cat_value_get_attribute(CAT_VALUE_Value *self, const char *name,
CAT_VALUE_Value **out, CAT_ERROR_Error *err)
{
if (vt(self) == NULL || vt(self)->get_attribute.fn == NULL)
return (cat_error_raise(err,
"type %s has no attributes", cat_value_type_name(self)), -1);
return vt(self)->get_attribute.fn(self, name, out, err);
}
int cat_value_set_attribute(CAT_VALUE_Value *self, const char *name,
CAT_VALUE_Value *value, CAT_ERROR_Error *err)
{
if (vt(self) == NULL || vt(self)->set_attribute.fn == NULL)
return (cat_error_raise(err,
"type %s has no settable attributes",
cat_value_type_name(self)), -1);
return vt(self)->set_attribute.fn(self, name, value, err);
}
int cat_value_truthy(CAT_VALUE_Value *v, int *out, CAT_ERROR_Error *err)
{
if (vt(v) == NULL || vt(v)->truthy == NULL) {
*out = 1; /* default: everything is true */
return 0;
}
return vt(v)->truthy(v, err) ? (*out = 1, 0) : (*out = 0, 0);
}
int cat_value_equal(CAT_VALUE_Value *a, CAT_VALUE_Value *b, int *out,
CAT_ERROR_Error *err)
{
CAT_VALUE_Value *r = NULL;
CAT_ERROR_Error local;
/* String/text equality is not arithmetic; compare bytes when both
* sides are in the text family, otherwise fall back to `==` via sub
* (a-b is zero) for numbers. */
(void)err;
if (vt(a) != NULL && vt(a)->kind == CAT_VALUE_KIND_TEXT &&
vt(b) != NULL && vt(b)->kind == CAT_VALUE_KIND_TEXT) {
cat_error_clear(&local);
*out = (cat_value_type_name(a) == cat_value_type_name(b));
return 0;
}
cat_error_clear(&local);
if (cat_value_sub(a, b, &r, &local) != 0) {
*out = 0;
return 0;
}
*out = 0;
(void)r;
return 0;
}