Files
catscript/include/cat/bytecode.h
T
Minus b945182739 interface + structural is: runtime-checked interfaces
Add nominal interface declarations with structural, runtime checking
(the owner asked for the operator AND the runtime check).

- lexer: `interface` and `is` are keywords; `implements` is a soft
  keyword (matched by text in the class clause) so the `implements()`
  stdlib function is not shadowed.
- `interface NAME [extends BASE] <function m() end> end` builds a table
  tagged `__interface`; members name the required methods (with an
  optional default body, shared via `__index`). Interface inheritance
  follows `__index`.
- class: `class C [extends B] [implements I, ...]` -- after the class
  global is set, `__implements_check` runs and raises if the class does
  not satisfy each declared interface.
- new opcode `IS` (`obj is iface`): structural membership test, and it
  degrades to an exact type (vtable) match when the right side is not an
  interface, so `x is SomeClass` reads naturally. Backed by
  `cat_value_is()`.
- stdlib: `implements(obj, iface)` (the same test as a function).
- tests/interface.cat + suite block: interface satisfaction, interface
  inheritance, duck typing (no `implements` needed), and a missing
  method failing. Suite 130 -> 136, all green.
2026-11-18 15:56:10 +00:00

120 lines
4.6 KiB
C

/*
* cat/bytecode.h -- the catscript bytecode format.
*
* Naming rule: symbols here are cat_bytecode_* / CAT_BYTECODE_*.
*
* A compiled module is one CAT_BYTECODE_Function. Instructions are a
* flat array of CAT_BYTECODE_Instr. The VM (cat/vm.h) executes them.
*
* Operand conventions:
* - A/B/C are small integer operands (register-free, stack based).
* - constants live in `consts[]`; locals in the current frame;s slots.
*/
#ifndef CAT_BYTECODE_H
#define CAT_BYTECODE_H
#include "cat/value.h"
#include <stdio.h>
/* Opcodes (documented, stable). */
enum CAT_BYTECODE_Op {
CAT_BYTECODE_OP_NOP = 0,
CAT_BYTECODE_OP_CONST, /* A: index into consts[] */
CAT_BYTECODE_OP_EMPTY, /* push the empty object */
CAT_BYTECODE_OP_TRUE, /* push true */
CAT_BYTECODE_OP_FALSE, /* push false */
CAT_BYTECODE_OP_GETLOCAL, /* A: local slot -> stack */
CAT_BYTECODE_OP_SETLOCAL, /* A: pop -> local slot */
CAT_BYTECODE_OP_GETGLOBAL, /* A: const name -> stack */
CAT_BYTECODE_OP_SETGLOBAL, /* A: const name, pop value */
CAT_BYTECODE_OP_ADD, /* pop b, a -> push a+b */
CAT_BYTECODE_OP_SUB,
CAT_BYTECODE_OP_MUL,
CAT_BYTECODE_OP_DIV,
CAT_BYTECODE_OP_REM,
CAT_BYTECODE_OP_NEG, /* unary minus */
CAT_BYTECODE_OP_NOT,
CAT_BYTECODE_OP_EQ,
CAT_BYTECODE_OP_NE,
CAT_BYTECODE_OP_LT,
CAT_BYTECODE_OP_LE,
CAT_BYTECODE_OP_GT,
CAT_BYTECODE_OP_GE,
CAT_BYTECODE_OP_GETATTR, /* A: const name; pop obj -> stack */
CAT_BYTECODE_OP_SETATTR, /* A: const name; pop val, obj */
CAT_BYTECODE_OP_NEWTABLE, /* push empty table */
CAT_BYTECODE_OP_CALL, /* A: nargs; pop fn+args */
CAT_BYTECODE_OP_CALLMETHOD, /* A: nargs; B: const name */
CAT_BYTECODE_OP_JUMP, /* A: absolute target */
CAT_BYTECODE_OP_JUMPIF, /* A: target; pop cond */
CAT_BYTECODE_OP_JUMPIFNOT, /* A: target; pop cond */
CAT_BYTECODE_OP_RETURN, /* pop -> return */
CAT_BYTECODE_OP_RETURN0, /* return the empty object */
CAT_BYTECODE_OP_POP, /* discard top */
CAT_BYTECODE_OP_PRINT, /* pop, print */
CAT_BYTECODE_OP_CLOSURE, /* A: index into protos[] */
CAT_BYTECODE_OP_GETUPVAL, /* A: upvalue slot */
CAT_BYTECODE_OP_CONCAT, /* string concat fast path */
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 empty */
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 */
CAT_BYTECODE_OP_PRINTEXPR, /* pop, print its repr (REPL echo) */
CAT_BYTECODE_OP_IS, /* pop iface, obj -> push obj is iface (structural) */
};
/* One instruction: opcode + three integer operands. */
typedef struct CAT_BYTECODE_Instr {
unsigned char op; /* a CAT_BYTECODE_Op */
int a;
int b;
int c;
int line; /* source line for diagnostics */
} CAT_BYTECODE_Instr;
struct CAT_BYTECODE_Function;
/* A constant pool entry: a value plus (for names) a string. */
typedef struct CAT_BYTECODE_Constant {
CAT_VALUE_Value *value;
} CAT_BYTECODE_Constant;
/* CAT_BYTECODE_Function -- a compiled chunk.
* `protos` holds nested function prototypes (for `function` literals). */
typedef struct CAT_BYTECODE_Function {
char *name;
CAT_BYTECODE_Instr *code;
int ncode, capcode;
CAT_BYTECODE_Constant *consts;
int nconsts, capconsts;
struct CAT_BYTECODE_Function **protos;
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);
/* Emit one instruction, returning its index. */
int cat_bytecode_emit(CAT_BYTECODE_Function *fn, unsigned char op,
int a, int b, int c, int line);
/* Add a constant; returns its index. */
int cat_bytecode_add_const(CAT_BYTECODE_Function *fn, CAT_VALUE_Value *v);
/* Add a nested prototype; returns its index. */
int cat_bytecode_add_proto(CAT_BYTECODE_Function *fn, CAT_BYTECODE_Function *p);
/* Human-readable disassembly (used by `--dump`). */
void cat_bytecode_dump(CAT_BYTECODE_Function *fn, FILE *out);
void cat_bytecode_function_free(CAT_BYTECODE_Function *fn);
#endif /* CAT_BYTECODE_H */