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
use std::mem;
use cljrs_types::span::Span;
/// A parsed Clojure form with its source location.
///
/// `PartialEq` ignores spans so test assertions can compare forms without
/// constructing exact span values.
#[derive(Debug, Clone)]
pub struct Form {
pub kind: FormKind,
pub span: Span,
}
impl Form {
pub fn new(kind: FormKind, span: Span) -> Self {
Self { kind, span }
}
/// Total heap bytes owned by this form tree (excluding the `Form` itself).
pub fn heap_size(&self) -> usize {
mem::size_of::<FormKind>() + self.kind.heap_size()
}
/// The annotated form with every `^meta` wrapper removed.
///
/// Returns `self` when the form carries no metadata. Stacked metadata
/// (`^:a ^:b x`) is peeled down to the innermost form.
pub fn unmeta(&self) -> &Form {
let mut form = self;
while let FormKind::Meta(_, inner) = &form.kind {
form = inner;
}
form
}
/// True when an evaluated-position `^meta` annotation on this form becomes
/// *runtime* metadata on the value it produces.
///
/// Only a form that constructs an `IObj` qualifies — a collection literal
/// or a function. Every other form (a call, a symbol, `quote`, `if`, `do`)
/// takes the annotation as a compile-time hint and evaluates to an
/// unannotated value, so `(meta ^{:a 1} (list 1))` and `(meta ^{:a 1} x)`
/// are both `nil`.
///
/// Every execution tier consults this one predicate: the tree-walker in
/// `interp::eval`, and IR lowering in `lower::anf` for the JIT and AOT
/// paths. A tier that disagreed would make `meta` depend on how hot the
/// code got.
///
/// True when the value this form denotes *as data* can carry metadata.
///
/// Inside `quote` every form is a literal, so whether an annotation lands
/// is a question about the form and needs no runtime test: `'^{:a 1} [1]`
/// carries it, `'^{:a 1} 42` cannot. Mirrors `supports_meta` in
/// `cljrs-runtime` over the values `form_to_value` produces — a reader
/// macro (`'x`, `@x`, `#'x`, `` `x ``) denotes a list, and `#(…)` denotes
/// the `fn*` list it expands to.
pub fn quoted_value_supports_meta(&self) -> bool {
match &self.kind {
FormKind::List(_)
| FormKind::Vector(_)
| FormKind::Map(_)
| FormKind::Set(_)
| FormKind::Symbol(_)
| FormKind::AutoSymbol(_)
| FormKind::AnonFn(_)
| FormKind::Quote(_)
| FormKind::SyntaxQuote(_)
| FormKind::Unquote(_)
| FormKind::UnquoteSplice(_)
| FormKind::Deref(_)
| FormKind::Var(_) => true,
FormKind::Meta(_, inner) => inner.quoted_value_supports_meta(),
_ => false,
}
}
/// Inside `quote` the rule does not apply: there the annotation is data and
/// lands on any value that can carry it.
pub fn takes_runtime_meta(&self) -> bool {
match &self.kind {
FormKind::Vector(_) | FormKind::Map(_) | FormKind::Set(_) | FormKind::AnonFn(_) => true,
// A list is a call, except when it *is* a function form.
FormKind::List(parts) => matches!(
parts.first().map(|f| &f.kind),
Some(FormKind::Symbol(s)) if s == "fn" || s == "fn*"
),
// Metadata stacks: `^:a ^:b [1]` annotates the vector twice.
FormKind::Meta(_, inner) => inner.takes_runtime_meta(),
_ => false,
}
}
/// The `^meta` forms attached to this form, outermost first, together with
/// the annotated form itself.
pub fn peel_meta(&self) -> (Vec<&Form>, &Form) {
let mut metas = Vec::new();
let mut form = self;
while let FormKind::Meta(meta, inner) = &form.kind {
metas.push(meta.as_ref());
form = inner;
}
(metas, form)
}
// ── Structural views ──────────────────────────────────────────────────────
//
// Every accessor below reports the shape of [`Form::unmeta`], so an
// annotated form has the same structural shape as the form it annotates.
/// The symbol name, if this form is a symbol.
pub fn as_symbol(&self) -> Option<&str> {
match &self.unmeta().kind {
FormKind::Symbol(s) => Some(s),
_ => None,
}
}
/// The keyword name (without the leading `:`), if this form is a keyword.
pub fn as_keyword(&self) -> Option<&str> {
match &self.unmeta().kind {
FormKind::Keyword(k) => Some(k),
_ => None,
}
}
/// The string contents, if this form is a string literal.
pub fn as_string(&self) -> Option<&str> {
match &self.unmeta().kind {
FormKind::Str(s) => Some(s),
_ => None,
}
}
/// The elements, if this form is a list.
pub fn as_list(&self) -> Option<&[Form]> {
match &self.unmeta().kind {
FormKind::List(v) => Some(v),
_ => None,
}
}
/// The elements, if this form is a vector.
pub fn as_vector(&self) -> Option<&[Form]> {
match &self.unmeta().kind {
FormKind::Vector(v) => Some(v),
_ => None,
}
}
/// The flat key/value pairs, if this form is a map literal.
pub fn as_map(&self) -> Option<&[Form]> {
match &self.unmeta().kind {
FormKind::Map(v) => Some(v),
_ => None,
}
}
}
impl FormKind {
/// Heap bytes owned by this node and all children.
pub fn heap_size(&self) -> usize {
match self {
// Inline scalars — no heap.
FormKind::Nil
| FormKind::Bool(_)
| FormKind::Int(_)
| FormKind::Float(_)
| FormKind::Char(_)
| FormKind::Symbolic(_) => 0,
// String payloads.
FormKind::BigInt(s)
| FormKind::BigDecimal(s)
| FormKind::Ratio(s)
| FormKind::Str(s)
| FormKind::Regex(s)
| FormKind::Symbol(s)
| FormKind::Keyword(s)
| FormKind::AutoKeyword(s)
| FormKind::AutoSymbol(s) => s.capacity(),
// Vec<Form> — Vec overhead + recursive children.
FormKind::List(v)
| FormKind::Vector(v)
| FormKind::Map(v)
| FormKind::Set(v)
| FormKind::AnonFn(v) => vec_heap_size(v),
// Box<Form> — one Form on heap.
FormKind::Quote(f)
| FormKind::SyntaxQuote(f)
| FormKind::Unquote(f)
| FormKind::UnquoteSplice(f)
| FormKind::Deref(f)
| FormKind::Var(f) => mem::size_of::<Form>() + f.heap_size(),
// Two Box<Form>.
FormKind::Meta(a, b) => mem::size_of::<Form>() * 2 + a.heap_size() + b.heap_size(),
// String + Box<Form>.
FormKind::TaggedLiteral(s, f) => s.capacity() + mem::size_of::<Form>() + f.heap_size(),
FormKind::ReaderCond { clauses, .. } => vec_heap_size(clauses),
}
}
}
impl PartialEq for Form {
fn eq(&self, other: &Self) -> bool {
self.kind == other.kind
}
}
/// The payload of a `Form` node.
#[derive(Debug, Clone, PartialEq)]
pub enum FormKind {
// ── Atoms ─────────────────────────────────────────────────────────────────
Nil,
Bool(bool),
Int(i64),
BigInt(String),
Float(f64), // NaN != NaN per IEEE 754 — acceptable for AST equality
BigDecimal(String),
Ratio(String),
Char(char),
Str(String),
Regex(String),
/// `##Inf` → `INFINITY`, `##-Inf` → `NEG_INFINITY`, `##NaN` → `NAN`
Symbolic(f64),
// ── Identifiers ───────────────────────────────────────────────────────────
Symbol(String),
Keyword(String),
/// `::kw` / `::alias/kw` - the namespace is resolved against the reading
/// namespace by the evaluator, not by the reader.
AutoKeyword(String),
/// A symbol whose namespace is auto-resolved the same way. There is no
/// surface syntax for one; the reader produces it for a bare symbol key in
/// an auto-resolved namespaced map (`#::{a 1}`, `#::alias{a 1}`).
AutoSymbol(String),
// ── Collections ───────────────────────────────────────────────────────────
List(Vec<Form>),
Vector(Vec<Form>),
/// Flat key/value pairs; length is always even.
Map(Vec<Form>),
Set(Vec<Form>),
// ── Wrapping reader macros ────────────────────────────────────────────────
Quote(Box<Form>),
SyntaxQuote(Box<Form>),
Unquote(Box<Form>),
UnquoteSplice(Box<Form>),
Deref(Box<Form>),
/// `#'symbol`
Var(Box<Form>),
/// `^meta-form annotated-form` — raw meta form kept as-is; evaluator
/// expands shorthand (`:kw` → `{:kw true}`, `Sym` → `{:tag Sym}`).
Meta(Box<Form>, Box<Form>),
// ── Dispatch forms ────────────────────────────────────────────────────────
/// `#(…)` anonymous function literal
AnonFn(Vec<Form>),
/// `#tag form` tagged literal
TaggedLiteral(String, Box<Form>),
// ── Reader conditionals ───────────────────────────────────────────────────
/// All branches are kept; the evaluator filters by `:rust`.
/// `clauses` is flat: `[keyword, form, keyword, form, …]`.
ReaderCond {
splicing: bool,
clauses: Vec<Form>,
},
}
fn vec_heap_size(forms: &[Form]) -> usize {
mem::size_of_val(forms) + forms.iter().map(|f| f.heap_size()).sum::<usize>()
}