Skip to main content

harn_vm/compiler/
mod.rs

1use harn_parser::{Node, SNode, TypeExpr};
2
3mod closures;
4mod concurrency;
5mod decls;
6mod error;
7mod error_handling;
8mod expressions;
9mod patterns;
10mod pipe;
11mod state;
12mod statements;
13#[cfg(test)]
14mod tests;
15mod type_facts;
16mod yield_scan;
17
18pub use error::CompileError;
19
20use crate::chunk::{Chunk, Constant, Op};
21
22/// Look through an `AttributedDecl` wrapper to the inner declaration.
23/// `compile_named` / `compile` use this so attributed declarations like
24/// `@test pipeline foo(...)` are still discoverable by name.
25fn peel_node(sn: &SNode) -> &Node {
26    match &sn.node {
27        Node::AttributedDecl { inner, .. } => &inner.node,
28        other => other,
29    }
30}
31
32/// Entry in the compiler's pending-finally stack. See the field-level doc on
33/// `Compiler::finally_bodies` for the unwind semantics each variant encodes.
34#[derive(Clone, Debug)]
35enum FinallyEntry {
36    Finally(Vec<SNode>),
37    CatchBarrier,
38}
39
40/// Tracks loop context for break/continue compilation.
41struct LoopContext {
42    /// Offset of the loop start (for continue).
43    start_offset: usize,
44    /// Positions of break jumps that need patching to the loop end.
45    break_patches: Vec<usize>,
46    /// True if this is a for-in loop (has an iterator to clean up on break).
47    has_iterator: bool,
48    /// Number of exception handlers active at loop entry.
49    handler_depth: usize,
50    /// Number of pending finally bodies at loop entry.
51    finally_depth: usize,
52    /// Lexical scope depth at loop entry.
53    scope_depth: usize,
54}
55
56#[derive(Clone, Copy, Debug)]
57struct LocalBinding {
58    slot: u16,
59    mutable: bool,
60}
61
62/// Compiles an AST into bytecode.
63pub struct Compiler {
64    chunk: Chunk,
65    line: u32,
66    column: u32,
67    /// Track enum type names so PropertyAccess on them can produce EnumVariant.
68    enum_names: std::collections::HashSet<String>,
69    /// Track struct type names to declared field order for indexed instances.
70    struct_layouts: std::collections::HashMap<String, Vec<String>>,
71    /// Track interface names → method names for runtime enforcement.
72    interface_methods: std::collections::HashMap<String, Vec<String>>,
73    /// Stack of active loop contexts for break/continue.
74    loop_stack: Vec<LoopContext>,
75    /// Current depth of exception handlers (for cleanup on break/continue).
76    handler_depth: usize,
77    /// Stack of pending finally bodies plus catch-handler barriers for
78    /// unwind-aware lowering of `throw`, `return`, `break`, and `continue`.
79    ///
80    /// A `Finally` entry is a pending finally body that must execute when
81    /// control exits its enclosing try block. A `CatchBarrier` marks the
82    /// boundary of an active `try/catch` handler: throws emitted inside
83    /// the try body are caught locally, so pre-running finallys *beyond*
84    /// the barrier would wrongly fire side effects for outer blocks the
85    /// throw never actually escapes. Throw lowering stops at the innermost
86    /// barrier; `return`/`break`/`continue`, which do transfer past local
87    /// handlers, still run every pending `Finally` up to their target.
88    finally_bodies: Vec<FinallyEntry>,
89    /// Counter for unique temp variable names.
90    temp_counter: usize,
91    /// Number of lexical block scopes currently active in this compiled frame.
92    scope_depth: usize,
93    /// Top-level `type` aliases, used to lower `schema_of(T)` and
94    /// `output_schema: T` into constant JSON-Schema dicts at compile time.
95    type_aliases: std::collections::HashMap<String, TypeExpr>,
96    /// Lightweight compiler-side type facts used only for conservative
97    /// bytecode specialization. This mirrors lexical scopes and is separate
98    /// from the parser's diagnostic type checker so compile-only callers keep
99    /// working without a required type-check pass.
100    type_scopes: Vec<std::collections::HashMap<String, TypeExpr>>,
101    /// Lexical variable slots for the current compiled frame. The compiler
102    /// only consults this for names declared inside the current function-like
103    /// body; all unresolved names stay on the existing dynamic/name path.
104    local_scopes: Vec<std::collections::HashMap<String, LocalBinding>>,
105    /// True when this compiler is emitting code outside any function-like
106    /// scope (module top-level statements). `try*` is rejected here
107    /// because the rethrow has no enclosing function to live in.
108    /// Pipeline bodies and nested `Compiler::new()` instances (fn,
109    /// closure, tool, etc.) flip this to false before compiling.
110    module_level: bool,
111}
112
113impl Compiler {
114    /// Compile a single AST node. Most arm bodies live in per-category
115    /// submodules (expressions, statements, closures, decls, patterns,
116    /// error_handling, concurrency); this function is a thin dispatcher.
117    fn compile_node(&mut self, snode: &SNode) -> Result<(), CompileError> {
118        self.line = snode.span.line as u32;
119        self.column = snode.span.column as u32;
120        self.chunk.set_column(self.column);
121        match &snode.node {
122            Node::IntLiteral(n) => {
123                let idx = self.chunk.add_constant(Constant::Int(*n));
124                self.chunk.emit_u16(Op::Constant, idx, self.line);
125            }
126            Node::FloatLiteral(n) => {
127                let idx = self.chunk.add_constant(Constant::Float(*n));
128                self.chunk.emit_u16(Op::Constant, idx, self.line);
129            }
130            Node::StringLiteral(s) | Node::RawStringLiteral(s) => {
131                let idx = self.chunk.add_constant(Constant::String(s.clone()));
132                self.chunk.emit_u16(Op::Constant, idx, self.line);
133            }
134            Node::BoolLiteral(true) => self.chunk.emit(Op::True, self.line),
135            Node::BoolLiteral(false) => self.chunk.emit(Op::False, self.line),
136            Node::NilLiteral => self.chunk.emit(Op::Nil, self.line),
137            Node::DurationLiteral(ms) => {
138                let idx = self.chunk.add_constant(Constant::Duration(*ms));
139                self.chunk.emit_u16(Op::Constant, idx, self.line);
140            }
141            Node::Identifier(name) => {
142                self.emit_get_binding(name);
143            }
144            Node::LetBinding { pattern, value, .. } => {
145                let binding_type = match &snode.node {
146                    Node::LetBinding {
147                        type_ann: Some(type_ann),
148                        ..
149                    } => Some(type_ann.clone()),
150                    _ => self.infer_expr_type(value),
151                };
152                self.compile_node(value)?;
153                self.compile_destructuring(pattern, false)?;
154                self.record_binding_type(pattern, binding_type);
155            }
156            Node::VarBinding { pattern, value, .. } => {
157                let binding_type = match &snode.node {
158                    Node::VarBinding {
159                        type_ann: Some(type_ann),
160                        ..
161                    } => Some(type_ann.clone()),
162                    _ => self.infer_expr_type(value),
163                };
164                self.compile_node(value)?;
165                self.compile_destructuring(pattern, true)?;
166                self.record_binding_type(pattern, binding_type);
167            }
168            Node::Assignment {
169                target, value, op, ..
170            } => {
171                self.compile_assignment(target, value, op)?;
172            }
173            Node::BinaryOp { op, left, right } => {
174                self.compile_binary_op(op, left, right)?;
175            }
176            Node::UnaryOp { op, operand } => {
177                self.compile_node(operand)?;
178                match op.as_str() {
179                    "-" => self.chunk.emit(Op::Negate, self.line),
180                    "!" => self.chunk.emit(Op::Not, self.line),
181                    _ => {}
182                }
183            }
184            Node::Ternary {
185                condition,
186                true_expr,
187                false_expr,
188            } => {
189                self.compile_node(condition)?;
190                let else_jump = self.chunk.emit_jump(Op::JumpIfFalse, self.line);
191                self.chunk.emit(Op::Pop, self.line);
192                self.compile_node(true_expr)?;
193                let end_jump = self.chunk.emit_jump(Op::Jump, self.line);
194                self.chunk.patch_jump(else_jump);
195                self.chunk.emit(Op::Pop, self.line);
196                self.compile_node(false_expr)?;
197                self.chunk.patch_jump(end_jump);
198            }
199            Node::FunctionCall { name, args } => {
200                self.compile_function_call(name, args)?;
201            }
202            Node::MethodCall {
203                object,
204                method,
205                args,
206            } => {
207                self.compile_method_call(object, method, args)?;
208            }
209            Node::OptionalMethodCall {
210                object,
211                method,
212                args,
213            } => {
214                self.compile_node(object)?;
215                for arg in args {
216                    self.compile_node(arg)?;
217                }
218                let name_idx = self.chunk.add_constant(Constant::String(method.clone()));
219                self.chunk
220                    .emit_method_call_opt(name_idx, args.len() as u8, self.line);
221            }
222            Node::PropertyAccess { object, property } => {
223                self.compile_property_access(object, property)?;
224            }
225            Node::OptionalPropertyAccess { object, property } => {
226                self.compile_node(object)?;
227                let idx = self.chunk.add_constant(Constant::String(property.clone()));
228                self.chunk.emit_u16(Op::GetPropertyOpt, idx, self.line);
229            }
230            Node::SubscriptAccess { object, index } => {
231                self.compile_node(object)?;
232                self.compile_node(index)?;
233                self.chunk.emit(Op::Subscript, self.line);
234            }
235            Node::OptionalSubscriptAccess { object, index } => {
236                self.compile_node(object)?;
237                self.compile_node(index)?;
238                self.chunk.emit(Op::SubscriptOpt, self.line);
239            }
240            Node::SliceAccess { object, start, end } => {
241                self.compile_node(object)?;
242                if let Some(s) = start {
243                    self.compile_node(s)?;
244                } else {
245                    self.chunk.emit(Op::Nil, self.line);
246                }
247                if let Some(e) = end {
248                    self.compile_node(e)?;
249                } else {
250                    self.chunk.emit(Op::Nil, self.line);
251                }
252                self.chunk.emit(Op::Slice, self.line);
253            }
254            Node::IfElse {
255                condition,
256                then_body,
257                else_body,
258            } => {
259                self.compile_if_else(condition, then_body, else_body)?;
260            }
261            Node::WhileLoop { condition, body } => {
262                self.compile_while_loop(condition, body)?;
263            }
264            Node::ForIn {
265                pattern,
266                iterable,
267                body,
268            } => {
269                self.compile_for_in(pattern, iterable, body)?;
270            }
271            Node::ReturnStmt { value } => {
272                self.compile_return_stmt(value)?;
273            }
274            Node::BreakStmt => {
275                self.compile_break_stmt()?;
276            }
277            Node::ContinueStmt => {
278                self.compile_continue_stmt()?;
279            }
280            Node::ListLiteral(elements) => {
281                self.compile_list_literal(elements)?;
282            }
283            Node::DictLiteral(entries) => {
284                self.compile_dict_literal(entries)?;
285            }
286            Node::InterpolatedString(segments) => {
287                self.compile_interpolated_string(segments)?;
288            }
289            Node::FnDecl {
290                name, params, body, ..
291            } => {
292                self.compile_fn_decl(name, params, body)?;
293            }
294            Node::ToolDecl {
295                name,
296                description,
297                params,
298                return_type,
299                body,
300                ..
301            } => {
302                self.compile_tool_decl(name, description, params, return_type, body)?;
303            }
304            Node::SkillDecl { name, fields, .. } => {
305                self.compile_skill_decl(name, fields)?;
306            }
307            Node::Closure { params, body, .. } => {
308                self.compile_closure(params, body)?;
309            }
310            Node::ThrowStmt { value } => {
311                self.compile_throw_stmt(value)?;
312            }
313            Node::MatchExpr { value, arms } => {
314                self.compile_match_expr(value, arms)?;
315            }
316            Node::RangeExpr {
317                start,
318                end,
319                inclusive,
320            } => {
321                let name_idx = self
322                    .chunk
323                    .add_constant(Constant::String("__range__".to_string()));
324                self.chunk.emit_u16(Op::Constant, name_idx, self.line);
325                self.compile_node(start)?;
326                self.compile_node(end)?;
327                if *inclusive {
328                    self.chunk.emit(Op::True, self.line);
329                } else {
330                    self.chunk.emit(Op::False, self.line);
331                }
332                self.chunk.emit_u8(Op::Call, 3, self.line);
333            }
334            Node::GuardStmt {
335                condition,
336                else_body,
337            } => {
338                self.compile_guard_stmt(condition, else_body)?;
339            }
340            Node::RequireStmt { condition, message } => {
341                self.compile_node(condition)?;
342                let ok_jump = self.chunk.emit_jump(Op::JumpIfTrue, self.line);
343                self.chunk.emit(Op::Pop, self.line);
344                if let Some(message) = message {
345                    self.compile_node(message)?;
346                } else {
347                    let idx = self
348                        .chunk
349                        .add_constant(Constant::String("require condition failed".to_string()));
350                    self.chunk.emit_u16(Op::Constant, idx, self.line);
351                }
352                self.chunk.emit(Op::Throw, self.line);
353                self.chunk.patch_jump(ok_jump);
354                self.chunk.emit(Op::Pop, self.line);
355            }
356            Node::Block(stmts) => {
357                self.compile_scoped_block(stmts)?;
358            }
359            Node::DeadlineBlock { duration, body } => {
360                self.compile_node(duration)?;
361                self.chunk.emit(Op::DeadlineSetup, self.line);
362                self.compile_scoped_block(body)?;
363                self.chunk.emit(Op::DeadlineEnd, self.line);
364            }
365            Node::MutexBlock { body } => {
366                self.begin_scope();
367                let key_idx = self
368                    .chunk
369                    .add_constant(Constant::String("__default__".to_string()));
370                self.chunk.emit_u16(Op::SyncMutexEnter, key_idx, self.line);
371                for sn in body {
372                    self.compile_node(sn)?;
373                    if Self::produces_value(&sn.node) {
374                        self.chunk.emit(Op::Pop, self.line);
375                    }
376                }
377                self.chunk.emit(Op::Nil, self.line);
378                self.end_scope();
379            }
380            Node::DeferStmt { body } => {
381                // Push onto the finally stack so it runs on return/throw/scope-exit.
382                self.finally_bodies
383                    .push(FinallyEntry::Finally(body.clone()));
384                self.chunk.emit(Op::Nil, self.line);
385            }
386            Node::YieldExpr { value } => {
387                if let Some(val) = value {
388                    self.compile_node(val)?;
389                } else {
390                    self.chunk.emit(Op::Nil, self.line);
391                }
392                self.chunk.emit(Op::Yield, self.line);
393            }
394            Node::EnumConstruct {
395                enum_name,
396                variant,
397                args,
398            } => {
399                self.compile_enum_construct(enum_name, variant, args)?;
400            }
401            Node::StructConstruct {
402                struct_name,
403                fields,
404            } => {
405                self.compile_struct_construct(struct_name, fields)?;
406            }
407            Node::ImportDecl { path } => {
408                let idx = self.chunk.add_constant(Constant::String(path.clone()));
409                self.chunk.emit_u16(Op::Import, idx, self.line);
410            }
411            Node::SelectiveImport { names, path } => {
412                let path_idx = self.chunk.add_constant(Constant::String(path.clone()));
413                let names_str = names.join(",");
414                let names_idx = self.chunk.add_constant(Constant::String(names_str));
415                self.chunk
416                    .emit_u16(Op::SelectiveImport, path_idx, self.line);
417                let hi = (names_idx >> 8) as u8;
418                let lo = names_idx as u8;
419                self.chunk.code.push(hi);
420                self.chunk.code.push(lo);
421                self.chunk.lines.push(self.line);
422                self.chunk.columns.push(self.column);
423                self.chunk.lines.push(self.line);
424                self.chunk.columns.push(self.column);
425            }
426            Node::TryOperator { operand } => {
427                self.compile_node(operand)?;
428                self.chunk.emit(Op::TryUnwrap, self.line);
429            }
430            // `try* EXPR`: evaluate EXPR; on throw, run pending finally
431            // blocks up to the innermost catch barrier and rethrow the
432            // original value. On success, leave EXPR's value on the stack.
433            //
434            // Per the issue-#26 desugaring:
435            //   { let _r = try { EXPR }
436            //     guard is_ok(_r) else { throw unwrap_err(_r) }
437            //     unwrap(_r) }
438            //
439            // The bytecode realizes this directly: install a try handler
440            // around EXPR so a throw lands in our catch path, where we
441            // pre-run pending finallys and re-emit `Throw`. Skipping the
442            // intermediate Result.Ok/Err wrapping that `TryExpr` does
443            // keeps the success path a no-op (operand value passes through
444            // as-is).
445            Node::TryStar { operand } => {
446                self.compile_try_star(operand)?;
447            }
448            Node::ImplBlock { type_name, methods } => {
449                self.compile_impl_block(type_name, methods)?;
450            }
451            Node::StructDecl { name, fields, .. } => {
452                self.compile_struct_decl(name, fields)?;
453            }
454            // Metadata-only declarations (no runtime effect).
455            Node::Pipeline { .. }
456            | Node::OverrideDecl { .. }
457            | Node::TypeDecl { .. }
458            | Node::EnumDecl { .. }
459            | Node::InterfaceDecl { .. } => {
460                self.chunk.emit(Op::Nil, self.line);
461            }
462            Node::TryCatch {
463                body,
464                error_var,
465                error_type,
466                catch_body,
467                finally_body,
468            } => {
469                self.compile_try_catch(body, error_var, error_type, catch_body, finally_body)?;
470            }
471            Node::TryExpr { body } => {
472                self.compile_try_expr(body)?;
473            }
474            Node::Retry { count, body } => {
475                self.compile_retry(count, body)?;
476            }
477            Node::Parallel {
478                mode,
479                expr,
480                variable,
481                body,
482                options,
483            } => {
484                self.compile_parallel(mode, expr, variable, body, options)?;
485            }
486            Node::SpawnExpr { body } => {
487                self.compile_spawn_expr(body)?;
488            }
489            Node::SelectExpr {
490                cases,
491                timeout,
492                default_body,
493            } => {
494                self.compile_select_expr(cases, timeout, default_body)?;
495            }
496            Node::Spread(_) => {
497                return Err(CompileError {
498                    message: "spread (...) can only be used inside list literals, dict literals, or function call arguments".into(),
499                    line: self.line,
500                });
501            }
502            Node::AttributedDecl { attributes, inner } => {
503                self.compile_attributed_decl(attributes, inner)?;
504            }
505            Node::OrPattern(_) => {
506                return Err(CompileError {
507                    message: "or-pattern (|) can only appear as a match arm pattern".into(),
508                    line: self.line,
509                });
510            }
511        }
512        Ok(())
513    }
514}