hara-native 0.1.13

HAL-free native host runtime and package launcher for Hara
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
//! Named global-form compilation (issue #223): `defn`, `declare`, `field`,
//! and `instance?` lower to registry-direct global
//! instructions. Basic `def`, `set!`, and `var` bindings live in `bindings.rs`.
//! Names resolve in two phases:
//! compile-time visibility (program-declared globals plus the
//! compilation namespace registry) decides whether a symbol may compile
//! to a global reference at all; the emitted instructions resolve the
//! var through the registry again at execution time, so globals are
//! late-bound through the shared var cell — the same semantics the JVM
//! runtime and the fixed tree evaluator exhibit. Split from
//! `compiler.rs` to stay under the repository's per-file line cap.

use std::rc::Rc;

use crate::core::{binding_symbol, definition_metadata, schema_var_reference};
use crate::kernel::{Form, Span};
use crate::lang::data::Metadata;
use crate::lang::data::Symbol;
use crate::lang::protocol::INamespaced;
use crate::vm::error::{CompileError, CompileErrorKind};
use crate::vm::opcode::Instruction;

use super::{Child, Compiler};

impl Compiler {
    pub(super) fn excluded_foundation_symbol(&self, name: &str) -> bool {
        let Some((namespace, _)) = name.split_once('/') else {
            return false;
        };
        self.excluded_foundation_libraries.iter().any(|library| {
            namespace == format!("std.foundation.{library}")
                || crate::kernel::generated::foundation_library_alias(library)
                    .is_some_and(|alias| alias == namespace)
        })
    }

