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

+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 */