- delete the nil type; catscript has no nil and no null - add the empty object (Object with no members), exposed as the global 'empty'; it is a shared immutable singleton - 'nil' is no longer a keyword: it is an undefined global, which raises - undefined global -> error (consistent with a missing attribute) - only 'false' is falsy (numbers/objects are truthy); booleans kept - OP_NIL -> OP_EMPTY; compiler, VM, stdlib, toml, ffi, generator, file, list, selector, value all migrated - rename the table type to Object - std/ front-end and examples/tests migrated to 'empty' - 84/84 tests
118 lines
4.5 KiB
C
118 lines
4.5 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 */
|
|
};
|
|
|
|
/* 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 */
|