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

+7
View File
@@ -58,6 +58,12 @@ enum CAT_BYTECODE_Op {
CAT_BYTECODE_OP_DUP, /* duplicate top of stack */
CAT_BYTECODE_OP_GETIDX, /* pop idx, obj -> push obj[idx] */
CAT_BYTECODE_OP_SETIDX, /* pop val, idx, obj -> obj[idx]=val */
CAT_BYTECODE_OP_YIELD, /* suspend a generator, yielding top */
CAT_BYTECODE_OP_NEXT, /* pop it -> push next(it) or nil */
CAT_BYTECODE_OP_TRYSETUP, /* A: handler target addr */
CAT_BYTECODE_OP_TRYEND, /* pop the innermost handler */
CAT_BYTECODE_OP_RAISE, /* pop value -> raise it */
CAT_BYTECODE_OP_FORNEXT, /* A: exit target; pop it -> push next or jump */
};
/* One instruction: opcode + three integer operands. */
@@ -88,6 +94,7 @@ typedef struct CAT_BYTECODE_Function {
int nprotos, capprotos;
int nlocals; /* number of local slots */
int nparams; /* number of parameters */
int is_generator; /* contains yield: calls produce a generator */
} CAT_BYTECODE_Function;
CAT_BYTECODE_Function *cat_bytecode_function_new(const char *name);
+9
View File
@@ -12,12 +12,17 @@
#include <stdbool.h>
struct CAT_VALUE_Value;
/* CAT_ERROR_Error -- an error slot passed into API calls. */
/* `raised` becomes true when an operation fails; `message` is a */
/* human-readable description (always NUL terminated when raised). */
/* `value` optionally carries a catscript value (what `raise` was */
/* given, or the message as text), which `try/except` binds. */
typedef struct CAT_ERROR_Error {
bool raised;
char message[256];
struct CAT_VALUE_Value *value;
} CAT_ERROR_Error;
/* Clear an error slot back to the "no error" state. */
@@ -26,4 +31,8 @@ void cat_error_clear(CAT_ERROR_Error *err);
/* Raise (fill) an error slot with a printf-style message. */
void cat_error_raise(CAT_ERROR_Error *err, const char *fmt, ...);
/* Raise with a carried catscript value (what `raise v` was given). */
void cat_error_raise_value(CAT_ERROR_Error *err, struct CAT_VALUE_Value *v,
const char *fmt, ...);
#endif /* CAT_ERROR_H */
+2
View File
@@ -26,6 +26,8 @@ enum CAT_LEXER_Kind {
CAT_LEXER_TOK_NIL, CAT_LEXER_TOK_NOT, CAT_LEXER_TOK_OR,
CAT_LEXER_TOK_REPEAT, CAT_LEXER_TOK_RETURN, CAT_LEXER_TOK_THEN,
CAT_LEXER_TOK_TRUE, CAT_LEXER_TOK_UNTIL, CAT_LEXER_TOK_WHILE,
CAT_LEXER_TOK_YIELD, CAT_LEXER_TOK_TRY, CAT_LEXER_TOK_EXCEPT,
CAT_LEXER_TOK_RAISE,
/* symbols */
CAT_LEXER_TOK_PLUS, CAT_LEXER_TOK_MINUS, CAT_LEXER_TOK_STAR,
CAT_LEXER_TOK_SLASH, CAT_LEXER_TOK_PERCENT, CAT_LEXER_TOK_CARET,
+42
View File
@@ -0,0 +1,42 @@
/*
* cat/types/generator.h -- generators / iterators.
*
* Naming rule: symbols here are cat_types_generator_* / CAT_TYPES_GENERATOR_*.
*
* A generator wraps a suspended CAT_VM_Frame. Calling a function whose
* body contains `yield` does not run it: it produces a generator whose
* `next()` resumes the frame until the next `yield` (or the function's
* return, which ends iteration). The same type is the general iterator
* protocol: `cat_value_next` on anything with a `next` slot advances it.
*/
#ifndef CAT_TYPES_GENERATOR_H
#define CAT_TYPES_GENERATOR_H
#include "cat/value.h"
#include "cat/vm.h"
/* CAT_TYPES_GENERATOR_Generator -- a resumable suspended frame. */
typedef struct CAT_TYPES_GENERATOR_Generator {
CAT_VALUE_Value base;
CAT_VM_State *vm;
CAT_TYPES_FUNCTION_Closure *cl;
CAT_VM_Frame frame; /* ip/sp/locals/handlers of the paused call */
CAT_VALUE_Value **stack; /* operand stack (lives across resumes) */
int started;
int done;
/* arguments captured for the first resume */
CAT_VALUE_Value *args[256];
int nargs;
} CAT_TYPES_GENERATOR_Generator;
/* Create a generator for `cl` invoked with `args`; nothing runs yet. */
CAT_VALUE_Value *cat_types_generator_new(CAT_VM_State *vm,
CAT_TYPES_FUNCTION_Closure *cl,
CAT_VALUE_Value **args, int nargs);
CAT_VALUE_Vtable *cat_types_generator_vtable(void);
/* True if v is a generator. */
int cat_types_generator_is(CAT_VALUE_Value *v);
#endif /* CAT_TYPES_GENERATOR_H */
+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 */
+32 -1
View File
@@ -10,9 +10,10 @@
#include "cat/error.h"
#include "cat/bytecode.h"
/* Forward declaration so the native-callback signature below refers to
/* Forward declarations so the native-callback signature below refers to
* the real CAT_VM_State type, not a parameter-list-scoped struct. */
typedef struct CAT_VM_State CAT_VM_State;
typedef struct CAT_TYPES_FUNCTION_Closure CAT_TYPES_FUNCTION_Closure;
/* One registered native function. Natives live in the VM (not in a
* process-global list) so distinct VMs never share mutable state and can
@@ -24,6 +25,30 @@ typedef struct CAT_VM_Native {
#define CAT_VM_MAXNATIVE 128
/* An active `try` handler: where to jump and the stack height to
* restore when an error is caught. */
typedef struct CAT_VM_Handler {
int target; /* instruction index of the handler */
int sp; /* stack height to restore */
} CAT_VM_Handler;
#define CAT_VM_MAXHANDLER 64
/* CAT_VM_Frame -- one function activation. A frame holds everything
* needed to suspend and resume execution (ip, operand stack, locals and
* active handlers), so the same interpreter drives both ordinary calls
* and generators that yield in the middle. */
typedef struct CAT_VM_Frame {
CAT_BYTECODE_Function *fn;
CAT_TYPES_FUNCTION_Closure *cl;
CAT_VALUE_Value **stack; /* operand stack (owned by the caller) */
CAT_VALUE_Value *locals[256];
int sp;
int ip;
CAT_VM_Handler handlers[CAT_VM_MAXHANDLER];
int nhandlers;
} CAT_VM_Frame;
/* CAT_VM_State -- globals + a call stack. Fully self-contained: two VMs
* share no mutable state, so they may execute on separate threads. */
typedef struct CAT_VM_State {
@@ -50,6 +75,12 @@ int cat_vm_register_native(CAT_VM_State *vm, const char *name,
* closures/natives invoked through the object system. */
CAT_VM_State *cat_vm_current(void);
/* Run (or resume) a frame. Returns 0 when the frame returns (*out set),
* 1 when it yields (*out set, frame left resumable), or -1 on error
* (vm->err set). The frame owns neither its operand stack nor its
* handlers' lifetime; the caller keeps the stack alive across resumes. */
int cat_vm_frame_run(CAT_VM_State *vm, CAT_VM_Frame *f, CAT_VALUE_Value **out);
/* Convenience: get/set a global. */
CAT_VALUE_Value *cat_vm_get_global(CAT_VM_State *vm, const char *name);
int cat_vm_set_global(CAT_VM_State *vm, const char *name, CAT_VALUE_Value *v);