harn-vm 0.7.62

Async bytecode virtual machine for the Harn programming language
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
use harn_parser::{Node, SNode, TypeExpr};

mod closures;
mod concurrency;
mod decls;
mod error;
mod error_handling;
mod expressions;
mod hitl;
mod patterns;
mod pipe;
mod state;
mod statements;
#[cfg(test)]
mod tests;
mod type_facts;
mod yield_scan;

pub use error::CompileError;

use crate::chunk::{Chunk, Constant, Op};

/// Look through an `AttributedDecl` wrapper to the inner declaration.
/// `compile_named` / `compile` use this so attributed declarations like
/// `@test pipeline foo(...)` are still discoverable by name.
fn peel_node(sn: &SNode) -> &Node {
    match &sn.node {
        Node::AttributedDecl { inner, .. } => &inner.node,
        other => other,
    }
}

/// Entry in the compiler's pending-finally stack. See the field-level doc on
/// `Compiler::finally_bodies` for the unwind semantics each variant encodes.
#[derive(Clone, Debug)]
enum FinallyEntry {
    Finally(Vec<SNode>),
    CatchBarrier,
}

/// Tracks loop context for break/continue compilation.
struct LoopContext {
    /// Offset of the loop start (for continue).
    start_offset: usize,
    /// Positions of break jumps that need patching to the loop end.
    break_patches: Vec<usize>,
    /// True if this is a for-in loop (has an iterator to clean up on break).
    has_iterator: bool,
    /// Number of exception handlers active at loop entry.
    handler_depth: usize,
    /// Number of pending finally bodies at loop entry.
    finally_depth: usize,
    /// Lexical scope depth at loop entry.
    scope_depth: usize,
}

#[derive(Clone, Copy, Debug)]
struct LocalBinding {
    slot: u16,
    mutable: bool,
}

/// Compiles an AST into bytecode.
pub struct Compiler {
    chunk: Chunk,
    line: u32,
    column: u32,
    /// Track enum type names so PropertyAccess on them can produce EnumVariant.
    enum_names: std::collections::HashSet<String>,
    /// Track struct type names to declared field order for indexed instances.
    struct_layouts: std::collections::HashMap<String, Vec<String>>,
    /// Track interface names → method names for runtime enforcement.
    interface_methods: std::collections::HashMap<String, Vec<String>>,
    /// Stack of active loop contexts for break/continue.
    loop_stack: Vec<LoopContext>,
    /// Current depth of exception handlers (for cleanup on break/continue).
    handler_depth: usize,
    /// Stack of pending finally bodies plus catch-handler barriers for
    /// unwind-aware lowering of `throw`, `return`, `break`, and `continue`.
    ///
    /// A `Finally` entry is a pending finally body that must execute when
    /// control exits its enclosing try block. A `CatchBarrier` marks the
    /// boundary of an active `try/catch` handler: throws emitted inside
    /// the try body are caught locally, so pre-running finallys *beyond*
    /// the barrier would wrongly fire side effects for outer blocks the
    /// throw never actually escapes. Throw lowering stops at the innermost
    /// barrier; `return`/`break`/`continue`, which do transfer past local
    /// handlers, still run every pending `Finally` up to their target.
    finally_bodies: Vec<FinallyEntry>,
    /// Counter for unique temp variable names.
    temp_counter: usize,
    /// Number of lexical block scopes currently active in this compiled frame.
    scope_depth: usize,
    /// Top-level `type` aliases, used to lower `schema_of(T)` and
    /// `output_schema: T` into constant JSON-Schema dicts at compile time.
    type_aliases: std::collections::HashMap<String, TypeExpr>,
    /// Lightweight compiler-side type facts used only for conservative
    /// bytecode specialization. This mirrors lexical scopes and is separate
    /// from the parser's diagnostic type checker so compile-only callers keep
    /// working without a required type-check pass.
    type_scopes: Vec<std::collections::HashMap<String, TypeExpr>>,
    /// Lexical variable slots for the current compiled frame. The compiler
    /// only consults this for names declared inside the current function-like
    /// body; all unresolved names stay on the existing dynamic/name path.
    local_scopes: Vec<std::collections::HashMap<String, LocalBinding>>,
    /// True when this compiler is emitting code outside any function-like
    /// scope (module top-level statements). `try*` is rejected here
    /// because the rethrow has no enclosing function to live in.
    /// Pipeline bodies and nested `Compiler::new()` instances (fn,
    /// closure, tool, etc.) flip this to false before compiling.
    module_level: bool,
}

