generators/iterators, raise/try/except, and slot-based object layout

- 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
This commit is contained in:
Minus committed 2026-11-16 01:48:47 +00:00
1 parent 52d118211f
commit 3c20496c78
19 files changed
+978 -99

No files matched your search

+53 -1
View File
@@ -17,6 +17,7 @@
#define CAT_VALUE_H
#include <stdbool.h>
#include <stddef.h>
struct CAT_ERROR_Error;
struct CAT_VALUE_Value;
@@ -82,6 +83,37 @@ typedef struct CAT_IMPL_SetAttribute {
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,
@@ -89,7 +121,8 @@ enum {
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_FUNCTION, /* closure */
CAT_VALUE_KIND_GENERATOR /* suspended generator / iterator */
};
/* ---- the vtable every value type fills in ---- */
@@ -109,6 +142,10 @@ struct CAT_VALUE_Vtable {
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) */
@@ -142,6 +179,21 @@ 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 */