    pub(super) fn compile_defmacro(
        &mut self,
        children: &[Child<'_>],
        span: &Span,
        top: bool,
    ) -> Result<(), CompileError> {
        if !top {
            return Err(unsupported(
                "defmacro is only supported as a top-level statement",
                span.start,
            ));
        }
        if children.len() < 4 {
            return Err(CompileError::new(
                CompileErrorKind::Arity,
                "defmacro expects a name, parameters, and a body",
                Some(span.start),
            ));
        }
        let (name, metadata) = binding_symbol(children[1].form, "defmacro name")
            .map_err(|message| unsupported(message, children[1].span.start))?;
        self.require_owned_global(&name, children[1].span)?;
        let raw = children
            .iter()
            .map(|child| child.form.clone())
            .collect::<Vec<_>>();
        let (metadata, rest) = definition_metadata(metadata, &raw[2..], false, true)
            .map_err(|message| unsupported(format!("{name}: {message}"), children[1].span.start))?;
        let offset = children.len() - rest.len();
        let rest_children = &children[offset..];
        if rest_children.is_empty() {
            return Err(CompileError::new(
                CompileErrorKind::Arity,
                "defmacro expects a name, parameters, and a body",
                Some(span.start),
            ));
        }
        let metadata = self.var_metadata(metadata);
        self.declare_program_global(&name);
        if matches!(
            crate::core::form_without_metadata(rest_children[0].form),
            Form::Vector(_)
        ) {
            let params = macro_params(rest_children[0].form)
                .map_err(|message| unsupported(message, rest_children[0].span.start))?;
            let params_child = Child {
                form: &params,
                span: rest_children[0].span,
                children: None,
            };
            self.compile_function(
                Some(&name),
                &params_child,
                &rest_children[1..],
                span,
                false,
                false,
            )?;
        } else {
            let mut count = 0usize;
            for clause in rest_children {
                let Form::List(parts) = crate::core::form_without_metadata(clause.form) else {
                    return Err(unsupported(
                        "defmacro multi-arity clauses must be lists",
                        clause.span.start,
                    ));
                };
                if parts.len() < 2 {
                    return Err(CompileError::new(
                        CompileErrorKind::Arity,
                        "defmacro clause expects parameters and a body",
                        Some(clause.span.start),
                    ));
                }
                let params = macro_params(&parts[0])
                    .map_err(|message| unsupported(message, clause.span.start))?;
                let params_child = Child {
                    form: &params,
                    span: clause.span,
                    children: None,
                };
                let body = parts[1..]
                    .iter()
                    .map(|form| Child {
                        form,
                        span: clause.span,
                        children: None,
                    })
                    .collect::<Vec<_>>();
                self.compile_function(None, &params_child, &body, span, false, false)?;
                count += 1;
                if count > usize::from(u8::MAX) {
                    return Err(CompileError::new(
                        CompileErrorKind::Limit,
                        "defmacro supports at most 255 arity clauses",
                        Some(span.start),
                    ));
                }
            }
            let name_constant = self.name_constant(&name, children[1].span)?;
            self.emit(
                Instruction::MakeMultiArity {
                    name: name_constant,
                    count: count as u8,
                },
                Some(span.start),
            );
        }
        let name = self.name_constant(&name, children[1].span)?;
        self.emit(Instruction::DefMacro { name, metadata }, Some(span.start));
        Ok(())
    }

    pub(super) fn require_owned_global(&self, name: &str, span: &Span) -> Result<(), CompileError> {
        if let Ok(registry) = crate::core::namespace_registry() {
            let namespace = registry
                .find(&self.namespace)
                .unwrap_or_else(|| registry.current());
            if let Some(var) = namespace.resolve(&Symbol::parse(name)) {
                let owned_by_compilation_namespace =
                    var.symbol().get_namespace().is_none_or(|owner| {
                        owner == self.namespace.as_str() || owner == "std.foundation"
                    });
                if !owned_by_compilation_namespace {
                    return Err(CompileError::new(
                        CompileErrorKind::UnsupportedForm,
                        format!("Cannot replace referred Var without ns omission: {name}"),
                        Some(span.start),
                    ));
                }
            }
        }
        Ok(())
    }

    /// A name constant's pool index (no instruction emitted). Global
    /// operands are string names resolved at execution time.
    pub(super) fn name_constant(&mut self, name: &str, span: &Span) -> Result<u32, CompileError> {
        self.constant_index_of(crate::core::Value::String(name.to_string()), span)
    }

    /// A global read is bound to the var visible in the compilation
    /// namespace.  Leaving the operand unqualified would resolve it again in
    /// the caller's current namespace when a compiled closure runs, allowing
    /// user refers to redirect Foundation helper calls.
    pub(super) fn global_name_constant(
        &mut self,
        name: &str,
        span: &Span,
    ) -> Result<u32, CompileError> {
        let current_alias_local = name.strip_prefix("-/");
        let qualified = crate::core::namespace_registry()
            .ok()
            .and_then(|registry| {
                let current = registry
                    .find(&self.namespace)
                    .unwrap_or_else(|| registry.current());
                if let Some(local) = current_alias_local {
                    Some(format!("{}/{}", self.namespace, local))
                } else if !name.contains('/') && self.globals.iter().any(|global| global == name) {
                    Some(format!("{}/{}", self.namespace, name))
                } else if let Some((alias, local)) = name.split_once('/') {
                    (if registry.find(alias).is_some() {
                        registry.resolve(&crate::lang::data::Symbol::parse(name))
                    } else {
                        current.resolve(&crate::lang::data::Symbol::parse(name))
                    })
                    .map(|var| var.symbol().as_str().to_owned())
                    .or_else(|| {
                        current
                            .lazy_target(alias)
                            .map(|target| format!("{}/{}", target.as_str(), local))
                    })
                } else {
                    registry
                        .resolve(&crate::lang::data::Symbol::parse(name))
                        .map(|var| var.symbol().as_str().to_owned())
                }
            })
            .unwrap_or_else(|| name.to_owned());
        self.constant_index_of(crate::core::Value::String(qualified), span)
    }

    /// Registers a name as program-declared: visible to global
    /// references compiled from this point on.
    pub(super) fn declare_program_global(&mut self, name: &str) {
        if !self.globals.iter().any(|global| global == name) {
            self.globals.push(name.to_string());
        }
    }

    /// Whether `name` may compile to a global reference: declared by
    /// this program, or resolvable through the compilation namespace
    /// registry (the Runtime path; the free `compile_source` path has
    /// none and only sees program-declared names).
    pub(super) fn visible_global(&self, name: &str) -> bool {
        if self.excluded_foundation_symbol(name) {
            return false;
        }
        if self.allow_unbound_globals
            && !crate::core::syntax_symbol(name)
            && crate::core::IntrinsicOp::from_symbol(name).is_none()
            && !self.visible_namespace(name)
        {
            return true;
        }
        let declared = name
            .strip_prefix("-/")
            .is_some_and(|local| self.globals.iter().any(|global| global == local))
            || self.globals.iter().any(|global| global == name)
            || name
                .strip_prefix(&format!("{}/", self.namespace))
                .is_some_and(|local| self.globals.iter().any(|global| global == local));
        declared
            || crate::core::namespace_registry()
                .map(|registry| {
                    let current = registry
                        .find(&self.namespace)
                        .unwrap_or_else(|| registry.current());
                    let lazy_visible = name
                        .split_once('/')
                        .is_some_and(|(alias, _)| current.lazy_target(alias).is_some());
                    lazy_visible
                        || (name
                            .split_once('/')
                            .and_then(|(namespace, _)| registry.find(namespace))
                            .and_then(|_| registry.resolve(&crate::lang::data::Symbol::parse(name)))
                            .or_else(|| current.resolve(&crate::lang::data::Symbol::parse(name)))
                            .or_else(|| registry.resolve(&crate::lang::data::Symbol::parse(name))))
                        .is_some_and(|var| {
                            crate::core::IntrinsicOp::from_symbol(name).is_none()
                                || var.symbol().get_namespace() != Some("std.foundation")
                        })
                })
                .unwrap_or(false)
    }

    /// Bare namespace aliases are first-class callable values in the
    /// evaluator (`(promise ...)` means calling the namespace's `run` Var).
    /// Keep them distinct from Vars: a namespace has no global Var entry, so
    /// it needs its own validated instruction and runtime lookup.
    pub(super) fn visible_namespace(&self, name: &str) -> bool {
        crate::core::namespace_registry()
            .map(|registry| {
                let current = registry
                    .find(&self.namespace)
                    .unwrap_or_else(|| registry.current());
                let visible = current.lazy_target(name).is_some()
                    || current
                        .aliases()
                        .into_iter()
                        .any(|(alias, _)| alias.as_str() == name)
                    || registry.find(name).is_some();
                visible
            })
            .unwrap_or(false)
    }

    /// Canonical native/protocol callables may be emitted before ordinary
    /// Foundation Vars are available. Their values are still resolved from
    /// the shared namespace registry at execution time.
    pub(super) fn visible_bytecode_callable(&self, name: &str) -> bool {
        if self.excluded_foundation_symbol(name) {
            return false;
        }
        let Some(canonical) = crate::core::canonical_intrinsic_callable_symbol(name) else {
            return false;
        };
        crate::core::namespace_registry()
            .map(|registry| {
                let current = registry
                    .find(&self.namespace)
                    .unwrap_or_else(|| registry.current());
                registry
                    .resolve(&crate::lang::data::Symbol::parse(&canonical))
                    .or_else(|| current.resolve(&crate::lang::data::Symbol::parse(&canonical)))
                    .is_some()
            })
            .unwrap_or(true)
    }

    /// Emits a `GetGlobal` for a visible name.
    pub(super) fn emit_get_global(&mut self, name: &str, span: &Span) -> Result<(), CompileError> {
        let index = self.global_name_constant(name, span)?;
        self.emit(Instruction::GetGlobal(index), Some(span.start));
        Ok(())
    }

    pub(super) fn var_metadata(&mut self, metadata: Option<Rc<Metadata>>) -> Option<u16> {
        metadata.map(|metadata| {
            let index = self.var_metadata.len() as u16;
            self.var_metadata.push(metadata);
            index
        })
    }

    /// `(declare name ...)`: interns a nil var per name without resetting
    /// any existing binding. It supplies forward visibility only; namespace
    /// omission, not declare, controls whether a referred name may be replaced.
    /// Top-level statements only
    /// (stricter than the evaluator, documented); evaluates to nil.
    pub(super) fn compile_declare(
        &mut self,
        children: &[Child<'_>],
        span: &Span,
        top: bool,
    ) -> Result<(), CompileError> {
        if !top {
            return Err(unsupported(
                "declare is only supported as a top-level statement",
                span.start,
            ));
        }
        let names = &children[1..];
        if names.is_empty() {
            self.emit(Instruction::Nil, Some(span.start));
            return Ok(());
        }
        for (index, child) in names.iter().enumerate() {
            let Form::Symbol(name) = child.form else {
                return Err(CompileError::new(
                    CompileErrorKind::Arity,
                    "declare expects name symbols",
                    Some(child.span.start),
                ));
            };
            self.declare_program_global(name);
            let constant = self.name_constant(name, child.span)?;
            self.emit(Instruction::DeclareGlobal(constant), Some(child.span.start));
            if index + 1 != names.len() {
                self.emit(Instruction::Pop, Some(child.span.start));
            }
        }
        Ok(())
    }

    /// `(field instance :name)`: direct access to one declared mutable field;
    /// the field name is a literal keyword or symbol.
    pub(super) fn compile_field(
        &mut self,
        children: &[Child<'_>],
        span: &Span,
    ) -> Result<(), CompileError> {
        if children.len() != 3 {
            return Err(CompileError::new(
                CompileErrorKind::Arity,
                "field expects a mutable value and field name",
                Some(span.start),
            ));
        }
        let field = match children[2].form {
            Form::Keyword(field) | Form::Symbol(field) if !field.contains('/') => field,
            _ => {
                return Err(unsupported(
                    "field name must be an unqualified keyword or symbol",
                    children[2].span.start,
                ))
            }
        };
        let instance = &children[1];
        self.compile_form(instance.form, instance.span, instance.children, false)?;
        if !self.ctx().fallthrough {
            return Ok(());
        }
        let index = self.name_constant(field, children[2].span)?;
        self.emit(Instruction::MutableFieldGet(index), Some(span.start));
        Ok(())
    }

    /// `(instance? type value)`: immutable or mutable named-type membership.
    pub(super) fn compile_instance_of(
        &mut self,
        children: &[Child<'_>],
        span: &Span,
    ) -> Result<(), CompileError> {
        if children.len() != 3 {
            return Err(CompileError::new(
                CompileErrorKind::Arity,
                "instance? expects a struct or mutable type and value",
                Some(span.start),
            ));
        }
        for argument in &children[1..] {
            self.compile_form(argument.form, argument.span, argument.children, false)?;
        }
        if !self.ctx().fallthrough {
            return Ok(());
        }
        self.emit(Instruction::InstanceOf, Some(span.start));
        Ok(())
    }

    /// `(defn name ...)` interns a real var holding
    /// the function (single arity) or arity dispatcher (multiple
    /// clauses) and evaluates to the var, matching the evaluator. Definitions
    /// may occur inside a function body as well as in a top-level sequence:
    /// the name is visible before the bodies compile, so self-recursion within
    /// the form resolves through the var (late binding). Referred Vars remain
    /// owned by their defining namespace and must be omitted before a local
    /// definition.
    pub(super) fn compile_defn(
        &mut self,
        children: &[Child<'_>],
        span: &Span,
        _top: bool,
    ) -> Result<(), CompileError> {
        if children.len() < 4 {
            return Err(CompileError::new(
                CompileErrorKind::Arity,
                "defn expects a name, parameters, and a body",
                Some(span.start),
            ));
        }
        let (name, metadata) = binding_symbol(children[1].form, "defn name")
            .map_err(|message| unsupported(message, children[1].span.start))?;
        self.require_owned_global(&name, children[1].span)?;
        // `definition_metadata` works on the raw forms; the surviving
        // `rest` is a suffix of the elements, so the matching children
        // (with spans) are the same suffix of `children`.
        let raw: Vec<Form> = children.iter().map(|child| child.form.clone()).collect();
        let (metadata, rest) = definition_metadata(metadata, &raw[2..], false, false)
            .map_err(|message| unsupported(format!("{name}: {message}"), children[1].span.start))?;
        if let Some(schema) = schema_var_reference(metadata.as_deref()) {
            let schema_name = schema.as_str();
            if !self.visible_global(schema_name) {
                return Err(CompileError::new(
                    CompileErrorKind::UnboundSymbol,
                    format!("schema Var does not exist: {schema_name}"),
                    Some(children[1].span.start),
                ));
            }
        }
        let offset = children.len() - rest.len();
        let rest_children = &children[offset..];
        if rest_children.is_empty() {
            return Err(CompileError::new(
                CompileErrorKind::Arity,
                "defn expects a name, parameters, and a body",
                Some(span.start),
            ));
        }
        let async_function = metadata.as_ref().is_some_and(|value| value.flag("async"));
        if let Some(crate::lang::data::MetadataValue::Symbol(target)) = metadata
            .as_ref()
            .and_then(|value| value.get_keyword("inline-target"))
        {
            self.inline_globals
                .insert(name.clone(), target.as_str().to_owned());
        }
        let metadata = self.var_metadata(metadata);
        self.declare_program_global(&name);
        let single_arity = matches!(
            crate::core::form_without_metadata(rest_children[0].form),
            Form::Vector(_)
        );
        if single_arity {
            let suspend_allowed = async_function
                || rest_children[1..]
                    .iter()
                    .any(|child| self.form_may_suspend(child.form));
            self.compile_function(
                Some(&name),
                &rest_children[0],
                &rest_children[1..],
                span,
                async_function,
                suspend_allowed,
            )?;
        } else {
            // Multi-arity: each clause is a list `(params body...)`.
            let mut count = 0usize;
            for clause in rest_children {
                let clause_forms: &[Form] = match clause.form {
                    Form::List(forms) => forms,
                    _ => {
                        return Err(unsupported(
                            "defn multi-arity clauses must be lists",
                            clause.span.start,
                        ))
                    }
                };
                let clause_children =
                    self.list_children(clause_forms, clause.span, clause.children);
                if clause_children.len() < 2 {
                    return Err(CompileError::new(
                        CompileErrorKind::Arity,
                        "defn clause expects parameters and a body",
                        Some(clause.span.start),
                    ));
                }
                let suspend_allowed = async_function
                    || clause_children[1..]
                        .iter()
                        .any(|child| self.form_may_suspend(child.form));
                self.compile_function(
                    None,
                    &clause_children[0],
                    &clause_children[1..],
                    span,
                    async_function,
                    suspend_allowed,
                )?;
                count += 1;
                if count > u8::MAX as usize {
                    return Err(CompileError::new(
                        CompileErrorKind::Limit,
                        "defn supports at most 255 arity clauses",
                        Some(span.start),
                    ));
                }
            }
            let name_constant = self.name_constant(&name, children[1].span)?;
            self.emit(
                Instruction::MakeMultiArity {
                    name: name_constant,
                    count: count as u8,
                },
                Some(span.start),
            );
        }
        if !self.ctx().fallthrough {
            return Ok(());
        }
        let name_index = self.name_constant(&name, children[1].span)?;
        self.emit(
            Instruction::DefGlobal {
                name: name_index,
                metadata,
            },
            Some(span.start),
        );
        self.emit(Instruction::Pop, Some(span.start));
        self.emit(Instruction::VarGlobal(name_index), Some(span.start));
        Ok(())
    }
}

fn macro_params(form: &Form) -> Result<Form, String> {
    let Form::Vector(params) = crate::core::form_without_metadata(form) else {
        return Err("macro parameters must be a vector".into());
    };
    let mut implicit = vec![Form::Symbol("&form".into()), Form::Symbol("&env".into())];
    implicit.extend_from_slice(params);
    Ok(Form::Vector(implicit))
}

fn unsupported(message: impl Into<String>, position: crate::kernel::Position) -> CompileError {
    CompileError::new(
        CompileErrorKind::UnsupportedForm,
        message.into(),
        Some(position),
    )
}