Skip to main content

blue_lang_runtime/
stdlib.rs

1//! blue's string and number core.
2//!
3//! tatara-lisp ships arithmetic, comparison, list and trig primitives and
4//! **no string operations at all** — no length, no concatenation, no case
5//! conversion, no number parsing. For a language whose surface is Ruby's, that
6//! is the largest parity gap there is: `String` is the most-used type in Ruby
7//! by a wide margin.
8//!
9//! # Why these live in blue and not in tatara-lisp
10//!
11//! The fleet rule is to extend the substrate rather than re-implement, and
12//! generic helpers belong upstream. These are not generic: the *semantics* are
13//! blue's, and they are Ruby's semantics specifically.
14//!
15//! The clearest case is `length`. Ruby's `String#length` counts **characters**;
16//! Rust's `str::len` counts **bytes**; Elixir's `String.length/1` counts
17//! grapheme clusters. Three languages, three answers, all defensible. Blue owes
18//! its users Ruby's answer, and encoding that choice into tatara-lisp would push
19//! one language's convention onto every other consumer of the substrate.
20//!
21//! Promoting a genuinely encoding-neutral core upstream later stays open; the
22//! character-counting ones are blue's by right.
23//!
24//! # Character, not byte, not grapheme
25//!
26//! Every index and length here is in **Unicode scalar values** (Rust `char`).
27//! That matches Ruby for the overwhelming majority of text and is stated rather
28//! than left to be discovered — a `length` that silently returns bytes is the
29//! bug that only appears once a user types a non-ASCII character.
30//!
31//! Grapheme clusters (Elixir's choice) would need a segmentation table; where
32//! the two differ — a family emoji, a combining accent — blue reports scalar
33//! values. `a_combining_sequence_counts_scalars_not_graphemes` pins it.
34
35use tatara_lisp_eval::ffi::Arity;
36use tatara_lisp_eval::{EvalError, Interpreter, Value};
37
38const TRIM_OPTIONS: &[&str] = &["side", "chars"];
39
40fn trim_with(text: &str, opts: &Value, s: tatara_lisp::Span) -> Result<Value, EvalError> {
41    let bad = |message: String| crate::errors::raise_kind("bad_option", message, s);
42    let mut side = "both".to_string();
43    let mut chars: Option<Vec<char>> = None;
44    match opts {
45        Value::Nil => {}
46        Value::Map(m) => {
47            for (k, v) in m.iter() {
48                let key = match k {
49                    tatara_lisp_eval::MapKey::Keyword(n)
50                    | tatara_lisp_eval::MapKey::Symbol(n)
51                    | tatara_lisp_eval::MapKey::Str(n) => n.to_string(),
52                    _ => String::new(),
53                };
54                match (key.as_str(), v) {
55                    ("side", Value::Keyword(k)) if matches!(&**k, "both" | "start" | "end") => {
56                        side = k.to_string();
57                    }
58                    ("side", _) => return Err(bad("side must be :both, :start or :end".into())),
59                    ("chars", Value::Str(c)) => chars = Some(c.chars().collect()),
60                    ("chars", _) => return Err(bad("chars must be text".into())),
61                    _ => {
62                        return Err(bad(format!(
63                            "unknown option {key}; this verb takes {}",
64                            TRIM_OPTIONS.join(", ")
65                        )))
66                    }
67                }
68            }
69        }
70        other => {
71            return Err(bad(format!(
72                "options must be a map, got {}",
73                other.type_name()
74            )))
75        }
76    }
77    let out = match (&chars, side.as_str()) {
78        (None, "both") => text.trim(),
79        (None, "start") => text.trim_start(),
80        (None, _) => text.trim_end(),
81        (Some(set), "both") => text.trim_matches(|c| set.contains(&c)),
82        (Some(set), "start") => text.trim_start_matches(|c| set.contains(&c)),
83        (Some(set), _) => text.trim_end_matches(|c| set.contains(&c)),
84    };
85    Ok(Value::Str(out.into()))
86}
87
88fn as_str(v: &Value, span: tatara_lisp::Span) -> Result<String, EvalError> {
89    match v {
90        Value::Str(s) => Ok(s.to_string()),
91        // A symbol is text the author wrote; accepting it makes `upcase(:ok)`
92        // work the way a Ruby programmer expects of a symbol.
93        Value::Symbol(s) | Value::Keyword(s) => Ok(s.to_string()),
94        other => Err(EvalError::type_mismatch(
95            "a string",
96            other.type_name(),
97            span,
98        )),
99    }
100}
101
102/// blue's one equality — what `==`, `!=`, `contains`, `index_of`, `count_of`
103/// and `distinct` all mean (okite D0001–D0003).
104///
105/// Ruby's answer: numbers compare by value across Int and Float (`4 == 4.0`),
106/// strings, symbols and keywords by text (each only with its own kind), lists
107/// element by element, maps by their entries whatever order they were built in,
108/// nil only with nil (not with `[]`, D0004), functions by identity. Until
109/// 2026-09-27 `==` was tatara's Scheme `equal?`, under which `4 == 4.0` was
110/// false and two equal maps were unequal unless they were the same object.
111/// tatara keeps Scheme's `equal?` for its own programs; blue redefines the
112/// name in its interpreter, and tatara's own `member?`, `position` and
113/// `distinct` call `equal?` by name, so they follow.
114pub fn blue_equal(a: &Value, b: &Value) -> bool {
115    match (a, b) {
116        (Value::Nil, Value::Nil) => true,
117        (Value::Bool(x), Value::Bool(y)) => x == y,
118        (Value::Int(x), Value::Int(y)) => x == y,
119        (Value::Float(x), Value::Float(y)) => x == y,
120        (Value::Int(x), Value::Float(y)) | (Value::Float(y), Value::Int(x)) => (*x as f64) == *y,
121        (Value::Str(x), Value::Str(y))
122        | (Value::Symbol(x), Value::Symbol(y))
123        | (Value::Keyword(x), Value::Keyword(y)) => x == y,
124        (Value::List(x), Value::List(y)) => {
125            x.len() == y.len() && x.iter().zip(y.iter()).all(|(p, q)| blue_equal(p, q))
126        }
127        (Value::Map(x), Value::Map(y)) => {
128            x.len() == y.len()
129                && x.iter()
130                    .all(|(k, v)| y.get(k).is_some_and(|w| blue_equal(v, w)))
131        }
132        (Value::Closure(x), Value::Closure(y)) => std::sync::Arc::ptr_eq(x, y),
133        (Value::NativeFn(x), Value::NativeFn(y)) => x.name == y.name,
134        (Value::Sexp(x, _), Value::Sexp(y, _)) => x == y,
135        _ => false,
136    }
137}
138
139#[derive(PartialEq, Eq, Hash)]
140enum EqualKey<'a> {
141    Nil,
142    Bool(bool),
143    Number(u64),
144    Str(&'a str),
145    Symbol(&'a str),
146    Keyword(&'a str),
147}
148
149fn number_key(n: f64) -> Option<EqualKey<'static>> {
150    (!n.is_nan()).then(|| EqualKey::Number(if n == 0.0 { 0 } else { n.to_bits() }))
151}
152
153fn equal_key(v: &Value) -> Option<EqualKey<'_>> {
154    match v {
155        Value::Nil => Some(EqualKey::Nil),
156        Value::Bool(b) => Some(EqualKey::Bool(*b)),
157        Value::Int(i) => number_key(*i as f64),
158        Value::Float(f) => number_key(*f),
159        Value::Str(s) => Some(EqualKey::Str(s)),
160        Value::Symbol(s) => Some(EqualKey::Symbol(s)),
161        Value::Keyword(s) => Some(EqualKey::Keyword(s)),
162        _ => None,
163    }
164}
165
166/// A map from each element to how many times it occurs, keyed as tatara's
167/// `hash-map` keys (an Int and a Float are two keys): the meaning of tatara's
168/// `frequencies`, built in one native pass.
169///
170/// # Errors
171///
172/// An element that cannot be a map key, as `hash-map` refuses it.
173pub fn frequencies<'a>(
174    xs: impl Iterator<Item = &'a Value>,
175    span: tatara_lisp::Span,
176) -> Result<Value, EvalError> {
177    let mut counts = tatara_lisp_eval::Map::new();
178    for x in xs {
179        let key = tatara_lisp_eval::value::MapKey::from_value(x).ok_or_else(|| {
180            EvalError::native_fn(
181                "hash-map",
182                format!("non-hashable key kind: {}", x.type_name()),
183                span,
184            )
185        })?;
186        let n = match counts.get(&key) {
187            Some(Value::Int(n)) => n + 1,
188            _ => 1,
189        };
190        counts.insert(key, Value::Int(n));
191    }
192    Ok(Value::Map(std::sync::Arc::new(counts)))
193}
194
195/// The elements with repeats removed under [`blue_equal`], first occurrence
196/// kept: the meaning of tatara's `distinct`, in linear time for the scalars.
197///
198/// Two values `blue_equal` calls equal always share a key, so a value is
199/// compared only with the kept values of its own key; values with no key
200/// (lists, maps, functions, NaN) are compared with every kept keyless value.
201pub fn distinct<'a>(xs: impl Iterator<Item = &'a Value>) -> Vec<Value> {
202    let mut kept: Vec<Value> = Vec::new();
203    let mut buckets: std::collections::HashMap<EqualKey<'a>, Vec<usize>> =
204        std::collections::HashMap::new();
205    let mut keyless: Vec<usize> = Vec::new();
206    for x in xs {
207        let seen = match equal_key(x) {
208            Some(k) => buckets.entry(k).or_default(),
209            None => &mut keyless,
210        };
211        if !seen.iter().any(|&i| blue_equal(&kept[i], x)) {
212            seen.push(kept.len());
213            kept.push(x.clone());
214        }
215    }
216    kept
217}
218
219/// `to_s` (okite D0005, D0006): text for a person. A string, symbol or keyword
220/// is its own text and nil is empty, as in Ruby; every other value is its blue
221/// literal. Until 2026-09-27 a map rendered as the word `map`, a list lost its
222/// brackets (`[1, {a: 2}]` came out `1 map`) and `1.0` came out `1`.
223fn chain_rules_value() -> Value {
224    use blue_lang_check::chains::{cases, Case, Kind, RULES};
225    let sym = |s: &str| Value::Symbol(std::sync::Arc::from(s));
226    let text = |s: &str| Value::Str(std::sync::Arc::from(s));
227    let hole = |n: usize| (0..n).map(|_| sym("x")).collect::<Vec<_>>();
228    let lambda = |body: Value| Value::list([sym("lambda"), Value::list([sym("x")]), body]);
229    let call = |name: &str, args: Vec<Value>| Value::list(std::iter::once(sym(name)).chain(args));
230    Value::list(RULES.iter().map(|rule| {
231        let rows = cases(rule).into_iter().map(|case| match case {
232            Case::Call { inner, argc } => {
233                let inner_call = call(inner, hole(argc));
234                Value::list([
235                    text(&format!("{}({}(x))", rule.outer, inner)),
236                    Value::Nil,
237                    lambda(call(rule.outer, vec![inner_call.clone()])),
238                    lambda(inner_call),
239                ])
240            }
241            Case::Literal(kind) => Value::list([
242                text(&format!("{}(<literal>)", rule.outer)),
243                Value::Keyword(std::sync::Arc::from(match kind {
244                    Kind::Text => "text",
245                    Kind::Int => "int",
246                    Kind::Float => "float",
247                })),
248                lambda(call(rule.outer, vec![sym("x")])),
249                lambda(sym("x")),
250            ]),
251        });
252        Value::list([
253            text(rule.id),
254            text(rule.slug),
255            text(rule.outer),
256            text(rule.soundness),
257            Value::list(rows),
258        ])
259    }))
260}
261
262fn render(v: &Value) -> String {
263    match v {
264        Value::Nil => String::new(),
265        Value::Str(s) | Value::Symbol(s) | Value::Keyword(s) => s.to_string(),
266        Value::Error(e) => crate::errors::describe(e),
267        other => literal(other),
268    }
269}
270
271/// A value as blue source writes it: `[1, "a", nil]`, `{a: 1, b: 2.0}`.
272/// Map entries are sorted by key, since a map has no order of its own and the
273/// text must be the same for equal maps (D0006).
274fn literal(v: &Value) -> String {
275    match v {
276        Value::Nil => "nil".into(),
277        Value::Bool(b) => b.to_string(),
278        Value::Int(n) => n.to_string(),
279        Value::Float(x) => float_text(*x),
280        Value::Str(s) => quoted(s),
281        Value::Symbol(s) => s.to_string(),
282        Value::Keyword(s) => format!(":{s}"),
283        Value::List(items) => {
284            let parts: Vec<String> = items.iter().map(literal).collect();
285            format!("[{}]", parts.join(", "))
286        }
287        Value::Map(m) => {
288            let mut entries: Vec<String> = m
289                .iter()
290                .map(|(k, v)| format!("{} {}", key_text(k), literal(v)))
291                .collect();
292            entries.sort();
293            format!("{{{}}}", entries.join(", "))
294        }
295        other => other.type_name().to_string(),
296    }
297}
298
299/// A map key as it is written before its value: `a:` for a keyword or symbol,
300/// `"a" =>` / `1 =>` for anything else.
301fn key_text(k: &tatara_lisp_eval::value::MapKey) -> String {
302    use tatara_lisp_eval::value::MapKey;
303    match k {
304        MapKey::Keyword(s) | MapKey::Symbol(s) => format!("{s}:"),
305        MapKey::Str(s) => format!("{} =>", quoted(s)),
306        MapKey::Int(n) => format!("{n} =>"),
307        MapKey::Float(bits) => format!("{} =>", float_text(f64::from_bits(*bits))),
308        MapKey::Bool(b) => format!("{b} =>"),
309        MapKey::Nil => "nil =>".into(),
310    }
311}
312
313/// A float keeps its point, as in Ruby: `1.0`, `2.5`.
314fn float_text(x: f64) -> String {
315    if x.is_finite() && x.fract() == 0.0 && x.abs() < 1e16 {
316        format!("{x:.1}")
317    } else {
318        format!("{x}")
319    }
320}
321
322fn quoted(s: &str) -> String {
323    let mut out = String::with_capacity(s.len() + 2);
324    out.push('"');
325    for c in s.chars() {
326        match c {
327            '"' => out.push_str("\\\""),
328            '\\' => out.push_str("\\\\"),
329            '\n' => out.push_str("\\n"),
330            '\t' => out.push_str("\\t"),
331            c => out.push(c),
332        }
333    }
334    out.push('"');
335    out
336}
337
338fn list(items: Vec<Value>) -> Value {
339    Value::list(items)
340}
341
342/// Install blue's string and number core.
343pub fn install_blue_stdlib<H: 'static>(interp: &mut Interpreter<H>) {
344    crate::errors::install(interp);
345    crate::paths::install(interp);
346    crate::spread::install(interp);
347    crate::collection::install(interp);
348    // ── text ──────────────────────────────────────────────────────────
349
350    // `length` counts CHARACTERS, per Ruby. Also accepts a list, where it is
351    // the element count — Ruby's `Array#length`.
352    interp.register_fn(
353        "length",
354        Arity::Exact(1),
355        |a: &[Value], _h: &mut H, span| match &a[0] {
356            Value::List(items) => Ok(Value::Int(items.len() as i64)),
357            other => Ok(Value::Int(as_str(other, span)?.chars().count() as i64)),
358        },
359    );
360
361    // The documented builtins, for a program that reasons about names (the
362    // catalogue's "shares a builtin's name" column).
363    interp.register_fn(
364        "builtin_names",
365        Arity::Exact(0),
366        |_a: &[Value], _h: &mut H, _s| {
367            Ok(Value::list(
368                crate::docs::NAMES
369                    .iter()
370                    .map(|n| Value::Str(std::sync::Arc::from(n.name))),
371            ))
372        },
373    );
374    interp.register_fn(
375        "chain_rules",
376        Arity::Exact(0),
377        |_a: &[Value], _h: &mut H, _s| Ok(chain_rules_value()),
378    );
379    interp.register_fn("to_s", Arity::Exact(1), |a: &[Value], _h: &mut H, _s| {
380        Ok(Value::Str(render(&a[0]).into()))
381    });
382
383    interp.register_fn("upcase", Arity::Exact(1), |a: &[Value], _h: &mut H, s| {
384        Ok(Value::Str(as_str(&a[0], s)?.to_uppercase().into()))
385    });
386
387    interp.register_fn("downcase", Arity::Exact(1), |a: &[Value], _h: &mut H, s| {
388        Ok(Value::Str(as_str(&a[0], s)?.to_lowercase().into()))
389    });
390
391    interp.register_fn("trim", Arity::Range(1, 2), |a: &[Value], _h: &mut H, s| {
392        let text = as_str(&a[0], s)?;
393        trim_with(&text, a.get(1).unwrap_or(&Value::Nil), s)
394    });
395
396    interp.register_fn(
397        blue_lang_syntax::LOWERED_OPTIONAL_ARITY,
398        Arity::Exact(4),
399        |a: &[Value], _h: &mut H, s| {
400            let name = as_str(&a[0], s)?;
401            let (Value::List(rest), Value::Int(required), Value::Int(most)) = (&a[1], &a[2], &a[3])
402            else {
403                return Err(EvalError::native_fn(
404                    blue_lang_syntax::LOWERED_OPTIONAL_ARITY,
405                    "expects a name, the rest list and two counts",
406                    s,
407                ));
408            };
409            if rest.len() as i64 > *most {
410                let want = Arity::Range(*required as usize, (*required + *most) as usize);
411                return Err(EvalError::native_fn(
412                    name.as_str(),
413                    format!(
414                        "takes {}, got {}",
415                        crate::messages::arguments(&want),
416                        *required as usize + rest.len()
417                    ),
418                    s,
419                ));
420            }
421            Ok(Value::Nil)
422        },
423    );
424
425    interp.register_fn(
426        blue_lang_syntax::LOWERED_OPTIONAL_ARG,
427        Arity::Exact(2),
428        |a: &[Value], _h: &mut H, s| match (&a[0], &a[1]) {
429            (Value::List(rest), Value::Int(i)) => {
430                Ok(rest.get(*i as usize).cloned().unwrap_or(Value::Nil))
431            }
432            _ => Err(EvalError::native_fn(
433                blue_lang_syntax::LOWERED_OPTIONAL_ARG,
434                "expects the rest list and an index",
435                s,
436            )),
437        },
438    );
439
440    // `concat(a, …)` joins the text of one or more arguments, left to right
441    // (okite D0007; Ruby's String#concat takes many). Two-only until
442    // 2026-09-27, which made `concat(a, b, c)` an arity error. `+` stays
443    // arithmetic: Ruby overloads `+` on String, but blue's `+` lowers to
444    // tatara's numeric `+`, and making it polymorphic would make a type error
445    // at a seam disappear into a string.
446    interp.register_fn(
447        "concat",
448        Arity::AtLeast(1),
449        |a: &[Value], _h: &mut H, _s| {
450            Ok(Value::Str(a.iter().map(render).collect::<String>().into()))
451        },
452    );
453
454    // ── equality and kinds (okite D0001–D0004, D0010) ─────────────────────
455    //
456    // `==` lowers to `equal?` and `!=` to `not=`; both are redefined here so
457    // blue's interpreter has one equality. See `blue_equal`.
458    interp.register_fn("equal?", Arity::Exact(2), |a: &[Value], _h: &mut H, _s| {
459        Ok(Value::Bool(blue_equal(&a[0], &a[1])))
460    });
461    interp.register_fn("not=", Arity::Exact(2), |a: &[Value], _h: &mut H, _s| {
462        Ok(Value::Bool(!blue_equal(&a[0], &a[1])))
463    });
464    interp.register_fn(
465        "distinct",
466        Arity::Exact(1),
467        |a: &[Value], _h: &mut H, span| match &a[0] {
468            Value::Nil => Ok(Value::list(std::iter::empty())),
469            Value::List(xs) => Ok(Value::list(distinct(xs.iter()))),
470            other => Err(EvalError::type_mismatch("a list", other.type_name(), span)),
471        },
472    );
473    interp.register_fn(
474        "frequencies",
475        Arity::Exact(1),
476        |a: &[Value], _h: &mut H, span| match &a[0] {
477            Value::Nil => Ok(frequencies(std::iter::empty(), span)?),
478            Value::List(xs) => Ok(frequencies(xs.iter(), span)?),
479            other => Err(EvalError::type_mismatch("a list", other.type_name(), span)),
480        },
481    );
482
483    // One predicate per kind, and exactly one holds of any value (D0010).
484    // tatara's `nil?` is its Scheme alias for `null?`, true of `[]` as well,
485    // and its `list?` is true of nil; blue's nil is absent and `[]` is an empty
486    // list (D0004), so blue redefines both. tatara's own library ends its
487    // recursion with `null?`, which is untouched.
488    interp.register_fn("nil?", Arity::Exact(1), |a: &[Value], _h: &mut H, _s| {
489        Ok(Value::Bool(matches!(a[0], Value::Nil)))
490    });
491    interp.register_fn("list?", Arity::Exact(1), |a: &[Value], _h: &mut H, _s| {
492        Ok(Value::Bool(matches!(a[0], Value::List(_))))
493    });
494    interp.register_fn("map?", Arity::Exact(1), |a: &[Value], _h: &mut H, _s| {
495        Ok(Value::Bool(matches!(a[0], Value::Map(_))))
496    });
497    interp.register_fn("float?", Arity::Exact(1), |a: &[Value], _h: &mut H, _s| {
498        Ok(Value::Bool(matches!(a[0], Value::Float(_))))
499    });
500    interp.register_fn("bool?", Arity::Exact(1), |a: &[Value], _h: &mut H, _s| {
501        Ok(Value::Bool(matches!(a[0], Value::Bool(_))))
502    });
503
504    // The empty list is always `[]`, never nil (D0011). tatara's `cdr` (and so
505    // `rest`, which is `cdr`) and `append` returned nil for an empty result,
506    // Scheme's convention, which under D0004 made `rest([1]) == []` false.
507    // Same contracts otherwise: `cdr` of an empty list is still an error.
508    interp.register_fn("cdr", Arity::Exact(1), |a: &[Value], _h: &mut H, span| {
509        match &a[0] {
510            // Shares the tail's structure: O(log n), not a copy of it.
511            Value::List(xs) if !xs.is_empty() => Ok(Value::List(std::sync::Arc::new(xs.skip(1)))),
512            Value::Nil | Value::List(_) => Err(EvalError::native_fn(
513                std::sync::Arc::<str>::from("cdr"),
514                "cdr of empty list",
515                span,
516            )),
517            other => Err(EvalError::type_mismatch("pair", other.type_name(), span)),
518        }
519    });
520    interp.register_owned_fn("append", Arity::Any, |a: Vec<Value>, _h: &mut H, span| {
521        // Persistent concatenation: each operand's structure is shared, so
522        // building a list by `append(acc, [x])` is linear overall, not
523        // quadratic (G7). The arguments arrive by value, so an accumulator
524        // the caller holds alone (tatara-lisp-eval runs a tail call to an
525        // owned primitive after releasing the caller's frame) is extended in
526        // its own allocation by `Arc::make_mut` instead of copied at the tail.
527        let mut lists = Vec::with_capacity(a.len());
528        for v in a {
529            match v {
530                Value::Nil => {}
531                Value::List(xs) => lists.push(xs),
532                other => return Err(EvalError::type_mismatch("list", other.type_name(), span)),
533            }
534        }
535        let mut lists = lists.into_iter();
536        let Some(mut out) = lists.next() else {
537            return Ok(Value::List(std::sync::Arc::new(
538                tatara_lisp_eval::List::new(),
539            )));
540        };
541        let acc = std::sync::Arc::make_mut(&mut out);
542        for xs in lists {
543            acc.append(
544                std::sync::Arc::try_unwrap(xs)
545                    .unwrap_or_else(|shared| tatara_lisp_eval::List::clone(&shared)),
546            );
547        }
548        Ok(Value::List(out))
549    });
550
551    interp.register_fn("split", Arity::Exact(2), |a: &[Value], _h: &mut H, s| {
552        let text = as_str(&a[0], s)?;
553        let sep = as_str(&a[1], s)?;
554        // An empty separator splits into characters, as Ruby's `split("")`
555        // does. Rust's `split("")` yields leading/trailing empties instead,
556        // which is the wrong answer here.
557        // Every field is kept: n separators give n + 1 fields, so `join` undoes
558        // `split` exactly (okite D0012). A deliberate deviation from Ruby, which
559        // drops trailing empty fields: tried on 2026-09-27, it broke nisshi's
560        // hash chain, kueri's scripts and moji's `lines`, because a parser whose
561        // split loses a field loses data and still reports success.
562        let parts: Vec<Value> = if sep.is_empty() {
563            text.chars()
564                .map(|c| Value::Str(c.to_string().into()))
565                .collect()
566        } else {
567            text.split(sep.as_str())
568                .map(|p| Value::Str(p.into()))
569                .collect()
570        };
571        Ok(list(parts))
572    });
573
574    interp.register_fn("join", Arity::Exact(2), |a: &[Value], _h: &mut H, s| {
575        let sep = as_str(&a[1], s)?;
576        match &a[0] {
577            Value::List(items) => Ok(Value::Str(
578                items
579                    .iter()
580                    .map(render)
581                    .collect::<Vec<_>>()
582                    .join(&sep)
583                    .into(),
584            )),
585            other => Err(EvalError::type_mismatch("a list", other.type_name(), s).into()),
586        }
587    });
588
589    interp.register_fn(
590        "contains?",
591        Arity::Exact(2),
592        |a: &[Value], _h: &mut H, s| {
593            Ok(Value::Bool(as_str(&a[0], s)?.contains(&as_str(&a[1], s)?)))
594        },
595    );
596
597    interp.register_fn(
598        "starts_with?",
599        Arity::Exact(2),
600        |a: &[Value], _h: &mut H, s| {
601            Ok(Value::Bool(
602                as_str(&a[0], s)?.starts_with(&as_str(&a[1], s)?),
603            ))
604        },
605    );
606
607    interp.register_fn(
608        "ends_with?",
609        Arity::Exact(2),
610        |a: &[Value], _h: &mut H, s| {
611            Ok(Value::Bool(as_str(&a[0], s)?.ends_with(&as_str(&a[1], s)?)))
612        },
613    );
614
615    interp.register_fn(
616        "text_index",
617        Arity::Exact(3),
618        |a: &[Value], _h: &mut H, s| {
619            Ok(Value::Int(text_index(
620                &as_str(&a[0], s)?,
621                &as_str(&a[1], s)?,
622                as_index(&a[2], s)?,
623            )))
624        },
625    );
626
627    interp.register_fn(
628        "text_last_index",
629        Arity::Exact(3),
630        |a: &[Value], _h: &mut H, s| {
631            Ok(Value::Int(text_last_index(
632                &as_str(&a[0], s)?,
633                &as_str(&a[1], s)?,
634                as_index(&a[2], s)?,
635            )))
636        },
637    );
638
639    interp.register_fn("replace", Arity::Exact(3), |a: &[Value], _h: &mut H, s| {
640        Ok(Value::Str(
641            as_str(&a[0], s)?
642                .replace(&as_str(&a[1], s)?, &as_str(&a[2], s)?)
643                .into(),
644        ))
645    });
646
647    interp.register_fn("reverse", Arity::Exact(1), |a: &[Value], _h: &mut H, s| {
648        match &a[0] {
649            Value::List(items) => Ok(Value::list(items.iter().rev().cloned())),
650            // Reversed by CHARACTER, so a multi-byte character survives. A
651            // byte-wise reverse produces invalid UTF-8.
652            other => Ok(Value::Str(
653                as_str(other, s)?.chars().rev().collect::<String>().into(),
654            )),
655        }
656    });
657
658    interp.register_fn("chars", Arity::Exact(1), |a: &[Value], _h: &mut H, s| {
659        Ok(list(
660            as_str(&a[0], s)?
661                .chars()
662                .map(|c| Value::Str(c.to_string().into()))
663                .collect(),
664        ))
665    });
666
667    interp.register_fn(
668        "text_bytes",
669        Arity::Exact(1),
670        |a: &[Value], _h: &mut H, s| {
671            Ok(list(
672                as_str(&a[0], s)?
673                    .as_bytes()
674                    .iter()
675                    .map(|b| Value::Int(i64::from(*b)))
676                    .collect(),
677            ))
678        },
679    );
680
681    interp.register_fn(
682        "bytes_text",
683        Arity::Exact(1),
684        |a: &[Value], _h: &mut H, s| {
685            let bytes = crate::crypto::message_bytes(&a[0])
686                .map_err(|e| EvalError::native_fn("bytes_text", e.to_string(), s))?;
687            Ok(match String::from_utf8(bytes) {
688                Ok(text) => Value::Str(text.into()),
689                Err(_) => Value::Nil,
690            })
691        },
692    );
693
694    // ── numbers ───────────────────────────────────────────────────────
695
696    // `to_int` RETURNS NIL on unparseable input rather than raising. Ruby's
697    // `String#to_i` answers 0 for garbage, which silently turns a parse failure
698    // into a plausible number; nil is falsy and cannot be mistaken for a
699    // result. `to_int!` is the raising form for callers who want the failure.
700    interp.register_fn(
701        "to_int",
702        Arity::Exact(1),
703        |a: &[Value], _h: &mut H, s| match &a[0] {
704            Value::Int(n) => Ok(Value::Int(*n)),
705            // A float with no Int (non-finite, or past i64) is nil like any
706            // other unconvertible input; `as i64` saturated it to a plausible
707            // wrong number.
708            Value::Float(x) => Ok(float_to_int(*x).map_or(Value::Nil, Value::Int)),
709            other => Ok(as_str(other, s)?
710                .trim()
711                .parse::<i64>()
712                .map_or(Value::Nil, Value::Int)),
713        },
714    );
715
716    interp.register_fn(
717        "to_int!",
718        Arity::Exact(1),
719        |a: &[Value], _h: &mut H, s| match &a[0] {
720            Value::Int(n) => Ok(Value::Int(*n)),
721            Value::Float(x) => float_to_int(*x)
722                .map(Value::Int)
723                .ok_or(EvalError::IntegerOverflow {
724                    op: "to_int!",
725                    at: s,
726                }),
727            other => {
728                let text = as_str(other, s)?;
729                text.trim().parse::<i64>().map(Value::Int).map_err(|_| {
730                    EvalError::native_fn(
731                        "to_int!",
732                        "`".to_string() + &text + "` is not an integer",
733                        s,
734                    )
735                    .into()
736                })
737            }
738        },
739    );
740
741    interp.register_fn(
742        "to_float",
743        Arity::Exact(1),
744        |a: &[Value], _h: &mut H, s| match &a[0] {
745            Value::Float(x) => Ok(Value::Float(*x)),
746            Value::Int(n) => Ok(Value::Float(*n as f64)),
747            other => Ok(as_str(other, s)?
748                .trim()
749                .parse::<f64>()
750                .map_or(Value::Nil, Value::Float)),
751        },
752    );
753
754    interp.register_fn(
755        "abs",
756        Arity::Exact(1),
757        |a: &[Value], _h: &mut H, s| match &a[0] {
758            // Checked, as tatara's `abs` is (G5): |i64::MIN| does not fit, and
759            // `n.abs()` wrapped in release and panicked in debug.
760            Value::Int(n) => n
761                .checked_abs()
762                .map(Value::Int)
763                .ok_or(EvalError::IntegerOverflow { op: "abs", at: s }),
764            Value::Float(x) => Ok(Value::Float(x.abs())),
765            other => Err(EvalError::type_mismatch("a number", other.type_name(), s).into()),
766        },
767    );
768
769    // ── ranges ────────────────────────────────────────────────────────
770
771    // `range` is a LOOP here, not a recursion. tatara-lisp's stdlib defines it
772    // in Lisp (`lisp_stdlib.tlisp`, where `range-impl` conses one frame per
773    // element), so `range(0, 5000)` overflowed the 8 MiB main stack and
774    // aborted the process. Everything built on it inherited the ceiling:
775    // retsu's `indexes`, `zip_with` and `enumerate`, and shomei's
776    // `chain_first_break`, which could not verify a 5,000-entry chain
777    // (measured 2026-09-24: 4,000 elements fine, 5,000 aborted).
778    //
779    // Same arities and answers as the Lisp definition: (end), (start, end),
780    // (start, end, step), ascending for a positive step, descending for a
781    // negative one, and each next element is the previous plus the step under
782    // the ordinary number rules (Int + Int is Int; a Float anywhere makes the
783    // rest Float). Two refusals the Lisp version lacked: a zero step, which
784    // recursed forever, and a wrong arity, which printed a message and
785    // returned nil.
786    //
787    // The destination is upstream: when tatara-lisp's own `range` is a loop,
788    // this registration is deleted.
789    interp.register_fn("range", Arity::Range(1, 3), |a: &[Value], _h: &mut H, s| {
790        let (start, end, step) = match a.len() {
791            1 => (Value::Int(0), a[0].clone(), Value::Int(1)),
792            2 => (a[0].clone(), a[1].clone(), Value::Int(1)),
793            _ => (a[0].clone(), a[1].clone(), a[2].clone()),
794        };
795        range_list(start, &end, &step, s)
796    });
797
798    // `write_stdout(s)` / `write_stderr(s)`: the text exactly, no quotes, no
799    // newline added. `print`, `println` and `display` all render a string
800    // with its quotes (a value printer, right for a REPL and wrong for a
801    // command's output), so until these a blue command-line tool could not
802    // print plain text. Output only, like `println`; anything else is not a
803    // string and is refused.
804    interp.register_fn(
805        "write_stdout",
806        Arity::Exact(1),
807        |a: &[Value], _h: &mut H, s| {
808            use std::io::Write;
809            let text = as_str(&a[0], s)?;
810            let mut out = std::io::stdout().lock();
811            out.write_all(text.as_bytes())
812                .and_then(|()| out.flush())
813                .map_err(|e| EvalError::native_fn("write_stdout", e.to_string(), s))?;
814            Ok(Value::Nil)
815        },
816    );
817    interp.register_fn(
818        "write_stderr",
819        Arity::Exact(1),
820        |a: &[Value], _h: &mut H, s| {
821            use std::io::Write;
822            let text = as_str(&a[0], s)?;
823            let mut err = std::io::stderr().lock();
824            err.write_all(text.as_bytes())
825                .and_then(|()| err.flush())
826                .map_err(|e| EvalError::native_fn("write_stderr", e.to_string(), s))?;
827            Ok(Value::Nil)
828        },
829    );
830
831    // `sort_keyed(key, xs)`: xs ordered by key(x), stably, in O(n log n),
832    // under `compare`'s rules. Every sort written in blue (junjo's) recurses
833    // per element and aborts the process near 600 elements; this is
834    // tatara-lisp's `sort-by-key` (0.3.60), which blue cannot name because
835    // it is kebab-case, bound to a name blue can. The engine is upstream,
836    // `hof::sort_keyed_values`; only the spelling lives here.
837    interp.register_higher_order_fn(
838        "sort_keyed",
839        Arity::Exact(2),
840        |a: &[Value], host: &mut H, caller: &tatara_lisp_eval::ffi::Caller<H>, s| {
841            let xs = match &a[1] {
842                Value::List(xs) => xs.to_vec(),
843                Value::Nil => Vec::new(),
844                other => {
845                    return Err(EvalError::type_mismatch("a list", other.type_name(), s));
846                }
847            };
848            let mut keyed = Vec::with_capacity(xs.len());
849            for x in xs {
850                keyed.push((caller.apply_value(&a[0], vec![x.clone()], host, s)?, x));
851            }
852            Ok(list(tatara_lisp_eval::hof::sort_keyed_values(keyed, s)?))
853        },
854    );
855
856    interp.register_fn(
857        "error_message",
858        Arity::Exact(1),
859        |a: &[Value], _h: &mut H, s| match &a[0] {
860            Value::Error(e) => Ok(Value::Str(e.message.clone())),
861            other => Err(EvalError::type_mismatch("an error", other.type_name(), s)),
862        },
863    );
864
865    interp.register_fn(
866        "error_kind",
867        Arity::Exact(1),
868        |a: &[Value], _h: &mut H, s| match &a[0] {
869            Value::Error(e) => Ok(Value::Keyword(e.tag.clone())),
870            other => Err(EvalError::type_mismatch("an error", other.type_name(), s)),
871        },
872    );
873
874    interp.register_fn(
875        "map_entries",
876        Arity::Exact(1),
877        |a: &[Value], _h: &mut H, s| match &a[0] {
878            Value::Map(m) => {
879                let keyed = m
880                    .iter()
881                    .map(|(k, v)| (k.to_value(), list(vec![k.to_value(), v.clone()])))
882                    .collect();
883                Ok(list(tatara_lisp_eval::hof::sort_keyed_values(keyed, s)?))
884            }
885            Value::Nil => Ok(list(Vec::new())),
886            other => Err(EvalError::type_mismatch("a map", other.type_name(), s)),
887        },
888    );
889}
890
891fn as_index(v: &Value, span: tatara_lisp::Span) -> Result<i64, EvalError> {
892    match v {
893        Value::Int(n) => Ok(*n),
894        other => Err(EvalError::type_mismatch(
895            "an integer",
896            other.type_name(),
897            span,
898        )),
899    }
900}
901
902fn text_index(text: &str, sub: &str, from: i64) -> i64 {
903    let from = usize::try_from(from.max(0)).unwrap_or(usize::MAX);
904    let start = match text.char_indices().nth(from) {
905        Some((b, _)) => b,
906        None if text.chars().count() == from => text.len(),
907        None => return -1,
908    };
909    text[start..].find(sub).map_or(-1, |b| {
910        i64::try_from(from + text[start..start + b].chars().count()).unwrap_or(i64::MAX)
911    })
912}
913
914fn text_last_index(text: &str, sub: &str, from: i64) -> i64 {
915    let Ok(from) = usize::try_from(from) else {
916        return -1;
917    };
918    let total = text.chars().count();
919    let width = sub.chars().count();
920    if width > total {
921        return -1;
922    }
923    let limit = from.min(total - width);
924    let end = text
925        .char_indices()
926        .nth(limit + width)
927        .map_or(text.len(), |(b, _)| b);
928    text[..end].rfind(sub).map_or(-1, |b| {
929        i64::try_from(text[..b].chars().count()).unwrap_or(i64::MAX)
930    })
931}
932
933/// A number's value as a float, or a type error naming `range`.
934fn range_num(v: &Value, s: tatara_lisp::Span) -> Result<f64, EvalError> {
935    match v {
936        Value::Int(n) => Ok(*n as f64),
937        Value::Float(x) => Ok(*x),
938        other => Err(EvalError::native_fn(
939            "range",
940            "expected a number, got ".to_string() + other.type_name(),
941            s,
942        )),
943    }
944}
945
946/// A float's integer part as an `i64`, or `None` when it is not finite or
947/// does not fit. `as i64` saturates instead.
948fn float_to_int(x: f64) -> Option<i64> {
949    const LIMIT: f64 = 9_223_372_036_854_775_808.0;
950    let t = x.trunc();
951    #[allow(clippy::cast_possible_truncation)]
952    (t.is_finite() && (-LIMIT..LIMIT).contains(&t)).then(|| t as i64)
953}
954
955/// `a + b` under the number rules the Lisp `range` used: Int + Int stays Int,
956/// anything with a Float is Float.
957fn range_add(a: &Value, b: &Value, s: tatara_lisp::Span) -> Result<Value, EvalError> {
958    match (a, b) {
959        (Value::Int(x), Value::Int(y)) => x.checked_add(*y).map(Value::Int).ok_or_else(|| {
960            EvalError::native_fn("range", "an element overflowed a 64-bit integer", s)
961        }),
962        _ => Ok(Value::Float(range_num(a, s)? + range_num(b, s)?)),
963    }
964}
965
966fn range_list(
967    start: Value,
968    end: &Value,
969    step: &Value,
970    s: tatara_lisp::Span,
971) -> Result<Value, EvalError> {
972    let end_f = range_num(end, s)?;
973    let step_f = range_num(step, s)?;
974    range_num(&start, s)?;
975    if step_f == 0.0 {
976        return Err(EvalError::native_fn(
977            "range",
978            "a step of zero never reaches the end",
979            s,
980        ));
981    }
982    let mut out = Vec::new();
983    let mut cur = start;
984    loop {
985        let c = range_num(&cur, s)?;
986        if (step_f > 0.0 && c >= end_f) || (step_f < 0.0 && c <= end_f) {
987            break;
988        }
989        let next = range_add(&cur, step, s)?;
990        out.push(cur);
991        cur = next;
992    }
993    Ok(list(out))
994}
995
996#[cfg(test)]
997mod tests {
998    use super::*;
999
1000    #[test]
1001    fn sort_keyed_is_stable_keyed_and_deep() {
1002        // Ties keep input order; the key is computed per element.
1003        assert_eq!(
1004            ints("map(fn(p) last(p) end, sort_keyed(fn(p) first(p) end, [[2, 1], [1, 2], [2, 3], [1, 4]]))"),
1005            vec![2, 4, 1, 3]
1006        );
1007        // 20,000 in reverse: every blue-written sort aborts far below this.
1008        assert_eq!(
1009            i("first(sort_keyed(fn(x) x end, reverse(range(0, 20000))))"),
1010            0
1011        );
1012        assert_eq!(i("last(sort_keyed(fn(x) 0 - x end, range(0, 20000)))"), 0);
1013        // The empty case, and a refusal for keys that cannot be ordered.
1014        assert_eq!(ints("sort_keyed(fn(x) x end, [])"), Vec::<i64>::new());
1015        assert!(crate::run("sort_keyed(fn(x) x end, [1, \"a\"])").is_err());
1016    }
1017
1018    #[test]
1019    fn distinct_is_native_and_keeps_tataras_meaning() {
1020        assert!(matches!(
1021            crate::interpreter_hostless().lookup_global("distinct"),
1022            Some(Value::NativeFn(_))
1023        ));
1024        let corpus = eval(
1025            "[1, 1.0, 0, -0.0, 0.0, 9007199254740993, 9007199254740992, 9007199254740992.0, \
1026             \"a\", :a, \"a\", nil, nil, true, false, true, [1, 2], [1, 2.0], [2], \
1027             {a: 1}, {a: 1.0}, 2.5, 2.5, -1, -1.0, \"\", \"\"]",
1028        );
1029        let Value::List(xs) = &corpus else {
1030            panic!("{corpus:?}")
1031        };
1032        let mut naive: Vec<Value> = Vec::new();
1033        for x in xs.iter() {
1034            if !naive.iter().any(|k| blue_equal(k, x)) {
1035                naive.push(x.clone());
1036            }
1037        }
1038        let fast = distinct(xs.iter());
1039        assert_eq!(fast.len(), naive.len());
1040        assert!(fast.iter().zip(&naive).all(|(a, b)| blue_equal(a, b)));
1041        assert_eq!(
1042            i("length(distinct(map((x) -> x % 997, range(300000))))"),
1043            997
1044        );
1045        assert_eq!(i("length(distinct(nil))"), 0);
1046    }
1047
1048    #[test]
1049    fn frequencies_is_native_and_counts_as_tatara_does() {
1050        assert!(matches!(
1051            crate::interpreter_hostless().lookup_global("frequencies"),
1052            Some(Value::NativeFn(_))
1053        ));
1054        let corpus = "(list 1 1.0 1 \"a\" 'a :a \"a\" #t #f #t 2.5 2.5 -0.0 0.0 0)";
1055        let mut upstream = tatara_lisp_eval::Interpreter::<()>::new();
1056        tatara_lisp_eval::install_full_stdlib_with(&mut upstream, &mut ());
1057        let forms = tatara_lisp::read_spanned(&format!("(frequencies {corpus})")).expect("reads");
1058        let expected = upstream
1059            .eval_program(&forms, &mut ())
1060            .expect("upstream counts");
1061        let Value::List(xs) = upstream
1062            .eval_program(&tatara_lisp::read_spanned(corpus).expect("reads"), &mut ())
1063            .expect("corpus")
1064        else {
1065            panic!("a list")
1066        };
1067        let ours = frequencies(xs.iter(), tatara_lisp::Span::synthetic()).expect("counts");
1068        assert!(blue_equal(&ours, &expected), "{ours:?} vs {expected:?}");
1069        assert!(crate::run("frequencies([[1]])").is_err());
1070        assert_eq!(
1071            i("get(frequencies(map((x) -> x % 7, range(70000))), 3)"),
1072            10000
1073        );
1074    }
1075
1076    fn eval(src: &str) -> Value {
1077        crate::run(src)
1078            .unwrap_or_else(|e| panic!("{src:?}: {e}"))
1079            .value
1080    }
1081
1082    fn s(src: &str) -> String {
1083        match eval(src) {
1084            Value::Str(v) => v.to_string(),
1085            other => panic!("{src:?} produced {other:?}"),
1086        }
1087    }
1088
1089    fn i(src: &str) -> i64 {
1090        match eval(src) {
1091            Value::Int(v) => v,
1092            other => panic!("{src:?} produced {other:?}"),
1093        }
1094    }
1095
1096    fn ints(src: &str) -> Vec<i64> {
1097        match eval(src) {
1098            Value::List(xs) => xs
1099                .iter()
1100                .map(|v| match v {
1101                    Value::Int(n) => *n,
1102                    other => panic!("{src:?} produced a non-Int element {other:?}"),
1103                })
1104                .collect(),
1105            other => panic!("{src:?} produced {other:?}"),
1106        }
1107    }
1108
1109    /// **`range` gives the Lisp definition's answers**, arity by arity,
1110    /// including the descending form and the empty cases.
1111    #[test]
1112    fn range_answers_as_the_lisp_definition_did() {
1113        assert_eq!(ints("range(5)"), vec![0, 1, 2, 3, 4]);
1114        assert_eq!(ints("range(2, 6)"), vec![2, 3, 4, 5]);
1115        assert_eq!(ints("range(0, 10, 2)"), vec![0, 2, 4, 6, 8]);
1116        assert_eq!(ints("range(10, 0, 0 - 2)"), vec![10, 8, 6, 4, 2]);
1117        assert!(ints("range(5, 5)").is_empty());
1118        assert!(ints("range(5, 2)").is_empty());
1119        assert!(ints("range(0)").is_empty());
1120        // A float step makes the elements after the first floats, as `+` did.
1121        match eval("range(0, 1, 0.5)") {
1122            Value::List(xs) => {
1123                assert!(matches!(xs[0], Value::Int(0)), "{xs:?}");
1124                assert!(matches!(xs[1], Value::Float(x) if x == 0.5), "{xs:?}");
1125                assert_eq!(xs.len(), 2);
1126            }
1127            other => panic!("{other:?}"),
1128        }
1129    }
1130
1131    /// **`range` is a loop, not a recursion.** The Lisp definition consed one
1132    /// stack frame per element and aborted the process near 5,000 elements on
1133    /// an 8 MiB stack. This runs on the test harness's 2 MiB thread, where the
1134    /// Lisp one died sooner still. Red run, 2026-09-24: with the registration
1135    /// removed, this test aborted the test binary with a stack overflow.
1136    #[test]
1137    fn range_builds_long_lists_without_recursing() {
1138        assert_eq!(i("length(range(0, 200000))"), 200_000);
1139        assert_eq!(i("nth(199999, range(0, 200000))"), 199_999);
1140    }
1141
1142    /// A zero step never reaches the end: refused, where the Lisp definition
1143    /// recursed until the stack gave out. A non-number is a named error.
1144    #[test]
1145    fn range_refuses_a_zero_step_and_a_non_number() {
1146        assert!(crate::run("range(0, 5, 0)").is_err());
1147        assert!(crate::run("range(0, \"x\")").is_err());
1148    }
1149
1150    /// **`length` counts CHARACTERS, as Ruby does — not bytes.** A `length`
1151    /// that returns bytes is the bug that only surfaces once a user types a
1152    /// non-ASCII character, which is exactly when it is hardest to trace.
1153    #[test]
1154    fn length_counts_characters_not_bytes() {
1155        assert_eq!(i("length(\"hello\")"), 5);
1156        // "héllo" is 6 bytes, 5 characters.
1157        assert_eq!(i("length(\"héllo\")"), 5, "must not be 6");
1158        // An emoji is 4 bytes, 1 character.
1159        assert_eq!(i("length(\"😀\")"), 1, "must not be 4");
1160    }
1161
1162    /// blue reports **scalar values**, not grapheme clusters. Elixir would say
1163    /// 1 here; blue says 2. Stated and pinned rather than left to surprise.
1164    #[test]
1165    fn a_combining_sequence_counts_scalars_not_graphemes() {
1166        // "e" + U+0301 COMBINING ACUTE — one grapheme, two scalars.
1167        assert_eq!(
1168            i("length(\"e\\u{301}\")"),
1169            2,
1170            "blue counts scalar values; Elixir's String.length would say 1"
1171        );
1172    }
1173
1174    #[test]
1175    fn length_also_works_on_a_list() {
1176        assert_eq!(i("length([1, 2, 3])"), 3);
1177    }
1178
1179    #[test]
1180    fn case_and_trim() {
1181        assert_eq!(s("upcase(\"abc\")"), "ABC");
1182        assert_eq!(s("downcase(\"ABC\")"), "abc");
1183        assert_eq!(s("trim(\"  hi  \")"), "hi");
1184        // Non-ASCII case works, which a byte-wise implementation would botch.
1185        assert_eq!(s("upcase(\"é\")"), "É");
1186    }
1187
1188    #[test]
1189    fn concat_and_to_s() {
1190        assert_eq!(s("concat(\"a\", \"b\")"), "ab");
1191        assert_eq!(s("concat(\"n=\", 42)"), "n=42");
1192        assert_eq!(s("to_s(42)"), "42");
1193        assert_eq!(s("to_s(true)"), "true");
1194    }
1195
1196    /// **`+` stays arithmetic.** Ruby overloads it on String, but blue's `+`
1197    /// lowers to tatara's numeric `+`; making it polymorphic would let a type
1198    /// error at a seam disappear into a string.
1199    #[test]
1200    fn plus_is_not_string_concatenation() {
1201        assert!(
1202            crate::run("\"a\" + \"b\"").is_err(),
1203            "`+` must not silently concatenate — use concat"
1204        );
1205    }
1206
1207    #[test]
1208    fn split_and_join() {
1209        assert_eq!(i("length(split(\"a,b,c\", \",\"))"), 3);
1210        assert_eq!(s("join(split(\"a,b,c\", \",\"), \"-\")"), "a-b-c");
1211        // Ruby's `split("")` yields characters.
1212        assert_eq!(i("length(split(\"abc\", \"\"))"), 3);
1213    }
1214
1215    #[test]
1216    fn predicates() {
1217        assert!(matches!(
1218            eval("contains?(\"hello\", \"ell\")"),
1219            Value::Bool(true)
1220        ));
1221        assert!(matches!(
1222            eval("contains?(\"hello\", \"xyz\")"),
1223            Value::Bool(false)
1224        ));
1225        assert!(matches!(
1226            eval("starts_with?(\"hello\", \"he\")"),
1227            Value::Bool(true)
1228        ));
1229        assert!(matches!(
1230            eval("ends_with?(\"hello\", \"lo\")"),
1231            Value::Bool(true)
1232        ));
1233    }
1234
1235    fn refused(src: &str) -> String {
1236        match crate::run(src) {
1237            Ok(v) => panic!("{src:?} should fail, produced {:?}", v.value),
1238            Err(e) => e.to_string(),
1239        }
1240    }
1241
1242    #[test]
1243    fn text_bytes_and_bytes_text_are_utf8_both_ways() {
1244        assert_eq!(ints("text_bytes(\"abc\")"), vec![97, 98, 99]);
1245        assert_eq!(ints("text_bytes(\"é\")"), vec![195, 169]);
1246        assert!(ints("text_bytes(\"\")").is_empty());
1247        assert_eq!(s("bytes_text([104, 195, 169])"), "hé");
1248        assert_eq!(s("bytes_text(text_bytes(\"€ x\"))"), "€ x");
1249        assert_eq!(s("bytes_text([])"), "");
1250        assert!(matches!(eval("bytes_text([195])"), Value::Nil));
1251        assert!(matches!(eval("bytes_text([255, 97])"), Value::Nil));
1252        assert!(refused("bytes_text([256])").contains("outside 0..=255"));
1253        assert!(refused("bytes_text(nil)").contains("string or a list of bytes"));
1254        assert!(refused("text_bytes(5)").contains("string"));
1255    }
1256
1257    #[test]
1258    fn trim_without_options_cuts_whitespace_at_both_ends() {
1259        assert_eq!(s("trim(\"  a b \\n\")"), "a b");
1260        assert_eq!(s("trim(\"  a \", nil)"), "a");
1261        assert_eq!(s("trim(\"  a \", {})"), "a");
1262        assert_eq!(s("trim(\"\")"), "");
1263        assert_eq!(s("trim(\"   \")"), "");
1264        assert_eq!(s("trim(\"\", {side: :start})"), "");
1265    }
1266
1267    #[test]
1268    fn trim_side_keeps_the_other_end() {
1269        assert_eq!(s("trim(\"  a  \", {side: :both})"), "a");
1270        assert_eq!(s("trim(\"  a  \", {side: :start})"), "a  ");
1271        assert_eq!(s("trim(\"  a  \", {side: :end})"), "  a");
1272    }
1273
1274    #[test]
1275    fn trim_chars_replaces_whitespace_as_the_set_to_cut() {
1276        assert_eq!(s("trim(\"--a-b--\", {chars: \"-\"})"), "a-b");
1277        assert_eq!(s("trim(\"-+a+-\", {chars: \"-+\", side: :start})"), "a+-");
1278        assert_eq!(s("trim(\"-+a+-\", {chars: \"-+\", side: :end})"), "-+a");
1279        assert_eq!(s("trim(\" a \", {chars: \"-\"})"), " a ");
1280        assert_eq!(s("trim(\"aaa\", {chars: \"a\"})"), "");
1281        assert_eq!(s("trim(\"héé\", {chars: \"é\", side: :end})"), "h");
1282        assert_eq!(s("trim(\"  a  \", {chars: \"\"})"), "  a  ");
1283    }
1284
1285    #[test]
1286    fn trim_refuses_what_it_does_not_take() {
1287        assert!(refused("trim(\"a\", {sid: :end})").contains("bad_option"));
1288        assert!(refused("trim(\"a\", {sid: :end})").contains("side, chars"));
1289        assert!(refused("trim(\"a\", {side: :middle})").contains("side must be"));
1290        assert!(refused("trim(\"a\", {side: \"end\"})").contains("bad_option"));
1291        assert!(refused("trim(\"a\", {chars: 1})").contains("chars must be text"));
1292        assert!(refused("trim(\"a\", 5)").contains("options must be a map"));
1293        assert!(refused("trim(\"a\", {}, {})").contains("1 to 2"));
1294        assert!(refused("trim()").contains("1 to 2"));
1295        assert!(refused("trim(5)").contains("string"));
1296    }
1297
1298    #[test]
1299    fn trim_is_still_a_value_other_functions_take() {
1300        assert_eq!(s("join(map(trim, [\" a\", \"b \"]), \",\")"), "a,b");
1301    }
1302
1303    #[test]
1304    fn text_index_answers_character_positions() {
1305        assert_eq!(i("text_index(\"héllo héllo\", \"llo\", 0)"), 2);
1306        assert_eq!(i("text_index(\"héllo héllo\", \"llo\", 3)"), 8);
1307        assert_eq!(i("text_index(\"héllo\", \"x\", 0)"), -1);
1308        assert_eq!(i("text_index(\"abc\", \"\", 3)"), 3);
1309        assert_eq!(i("text_index(\"abc\", \"\", 4)"), -1);
1310        assert_eq!(i("text_index(\"abc\", \"a\", -5)"), 0);
1311        assert_eq!(i("text_index(\"\", \"a\", 0)"), -1);
1312    }
1313
1314    #[test]
1315    fn text_last_index_finds_the_last_start_at_or_before_from() {
1316        assert_eq!(i("text_last_index(\"héllo héllo\", \"llo\", 99)"), 8);
1317        assert_eq!(i("text_last_index(\"héllo héllo\", \"llo\", 7)"), 2);
1318        assert_eq!(i("text_last_index(\"aaa\", \"aa\", 1)"), 1);
1319        assert_eq!(i("text_last_index(\"aaa\", \"aa\", 0)"), 0);
1320        assert_eq!(i("text_last_index(\"abc\", \"x\", 2)"), -1);
1321        assert_eq!(i("text_last_index(\"abc\", \"a\", -1)"), -1);
1322        assert_eq!(i("text_last_index(\"ab\", \"abc\", 0)"), -1);
1323        assert_eq!(i("text_last_index(\"abc\", \"\", 9)"), 3);
1324    }
1325
1326    #[test]
1327    fn replace_and_chars() {
1328        assert_eq!(s("replace(\"a-b-c\", \"-\", \"+\")"), "a+b+c");
1329        assert_eq!(i("length(chars(\"abc\"))"), 3);
1330    }
1331
1332    /// Reversed by character, so a multi-byte character survives. A byte-wise
1333    /// reverse produces invalid UTF-8.
1334    #[test]
1335    fn reverse_is_character_wise() {
1336        assert_eq!(s("reverse(\"abc\")"), "cba");
1337        assert_eq!(s("reverse(\"héllo\")"), "olléh", "must not corrupt the é");
1338    }
1339
1340    #[test]
1341    fn reverse_also_works_on_a_list() {
1342        assert_eq!(s("join(reverse([1, 2, 3]), \",\")"), "3,2,1");
1343    }
1344
1345    /// **`to_int` answers nil on garbage, not 0.** Ruby's `String#to_i` returns
1346    /// 0, which silently turns a parse failure into a plausible number — a
1347    /// deliberate divergence, and the reason `to_int!` exists for callers who
1348    /// want the failure loudly.
1349    #[test]
1350    fn to_int_is_nil_on_garbage_rather_than_zero() {
1351        assert_eq!(i("to_int(\"42\")"), 42);
1352        assert!(
1353            matches!(eval("to_int(\"banana\")"), Value::Nil),
1354            "Ruby would say 0 here; a falsy nil cannot be mistaken for a result"
1355        );
1356        assert!(
1357            matches!(eval("to_int(\"0\")"), Value::Int(0)),
1358            "and a real 0 is still a real 0 — the two must stay distinguishable"
1359        );
1360    }
1361
1362    #[test]
1363    fn to_int_bang_raises_on_garbage() {
1364        assert_eq!(i("to_int!(\"42\")"), 42);
1365        let err = crate::run("to_int!(\"banana\")").expect_err("must raise");
1366        assert!(err.to_string().contains("banana"), "must name it: {err}");
1367    }
1368
1369    #[test]
1370    fn numeric_conversions_and_abs() {
1371        assert_eq!(i("to_int(3.9)"), 3);
1372        assert_eq!(i("abs(0 - 5)"), 5);
1373        assert!(matches!(eval("to_float(\"1.5\")"), Value::Float(_)));
1374        assert!(matches!(eval("to_float(\"nope\")"), Value::Nil));
1375    }
1376
1377    /// A wrong-typed argument is a typed error, not a silent coercion.
1378    #[test]
1379    fn a_non_string_argument_is_a_type_error() {
1380        assert!(crate::run("upcase([1, 2])").is_err());
1381        assert!(crate::run("join(\"not a list\", \",\")").is_err());
1382    }
1383}