Skip to main content

harn_vm/
chunk.rs

1use std::fmt;
2
3/// Bytecode opcodes for the Harn VM.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5#[repr(u8)]
6pub enum Op {
7    /// Push a constant from the constant pool onto the stack.
8    Constant, // arg: u16 constant index
9    /// Push nil onto the stack.
10    Nil,
11    /// Push true onto the stack.
12    True,
13    /// Push false onto the stack.
14    False,
15
16    // --- Variable operations ---
17    /// Get a variable by name (from constant pool).
18    GetVar, // arg: u16 constant index (name)
19    /// Define a new immutable variable. Pops value from stack.
20    DefLet, // arg: u16 constant index (name)
21    /// Define a new mutable variable. Pops value from stack.
22    DefVar, // arg: u16 constant index (name)
23    /// Assign to an existing mutable variable. Pops value from stack.
24    SetVar, // arg: u16 constant index (name)
25
26    // --- Arithmetic ---
27    Add,
28    Sub,
29    Mul,
30    Div,
31    Mod,
32    Negate,
33
34    // --- Comparison ---
35    Equal,
36    NotEqual,
37    Less,
38    Greater,
39    LessEqual,
40    GreaterEqual,
41
42    // --- Logical ---
43    Not,
44
45    // --- Control flow ---
46    /// Jump unconditionally. arg: u16 offset.
47    Jump,
48    /// Jump if top of stack is falsy. Does not pop. arg: u16 offset.
49    JumpIfFalse,
50    /// Jump if top of stack is truthy. Does not pop. arg: u16 offset.
51    JumpIfTrue,
52    /// Pop top of stack (discard).
53    Pop,
54
55    // --- Functions ---
56    /// Call a function/builtin. arg: u8 = arg count. Name is on stack below args.
57    Call,
58    /// Tail call: like Call, but replaces the current frame instead of pushing
59    /// a new one. Used for `return f(x)` to enable tail call optimization.
60    /// For builtins, behaves like a regular Call (no frame to replace).
61    TailCall,
62    /// Return from current function. Pops return value.
63    Return,
64    /// Create a closure. arg: u16 = chunk index in function table.
65    Closure,
66
67    // --- Collections ---
68    /// Build a list. arg: u16 = element count. Elements are on stack.
69    BuildList,
70    /// Build a dict. arg: u16 = entry count. Key-value pairs on stack.
71    BuildDict,
72    /// Subscript access: stack has [object, index]. Pushes result.
73    Subscript,
74
75    // --- Object operations ---
76    /// Property access. arg: u16 = constant index (property name).
77    GetProperty,
78    /// Property assignment. arg: u16 = constant index (property name).
79    /// Stack: [value] → assigns to the named variable's property.
80    SetProperty,
81    /// Subscript assignment. arg: u16 = constant index (variable name).
82    /// Stack: [index, value] → assigns to variable[index] = value.
83    SetSubscript,
84    /// Method call. arg1: u16 = constant index (method name), arg2: u8 = arg count.
85    MethodCall,
86
87    // --- String ---
88    /// String concatenation of N parts. arg: u16 = part count.
89    Concat,
90
91    // --- Iteration ---
92    /// Set up a for-in loop. Expects iterable on stack. Pushes iterator state.
93    IterInit,
94    /// Advance iterator. If exhausted, jumps. arg: u16 = jump offset.
95    /// Pushes next value and the variable name is set via DefVar before the loop.
96    IterNext,
97
98    // --- Pipe ---
99    /// Pipe: pops [value, callable], invokes callable(value).
100    Pipe,
101
102    // --- Error handling ---
103    /// Pop value, raise as error.
104    Throw,
105    /// Push exception handler. arg: u16 = offset to catch handler.
106    TryCatchSetup,
107    /// Remove top exception handler (end of try body).
108    PopHandler,
109
110    // --- Concurrency ---
111    /// Execute closure N times sequentially, push results as list.
112    /// Stack: count, closure → result_list
113    Parallel,
114    /// Execute closure for each item in list, push results as list.
115    /// Stack: list, closure → result_list
116    ParallelMap,
117    /// Store closure for deferred execution, push TaskHandle.
118    /// Stack: closure → TaskHandle
119    Spawn,
120
121    // --- Imports ---
122    /// Import a file. arg: u16 = constant index (path string).
123    Import,
124    /// Selective import. arg1: u16 = path string, arg2: u16 = names list constant.
125    SelectiveImport,
126
127    // --- Deadline ---
128    /// Pop duration value, push deadline onto internal deadline stack.
129    DeadlineSetup,
130    /// Pop deadline from internal deadline stack.
131    DeadlineEnd,
132
133    // --- Enum ---
134    /// Build an enum variant value.
135    /// arg1: u16 = constant index (enum name), arg2: u16 = constant index (variant name),
136    /// arg3: u16 = field count. Fields are on stack.
137    BuildEnum,
138
139    // --- Match ---
140    /// Match an enum pattern. Checks enum_name + variant on the top of stack (dup'd match value).
141    /// arg1: u16 = constant index (enum name), arg2: u16 = constant index (variant name).
142    /// If match succeeds, pushes true; else pushes false.
143    MatchEnum,
144
145    // --- Loop control ---
146    /// Pop the top iterator from the iterator stack (cleanup on break from for-in).
147    PopIterator,
148
149    // --- Misc ---
150    /// Duplicate top of stack.
151    Dup,
152    /// Swap top two stack values.
153    Swap,
154}
155
156/// A constant value in the constant pool.
157#[derive(Debug, Clone, PartialEq)]
158pub enum Constant {
159    Int(i64),
160    Float(f64),
161    String(String),
162    Bool(bool),
163    Nil,
164    Duration(u64),
165}
166
167impl fmt::Display for Constant {
168    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
169        match self {
170            Constant::Int(n) => write!(f, "{n}"),
171            Constant::Float(n) => write!(f, "{n}"),
172            Constant::String(s) => write!(f, "\"{s}\""),
173            Constant::Bool(b) => write!(f, "{b}"),
174            Constant::Nil => write!(f, "nil"),
175            Constant::Duration(ms) => write!(f, "{ms}ms"),
176        }
177    }
178}
179
180/// A compiled chunk of bytecode.
181#[derive(Debug, Clone)]
182pub struct Chunk {
183    /// The bytecode instructions.
184    pub code: Vec<u8>,
185    /// Constant pool.
186    pub constants: Vec<Constant>,
187    /// Source line numbers for each instruction (for error reporting).
188    pub lines: Vec<u32>,
189    /// Compiled function bodies (for closures).
190    pub functions: Vec<CompiledFunction>,
191}
192
193/// A compiled function (closure body).
194#[derive(Debug, Clone)]
195pub struct CompiledFunction {
196    pub name: String,
197    pub params: Vec<String>,
198    pub chunk: Chunk,
199}
200
201impl Chunk {
202    pub fn new() -> Self {
203        Self {
204            code: Vec::new(),
205            constants: Vec::new(),
206            lines: Vec::new(),
207            functions: Vec::new(),
208        }
209    }
210
211    /// Add a constant and return its index.
212    pub fn add_constant(&mut self, constant: Constant) -> u16 {
213        // Reuse existing constant if possible
214        for (i, c) in self.constants.iter().enumerate() {
215            if c == &constant {
216                return i as u16;
217            }
218        }
219        let idx = self.constants.len();
220        self.constants.push(constant);
221        idx as u16
222    }
223
224    /// Emit a single-byte instruction.
225    pub fn emit(&mut self, op: Op, line: u32) {
226        self.code.push(op as u8);
227        self.lines.push(line);
228    }
229
230    /// Emit an instruction with a u16 argument.
231    pub fn emit_u16(&mut self, op: Op, arg: u16, line: u32) {
232        self.code.push(op as u8);
233        self.code.push((arg >> 8) as u8);
234        self.code.push((arg & 0xFF) as u8);
235        self.lines.push(line);
236        self.lines.push(line);
237        self.lines.push(line);
238    }
239
240    /// Emit an instruction with a u8 argument.
241    pub fn emit_u8(&mut self, op: Op, arg: u8, line: u32) {
242        self.code.push(op as u8);
243        self.code.push(arg);
244        self.lines.push(line);
245        self.lines.push(line);
246    }
247
248    /// Emit a method call: op + u16 (method name) + u8 (arg count).
249    pub fn emit_method_call(&mut self, name_idx: u16, arg_count: u8, line: u32) {
250        self.code.push(Op::MethodCall as u8);
251        self.code.push((name_idx >> 8) as u8);
252        self.code.push((name_idx & 0xFF) as u8);
253        self.code.push(arg_count);
254        self.lines.push(line);
255        self.lines.push(line);
256        self.lines.push(line);
257        self.lines.push(line);
258    }
259
260    /// Current code offset (for jump patching).
261    pub fn current_offset(&self) -> usize {
262        self.code.len()
263    }
264
265    /// Emit a jump instruction with a placeholder offset. Returns the position to patch.
266    pub fn emit_jump(&mut self, op: Op, line: u32) -> usize {
267        self.code.push(op as u8);
268        let patch_pos = self.code.len();
269        self.code.push(0xFF);
270        self.code.push(0xFF);
271        self.lines.push(line);
272        self.lines.push(line);
273        self.lines.push(line);
274        patch_pos
275    }
276
277    /// Patch a jump instruction at the given position to jump to the current offset.
278    pub fn patch_jump(&mut self, patch_pos: usize) {
279        let target = self.code.len() as u16;
280        self.code[patch_pos] = (target >> 8) as u8;
281        self.code[patch_pos + 1] = (target & 0xFF) as u8;
282    }
283
284    /// Read a u16 argument at the given position.
285    pub fn read_u16(&self, pos: usize) -> u16 {
286        ((self.code[pos] as u16) << 8) | (self.code[pos + 1] as u16)
287    }
288
289    /// Disassemble for debugging.
290    pub fn disassemble(&self, name: &str) -> String {
291        let mut out = format!("== {name} ==\n");
292        let mut ip = 0;
293        while ip < self.code.len() {
294            let op = self.code[ip];
295            let line = self.lines.get(ip).copied().unwrap_or(0);
296            out.push_str(&format!("{:04} [{:>4}] ", ip, line));
297            ip += 1;
298
299            match op {
300                x if x == Op::Constant as u8 => {
301                    let idx = self.read_u16(ip);
302                    ip += 2;
303                    let val = &self.constants[idx as usize];
304                    out.push_str(&format!("CONSTANT {:>4} ({})\n", idx, val));
305                }
306                x if x == Op::Nil as u8 => out.push_str("NIL\n"),
307                x if x == Op::True as u8 => out.push_str("TRUE\n"),
308                x if x == Op::False as u8 => out.push_str("FALSE\n"),
309                x if x == Op::GetVar as u8 => {
310                    let idx = self.read_u16(ip);
311                    ip += 2;
312                    out.push_str(&format!(
313                        "GET_VAR {:>4} ({})\n",
314                        idx, self.constants[idx as usize]
315                    ));
316                }
317                x if x == Op::DefLet as u8 => {
318                    let idx = self.read_u16(ip);
319                    ip += 2;
320                    out.push_str(&format!(
321                        "DEF_LET {:>4} ({})\n",
322                        idx, self.constants[idx as usize]
323                    ));
324                }
325                x if x == Op::DefVar as u8 => {
326                    let idx = self.read_u16(ip);
327                    ip += 2;
328                    out.push_str(&format!(
329                        "DEF_VAR {:>4} ({})\n",
330                        idx, self.constants[idx as usize]
331                    ));
332                }
333                x if x == Op::SetVar as u8 => {
334                    let idx = self.read_u16(ip);
335                    ip += 2;
336                    out.push_str(&format!(
337                        "SET_VAR {:>4} ({})\n",
338                        idx, self.constants[idx as usize]
339                    ));
340                }
341                x if x == Op::Add as u8 => out.push_str("ADD\n"),
342                x if x == Op::Sub as u8 => out.push_str("SUB\n"),
343                x if x == Op::Mul as u8 => out.push_str("MUL\n"),
344                x if x == Op::Div as u8 => out.push_str("DIV\n"),
345                x if x == Op::Mod as u8 => out.push_str("MOD\n"),
346                x if x == Op::Negate as u8 => out.push_str("NEGATE\n"),
347                x if x == Op::Equal as u8 => out.push_str("EQUAL\n"),
348                x if x == Op::NotEqual as u8 => out.push_str("NOT_EQUAL\n"),
349                x if x == Op::Less as u8 => out.push_str("LESS\n"),
350                x if x == Op::Greater as u8 => out.push_str("GREATER\n"),
351                x if x == Op::LessEqual as u8 => out.push_str("LESS_EQUAL\n"),
352                x if x == Op::GreaterEqual as u8 => out.push_str("GREATER_EQUAL\n"),
353                x if x == Op::Not as u8 => out.push_str("NOT\n"),
354                x if x == Op::Jump as u8 => {
355                    let target = self.read_u16(ip);
356                    ip += 2;
357                    out.push_str(&format!("JUMP {:>4}\n", target));
358                }
359                x if x == Op::JumpIfFalse as u8 => {
360                    let target = self.read_u16(ip);
361                    ip += 2;
362                    out.push_str(&format!("JUMP_IF_FALSE {:>4}\n", target));
363                }
364                x if x == Op::JumpIfTrue as u8 => {
365                    let target = self.read_u16(ip);
366                    ip += 2;
367                    out.push_str(&format!("JUMP_IF_TRUE {:>4}\n", target));
368                }
369                x if x == Op::Pop as u8 => out.push_str("POP\n"),
370                x if x == Op::Call as u8 => {
371                    let argc = self.code[ip];
372                    ip += 1;
373                    out.push_str(&format!("CALL {:>4}\n", argc));
374                }
375                x if x == Op::TailCall as u8 => {
376                    let argc = self.code[ip];
377                    ip += 1;
378                    out.push_str(&format!("TAIL_CALL {:>4}\n", argc));
379                }
380                x if x == Op::Return as u8 => out.push_str("RETURN\n"),
381                x if x == Op::Closure as u8 => {
382                    let idx = self.read_u16(ip);
383                    ip += 2;
384                    out.push_str(&format!("CLOSURE {:>4}\n", idx));
385                }
386                x if x == Op::BuildList as u8 => {
387                    let count = self.read_u16(ip);
388                    ip += 2;
389                    out.push_str(&format!("BUILD_LIST {:>4}\n", count));
390                }
391                x if x == Op::BuildDict as u8 => {
392                    let count = self.read_u16(ip);
393                    ip += 2;
394                    out.push_str(&format!("BUILD_DICT {:>4}\n", count));
395                }
396                x if x == Op::Subscript as u8 => out.push_str("SUBSCRIPT\n"),
397                x if x == Op::GetProperty as u8 => {
398                    let idx = self.read_u16(ip);
399                    ip += 2;
400                    out.push_str(&format!(
401                        "GET_PROPERTY {:>4} ({})\n",
402                        idx, self.constants[idx as usize]
403                    ));
404                }
405                x if x == Op::SetProperty as u8 => {
406                    let idx = self.read_u16(ip);
407                    ip += 2;
408                    out.push_str(&format!(
409                        "SET_PROPERTY {:>4} ({})\n",
410                        idx, self.constants[idx as usize]
411                    ));
412                }
413                x if x == Op::SetSubscript as u8 => {
414                    let idx = self.read_u16(ip);
415                    ip += 2;
416                    out.push_str(&format!(
417                        "SET_SUBSCRIPT {:>4} ({})\n",
418                        idx, self.constants[idx as usize]
419                    ));
420                }
421                x if x == Op::MethodCall as u8 => {
422                    let idx = self.read_u16(ip);
423                    ip += 2;
424                    let argc = self.code[ip];
425                    ip += 1;
426                    out.push_str(&format!(
427                        "METHOD_CALL {:>4} ({}) argc={}\n",
428                        idx, self.constants[idx as usize], argc
429                    ));
430                }
431                x if x == Op::Concat as u8 => {
432                    let count = self.read_u16(ip);
433                    ip += 2;
434                    out.push_str(&format!("CONCAT {:>4}\n", count));
435                }
436                x if x == Op::IterInit as u8 => out.push_str("ITER_INIT\n"),
437                x if x == Op::IterNext as u8 => {
438                    let target = self.read_u16(ip);
439                    ip += 2;
440                    out.push_str(&format!("ITER_NEXT {:>4}\n", target));
441                }
442                x if x == Op::Throw as u8 => out.push_str("THROW\n"),
443                x if x == Op::TryCatchSetup as u8 => {
444                    let target = self.read_u16(ip);
445                    ip += 2;
446                    out.push_str(&format!("TRY_CATCH_SETUP {:>4}\n", target));
447                }
448                x if x == Op::PopHandler as u8 => out.push_str("POP_HANDLER\n"),
449                x if x == Op::Pipe as u8 => out.push_str("PIPE\n"),
450                x if x == Op::Parallel as u8 => out.push_str("PARALLEL\n"),
451                x if x == Op::ParallelMap as u8 => out.push_str("PARALLEL_MAP\n"),
452                x if x == Op::Spawn as u8 => out.push_str("SPAWN\n"),
453                x if x == Op::Import as u8 => {
454                    let idx = self.read_u16(ip);
455                    ip += 2;
456                    out.push_str(&format!(
457                        "IMPORT {:>4} ({})\n",
458                        idx, self.constants[idx as usize]
459                    ));
460                }
461                x if x == Op::SelectiveImport as u8 => {
462                    let path_idx = self.read_u16(ip);
463                    ip += 2;
464                    let names_idx = self.read_u16(ip);
465                    ip += 2;
466                    out.push_str(&format!(
467                        "SELECTIVE_IMPORT {:>4} ({}) names: {:>4} ({})\n",
468                        path_idx,
469                        self.constants[path_idx as usize],
470                        names_idx,
471                        self.constants[names_idx as usize]
472                    ));
473                }
474                x if x == Op::DeadlineSetup as u8 => out.push_str("DEADLINE_SETUP\n"),
475                x if x == Op::DeadlineEnd as u8 => out.push_str("DEADLINE_END\n"),
476                x if x == Op::BuildEnum as u8 => {
477                    let enum_idx = self.read_u16(ip);
478                    ip += 2;
479                    let variant_idx = self.read_u16(ip);
480                    ip += 2;
481                    let field_count = self.read_u16(ip);
482                    ip += 2;
483                    out.push_str(&format!(
484                        "BUILD_ENUM {:>4} ({}) {:>4} ({}) fields={}\n",
485                        enum_idx,
486                        self.constants[enum_idx as usize],
487                        variant_idx,
488                        self.constants[variant_idx as usize],
489                        field_count
490                    ));
491                }
492                x if x == Op::MatchEnum as u8 => {
493                    let enum_idx = self.read_u16(ip);
494                    ip += 2;
495                    let variant_idx = self.read_u16(ip);
496                    ip += 2;
497                    out.push_str(&format!(
498                        "MATCH_ENUM {:>4} ({}) {:>4} ({})\n",
499                        enum_idx,
500                        self.constants[enum_idx as usize],
501                        variant_idx,
502                        self.constants[variant_idx as usize]
503                    ));
504                }
505                x if x == Op::PopIterator as u8 => out.push_str("POP_ITERATOR\n"),
506                x if x == Op::Dup as u8 => out.push_str("DUP\n"),
507                x if x == Op::Swap as u8 => out.push_str("SWAP\n"),
508                _ => {
509                    out.push_str(&format!("UNKNOWN(0x{:02x})\n", op));
510                }
511            }
512        }
513        out
514    }
515}
516
517impl Default for Chunk {
518    fn default() -> Self {
519        Self::new()
520    }
521}