impl Compiler {
    /// Compile a single AST node. Most arm bodies live in per-category
    /// submodules (expressions, statements, closures, decls, patterns,
    /// error_handling, concurrency); this function is a thin dispatcher.
    fn compile_node(&mut self, snode: &SNode) -> Result<(), CompileError> {
        self.line = snode.span.line as u32;
        self.column = snode.span.column as u32;
        self.chunk.set_column(self.column);
        match &snode.node {
            Node::IntLiteral(n) => {
                let idx = self.chunk.add_constant(Constant::Int(*n));
                self.chunk.emit_u16(Op::Constant, idx, self.line);
            }
            Node::FloatLiteral(n) => {
                let idx = self.chunk.add_constant(Constant::Float(*n));
                self.chunk.emit_u16(Op::Constant, idx, self.line);
            }
            Node::StringLiteral(s) | Node::RawStringLiteral(s) => {
                let idx = self.chunk.add_constant(Constant::String(s.clone()));
                self.chunk.emit_u16(Op::Constant, idx, self.line);
            }
            Node::BoolLiteral(true) => self.chunk.emit(Op::True, self.line),
            Node::BoolLiteral(false) => self.chunk.emit(Op::False, self.line),
            Node::NilLiteral => self.chunk.emit(Op::Nil, self.line),
            Node::DurationLiteral(ms) => {
                let ms = i64::try_from(*ms).map_err(|_| CompileError {
                    message: "duration literal is too large".to_string(),
                    line: self.line,
                })?;
                let idx = self.chunk.add_constant(Constant::Duration(ms));
                self.chunk.emit_u16(Op::Constant, idx, self.line);
            }
            Node::Identifier(name) => {
                self.emit_get_binding(name);
            }
            Node::LetBinding { pattern, value, .. } => {
                let binding_type = match &snode.node {
                    Node::LetBinding {
                        type_ann: Some(type_ann),
                        ..
                    } => Some(type_ann.clone()),
                    _ => self.infer_expr_type(value),
                };
                self.compile_node(value)?;
                self.compile_destructuring(pattern, false)?;
                self.record_binding_type(pattern, binding_type);
            }
            Node::VarBinding { pattern, value, .. } => {
                let binding_type = match &snode.node {
                    Node::VarBinding {
                        type_ann: Some(type_ann),
                        ..
                    } => Some(type_ann.clone()),
                    _ => self.infer_expr_type(value),
                };
                self.compile_node(value)?;
                self.compile_destructuring(pattern, true)?;
                self.record_binding_type(pattern, binding_type);
            }
            Node::Assignment {
                target, value, op, ..
            } => {
                self.compile_assignment(target, value, op)?;
            }
            Node::BinaryOp { op, left, right } => {
                self.compile_binary_op(op, left, right)?;
            }
            Node::UnaryOp { op, operand } => {
                self.compile_node(operand)?;
                match op.as_str() {
                    "-" => self.chunk.emit(Op::Negate, self.line),
                    "!" => self.chunk.emit(Op::Not, self.line),
                    _ => {}
                }
            }
            Node::Ternary {
                condition,
                true_expr,
                false_expr,
            } => {
                self.compile_node(condition)?;
                let else_jump = self.chunk.emit_jump(Op::JumpIfFalse, self.line);
                self.chunk.emit(Op::Pop, self.line);
                self.compile_node(true_expr)?;
                let end_jump = self.chunk.emit_jump(Op::Jump, self.line);
                self.chunk.patch_jump(else_jump);
                self.chunk.emit(Op::Pop, self.line);
                self.compile_node(false_expr)?;
                self.chunk.patch_jump(end_jump);
            }
            Node::FunctionCall { name, args, .. } => {
                self.compile_function_call(name, args)?;
            }
            Node::MethodCall {
                object,
                method,
                args,
            } => {
                self.compile_method_call(object, method, args)?;
            }
            Node::OptionalMethodCall {
                object,
                method,
                args,
            } => {
                self.compile_node(object)?;
                for arg in args {
                    self.compile_node(arg)?;
                }
                let name_idx = self.chunk.add_constant(Constant::String(method.clone()));
                self.chunk
                    .emit_method_call_opt(name_idx, args.len() as u8, self.line);
            }
            Node::PropertyAccess { object, property } => {
                self.compile_property_access(object, property)?;
            }
            Node::OptionalPropertyAccess { object, property } => {
                self.compile_node(object)?;
                let idx = self.chunk.add_constant(Constant::String(property.clone()));
                self.chunk.emit_u16(Op::GetPropertyOpt, idx, self.line);
            }
            Node::SubscriptAccess { object, index } => {
                self.compile_node(object)?;
                self.compile_node(index)?;
                self.chunk.emit(Op::Subscript, self.line);
            }
            Node::OptionalSubscriptAccess { object, index } => {
                self.compile_node(object)?;
                self.compile_node(index)?;
                self.chunk.emit(Op::SubscriptOpt, self.line);
            }
            Node::SliceAccess { object, start, end } => {
                self.compile_node(object)?;
                if let Some(s) = start {
                    self.compile_node(s)?;
                } else {
                    self.chunk.emit(Op::Nil, self.line);
                }
                if let Some(e) = end {
                    self.compile_node(e)?;
                } else {
                    self.chunk.emit(Op::Nil, self.line);
                }
                self.chunk.emit(Op::Slice, self.line);
            }
            Node::IfElse {
                condition,
                then_body,
                else_body,
            } => {
                self.compile_if_else(condition, then_body, else_body)?;
            }
            Node::WhileLoop { condition, body } => {
                self.compile_while_loop(condition, body)?;
            }
            Node::ForIn {
                pattern,
                iterable,
                body,
            } => {
                self.compile_for_in(pattern, iterable, body)?;
            }
            Node::ReturnStmt { value } => {
                self.compile_return_stmt(value)?;
            }
            Node::BreakStmt => {
                self.compile_break_stmt()?;
            }
            Node::ContinueStmt => {
                self.compile_continue_stmt()?;
            }
            Node::ListLiteral(elements) => {
                self.compile_list_literal(elements)?;
            }
            Node::DictLiteral(entries) => {
                self.compile_dict_literal(entries)?;
            }
            Node::InterpolatedString(segments) => {
                self.compile_interpolated_string(segments)?;
            }
            Node::FnDecl {
                name,
                type_params,
                params,
                body,
                is_stream,
                ..
            } => {
                self.compile_fn_decl(name, type_params, params, body, *is_stream)?;
            }
            Node::ToolDecl {
                name,
                description,
                params,
                return_type,
                body,
                ..
            } => {
                self.compile_tool_decl(name, description, params, return_type, body)?;
            }
            Node::SkillDecl { name, fields, .. } => {
                self.compile_skill_decl(name, fields)?;
            }
            Node::EvalPackDecl {
                binding_name,
                pack_id,
                fields,
                body,
                summarize,
                ..
            } => {
                self.compile_eval_pack_decl(binding_name, pack_id, fields, body, summarize, true)?;
            }
            Node::Closure { params, body, .. } => {
                self.compile_closure(params, body)?;
            }
            Node::ThrowStmt { value } => {
                self.compile_throw_stmt(value)?;
            }
            Node::MatchExpr { value, arms } => {
                self.compile_match_expr(value, arms)?;
            }
            Node::RangeExpr {
                start,
                end,
                inclusive,
            } => {
                let name_idx = self
                    .chunk
                    .add_constant(Constant::String("__range__".to_string()));
                self.chunk.emit_u16(Op::Constant, name_idx, self.line);
                self.compile_node(start)?;
                self.compile_node(end)?;
                if *inclusive {
                    self.chunk.emit(Op::True, self.line);
                } else {
                    self.chunk.emit(Op::False, self.line);
                }
                self.chunk.emit_u8(Op::Call, 3, self.line);
            }
            Node::GuardStmt {
                condition,
                else_body,
            } => {
                self.compile_guard_stmt(condition, else_body)?;
            }
            Node::RequireStmt { condition, message } => {
                self.compile_node(condition)?;
                let ok_jump = self.chunk.emit_jump(Op::JumpIfTrue, self.line);
                self.chunk.emit(Op::Pop, self.line);
                if let Some(message) = message {
                    self.compile_node(message)?;
                } else {
                    let idx = self
                        .chunk
                        .add_constant(Constant::String("require condition failed".to_string()));
                    self.chunk.emit_u16(Op::Constant, idx, self.line);
                }
                self.chunk.emit(Op::Throw, self.line);
                self.chunk.patch_jump(ok_jump);
                self.chunk.emit(Op::Pop, self.line);
            }
            Node::Block(stmts) => {
                self.compile_scoped_block(stmts)?;
            }
            Node::DeadlineBlock { duration, body } => {
                self.compile_node(duration)?;
                self.chunk.emit(Op::DeadlineSetup, self.line);
                self.compile_scoped_block(body)?;
                self.chunk.emit(Op::DeadlineEnd, self.line);
            }
            Node::MutexBlock { body } => {
                self.begin_scope();
                let key_idx = self
                    .chunk
                    .add_constant(Constant::String("__default__".to_string()));
                self.chunk.emit_u16(Op::SyncMutexEnter, key_idx, self.line);
                for sn in body {
                    self.compile_node(sn)?;
                    if Self::produces_value(&sn.node) {
                        self.chunk.emit(Op::Pop, self.line);
                    }
                }
                self.chunk.emit(Op::Nil, self.line);
                self.end_scope();
            }
            Node::DeferStmt { body } => {
                // Push onto the finally stack so it runs on return/throw/scope-exit.
                self.finally_bodies
                    .push(FinallyEntry::Finally(body.clone()));
                self.chunk.emit(Op::Nil, self.line);
            }
            Node::YieldExpr { value } => {
                if let Some(val) = value {
                    self.compile_node(val)?;
                } else {
                    self.chunk.emit(Op::Nil, self.line);
                }
                self.chunk.emit(Op::Yield, self.line);
            }
            Node::EmitExpr { value } => {
                self.compile_node(value)?;
                self.chunk.emit(Op::Yield, self.line);
            }
            Node::EnumConstruct {
                enum_name,
                variant,
                args,
            } => {
                self.compile_enum_construct(enum_name, variant, args)?;
            }
            Node::StructConstruct {
                struct_name,
                fields,
            } => {
                self.compile_struct_construct(struct_name, fields)?;
            }
            Node::ImportDecl { path, .. } => {
                let idx = self.chunk.add_constant(Constant::String(path.clone()));
                self.chunk.emit_u16(Op::Import, idx, self.line);
            }
            Node::SelectiveImport { names, path, .. } => {
                let path_idx = self.chunk.add_constant(Constant::String(path.clone()));
                let names_str = names.join(",");
                let names_idx = self.chunk.add_constant(Constant::String(names_str));
                self.chunk
                    .emit_u16(Op::SelectiveImport, path_idx, self.line);
                let hi = (names_idx >> 8) as u8;
                let lo = names_idx as u8;
                self.chunk.code.push(hi);
                self.chunk.code.push(lo);
                self.chunk.lines.push(self.line);
                self.chunk.columns.push(self.column);
                self.chunk.lines.push(self.line);
                self.chunk.columns.push(self.column);
            }
            Node::TryOperator { operand } => {
                self.compile_node(operand)?;
                self.chunk.emit(Op::TryUnwrap, self.line);
            }
            // `try* EXPR`: evaluate EXPR; on throw, run pending finally
            // blocks up to the innermost catch barrier and rethrow the
            // original value. On success, leave EXPR's value on the stack.
            //
            // Per the issue-#26 desugaring:
            //   { let _r = try { EXPR }
            //     guard is_ok(_r) else { throw unwrap_err(_r) }
            //     unwrap(_r) }
            //
            // The bytecode realizes this directly: install a try handler
            // around EXPR so a throw lands in our catch path, where we
            // pre-run pending finallys and re-emit `Throw`. Skipping the
            // intermediate Result.Ok/Err wrapping that `TryExpr` does
            // keeps the success path a no-op (operand value passes through
            // as-is).
            Node::TryStar { operand } => {
                self.compile_try_star(operand)?;
            }
            Node::ImplBlock { type_name, methods } => {
                self.compile_impl_block(type_name, methods)?;
            }
            Node::StructDecl { name, fields, .. } => {
                self.compile_struct_decl(name, fields)?;
            }
            // Metadata-only declarations (no runtime effect).
            Node::Pipeline { .. }
            | Node::OverrideDecl { .. }
            | Node::TypeDecl { .. }
            | Node::EnumDecl { .. }
            | Node::InterfaceDecl { .. } => {
                self.chunk.emit(Op::Nil, self.line);
            }
            Node::TryCatch {
                body,
                error_var,
                error_type,
                catch_body,
                finally_body,
            } => {
                self.compile_try_catch(body, error_var, error_type, catch_body, finally_body)?;
            }
            Node::TryExpr { body } => {
                self.compile_try_expr(body)?;
            }
            Node::Retry { count, body } => {
                self.compile_retry(count, body)?;
            }
            Node::CostRoute { options, body } => {
                self.compile_cost_route(options, body)?;
            }
            Node::Parallel {
                mode,
                expr,
                variable,
                body,
                options,
            } => {
                self.compile_parallel(mode, expr, variable, body, options)?;
            }
            Node::SpawnExpr { body } => {
                self.compile_spawn_expr(body)?;
            }
            Node::HitlExpr { kind, args } => {
                self.compile_hitl_expr(*kind, args)?;
            }
            Node::SelectExpr {
                cases,
                timeout,
                default_body,
            } => {
                self.compile_select_expr(cases, timeout, default_body)?;
            }
            Node::Spread(_) => {
                return Err(CompileError {
                    message: "spread (...) can only be used inside list literals, dict literals, or function call arguments".into(),
                    line: self.line,
                });
            }
            Node::AttributedDecl { attributes, inner } => {
                self.compile_attributed_decl(attributes, inner)?;
            }
            Node::OrPattern(_) => {
                return Err(CompileError {
                    message: "or-pattern (|) can only appear as a match arm pattern".into(),
                    line: self.line,
                });
            }
        }
        Ok(())
    }
}