nomiscript/ast_pair.rs
1//! Pair element types and struct layout ids.
2
3use core::fmt;
4
5use crate::ast::{ClosureSigId, EntityKind, WasmType};
6
7/// Element type of a homogeneous `$pair` chain. Tracked at compile time so
8/// CAR/CDR emit the right downcast and CONS refuses heterogeneous mixing
9/// with a structured type error. Kept as a `Copy` enum so `WasmType` stays
10/// `Copy` — and so adding a new element type stays a one-line variant
11/// addition. Nesting (pairs-of-pairs) waits for a follow-up sub-slice
12/// once flat lists are stable across the consumers.
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
14pub enum PairElement {
15 I32,
16 /// Truth-value cell: shares `I32`'s i31-boxed car representation (CAR/CDR
17 /// emit the same i31 downcast), but distinct so a bool extracted from a
18 /// list serializes as `Nil` / `Bool`, not `Number` — the list-element
19 /// analogue of [`WasmType::Bool`] vs `I32`.
20 Bool,
21 Ratio,
22 Commodity,
23 StringRef,
24 /// Runtime symbol or keyword: a `$symbol` struct carrying its printed name.
25 Symbol,
26 Entity(EntityKind),
27 /// A `DEFSTRUCT` instance of this layout: a `$struct` reference.
28 Struct(StructId),
29 /// A closure of this signature: a closure struct reference.
30 Closure(ClosureSigId),
31 /// Heterogeneous escape hatch (ADR-0025). Cars stay as raw
32 /// `anyref`; CAR returns `WasmType::AnyRef` and the script must
33 /// downcast at the use site (or consume via type-test natives like
34 /// `ok?` / `err-code`). CONS widens to this variant when the two
35 /// arms disagree on element type, and host fns that produce
36 /// fundamentally heterogeneous lists (e.g. =catch-each= result
37 /// cells) construct directly into `PairRef(AnyRef)`.
38 AnyRef,
39}
40
41impl fmt::Display for PairElement {
42 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43 match self {
44 Self::I32 => write!(f, "i32"),
45 Self::Bool => write!(f, "bool"),
46 Self::Ratio => write!(f, "ratio"),
47 Self::Commodity => write!(f, "commodity"),
48 Self::StringRef => write!(f, "string"),
49 Self::Symbol => write!(f, "symbol"),
50 Self::Entity(kind) => write!(f, "{kind}"),
51 Self::Struct(id) => write!(f, "{id}"),
52 Self::Closure(sig) => write!(f, "{}", WasmType::Closure(*sig)),
53 Self::AnyRef => write!(f, "any"),
54 }
55 }
56}
57
58impl PairElement {
59 /// The matching `WasmType` for a pair's car when extracted.
60 #[must_use]
61 pub fn as_wasm_type(self) -> WasmType {
62 match self {
63 Self::I32 => WasmType::I32,
64 Self::Bool => WasmType::Bool,
65 Self::Ratio => WasmType::Ratio,
66 Self::Commodity => WasmType::Commodity,
67 Self::StringRef => WasmType::StringRef,
68 Self::Symbol => WasmType::SymbolRef,
69 Self::Entity(kind) => WasmType::EntityRef(kind),
70 Self::Struct(id) => WasmType::StructRef(id),
71 Self::Closure(sig) => WasmType::Closure(sig),
72 Self::AnyRef => WasmType::AnyRef,
73 }
74 }
75
76 /// Inverse of `as_wasm_type`: `None` for a nested list, which has no
77 /// slot of its own. `WasmType::AnyRef` round-trips to `PairElement::AnyRef`.
78 #[must_use]
79 pub fn from_wasm_type(ty: WasmType) -> Option<Self> {
80 match ty {
81 WasmType::I32 => Some(Self::I32),
82 // `Bool` keeps its own slot — it shares `I32`'s i31-boxed car
83 // representation (the CAR/CDR downcast is identical) but stays
84 // distinct so a bool extracted from a list serializes as Nil/Bool,
85 // not Number.
86 WasmType::Bool => Some(Self::Bool),
87 WasmType::Ratio => Some(Self::Ratio),
88 WasmType::Commodity => Some(Self::Commodity),
89 WasmType::StringRef => Some(Self::StringRef),
90 WasmType::SymbolRef => Some(Self::Symbol),
91 WasmType::EntityRef(kind) => Some(Self::Entity(kind)),
92 WasmType::StructRef(id) => Some(Self::Struct(id)),
93 WasmType::Closure(sig) => Some(Self::Closure(sig)),
94 WasmType::AnyRef => Some(Self::AnyRef),
95 WasmType::PairRef(_) => None,
96 }
97 }
98
99 /// Widen `self` against `other` to the most-specific common
100 /// element. Identical types stay; everything else widens to
101 /// `AnyRef` so heterogeneous CONS no longer errors. Symmetric.
102 #[must_use]
103 pub fn widen(self, other: Self) -> Self {
104 if self == other { self } else { Self::AnyRef }
105 }
106}
107
108/// Index of a `DEFSTRUCT` layout in the session's struct registry. Rides
109/// inside `WasmType` like `ClosureSigId`, so it stays small and `Copy`.
110#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
111pub struct StructId(pub u32);
112
113impl fmt::Display for StructId {
114 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
115 write!(f, "struct#{}", self.0)
116 }
117}