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