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    /// Push a new lexical scope onto the environment stack.
26    PushScope,
27    /// Pop the current lexical scope from the environment stack.
28    PopScope,
29
30    // --- Arithmetic ---
31    Add,
32    Sub,
33    Mul,
34    Div,
35    Mod,
36    Pow,
37    Negate,
38
39    // --- Comparison ---
40    Equal,
41    NotEqual,
42    Less,
43    Greater,
44    LessEqual,
45    GreaterEqual,
46
47    // --- Logical ---
48    Not,
49
50    // --- Control flow ---
51    /// Jump unconditionally. arg: u16 offset.
52    Jump,
53    /// Jump if top of stack is falsy. Does not pop. arg: u16 offset.
54    JumpIfFalse,
55    /// Jump if top of stack is truthy. Does not pop. arg: u16 offset.
56    JumpIfTrue,
57    /// Pop top of stack (discard).
58    Pop,
59
60    // --- Functions ---
61    /// Call a function/builtin. arg: u8 = arg count. Name is on stack below args.
62    Call,
63    /// Tail call: like Call, but replaces the current frame instead of pushing
64    /// a new one. Used for `return f(x)` to enable tail call optimization.
65    /// For builtins, behaves like a regular Call (no frame to replace).
66    TailCall,
67    /// Return from current function. Pops return value.
68    Return,
69    /// Create a closure. arg: u16 = chunk index in function table.
70    Closure,
71
72    // --- Collections ---
73    /// Build a list. arg: u16 = element count. Elements are on stack.
74    BuildList,
75    /// Build a dict. arg: u16 = entry count. Key-value pairs on stack.
76    BuildDict,
77    /// Subscript access: stack has [object, index]. Pushes result.
78    Subscript,
79    /// Slice access: stack has [object, start_or_nil, end_or_nil]. Pushes sublist/substring.
80    Slice,
81
82    // --- Object operations ---
83    /// Property access. arg: u16 = constant index (property name).
84    GetProperty,
85    /// Optional property access (?.). Like GetProperty but returns nil
86    /// instead of erroring when the object is nil. arg: u16 = constant index.
87    GetPropertyOpt,
88    /// Property assignment. arg: u16 = constant index (property name).
89    /// Stack: [value] → assigns to the named variable's property.
90    SetProperty,
91    /// Subscript assignment. arg: u16 = constant index (variable name).
92    /// Stack: [index, value] → assigns to variable[index] = value.
93    SetSubscript,
94    /// Method call. arg1: u16 = constant index (method name), arg2: u8 = arg count.
95    MethodCall,
96    /// Optional method call (?.). Like MethodCall but returns nil if the
97    /// receiver is nil instead of dispatching. arg1: u16, arg2: u8.
98    MethodCallOpt,
99
100    // --- String ---
101    /// String concatenation of N parts. arg: u16 = part count.
102    Concat,
103
104    // --- Iteration ---
105    /// Set up a for-in loop. Expects iterable on stack. Pushes iterator state.
106    IterInit,
107    /// Advance iterator. If exhausted, jumps. arg: u16 = jump offset.
108    /// Pushes next value and the variable name is set via DefVar before the loop.
109    IterNext,
110
111    // --- Pipe ---
112    /// Pipe: pops [value, callable], invokes callable(value).
113    Pipe,
114
115    // --- Error handling ---
116    /// Pop value, raise as error.
117    Throw,
118    /// Push exception handler. arg: u16 = offset to catch handler.
119    TryCatchSetup,
120    /// Remove top exception handler (end of try body).
121    PopHandler,
122
123    // --- Concurrency ---
124    /// Execute closure N times sequentially, push results as list.
125    /// Stack: count, closure → result_list
126    Parallel,
127    /// Execute closure for each item in list, push results as list.
128    /// Stack: list, closure → result_list
129    ParallelMap,
130    /// Like ParallelMap but wraps each result in Result.Ok/Err, never fails.
131    /// Stack: list, closure → {results: [Result], succeeded: int, failed: int}
132    ParallelSettle,
133    /// Store closure for deferred execution, push TaskHandle.
134    /// Stack: closure → TaskHandle
135    Spawn,
136
137    // --- Imports ---
138    /// Import a file. arg: u16 = constant index (path string).
139    Import,
140    /// Selective import. arg1: u16 = path string, arg2: u16 = names list constant.
141    SelectiveImport,
142
143    // --- Deadline ---
144    /// Pop duration value, push deadline onto internal deadline stack.
145    DeadlineSetup,
146    /// Pop deadline from internal deadline stack.
147    DeadlineEnd,
148
149    // --- Enum ---
150    /// Build an enum variant value.
151    /// arg1: u16 = constant index (enum name), arg2: u16 = constant index (variant name),
152    /// arg3: u16 = field count. Fields are on stack.
153    BuildEnum,
154
155    // --- Match ---
156    /// Match an enum pattern. Checks enum_name + variant on the top of stack (dup'd match value).
157    /// arg1: u16 = constant index (enum name), arg2: u16 = constant index (variant name).
158    /// If match succeeds, pushes true; else pushes false.
159    MatchEnum,
160
161    // --- Loop control ---
162    /// Pop the top iterator from the iterator stack (cleanup on break from for-in).
163    PopIterator,
164
165    // --- Defaults ---
166    /// Push the number of arguments passed to the current function call.
167    GetArgc,
168
169    // --- Type checking ---
170    /// Runtime type check on a variable.
171    /// arg1: u16 = constant index (variable name),
172    /// arg2: u16 = constant index (expected type name).
173    /// Throws a TypeError if the variable's type doesn't match.
174    CheckType,
175
176    // --- Result try operator ---
177    /// Try-unwrap: if top is Result.Ok(v), replace with v. If Result.Err(e), return it.
178    TryUnwrap,
179
180    // --- Spread call ---
181    /// Call with spread arguments. Stack: [callee, args_list] -> result.
182    CallSpread,
183    /// Method call with spread arguments. Stack: [object, args_list] -> result.
184    /// Followed by 2 bytes for method name constant index.
185    MethodCallSpread,
186
187    // --- Misc ---
188    /// Duplicate top of stack.
189    Dup,
190    /// Swap top two stack values.
191    Swap,
192    /// Membership test: stack has [item, collection]. Pushes bool.
193    /// Works for lists (item in list), dicts (key in dict), strings (substr in string), and sets.
194    Contains,
195
196    /// Yield a value from a generator. Pops value, sends through channel, suspends.
197    Yield,
198}
199
200/// A constant value in the constant pool.
201#[derive(Debug, Clone, PartialEq)]
202pub enum Constant {
203    Int(i64),
204    Float(f64),
205    String(String),
206    Bool(bool),
207    Nil,
208    Duration(u64),
209}
210
211impl fmt::Display for Constant {
212    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
213        match self {
214            Constant::Int(n) => write!(f, "{n}"),
215            Constant::Float(n) => write!(f, "{n}"),
216            Constant::String(s) => write!(f, "\"{s}\""),
217            Constant::Bool(b) => write!(f, "{b}"),
218            Constant::Nil => write!(f, "nil"),
219            Constant::Duration(ms) => write!(f, "{ms}ms"),
220        }
221    }
222}
223
224/// A compiled chunk of bytecode.
225#[derive(Debug, Clone)]
226pub struct Chunk {
227    /// The bytecode instructions.
228    pub code: Vec<u8>,
229    /// Constant pool.
230    pub constants: Vec<Constant>,
231    /// Source line numbers for each instruction (for error reporting).
232    pub lines: Vec<u32>,
233    /// Source column numbers for each instruction (for error reporting).
234    /// Parallel to `lines`; 0 means no column info available.
235    pub columns: Vec<u32>,
236    /// Source file that this chunk was compiled from, when known. Set for
237    /// chunks compiled from imported modules so runtime errors can report
238    /// the correct file path for each frame instead of always pointing at
239    /// the entry-point pipeline.
240    pub source_file: Option<String>,
241    /// Current column to use when emitting instructions (set by compiler).
242    current_col: u32,
243    /// Compiled function bodies (for closures).
244    pub functions: Vec<CompiledFunction>,
245}
246
247/// A compiled function (closure body).
248#[derive(Debug, Clone)]
249pub struct CompiledFunction {
250    pub name: String,
251    pub params: Vec<String>,
252    /// Index of the first parameter with a default value, or None if all required.
253    pub default_start: Option<usize>,
254    pub chunk: Chunk,
255    /// True if the function body contains `yield` expressions (generator function).
256    pub is_generator: bool,
257    /// True if the last parameter is a rest parameter (`...name`).
258    pub has_rest_param: bool,
259}
260
261impl Chunk {
262    pub fn new() -> Self {
263        Self {
264            code: Vec::new(),
265            constants: Vec::new(),
266            lines: Vec::new(),
267            columns: Vec::new(),
268            source_file: None,
269            current_col: 0,
270            functions: Vec::new(),
271        }
272    }
273
274    /// Set the current column for subsequent emit calls.
275    pub fn set_column(&mut self, col: u32) {
276        self.current_col = col;
277    }
278
279    /// Add a constant and return its index.
280    pub fn add_constant(&mut self, constant: Constant) -> u16 {
281        for (i, c) in self.constants.iter().enumerate() {
282            if c == &constant {
283                return i as u16;
284            }
285        }
286        let idx = self.constants.len();
287        self.constants.push(constant);
288        idx as u16
289    }
290
291    /// Emit a single-byte instruction.
292    pub fn emit(&mut self, op: Op, line: u32) {
293        let col = self.current_col;
294        self.code.push(op as u8);
295        self.lines.push(line);
296        self.columns.push(col);
297    }
298
299    /// Emit an instruction with a u16 argument.
300    pub fn emit_u16(&mut self, op: Op, arg: u16, line: u32) {
301        let col = self.current_col;
302        self.code.push(op as u8);
303        self.code.push((arg >> 8) as u8);
304        self.code.push((arg & 0xFF) as u8);
305        self.lines.push(line);
306        self.lines.push(line);
307        self.lines.push(line);
308        self.columns.push(col);
309        self.columns.push(col);
310        self.columns.push(col);
311    }
312
313    /// Emit an instruction with a u8 argument.
314    pub fn emit_u8(&mut self, op: Op, arg: u8, line: u32) {
315        let col = self.current_col;
316        self.code.push(op as u8);
317        self.code.push(arg);
318        self.lines.push(line);
319        self.lines.push(line);
320        self.columns.push(col);
321        self.columns.push(col);
322    }
323
324    /// Emit a method call: op + u16 (method name) + u8 (arg count).
325    pub fn emit_method_call(&mut self, name_idx: u16, arg_count: u8, line: u32) {
326        self.emit_method_call_inner(Op::MethodCall, name_idx, arg_count, line);
327    }
328
329    /// Emit an optional method call (?.) — returns nil if receiver is nil.
330    pub fn emit_method_call_opt(&mut self, name_idx: u16, arg_count: u8, line: u32) {
331        self.emit_method_call_inner(Op::MethodCallOpt, name_idx, arg_count, line);
332    }
333
334    fn emit_method_call_inner(&mut self, op: Op, name_idx: u16, arg_count: u8, line: u32) {
335        let col = self.current_col;
336        self.code.push(op as u8);
337        self.code.push((name_idx >> 8) as u8);
338        self.code.push((name_idx & 0xFF) as u8);
339        self.code.push(arg_count);
340        self.lines.push(line);
341        self.lines.push(line);
342        self.lines.push(line);
343        self.lines.push(line);
344        self.columns.push(col);
345        self.columns.push(col);
346        self.columns.push(col);
347        self.columns.push(col);
348    }
349
350    /// Current code offset (for jump patching).
351    pub fn current_offset(&self) -> usize {
352        self.code.len()
353    }
354
355    /// Emit a jump instruction with a placeholder offset. Returns the position to patch.
356    pub fn emit_jump(&mut self, op: Op, line: u32) -> usize {
357        let col = self.current_col;
358        self.code.push(op as u8);
359        let patch_pos = self.code.len();
360        self.code.push(0xFF);
361        self.code.push(0xFF);
362        self.lines.push(line);
363        self.lines.push(line);
364        self.lines.push(line);
365        self.columns.push(col);
366        self.columns.push(col);
367        self.columns.push(col);
368        patch_pos
369    }
370
371    /// Patch a jump instruction at the given position to jump to the current offset.
372    pub fn patch_jump(&mut self, patch_pos: usize) {
373        let target = self.code.len() as u16;
374        self.code[patch_pos] = (target >> 8) as u8;
375        self.code[patch_pos + 1] = (target & 0xFF) as u8;
376    }
377
378    /// Patch a jump to a specific target position.
379    pub fn patch_jump_to(&mut self, patch_pos: usize, target: usize) {
380        let target = target as u16;
381        self.code[patch_pos] = (target >> 8) as u8;
382        self.code[patch_pos + 1] = (target & 0xFF) as u8;
383    }
384
385    /// Read a u16 argument at the given position.
386    pub fn read_u16(&self, pos: usize) -> u16 {
387        ((self.code[pos] as u16) << 8) | (self.code[pos + 1] as u16)
388    }
389
390    /// Disassemble for debugging.
391    pub fn disassemble(&self, name: &str) -> String {
392        let mut out = format!("== {name} ==\n");
393        let mut ip = 0;
394        while ip < self.code.len() {
395            let op = self.code[ip];
396            let line = self.lines.get(ip).copied().unwrap_or(0);
397            out.push_str(&format!("{:04} [{:>4}] ", ip, line));
398            ip += 1;
399
400            match op {
401                x if x == Op::Constant as u8 => {
402                    let idx = self.read_u16(ip);
403                    ip += 2;
404                    let val = &self.constants[idx as usize];
405                    out.push_str(&format!("CONSTANT {:>4} ({})\n", idx, val));
406                }
407                x if x == Op::Nil as u8 => out.push_str("NIL\n"),
408                x if x == Op::True as u8 => out.push_str("TRUE\n"),
409                x if x == Op::False as u8 => out.push_str("FALSE\n"),
410                x if x == Op::GetVar as u8 => {
411                    let idx = self.read_u16(ip);
412                    ip += 2;
413                    out.push_str(&format!(
414                        "GET_VAR {:>4} ({})\n",
415                        idx, self.constants[idx as usize]
416                    ));
417                }
418                x if x == Op::DefLet as u8 => {
419                    let idx = self.read_u16(ip);
420                    ip += 2;
421                    out.push_str(&format!(
422                        "DEF_LET {:>4} ({})\n",
423                        idx, self.constants[idx as usize]
424                    ));
425                }
426                x if x == Op::DefVar as u8 => {
427                    let idx = self.read_u16(ip);
428                    ip += 2;
429                    out.push_str(&format!(
430                        "DEF_VAR {:>4} ({})\n",
431                        idx, self.constants[idx as usize]
432                    ));
433                }
434                x if x == Op::SetVar as u8 => {
435                    let idx = self.read_u16(ip);
436                    ip += 2;
437                    out.push_str(&format!(
438                        "SET_VAR {:>4} ({})\n",
439                        idx, self.constants[idx as usize]
440                    ));
441                }
442                x if x == Op::PushScope as u8 => out.push_str("PUSH_SCOPE\n"),
443                x if x == Op::PopScope as u8 => out.push_str("POP_SCOPE\n"),
444                x if x == Op::Add as u8 => out.push_str("ADD\n"),
445                x if x == Op::Sub as u8 => out.push_str("SUB\n"),
446                x if x == Op::Mul as u8 => out.push_str("MUL\n"),
447                x if x == Op::Div as u8 => out.push_str("DIV\n"),
448                x if x == Op::Mod as u8 => out.push_str("MOD\n"),
449                x if x == Op::Pow as u8 => out.push_str("POW\n"),
450                x if x == Op::Negate as u8 => out.push_str("NEGATE\n"),
451                x if x == Op::Equal as u8 => out.push_str("EQUAL\n"),
452                x if x == Op::NotEqual as u8 => out.push_str("NOT_EQUAL\n"),
453                x if x == Op::Less as u8 => out.push_str("LESS\n"),
454                x if x == Op::Greater as u8 => out.push_str("GREATER\n"),
455                x if x == Op::LessEqual as u8 => out.push_str("LESS_EQUAL\n"),
456                x if x == Op::GreaterEqual as u8 => out.push_str("GREATER_EQUAL\n"),
457                x if x == Op::Contains as u8 => out.push_str("CONTAINS\n"),
458                x if x == Op::Not as u8 => out.push_str("NOT\n"),
459                x if x == Op::Jump as u8 => {
460                    let target = self.read_u16(ip);
461                    ip += 2;
462                    out.push_str(&format!("JUMP {:>4}\n", target));
463                }
464                x if x == Op::JumpIfFalse as u8 => {
465                    let target = self.read_u16(ip);
466                    ip += 2;
467                    out.push_str(&format!("JUMP_IF_FALSE {:>4}\n", target));
468                }
469                x if x == Op::JumpIfTrue as u8 => {
470                    let target = self.read_u16(ip);
471                    ip += 2;
472                    out.push_str(&format!("JUMP_IF_TRUE {:>4}\n", target));
473                }
474                x if x == Op::Pop as u8 => out.push_str("POP\n"),
475                x if x == Op::Call as u8 => {
476                    let argc = self.code[ip];
477                    ip += 1;
478                    out.push_str(&format!("CALL {:>4}\n", argc));
479                }
480                x if x == Op::TailCall as u8 => {
481                    let argc = self.code[ip];
482                    ip += 1;
483                    out.push_str(&format!("TAIL_CALL {:>4}\n", argc));
484                }
485                x if x == Op::Return as u8 => out.push_str("RETURN\n"),
486                x if x == Op::Closure as u8 => {
487                    let idx = self.read_u16(ip);
488                    ip += 2;
489                    out.push_str(&format!("CLOSURE {:>4}\n", idx));
490                }
491                x if x == Op::BuildList as u8 => {
492                    let count = self.read_u16(ip);
493                    ip += 2;
494                    out.push_str(&format!("BUILD_LIST {:>4}\n", count));
495                }
496                x if x == Op::BuildDict as u8 => {
497                    let count = self.read_u16(ip);
498                    ip += 2;
499                    out.push_str(&format!("BUILD_DICT {:>4}\n", count));
500                }
501                x if x == Op::Subscript as u8 => out.push_str("SUBSCRIPT\n"),
502                x if x == Op::Slice as u8 => out.push_str("SLICE\n"),
503                x if x == Op::GetProperty as u8 => {
504                    let idx = self.read_u16(ip);
505                    ip += 2;
506                    out.push_str(&format!(
507                        "GET_PROPERTY {:>4} ({})\n",
508                        idx, self.constants[idx as usize]
509                    ));
510                }
511                x if x == Op::GetPropertyOpt as u8 => {
512                    let idx = self.read_u16(ip);
513                    ip += 2;
514                    out.push_str(&format!(
515                        "GET_PROPERTY_OPT {:>4} ({})\n",
516                        idx, self.constants[idx as usize]
517                    ));
518                }
519                x if x == Op::SetProperty as u8 => {
520                    let idx = self.read_u16(ip);
521                    ip += 2;
522                    out.push_str(&format!(
523                        "SET_PROPERTY {:>4} ({})\n",
524                        idx, self.constants[idx as usize]
525                    ));
526                }
527                x if x == Op::SetSubscript as u8 => {
528                    let idx = self.read_u16(ip);
529                    ip += 2;
530                    out.push_str(&format!(
531                        "SET_SUBSCRIPT {:>4} ({})\n",
532                        idx, self.constants[idx as usize]
533                    ));
534                }
535                x if x == Op::MethodCall as u8 => {
536                    let idx = self.read_u16(ip);
537                    ip += 2;
538                    let argc = self.code[ip];
539                    ip += 1;
540                    out.push_str(&format!(
541                        "METHOD_CALL {:>4} ({}) argc={}\n",
542                        idx, self.constants[idx as usize], argc
543                    ));
544                }
545                x if x == Op::MethodCallOpt as u8 => {
546                    let idx = self.read_u16(ip);
547                    ip += 2;
548                    let argc = self.code[ip];
549                    ip += 1;
550                    out.push_str(&format!(
551                        "METHOD_CALL_OPT {:>4} ({}) argc={}\n",
552                        idx, self.constants[idx as usize], argc
553                    ));
554                }
555                x if x == Op::Concat as u8 => {
556                    let count = self.read_u16(ip);
557                    ip += 2;
558                    out.push_str(&format!("CONCAT {:>4}\n", count));
559                }
560                x if x == Op::IterInit as u8 => out.push_str("ITER_INIT\n"),
561                x if x == Op::IterNext as u8 => {
562                    let target = self.read_u16(ip);
563                    ip += 2;
564                    out.push_str(&format!("ITER_NEXT {:>4}\n", target));
565                }
566                x if x == Op::Throw as u8 => out.push_str("THROW\n"),
567                x if x == Op::TryCatchSetup as u8 => {
568                    let target = self.read_u16(ip);
569                    ip += 2;
570                    out.push_str(&format!("TRY_CATCH_SETUP {:>4}\n", target));
571                }
572                x if x == Op::PopHandler as u8 => out.push_str("POP_HANDLER\n"),
573                x if x == Op::Pipe as u8 => out.push_str("PIPE\n"),
574                x if x == Op::Parallel as u8 => out.push_str("PARALLEL\n"),
575                x if x == Op::ParallelMap as u8 => out.push_str("PARALLEL_MAP\n"),
576                x if x == Op::ParallelSettle as u8 => out.push_str("PARALLEL_SETTLE\n"),
577                x if x == Op::Spawn as u8 => out.push_str("SPAWN\n"),
578                x if x == Op::Import as u8 => {
579                    let idx = self.read_u16(ip);
580                    ip += 2;
581                    out.push_str(&format!(
582                        "IMPORT {:>4} ({})\n",
583                        idx, self.constants[idx as usize]
584                    ));
585                }
586                x if x == Op::SelectiveImport as u8 => {
587                    let path_idx = self.read_u16(ip);
588                    ip += 2;
589                    let names_idx = self.read_u16(ip);
590                    ip += 2;
591                    out.push_str(&format!(
592                        "SELECTIVE_IMPORT {:>4} ({}) names: {:>4} ({})\n",
593                        path_idx,
594                        self.constants[path_idx as usize],
595                        names_idx,
596                        self.constants[names_idx as usize]
597                    ));
598                }
599                x if x == Op::DeadlineSetup as u8 => out.push_str("DEADLINE_SETUP\n"),
600                x if x == Op::DeadlineEnd as u8 => out.push_str("DEADLINE_END\n"),
601                x if x == Op::BuildEnum as u8 => {
602                    let enum_idx = self.read_u16(ip);
603                    ip += 2;
604                    let variant_idx = self.read_u16(ip);
605                    ip += 2;
606                    let field_count = self.read_u16(ip);
607                    ip += 2;
608                    out.push_str(&format!(
609                        "BUILD_ENUM {:>4} ({}) {:>4} ({}) fields={}\n",
610                        enum_idx,
611                        self.constants[enum_idx as usize],
612                        variant_idx,
613                        self.constants[variant_idx as usize],
614                        field_count
615                    ));
616                }
617                x if x == Op::MatchEnum as u8 => {
618                    let enum_idx = self.read_u16(ip);
619                    ip += 2;
620                    let variant_idx = self.read_u16(ip);
621                    ip += 2;
622                    out.push_str(&format!(
623                        "MATCH_ENUM {:>4} ({}) {:>4} ({})\n",
624                        enum_idx,
625                        self.constants[enum_idx as usize],
626                        variant_idx,
627                        self.constants[variant_idx as usize]
628                    ));
629                }
630                x if x == Op::PopIterator as u8 => out.push_str("POP_ITERATOR\n"),
631                x if x == Op::TryUnwrap as u8 => out.push_str("TRY_UNWRAP\n"),
632                x if x == Op::CallSpread as u8 => out.push_str("CALL_SPREAD\n"),
633                x if x == Op::MethodCallSpread as u8 => {
634                    let idx = self.read_u16(ip + 1);
635                    ip += 2;
636                    out.push_str(&format!("METHOD_CALL_SPREAD {idx}\n"));
637                }
638                x if x == Op::Dup as u8 => out.push_str("DUP\n"),
639                x if x == Op::Swap as u8 => out.push_str("SWAP\n"),
640                x if x == Op::Yield as u8 => out.push_str("YIELD\n"),
641                _ => {
642                    out.push_str(&format!("UNKNOWN(0x{:02x})\n", op));
643                }
644            }
645        }
646        out
647    }
648}
649
650impl Default for Chunk {
651    fn default() -> Self {
652        Self::new()
653    }
654}