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
22fn peel_node(sn: &SNode) -> &Node {
26 match &sn.node {
27 Node::AttributedDecl { inner, .. } => &inner.node,
28 other => other,
29 }
30}
31
32#[derive(Clone, Debug)]
35enum FinallyEntry {
36 Finally(Vec<SNode>),
37 CatchBarrier,
38}
39
40struct LoopContext {
42 start_offset: usize,
44 break_patches: Vec<usize>,
46 has_iterator: bool,
48 handler_depth: usize,
50 finally_depth: usize,
52 scope_depth: usize,
54}
55
56#[derive(Clone, Copy, Debug)]
57struct LocalBinding {
58 slot: u16,
59 mutable: bool,
60}
61
62pub struct Compiler {
64 chunk: Chunk,
65 line: u32,
66 column: u32,
67 enum_names: std::collections::HashSet<String>,
69 struct_layouts: std::collections::HashMap<String, Vec<String>>,
71 interface_methods: std::collections::HashMap<String, Vec<String>>,
73 loop_stack: Vec<LoopContext>,
75 handler_depth: usize,
77 finally_bodies: Vec<FinallyEntry>,
89 temp_counter: usize,
91 scope_depth: usize,
93 type_aliases: std::collections::HashMap<String, TypeExpr>,
96 type_scopes: Vec<std::collections::HashMap<String, TypeExpr>>,
101 local_scopes: Vec<std::collections::HashMap<String, LocalBinding>>,
105 module_level: bool,
111}
112
113impl Compiler {
114 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 ms = i64::try_from(*ms).map_err(|_| CompileError {
139 message: "duration literal is too large".to_string(),
140 line: self.line,
141 })?;
142 let idx = self.chunk.add_constant(Constant::Duration(ms));
143 self.chunk.emit_u16(Op::Constant, idx, self.line);
144 }
145 Node::Identifier(name) => {
146 self.emit_get_binding(name);
147 }
148 Node::LetBinding { pattern, value, .. } => {
149 let binding_type = match &snode.node {
150 Node::LetBinding {
151 type_ann: Some(type_ann),
152 ..
153 } => Some(type_ann.clone()),
154 _ => self.infer_expr_type(value),
155 };
156 self.compile_node(value)?;
157 self.compile_destructuring(pattern, false)?;
158 self.record_binding_type(pattern, binding_type);
159 }
160 Node::VarBinding { pattern, value, .. } => {
161 let binding_type = match &snode.node {
162 Node::VarBinding {
163 type_ann: Some(type_ann),
164 ..
165 } => Some(type_ann.clone()),
166 _ => self.infer_expr_type(value),
167 };
168 self.compile_node(value)?;
169 self.compile_destructuring(pattern, true)?;
170 self.record_binding_type(pattern, binding_type);
171 }
172 Node::Assignment {
173 target, value, op, ..
174 } => {
175 self.compile_assignment(target, value, op)?;
176 }
177 Node::BinaryOp { op, left, right } => {
178 self.compile_binary_op(op, left, right)?;
179 }
180 Node::UnaryOp { op, operand } => {
181 self.compile_node(operand)?;
182 match op.as_str() {
183 "-" => self.chunk.emit(Op::Negate, self.line),
184 "!" => self.chunk.emit(Op::Not, self.line),
185 _ => {}
186 }
187 }
188 Node::Ternary {
189 condition,
190 true_expr,
191 false_expr,
192 } => {
193 self.compile_node(condition)?;
194 let else_jump = self.chunk.emit_jump(Op::JumpIfFalse, self.line);
195 self.chunk.emit(Op::Pop, self.line);
196 self.compile_node(true_expr)?;
197 let end_jump = self.chunk.emit_jump(Op::Jump, self.line);
198 self.chunk.patch_jump(else_jump);
199 self.chunk.emit(Op::Pop, self.line);
200 self.compile_node(false_expr)?;
201 self.chunk.patch_jump(end_jump);
202 }
203 Node::FunctionCall { name, args, .. } => {
204 self.compile_function_call(name, args)?;
205 }
206 Node::MethodCall {
207 object,
208 method,
209 args,
210 } => {
211 self.compile_method_call(object, method, args)?;
212 }
213 Node::OptionalMethodCall {
214 object,
215 method,
216 args,
217 } => {
218 self.compile_node(object)?;
219 for arg in args {
220 self.compile_node(arg)?;
221 }
222 let name_idx = self.chunk.add_constant(Constant::String(method.clone()));
223 self.chunk
224 .emit_method_call_opt(name_idx, args.len() as u8, self.line);
225 }
226 Node::PropertyAccess { object, property } => {
227 self.compile_property_access(object, property)?;
228 }
229 Node::OptionalPropertyAccess { object, property } => {
230 self.compile_node(object)?;
231 let idx = self.chunk.add_constant(Constant::String(property.clone()));
232 self.chunk.emit_u16(Op::GetPropertyOpt, idx, self.line);
233 }
234 Node::SubscriptAccess { object, index } => {
235 self.compile_node(object)?;
236 self.compile_node(index)?;
237 self.chunk.emit(Op::Subscript, self.line);
238 }
239 Node::OptionalSubscriptAccess { object, index } => {
240 self.compile_node(object)?;
241 self.compile_node(index)?;
242 self.chunk.emit(Op::SubscriptOpt, self.line);
243 }
244 Node::SliceAccess { object, start, end } => {
245 self.compile_node(object)?;
246 if let Some(s) = start {
247 self.compile_node(s)?;
248 } else {
249 self.chunk.emit(Op::Nil, self.line);
250 }
251 if let Some(e) = end {
252 self.compile_node(e)?;
253 } else {
254 self.chunk.emit(Op::Nil, self.line);
255 }
256 self.chunk.emit(Op::Slice, self.line);
257 }
258 Node::IfElse {
259 condition,
260 then_body,
261 else_body,
262 } => {
263 self.compile_if_else(condition, then_body, else_body)?;
264 }
265 Node::WhileLoop { condition, body } => {
266 self.compile_while_loop(condition, body)?;
267 }
268 Node::ForIn {
269 pattern,
270 iterable,
271 body,
272 } => {
273 self.compile_for_in(pattern, iterable, body)?;
274 }
275 Node::ReturnStmt { value } => {
276 self.compile_return_stmt(value)?;
277 }
278 Node::BreakStmt => {
279 self.compile_break_stmt()?;
280 }
281 Node::ContinueStmt => {
282 self.compile_continue_stmt()?;
283 }
284 Node::ListLiteral(elements) => {
285 self.compile_list_literal(elements)?;
286 }
287 Node::DictLiteral(entries) => {
288 self.compile_dict_literal(entries)?;
289 }
290 Node::InterpolatedString(segments) => {
291 self.compile_interpolated_string(segments)?;
292 }
293 Node::FnDecl {
294 name,
295 params,
296 body,
297 is_stream,
298 ..
299 } => {
300 self.compile_fn_decl(name, params, body, *is_stream)?;
301 }
302 Node::ToolDecl {
303 name,
304 description,
305 params,
306 return_type,
307 body,
308 ..
309 } => {
310 self.compile_tool_decl(name, description, params, return_type, body)?;
311 }
312 Node::SkillDecl { name, fields, .. } => {
313 self.compile_skill_decl(name, fields)?;
314 }
315 Node::EvalPackDecl {
316 binding_name,
317 pack_id,
318 fields,
319 body,
320 summarize,
321 ..
322 } => {
323 self.compile_eval_pack_decl(binding_name, pack_id, fields, body, summarize, true)?;
324 }
325 Node::Closure { params, body, .. } => {
326 self.compile_closure(params, body)?;
327 }
328 Node::ThrowStmt { value } => {
329 self.compile_throw_stmt(value)?;
330 }
331 Node::MatchExpr { value, arms } => {
332 self.compile_match_expr(value, arms)?;
333 }
334 Node::RangeExpr {
335 start,
336 end,
337 inclusive,
338 } => {
339 let name_idx = self
340 .chunk
341 .add_constant(Constant::String("__range__".to_string()));
342 self.chunk.emit_u16(Op::Constant, name_idx, self.line);
343 self.compile_node(start)?;
344 self.compile_node(end)?;
345 if *inclusive {
346 self.chunk.emit(Op::True, self.line);
347 } else {
348 self.chunk.emit(Op::False, self.line);
349 }
350 self.chunk.emit_u8(Op::Call, 3, self.line);
351 }
352 Node::GuardStmt {
353 condition,
354 else_body,
355 } => {
356 self.compile_guard_stmt(condition, else_body)?;
357 }
358 Node::RequireStmt { condition, message } => {
359 self.compile_node(condition)?;
360 let ok_jump = self.chunk.emit_jump(Op::JumpIfTrue, self.line);
361 self.chunk.emit(Op::Pop, self.line);
362 if let Some(message) = message {
363 self.compile_node(message)?;
364 } else {
365 let idx = self
366 .chunk
367 .add_constant(Constant::String("require condition failed".to_string()));
368 self.chunk.emit_u16(Op::Constant, idx, self.line);
369 }
370 self.chunk.emit(Op::Throw, self.line);
371 self.chunk.patch_jump(ok_jump);
372 self.chunk.emit(Op::Pop, self.line);
373 }
374 Node::Block(stmts) => {
375 self.compile_scoped_block(stmts)?;
376 }
377 Node::DeadlineBlock { duration, body } => {
378 self.compile_node(duration)?;
379 self.chunk.emit(Op::DeadlineSetup, self.line);
380 self.compile_scoped_block(body)?;
381 self.chunk.emit(Op::DeadlineEnd, self.line);
382 }
383 Node::MutexBlock { body } => {
384 self.begin_scope();
385 let key_idx = self
386 .chunk
387 .add_constant(Constant::String("__default__".to_string()));
388 self.chunk.emit_u16(Op::SyncMutexEnter, key_idx, self.line);
389 for sn in body {
390 self.compile_node(sn)?;
391 if Self::produces_value(&sn.node) {
392 self.chunk.emit(Op::Pop, self.line);
393 }
394 }
395 self.chunk.emit(Op::Nil, self.line);
396 self.end_scope();
397 }
398 Node::DeferStmt { body } => {
399 self.finally_bodies
401 .push(FinallyEntry::Finally(body.clone()));
402 self.chunk.emit(Op::Nil, self.line);
403 }
404 Node::YieldExpr { value } => {
405 if let Some(val) = value {
406 self.compile_node(val)?;
407 } else {
408 self.chunk.emit(Op::Nil, self.line);
409 }
410 self.chunk.emit(Op::Yield, self.line);
411 }
412 Node::EmitExpr { value } => {
413 self.compile_node(value)?;
414 self.chunk.emit(Op::Yield, self.line);
415 }
416 Node::EnumConstruct {
417 enum_name,
418 variant,
419 args,
420 } => {
421 self.compile_enum_construct(enum_name, variant, args)?;
422 }
423 Node::StructConstruct {
424 struct_name,
425 fields,
426 } => {
427 self.compile_struct_construct(struct_name, fields)?;
428 }
429 Node::ImportDecl { path, .. } => {
430 let idx = self.chunk.add_constant(Constant::String(path.clone()));
431 self.chunk.emit_u16(Op::Import, idx, self.line);
432 }
433 Node::SelectiveImport { names, path, .. } => {
434 let path_idx = self.chunk.add_constant(Constant::String(path.clone()));
435 let names_str = names.join(",");
436 let names_idx = self.chunk.add_constant(Constant::String(names_str));
437 self.chunk
438 .emit_u16(Op::SelectiveImport, path_idx, self.line);
439 let hi = (names_idx >> 8) as u8;
440 let lo = names_idx as u8;
441 self.chunk.code.push(hi);
442 self.chunk.code.push(lo);
443 self.chunk.lines.push(self.line);
444 self.chunk.columns.push(self.column);
445 self.chunk.lines.push(self.line);
446 self.chunk.columns.push(self.column);
447 }
448 Node::TryOperator { operand } => {
449 self.compile_node(operand)?;
450 self.chunk.emit(Op::TryUnwrap, self.line);
451 }
452 Node::TryStar { operand } => {
468 self.compile_try_star(operand)?;
469 }
470 Node::ImplBlock { type_name, methods } => {
471 self.compile_impl_block(type_name, methods)?;
472 }
473 Node::StructDecl { name, fields, .. } => {
474 self.compile_struct_decl(name, fields)?;
475 }
476 Node::Pipeline { .. }
478 | Node::OverrideDecl { .. }
479 | Node::TypeDecl { .. }
480 | Node::EnumDecl { .. }
481 | Node::InterfaceDecl { .. } => {
482 self.chunk.emit(Op::Nil, self.line);
483 }
484 Node::TryCatch {
485 body,
486 error_var,
487 error_type,
488 catch_body,
489 finally_body,
490 } => {
491 self.compile_try_catch(body, error_var, error_type, catch_body, finally_body)?;
492 }
493 Node::TryExpr { body } => {
494 self.compile_try_expr(body)?;
495 }
496 Node::Retry { count, body } => {
497 self.compile_retry(count, body)?;
498 }
499 Node::CostRoute { options, body } => {
500 self.compile_cost_route(options, body)?;
501 }
502 Node::Parallel {
503 mode,
504 expr,
505 variable,
506 body,
507 options,
508 } => {
509 self.compile_parallel(mode, expr, variable, body, options)?;
510 }
511 Node::SpawnExpr { body } => {
512 self.compile_spawn_expr(body)?;
513 }
514 Node::SelectExpr {
515 cases,
516 timeout,
517 default_body,
518 } => {
519 self.compile_select_expr(cases, timeout, default_body)?;
520 }
521 Node::Spread(_) => {
522 return Err(CompileError {
523 message: "spread (...) can only be used inside list literals, dict literals, or function call arguments".into(),
524 line: self.line,
525 });
526 }
527 Node::AttributedDecl { attributes, inner } => {
528 self.compile_attributed_decl(attributes, inner)?;
529 }
530 Node::OrPattern(_) => {
531 return Err(CompileError {
532 message: "or-pattern (|) can only appear as a match arm pattern".into(),
533 line: self.line,
534 });
535 }
536 }
537 Ok(())
538 }
539}