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:
1 parent
52d118211f
commit
3c20496c78
19 files changed
+978
-99
No files matched your search
+32
-1
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user