Skip to main content

zapcode_core/compiler/
instruction.rs

1use serde::{Deserialize, Serialize};
2
3/// Bytecode instructions for the Zapcode VM.
4#[derive(Debug, Clone, Serialize, Deserialize)]
5pub enum Instruction {
6    // Stack
7    Push(Constant),
8    Pop,
9    Dup,
10
11    // Variables
12    LoadLocal(usize),
13    StoreLocal(usize),
14    LoadGlobal(String),
15    StoreGlobal(String),
16    DeclareLocal(String),
17
18    // Arithmetic
19    Add,
20    Sub,
21    Mul,
22    Div,
23    Rem,
24    Pow,
25    Neg,
26    BitNot,
27    BitAnd,
28    BitOr,
29    BitXor,
30    Shl,
31    Shr,
32    Ushr,
33
34    // Comparison
35    Eq,
36    Neq,
37    StrictEq,
38    StrictNeq,
39    Lt,
40    Lte,
41    Gt,
42    Gte,
43
44    // Logical
45    Not,
46
47    // Objects & Arrays
48    CreateArray(usize),
49    CreateObject(usize),
50    GetProperty(String),
51    SetProperty(String),
52    GetIndex,
53    SetIndex,
54    Spread,
55    In,
56    InstanceOf,
57
58    // Functions
59    CreateClosure(usize),
60    Call(usize),
61    Return,
62    CallExternal(String, usize),
63
64    // Control flow
65    Jump(usize),
66    JumpIfFalse(usize),
67    JumpIfTrue(usize),
68    JumpIfNullish(usize),
69
70    // Loops
71    SetupLoop,
72    Break,
73    Continue,
74
75    // Iterators
76    GetIterator,
77    IteratorNext,
78    IteratorDone,
79
80    // Error handling
81    SetupTry(usize, Option<usize>),
82    Throw,
83    EndTry,
84
85    // Typeof
86    TypeOf,
87
88    // Void
89    Void,
90
91    // Update
92    Increment,
93    Decrement,
94
95    // Template literals
96    ConcatStrings(usize),
97
98    // Destructuring
99    DestructureObject(Vec<String>),
100    DestructureArray(usize),
101
102    // Classes
103    /// Create a class: pops constructor closure (or undefined), then n_methods method closures
104    /// with method names, then n_static static closures with names, then optional super class.
105    /// Pushes the class object (an Object with __constructor__, __prototype__, __class_name__).
106    CreateClass {
107        name: String,
108        n_methods: usize,
109        n_statics: usize,
110        has_super: bool,
111    },
112    /// Construct: pops class object + args, creates instance, calls constructor, pushes instance.
113    Construct(usize),
114    /// Load `this` from the current call frame.
115    LoadThis,
116    /// Store a value as the current `this` (used for this.prop = val).
117    StoreThis,
118    /// Call super constructor with n args. Pops args, looks up __super__.__constructor__,
119    /// calls it with current `this`.
120    CallSuper(usize),
121
122    // Generators
123    /// Create a generator object from a function index (like CreateClosure but for generators).
124    CreateGenerator(usize),
125    /// Yield a value from a generator. Pops the value, suspends execution.
126    Yield,
127
128    /// Await: if the top-of-stack is a resolved Promise object, unwrap its value.
129    /// If it's a regular value, leave it as-is. External call suspension is handled
130    /// by CallExternal before Await is reached.
131    Await,
132
133    // Misc
134    Nop,
135}
136
137#[derive(Debug, Clone, Serialize, Deserialize)]
138pub enum Constant {
139    Undefined,
140    Null,
141    Bool(bool),
142    Int(i64),
143    Float(f64),
144    String(String),
145}