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

+98 -2
View File
@@ -41,6 +41,7 @@ typedef struct {
CAT_BYTECODE_Function *fn;
char locals[MAXLOCALS][64];
int nlocals;
int is_generator; /* body used `yield` */
} FuncCtx;
typedef struct {
@@ -265,6 +266,9 @@ static int compile_function_literal(Compiler *c, const char *name)
c->stack[c->nstack++] = nc;
block(c);
expect(c, CAT_LEXER_TOK_END, "'end'");
/* A body that used `yield` makes this a generator. */
if (c->stack[c->nstack - 1].is_generator)
sub->is_generator = 1;
emit(c, CAT_BYTECODE_OP_RETURN0, 0, 0, c->tok.line);
c->nstack--;
@@ -648,6 +652,37 @@ static void stat(Compiler *c)
next(c);
}
skip_type_hint(c);
if (check(c, CAT_LEXER_TOK_IN)) {
/* for NAME in expr do <body> end -- iterate any iterator
* (generator). Each round calls NEXT; nil ends the loop. */
int islot;
next(c);
expr(c); /* the iterable */
expect(c, CAT_LEXER_TOK_DO, "'do'");
slot = add_local(c, var);
islot = add_local(c, " .it");
emit(c, CAT_BYTECODE_OP_SETLOCAL, islot, 0, line);
jcond = cur_fn(c)->ncode;
emit(c, CAT_BYTECODE_OP_GETLOCAL, islot, 0, line);
jend = emit(c, CAT_BYTECODE_OP_FORNEXT, 0, 0, line);
emit(c, CAT_BYTECODE_OP_SETLOCAL, slot, 0, line);
{
int lv = c->nloops++;
c->brk_n[lv] = 0;
block(c);
c->nloops--;
emit(c, CAT_BYTECODE_OP_JUMP, jcond, 0, line);
cur_fn(c)->code[jend].a = cur_fn(c)->ncode;
{
int k;
for (k = 0; k < c->brk_n[lv]; k++)
cur_fn(c)->code[c->brk_ends[lv][k]].a =
cur_fn(c)->ncode;
}
}
expect(c, CAT_LEXER_TOK_END, "'end'");
break;
}
expect(c, CAT_LEXER_TOK_ASSIGN, "'='");
expr(c); /* start */
expect(c, CAT_LEXER_TOK_COMMA, "','");
@@ -711,7 +746,8 @@ static void stat(Compiler *c)
next(c);
if (!check(c, CAT_LEXER_TOK_END) && !check(c, CAT_LEXER_TOK_EOF) &&
!check(c, CAT_LEXER_TOK_SEMI) && !check(c, CAT_LEXER_TOK_ELSE) &&
!check(c, CAT_LEXER_TOK_ELSEIF) && !check(c, CAT_LEXER_TOK_UNTIL)) {
!check(c, CAT_LEXER_TOK_ELSEIF) && !check(c, CAT_LEXER_TOK_UNTIL) &&
!check(c, CAT_LEXER_TOK_EXCEPT)) {
expr(c);
emit(c, CAT_BYTECODE_OP_RETURN, 0, 0, line);
} else {
@@ -720,6 +756,66 @@ static void stat(Compiler *c)
accept(c, CAT_LEXER_TOK_SEMI);
break;
case CAT_LEXER_TOK_YIELD:
/* `yield [expr]` suspends the enclosing function; its body now
* marks the function as a generator (calls return a generator). */
next(c);
if (!check(c, CAT_LEXER_TOK_END) && !check(c, CAT_LEXER_TOK_EOF) &&
!check(c, CAT_LEXER_TOK_SEMI) && !check(c, CAT_LEXER_TOK_ELSE) &&
!check(c, CAT_LEXER_TOK_ELSEIF) && !check(c, CAT_LEXER_TOK_UNTIL) &&
!check(c, CAT_LEXER_TOK_EXCEPT))
expr(c);
else
emit(c, CAT_BYTECODE_OP_NIL, 0, 0, line);
emit(c, CAT_BYTECODE_OP_YIELD, 0, 0, line);
ctx(c)->is_generator = 1;
accept(c, CAT_LEXER_TOK_SEMI);
break;
case CAT_LEXER_TOK_RAISE:
/* `raise expr` throws the value; caught by an enclosing try. */
next(c);
if (!check(c, CAT_LEXER_TOK_END) && !check(c, CAT_LEXER_TOK_EOF) &&
!check(c, CAT_LEXER_TOK_SEMI))
expr(c);
else
emit(c, CAT_BYTECODE_OP_NIL, 0, 0, line);
emit(c, CAT_BYTECODE_OP_RAISE, 0, 0, line);
accept(c, CAT_LEXER_TOK_SEMI);
break;
case CAT_LEXER_TOK_TRY: {
/* try <block> except [NAME] <block> end. On error the VM
* unwinds to the handler with the error value on the stack. */
int setup, jend;
next(c);
setup = emit(c, CAT_BYTECODE_OP_TRYSETUP, 0, 0, line);
block(c);
emit(c, CAT_BYTECODE_OP_TRYEND, 0, 0, line);
jend = emit(c, CAT_BYTECODE_OP_JUMP, 0, 0, line);
/* handler entry */
cur_fn(c)->code[setup].a = cur_fn(c)->ncode;
if (accept(c, CAT_LEXER_TOK_EXCEPT)) {
if (check(c, CAT_LEXER_TOK_NAME)) {
char vn[64];
int slot;
snprintf(vn, sizeof(vn), "%s", c->tok.text);
next(c);
slot = add_local(c, vn);
if (slot >= 0)
emit(c, CAT_BYTECODE_OP_SETLOCAL, slot, 0, line);
else
emit(c, CAT_BYTECODE_OP_POP, 0, 0, line);
} else {
emit(c, CAT_BYTECODE_OP_POP, 0, 0, line);
}
block(c);
}
cur_fn(c)->code[jend].a = cur_fn(c)->ncode;
expect(c, CAT_LEXER_TOK_END, "'end'");
break;
}
case CAT_LEXER_TOK_NAME: {
/* assignment if `name =`, or field assignment if `name.attr =`.
* Otherwise rewind and parse as an ordinary expression. */
@@ -802,7 +898,7 @@ static void block(Compiler *c)
{
while (!check(c, CAT_LEXER_TOK_END) && !check(c, CAT_LEXER_TOK_EOF) &&
!check(c, CAT_LEXER_TOK_ELSE) && !check(c, CAT_LEXER_TOK_ELSEIF) &&
!check(c, CAT_LEXER_TOK_UNTIL)) {
!check(c, CAT_LEXER_TOK_UNTIL) && !check(c, CAT_LEXER_TOK_EXCEPT)) {
int before = cur_fn(c)->ncode;
/* a lone ';' is a valid empty statement, not a parse error */
if (check(c, CAT_LEXER_TOK_SEMI)) {