2024-11-01 12:06:28 +00:00
|
|
|
export type Program = Ins[];
|
2024-11-18 09:42:11 +00:00
|
|
|
export type Ins = Ops | number;
|
|
|
|
|
2024-11-08 11:22:42 +00:00
|
|
|
// NOTICE: keep up to date with runtime/arch.hpp
|
|
|
|
|
2024-11-08 08:24:09 +00:00
|
|
|
export type Ops = typeof Ops;
|
2024-11-01 12:06:28 +00:00
|
|
|
export const Ops = {
|
2024-12-10 22:30:15 +00:00
|
|
|
Nop: 0x00,
|
|
|
|
PushNull: 0x01,
|
|
|
|
PushInt: 0x02,
|
|
|
|
PushBool: 0x03,
|
|
|
|
PushString: 0x04,
|
|
|
|
PushPtr: 0x05,
|
|
|
|
Pop: 0x06,
|
|
|
|
ReserveStatic: 0x07,
|
|
|
|
LoadStatic: 0x08,
|
|
|
|
StoreStatic: 0x09,
|
|
|
|
LoadLocal: 0x0a,
|
|
|
|
StoreLocal: 0x0b,
|
|
|
|
Call: 0x0c,
|
|
|
|
Return: 0x0d,
|
|
|
|
Jump: 0x0e,
|
|
|
|
JumpIfTrue: 0x0f,
|
|
|
|
Builtin: 0x10,
|
|
|
|
Add: 0x20,
|
|
|
|
Subtract: 0x21,
|
|
|
|
Multiply: 0x22,
|
|
|
|
Divide: 0x23,
|
|
|
|
Remainder: 0x24,
|
|
|
|
Equal: 0x25,
|
|
|
|
LessThan: 0x26,
|
|
|
|
And: 0x27,
|
|
|
|
Or: 0x28,
|
|
|
|
Xor: 0x29,
|
|
|
|
Not: 0x2a,
|
|
|
|
SourceMap: 0x30,
|
2024-12-10 13:36:41 +00:00
|
|
|
} as const;
|
|
|
|
|
|
|
|
export type Builtins = typeof Builtins;
|
|
|
|
export const Builtins = {
|
2024-12-10 22:30:15 +00:00
|
|
|
StringConcat: 0x10,
|
|
|
|
StringEqual: 0x11,
|
2024-12-12 15:07:59 +00:00
|
|
|
StringCharAt: 0x12,
|
|
|
|
StringLength: 0x13,
|
|
|
|
StringPushChar: 0x14,
|
|
|
|
ArrayNew: 0x20,
|
|
|
|
ArraySet: 0x21,
|
|
|
|
ArrayPush: 0x22,
|
|
|
|
ArrayAt: 0x23,
|
|
|
|
ArrayLength: 0x24,
|
2024-12-10 22:30:15 +00:00
|
|
|
StructSet: 0x30,
|
2024-12-11 11:36:19 +00:00
|
|
|
Print: 0x40,
|
2024-11-01 12:06:28 +00:00
|
|
|
} as const;
|
2024-12-10 22:30:15 +00:00
|
|
|
|
|
|
|
export function opToString(op: number): string {
|
2024-12-13 05:09:10 +00:00
|
|
|
return Object.entries(Ops)
|
|
|
|
.find(([_key, value]) => value === op)
|
|
|
|
?.[0] ?? `<unknown Op ${op}>`;
|
2024-12-10 22:30:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function builtinToString(builtin: number): string {
|
2024-12-13 05:09:10 +00:00
|
|
|
return Object.entries(Builtins)
|
|
|
|
.find(([_key, value]) => value === builtin)
|
|
|
|
?.[0] ?? `<unknown Builtin ${builtin}>`;
|
2024-12-10 22:30:15 +00:00
|
|
|
}
|