- VM refactor: resumable CAT_VM_Frame (ip/sp/locals/handlers) - yield/next/generators: calling a yield-function returns a generator - for..in over any iterator, ends on explicit exhaustion status - raise/try/except: structured error values, nearest-handler unwinding - slots: C-declared member layout (offsetof), PyTypeObject/tp_members style - File now exposes path/mode/closed via slots - examples: generators.cat, errors.cat, slots.cat; 40/40 tests
200 lines
7.4 KiB
C
200 lines
7.4 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>
|
|
#include <stddef.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;
|
|
|
|
/* Cat_ImplementsNext -- advance an iterator/generator: -> *out (nil ends). */
|
|
typedef struct CAT_IMPL_Next {
|
|
int (*fn)(CAT_VALUE_Value *self, CAT_VALUE_Value **out,
|
|
CAT_ERROR_Error *err);
|
|
} CAT_IMPL_Next;
|
|
|
|
/* ---- slots: a C-known object layout (PyTypeObject / tp_members) ----
|
|
*
|
|
* A C module declares its concrete layout once, as a fixed array of
|
|
* CAT_VALUE_Slot descriptors. The VM and the attribute dispatcher then
|
|
* read/write members by name with no per-access C code, and the layout
|
|
* is known to every C module that includes the type's header (the
|
|
* offsets come from offsetof). This is the analogue of CPython's
|
|
* `tp_members` / `__slots__`.
|
|
*/
|
|
typedef struct CAT_VALUE_Slot {
|
|
const char *name; /* attribute name */
|
|
size_t offset; /* byte offset from the value base pointer */
|
|
int kind; /* one of CAT_VALUE_SLOT_* */
|
|
int readonly; /* 1 => assign raises */
|
|
} CAT_VALUE_Slot;
|
|
|
|
enum {
|
|
CAT_VALUE_SLOT_VALUE, /* CAT_VALUE_Value * member */
|
|
CAT_VALUE_SLOT_NUMBER, /* long long member */
|
|
CAT_VALUE_SLOT_DOUBLE, /* double member */
|
|
CAT_VALUE_SLOT_TEXT, /* char * member */
|
|
CAT_VALUE_SLOT_BOOL, /* int/bool member */
|
|
CAT_VALUE_SLOT_NIL /* no storage; always reads nil */
|
|
};
|
|
|
|
/* 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 */
|
|
CAT_VALUE_KIND_GENERATOR /* suspended generator / iterator */
|
|
};
|
|
|
|
/* ---- 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;
|
|
CAT_IMPL_Next next;
|
|
/* fixed member layout, when this type has one (may be NULL) */
|
|
const CAT_VALUE_Slot *slots;
|
|
int nslots;
|
|
/* 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);
|
|
|
|
/* Advance an iterator/generator. Returns 0 and sets *out to the next
|
|
* value, 1 when the iterator is exhausted (*out untouched), or -1 on
|
|
* error. A separate exhaustion status (rather than a nil/sentinel
|
|
* value) lets generators yield nil, false or 0 without ending the loop. */
|
|
int cat_value_next(CAT_VALUE_Value *v, CAT_VALUE_Value **out,
|
|
CAT_ERROR_Error *err);
|
|
|
|
/* Slot-based member access. `cat_value_slot_find` returns the Slot or
|
|
* NULL; get/set read or write the member through its offset. */
|
|
const CAT_VALUE_Slot *cat_value_slot_find(CAT_VALUE_Value *v, const char *name);
|
|
int cat_value_slot_get(CAT_VALUE_Value *v, const CAT_VALUE_Slot *s,
|
|
CAT_VALUE_Value **out, CAT_ERROR_Error *err);
|
|
int cat_value_slot_set(CAT_VALUE_Value *v, const CAT_VALUE_Slot *s,
|
|
CAT_VALUE_Value *value, CAT_ERROR_Error *err);
|
|
|
|
const char *cat_value_type_name(CAT_VALUE_Value *v);
|
|
|
|
#endif /* CAT_VALUE_H */
|