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::Closure { params, body, .. } => {
267                self.compile_closure(params, body)?;
268            }
269            Node::ThrowStmt { value } => {
270                self.compile_throw_stmt(value)?;
271            }
272            Node::MatchExpr { value, arms } => {
273                self.compile_match_expr(value, arms)?;
274            }
275            Node::RangeExpr {
276                start,
277                end,
278                inclusive,
279            } => {
280                let name_idx = self
281                    .chunk
282                    .add_constant(Constant::String("__range__".to_string()));
283                self.chunk.emit_u16(Op::Constant, name_idx, self.line);
284                self.compile_node(start)?;
285                self.compile_node(end)?;
286                if *inclusive {
287                    self.chunk.emit(Op::True, self.line);
288                } else {
289                    self.chunk.emit(Op::False, self.line);
290                }
291                self.chunk.emit_u8(Op::Call, 3, self.line);
292            }
293            Node::GuardStmt {
294                condition,
295                else_body,
296            } => {
297                self.compile_guard_stmt(condition, else_body)?;
298            }
299            Node::RequireStmt { condition, message } => {
300                self.compile_node(condition)?;
301                let ok_jump = self.chunk.emit_jump(Op::JumpIfTrue, self.line);
302                self.chunk.emit(Op::Pop, self.line);
303                if let Some(message) = message {
304                    self.compile_node(message)?;
305                } else {
306                    let idx = self
307                        .chunk
308                        .add_constant(Constant::String("require condition failed".to_string()));
309                    self.chunk.emit_u16(Op::Constant, idx, self.line);
310                }
311                self.chunk.emit(Op::Throw, self.line);
312                self.chunk.patch_jump(ok_jump);
313                self.chunk.emit(Op::Pop, self.line);
314            }
315            Node::Block(stmts) => {
316                self.compile_scoped_block(stmts)?;
317            }
318            Node::DeadlineBlock { duration, body } => {
319                self.compile_node(duration)?;
320                self.chunk.emit(Op::DeadlineSetup, self.line);
321                self.compile_scoped_block(body)?;
322                self.chunk.emit(Op::DeadlineEnd, self.line);
323            }
324            Node::MutexBlock { body } => {
325                // v1: single-threaded, but still uses a lexical block scope.
326                self.begin_scope();
327                for sn in body {
328                    self.compile_node(sn)?;
329                    if Self::produces_value(&sn.node) {
330                        self.chunk.emit(Op::Pop, self.line);
331                    }
332                }
333                self.chunk.emit(Op::Nil, self.line);
334                self.end_scope();
335            }
336            Node::DeferStmt { body } => {
337                // Push onto the finally stack so it runs on return/throw/scope-exit.
338                self.finally_bodies
339                    .push(FinallyEntry::Finally(body.clone()));
340                self.chunk.emit(Op::Nil, self.line);
341            }
342            Node::YieldExpr { value } => {
343                if let Some(val) = value {
344                    self.compile_node(val)?;
345                } else {
346                    self.chunk.emit(Op::Nil, self.line);
347                }
348                self.chunk.emit(Op::Yield, self.line);
349            }
350            Node::EnumConstruct {
351                enum_name,
352                variant,
353                args,
354            } => {
355                self.compile_enum_construct(enum_name, variant, args)?;
356            }
357            Node::StructConstruct {
358                struct_name,
359                fields,
360            } => {
361                self.compile_struct_construct(struct_name, fields)?;
362            }
363            Node::ImportDecl { path } => {
364                let idx = self.chunk.add_constant(Constant::String(path.clone()));
365                self.chunk.emit_u16(Op::Import, idx, self.line);
366            }
367            Node::SelectiveImport { names, path } => {
368                let path_idx = self.chunk.add_constant(Constant::String(path.clone()));
369                let names_str = names.join(",");
370                let names_idx = self.chunk.add_constant(Constant::String(names_str));
371                self.chunk
372                    .emit_u16(Op::SelectiveImport, path_idx, self.line);
373                let hi = (names_idx >> 8) as u8;
374                let lo = names_idx as u8;
375                self.chunk.code.push(hi);
376                self.chunk.code.push(lo);
377                self.chunk.lines.push(self.line);
378                self.chunk.columns.push(self.column);
379                self.chunk.lines.push(self.line);
380                self.chunk.columns.push(self.column);
381            }
382            Node::TryOperator { operand } => {
383                self.compile_node(operand)?;
384                self.chunk.emit(Op::TryUnwrap, self.line);
385            }
386            // `try* EXPR`: evaluate EXPR; on throw, run pending finally
387            // blocks up to the innermost catch barrier and rethrow the
388            // original value. On success, leave EXPR's value on the stack.
389            //
390            // Per the issue-#26 desugaring:
391            //   { let _r = try { EXPR }
392            //     guard is_ok(_r) else { throw unwrap_err(_r) }
393            //     unwrap(_r) }
394            //
395            // The bytecode realizes this directly: install a try handler
396            // around EXPR so a throw lands in our catch path, where we
397            // pre-run pending finallys and re-emit `Throw`. Skipping the
398            // intermediate Result.Ok/Err wrapping that `TryExpr` does
399            // keeps the success path a no-op (operand value passes through
400            // as-is).
401            Node::TryStar { operand } => {
402                self.compile_try_star(operand)?;
403            }
404            Node::ImplBlock { type_name, methods } => {
405                self.compile_impl_block(type_name, methods)?;
406            }
407            Node::StructDecl { name, .. } => {
408                self.compile_struct_decl(name)?;
409            }
410            // Metadata-only declarations (no runtime effect).
411            Node::Pipeline { .. }
412            | Node::OverrideDecl { .. }
413            | Node::TypeDecl { .. }
414            | Node::EnumDecl { .. }
415            | Node::InterfaceDecl { .. } => {
416                self.chunk.emit(Op::Nil, self.line);
417            }
418            Node::TryCatch {
419                body,
420                error_var,
421                error_type,
422                catch_body,
423                finally_body,
424            } => {
425                self.compile_try_catch(body, error_var, error_type, catch_body, finally_body)?;
426            }
427            Node::TryExpr { body } => {
428                self.compile_try_expr(body)?;
429            }
430            Node::Retry { count, body } => {
431                self.compile_retry(count, body)?;
432            }
433            Node::Parallel {
434                mode,
435                expr,
436                variable,
437                body,
438                options,
439            } => {
440                self.compile_parallel(mode, expr, variable, body, options)?;
441            }
442            Node::SpawnExpr { body } => {
443                self.compile_spawn_expr(body)?;
444            }
445            Node::SelectExpr {
446                cases,
447                timeout,
448                default_body,
449            } => {
450                self.compile_select_expr(cases, timeout, default_body)?;
451            }
452            Node::Spread(_) => {
453                return Err(CompileError {
454                    message: "spread (...) can only be used inside list literals, dict literals, or function call arguments".into(),
455                    line: self.line,
456                });
457            }
458            Node::AttributedDecl { attributes, inner } => {
459                self.compile_attributed_decl(attributes, inner)?;
460            }
461        }
462        Ok(())
463    }
464}