Files
catscript/include/cat/bytecode.h
T
Minus 52d118211f catscript: núcleo C, stdlib (TOML), família de arquivos e front-end self-hosted
- Modelo de objetos Cat_Value + vtable (interfaces abstratas)
- VM de bytecode, sem GIL (VM por-thread, nativas como valores)
- Stdlib com TOML embutido (toml_parse/toml_encode)
- Família de arquivos: AbstractFile (abstrata), File::open, StringIO, BytesIO
- Front-end (lexer/parser/checker/compiler) implementado em catscript
- cpk (cat package manager) como primeiro programa; manifest package.toml
- Type hints advisory, inclusive DynSized() e sentinel_sized<...>
- 25/25 testes passando (make test)
2026-11-16 01:27:44 +00:00

111 lines
4.0 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_NIL, /* push nil */
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 nil */
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 */
};
/* 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 */
} 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 */