ReefVM/src/opcode.ts
Chris Wanstrath df9af925d3 STR_CONCAT #n
2025-10-14 12:14:27 -07:00

70 lines
3.6 KiB
TypeScript

export enum OpCode {
// stack
PUSH, // operand: constant index (number) | stack: [] → [value]
POP, // operand: none | stack: [value] → []
DUP, // operand: none | stack: [value] → [value, value]
// variables
LOAD, // operand: variable name (identifier) | stack: [] → [value]
STORE, // operand: variable name (identifier) | stack: [value] → []
TRY_LOAD, // operand: variable name (identifier) | stack: [] → [value] | load a variable (if found) or a string
// math (coerce to number, pop 2, push result)
ADD, // operand: none | stack: [a, b] → [a + b]
SUB, // operand: none | stack: [a, b] → [a - b]
MUL, // operand: none | stack: [a, b] → [a * b]
DIV, // operand: none | stack: [a, b] → [a / b]
MOD, // operand: none | stack: [a, b] → [a % b]
// comparison (pop 2, push boolean)
EQ, // operand: none | stack: [a, b] → [a == b] (deep equality)
NEQ, // operand: none | stack: [a, b] → [a != b]
LT, // operand: none | stack: [a, b] → [a < b] (numeric)
GT, // operand: none | stack: [a, b] → [a > b] (numeric)
LTE, // operand: none | stack: [a, b] → [a <= b] (numeric)
GTE, // operand: none | stack: [a, b] → [a >= b] (numeric)
// logical
NOT, // operand: none | stack: [a] → [!isTrue(a)]
// control flow
JUMP, // operand: relative offset (number) | PC-relative jump
JUMP_IF_FALSE, // operand: relative offset (number) | stack: [condition] → [] | jump if falsy
JUMP_IF_TRUE, // operand: relative offset (number) | stack: [condition] → [] | jump if truthy
BREAK, // operand: none | unwind call stack to break target
// exception handling
PUSH_TRY, // operand: absolute catch address (number) | register exception handler
PUSH_FINALLY, // operand: absolute finally address (number) | add finally to current handler
POP_TRY, // operand: none | remove exception handler (try completed successfully)
THROW, // operand: none | stack: [error] → (unwound) | throw exception
// functions
MAKE_FUNCTION, // operand: function def index (number) | stack: [] → [function] | captures scope
CALL, // operand: none | stack: [fn, ...args, posCount, namedCount] → [result] | marks break target
TAIL_CALL, // operand: none | stack: [fn, ...args, posCount, namedCount] → [result] | reuses frame
RETURN, // operand: none | stack: [value] → (restored with value) | return from function
TRY_CALL, /// operand: variable name (identifier) | stack: [] → [value] | call a function, load a variable, or load a string
// arrays
MAKE_ARRAY, // operand: item count (number) | stack: [item1, ..., itemN] → [array]
ARRAY_GET, // operand: none | stack: [array, index] → [value]
ARRAY_SET, // operand: none | stack: [array, index, value] → [] | mutates array
ARRAY_PUSH, // operand: none | stack: [array, value] → [] | mutates array
ARRAY_LEN, // operand: none | stack: [array] → [length]
// dicts
MAKE_DICT, // operand: pair count (number) | stack: [key1, val1, ..., keyN, valN] → [dict]
DICT_GET, // operand: none | stack: [dict, key] → [value] | returns null if missing
DICT_SET, // operand: none | stack: [dict, key, value] → [] | mutates dict
DICT_HAS, // operand: none | stack: [dict, key] → [boolean]
// strings
STR_CONCAT, // operand: value count (number) | stack: [val1, ..., valN] → [string] | concatenate N values
// typescript interop
CALL_NATIVE, // operand: function name (identifier) | stack: [...args] → [result] | consumes entire stack
// special
HALT // operand: none | stop execution
}