Skip to main content

sim_lib_pattern/
runtime.rs

1//! The pattern organ as a loadable kernel [`Lib`].
2//!
3//! Registers the pattern special forms as callables against the kernel
4//! [`Lib`]/[`Linker`] contract. Today that is the `match` form (COOKBOOK_7
5//! Category B); the crate's `Shape`/ADT machinery is the substrate it drives.
6
7use std::sync::Arc;
8
9use sim_kernel::{
10    AbiVersion, Cx, Export, Lib, LibManifest, LibTarget, Linker, Result, Symbol, Version,
11};
12
13use crate::match_form::MatchForm;
14
15const PATTERN_LIB_ID: &str = "pattern";
16
17/// Returns the `sim/pattern` manifest id under which this lib registers.
18pub fn manifest_name() -> Symbol {
19    Symbol::qualified("sim", PATTERN_LIB_ID)
20}
21
22/// The pattern organ lib: installs the pattern special forms as callables.
23pub struct PatternLib;
24
25impl Lib for PatternLib {
26    fn manifest(&self) -> LibManifest {
27        LibManifest {
28            id: manifest_name(),
29            version: Version(env!("CARGO_PKG_VERSION").to_owned()),
30            abi: AbiVersion { major: 0, minor: 1 },
31            target: LibTarget::HostRegistered,
32            requires: Vec::new(),
33            capabilities: Vec::new(),
34            exports: pattern_exports(),
35        }
36    }
37
38    fn load(&self, cx: &mut sim_kernel::LoadCx, linker: &mut Linker<'_>) -> Result<()> {
39        linker.function_value(
40            MatchForm::symbol(),
41            cx.factory().opaque(Arc::new(MatchForm))?,
42        )?;
43        Ok(())
44    }
45}
46
47/// Returns the lib's exported pattern forms as kernel [`Export`]s.
48pub fn pattern_exports() -> Vec<Export> {
49    vec![Export::Function {
50        symbol: MatchForm::symbol(),
51        function_id: None,
52    }]
53}
54
55/// Installs the pattern organ into `cx` (idempotent).
56pub fn install_pattern_lib(cx: &mut Cx) -> Result<()> {
57    if cx.registry().lib(&manifest_name()).is_some() {
58        return Ok(());
59    }
60    cx.load_lib(&PatternLib)?;
61    Ok(())
62}