Files
catscript/include/cat/value.h
T
Minus 52d118211f 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)
2026-11-16 01:27:44 +00:00

148 lines
5.2 KiB
C

/*
* cat/value.h -- the object model at the heart of catscript.
*
* Naming rule: symbols declared here are cat_value_* / CAT_VALUE_*.
*
* Every catscript value is an object: a pointer to a CAT_VALUE_Value
* whose first member is a vtable pointer. The vtable (Cat_ValueType)
* carries one sub-interface per supported operation. A type that does
* not support an operation leaves the slot empty; the call helper then
* raises a structured error instead of crashing.
*
* The abstract interfaces requested for the language live here:
* Cat_ValueType, Cat_ImplementsAdd/Sub/Mul/Div/Rem/Call/Raise,
* Cat_ImplementsGetAttribute, Cat_ImplementsSetAttribute.
*/
#ifndef CAT_VALUE_H
#define CAT_VALUE_H
#include <stdbool.h>
struct CAT_ERROR_Error;
struct CAT_VALUE_Value;
struct CAT_VALUE_Vtable;
typedef struct CAT_VALUE_Value CAT_VALUE_Value;
typedef struct CAT_VALUE_Vtable CAT_VALUE_Vtable;
typedef struct CAT_ERROR_Error CAT_ERROR_Error;
/* ---- abstract interfaces (each is a single-slot method table) ---- */
/* Cat_ImplementsAdd -- binary + : self OP other -> *out. */
typedef struct CAT_IMPL_Add {
int (*fn)(CAT_VALUE_Value *self, CAT_VALUE_Value *other,
CAT_VALUE_Value **out, CAT_ERROR_Error *err);
} CAT_IMPL_Add;
/* Cat_ImplementsSub -- binary - . */
typedef struct CAT_IMPL_Sub {
int (*fn)(CAT_VALUE_Value *self, CAT_VALUE_Value *other,
CAT_VALUE_Value **out, CAT_ERROR_Error *err);
} CAT_IMPL_Sub;
/* Cat_ImplementsMul -- binary * . */
typedef struct CAT_IMPL_Mul {
int (*fn)(CAT_VALUE_Value *self, CAT_VALUE_Value *other,
CAT_VALUE_Value **out, CAT_ERROR_Error *err);
} CAT_IMPL_Mul;
/* Cat_ImplementsDiv -- binary / . */
typedef struct CAT_IMPL_Div {
int (*fn)(CAT_VALUE_Value *self, CAT_VALUE_Value *other,
CAT_VALUE_Value **out, CAT_ERROR_Error *err);
} CAT_IMPL_Div;
/* Cat_ImplementsRem -- binary % . */
typedef struct CAT_IMPL_Rem {
int (*fn)(CAT_VALUE_Value *self, CAT_VALUE_Value *other,
CAT_VALUE_Value **out, CAT_ERROR_Error *err);
} CAT_IMPL_Rem;
/* Cat_ImplementsCall -- self(args...) -> *out. */
typedef struct CAT_IMPL_Call {
int (*fn)(CAT_VALUE_Value *self, CAT_VALUE_Value **args, int nargs,
CAT_VALUE_Value **out, CAT_ERROR_Error *err);
} CAT_IMPL_Call;
/* Cat_ImplementsRaise -- raise a structured error from this object. */
typedef struct CAT_IMPL_Raise {
int (*fn)(CAT_VALUE_Value *self, CAT_VALUE_Value *message,
CAT_ERROR_Error *err);
} CAT_IMPL_Raise;
/* Cat_ImplementsGetAttribute -- self.name -> *out. */
typedef struct CAT_IMPL_GetAttribute {
int (*fn)(CAT_VALUE_Value *self, const char *name,
CAT_VALUE_Value **out, CAT_ERROR_Error *err);
} CAT_IMPL_GetAttribute;
/* Cat_ImplementsSetAttribute -- self.name = value. */
typedef struct CAT_IMPL_SetAttribute {
int (*fn)(CAT_VALUE_Value *self, const char *name, CAT_VALUE_Value *value,
CAT_ERROR_Error *err);
} CAT_IMPL_SetAttribute;
/* Runtime kind tag: structural families the VM can test cheaply. */
enum {
CAT_VALUE_KIND_NIL,
CAT_VALUE_KIND_BOOL, /* boolean family */
CAT_VALUE_KIND_NUMBER, /* Cat_Number family */
CAT_VALUE_KIND_TEXT, /* Cat_Text family */
CAT_VALUE_KIND_TABLE, /* table / object */
CAT_VALUE_KIND_FUNCTION /* closure */
};
/* ---- the vtable every value type fills in ---- */
/* CAT_VALUE_Vtable == Cat_ValueType.
* Slots left NULL mean "operation not supported by this type"; the
* dispatch helpers turn that into an error. */
struct CAT_VALUE_Vtable {
const char *type_name;
int kind; /* one of CAT_VALUE_KIND_* */
CAT_IMPL_Add add;
CAT_IMPL_Sub sub;
CAT_IMPL_Mul mul;
CAT_IMPL_Div div;
CAT_IMPL_Rem rem;
CAT_IMPL_Call call;
CAT_IMPL_Raise raise;
CAT_IMPL_GetAttribute get_attribute;
CAT_IMPL_SetAttribute set_attribute;
/* truthiness for control flow (NULL => everything is true) */
int (*truthy)(CAT_VALUE_Value *self, CAT_ERROR_Error *err);
/* release the object's own storage (shallow) */
void (*destroy)(CAT_VALUE_Value *self);
};
/* The common header shared by every concrete value. */
struct CAT_VALUE_Value {
CAT_VALUE_Vtable *vtable;
};
/* ---- dispatch helpers used by compiler/VM and embedders ---- */
int cat_value_add(CAT_VALUE_Value *a, CAT_VALUE_Value *b,
CAT_VALUE_Value **out, CAT_ERROR_Error *err);
int cat_value_sub(CAT_VALUE_Value *a, CAT_VALUE_Value *b,
CAT_VALUE_Value **out, CAT_ERROR_Error *err);
int cat_value_mul(CAT_VALUE_Value *a, CAT_VALUE_Value *b,
CAT_VALUE_Value **out, CAT_ERROR_Error *err);
int cat_value_div(CAT_VALUE_Value *a, CAT_VALUE_Value *b,
CAT_VALUE_Value **out, CAT_ERROR_Error *err);
int cat_value_rem(CAT_VALUE_Value *a, CAT_VALUE_Value *b,
CAT_VALUE_Value **out, CAT_ERROR_Error *err);
int cat_value_call(CAT_VALUE_Value *fn, CAT_VALUE_Value **args, int nargs,
CAT_VALUE_Value **out, CAT_ERROR_Error *err);
int cat_value_get_attribute(CAT_VALUE_Value *self, const char *name,
CAT_VALUE_Value **out, CAT_ERROR_Error *err);
int cat_value_set_attribute(CAT_VALUE_Value *self, const char *name,
CAT_VALUE_Value *value, CAT_ERROR_Error *err);
int cat_value_truthy(CAT_VALUE_Value *v, int *out, CAT_ERROR_Error *err);
int cat_value_equal(CAT_VALUE_Value *a, CAT_VALUE_Value *b, int *out,
CAT_ERROR_Error *err);
const char *cat_value_type_name(CAT_VALUE_Value *v);
#endif /* CAT_VALUE_H */