1use tatara_lisp_eval::ffi::Arity;
36use tatara_lisp_eval::{EvalError, Interpreter, Value};
37
38fn as_str(v: &Value, span: tatara_lisp::Span) -> Result<String, EvalError> {
39 match v {
40 Value::Str(s) => Ok(s.to_string()),
41 Value::Symbol(s) | Value::Keyword(s) => Ok(s.to_string()),
44 other => Err(EvalError::type_mismatch(
45 "a string",
46 other.type_name(),
47 span,
48 )),
49 }
50}
51
52pub fn blue_equal(a: &Value, b: &Value) -> bool {
65 match (a, b) {
66 (Value::Nil, Value::Nil) => true,
67 (Value::Bool(x), Value::Bool(y)) => x == y,
68 (Value::Int(x), Value::Int(y)) => x == y,
69 (Value::Float(x), Value::Float(y)) => x == y,
70 (Value::Int(x), Value::Float(y)) | (Value::Float(y), Value::Int(x)) => (*x as f64) == *y,
71 (Value::Str(x), Value::Str(y))
72 | (Value::Symbol(x), Value::Symbol(y))
73 | (Value::Keyword(x), Value::Keyword(y)) => x == y,
74 (Value::List(x), Value::List(y)) => {
75 x.len() == y.len() && x.iter().zip(y.iter()).all(|(p, q)| blue_equal(p, q))
76 }
77 (Value::Map(x), Value::Map(y)) => {
78 x.len() == y.len()
79 && x.iter()
80 .all(|(k, v)| y.get(k).is_some_and(|w| blue_equal(v, w)))
81 }
82 (Value::Closure(x), Value::Closure(y)) => std::sync::Arc::ptr_eq(x, y),
83 (Value::NativeFn(x), Value::NativeFn(y)) => x.name == y.name,
84 (Value::Sexp(x, _), Value::Sexp(y, _)) => x == y,
85 _ => false,
86 }
87}
88
89fn render(v: &Value) -> String {
94 match v {
95 Value::Nil => String::new(),
96 Value::Str(s) | Value::Symbol(s) | Value::Keyword(s) => s.to_string(),
97 other => literal(other),
98 }
99}
100
101fn literal(v: &Value) -> String {
105 match v {
106 Value::Nil => "nil".into(),
107 Value::Bool(b) => b.to_string(),
108 Value::Int(n) => n.to_string(),
109 Value::Float(x) => float_text(*x),
110 Value::Str(s) => quoted(s),
111 Value::Symbol(s) => s.to_string(),
112 Value::Keyword(s) => format!(":{s}"),
113 Value::List(items) => {
114 let parts: Vec<String> = items.iter().map(literal).collect();
115 format!("[{}]", parts.join(", "))
116 }
117 Value::Map(m) => {
118 let mut entries: Vec<String> = m
119 .iter()
120 .map(|(k, v)| format!("{} {}", key_text(k), literal(v)))
121 .collect();
122 entries.sort();
123 format!("{{{}}}", entries.join(", "))
124 }
125 other => other.type_name().to_string(),
126 }
127}
128
129fn key_text(k: &tatara_lisp_eval::value::MapKey) -> String {
132 use tatara_lisp_eval::value::MapKey;
133 match k {
134 MapKey::Keyword(s) | MapKey::Symbol(s) => format!("{s}:"),
135 MapKey::Str(s) => format!("{} =>", quoted(s)),
136 MapKey::Int(n) => format!("{n} =>"),
137 MapKey::Float(bits) => format!("{} =>", float_text(f64::from_bits(*bits))),
138 MapKey::Bool(b) => format!("{b} =>"),
139 MapKey::Nil => "nil =>".into(),
140 }
141}
142
143fn float_text(x: f64) -> String {
145 if x.is_finite() && x.fract() == 0.0 && x.abs() < 1e16 {
146 format!("{x:.1}")
147 } else {
148 format!("{x}")
149 }
150}
151
152fn quoted(s: &str) -> String {
153 let mut out = String::with_capacity(s.len() + 2);
154 out.push('"');
155 for c in s.chars() {
156 match c {
157 '"' => out.push_str("\\\""),
158 '\\' => out.push_str("\\\\"),
159 '\n' => out.push_str("\\n"),
160 '\t' => out.push_str("\\t"),
161 c => out.push(c),
162 }
163 }
164 out.push('"');
165 out
166}
167
168fn list(items: Vec<Value>) -> Value {
169 Value::list(items)
170}
171
172pub fn install_blue_stdlib<H: 'static>(interp: &mut Interpreter<H>) {
174 interp.register_fn(
179 "length",
180 Arity::Exact(1),
181 |a: &[Value], _h: &mut H, span| match &a[0] {
182 Value::List(items) => Ok(Value::Int(items.len() as i64)),
183 other => Ok(Value::Int(as_str(other, span)?.chars().count() as i64)),
184 },
185 );
186
187 interp.register_fn(
190 "builtin_names",
191 Arity::Exact(0),
192 |_a: &[Value], _h: &mut H, _s| {
193 Ok(Value::list(
194 crate::docs::NAMES
195 .iter()
196 .map(|n| Value::Str(std::sync::Arc::from(n.name))),
197 ))
198 },
199 );
200 interp.register_fn("to_s", Arity::Exact(1), |a: &[Value], _h: &mut H, _s| {
201 Ok(Value::Str(render(&a[0]).into()))
202 });
203
204 interp.register_fn("upcase", Arity::Exact(1), |a: &[Value], _h: &mut H, s| {
205 Ok(Value::Str(as_str(&a[0], s)?.to_uppercase().into()))
206 });
207
208 interp.register_fn("downcase", Arity::Exact(1), |a: &[Value], _h: &mut H, s| {
209 Ok(Value::Str(as_str(&a[0], s)?.to_lowercase().into()))
210 });
211
212 interp.register_fn("trim", Arity::Exact(1), |a: &[Value], _h: &mut H, s| {
213 Ok(Value::Str(as_str(&a[0], s)?.trim().into()))
214 });
215
216 interp.register_fn(
223 "concat",
224 Arity::AtLeast(1),
225 |a: &[Value], _h: &mut H, _s| {
226 Ok(Value::Str(a.iter().map(render).collect::<String>().into()))
227 },
228 );
229
230 interp.register_fn("equal?", Arity::Exact(2), |a: &[Value], _h: &mut H, _s| {
235 Ok(Value::Bool(blue_equal(&a[0], &a[1])))
236 });
237 interp.register_fn("not=", Arity::Exact(2), |a: &[Value], _h: &mut H, _s| {
238 Ok(Value::Bool(!blue_equal(&a[0], &a[1])))
239 });
240
241 interp.register_fn("nil?", Arity::Exact(1), |a: &[Value], _h: &mut H, _s| {
247 Ok(Value::Bool(matches!(a[0], Value::Nil)))
248 });
249 interp.register_fn("list?", Arity::Exact(1), |a: &[Value], _h: &mut H, _s| {
250 Ok(Value::Bool(matches!(a[0], Value::List(_))))
251 });
252 interp.register_fn("map?", Arity::Exact(1), |a: &[Value], _h: &mut H, _s| {
253 Ok(Value::Bool(matches!(a[0], Value::Map(_))))
254 });
255 interp.register_fn("float?", Arity::Exact(1), |a: &[Value], _h: &mut H, _s| {
256 Ok(Value::Bool(matches!(a[0], Value::Float(_))))
257 });
258 interp.register_fn("bool?", Arity::Exact(1), |a: &[Value], _h: &mut H, _s| {
259 Ok(Value::Bool(matches!(a[0], Value::Bool(_))))
260 });
261
262 interp.register_fn("cdr", Arity::Exact(1), |a: &[Value], _h: &mut H, span| {
267 match &a[0] {
268 Value::List(xs) if !xs.is_empty() => Ok(Value::List(std::sync::Arc::new(xs.skip(1)))),
270 Value::Nil | Value::List(_) => Err(EvalError::native_fn(
271 std::sync::Arc::<str>::from("cdr"),
272 "cdr of empty list",
273 span,
274 )),
275 other => Err(EvalError::type_mismatch("pair", other.type_name(), span)),
276 }
277 });
278 interp.register_fn("append", Arity::Any, |a: &[Value], _h: &mut H, span| {
279 let mut out = tatara_lisp_eval::List::new();
283 for v in a {
284 match v {
285 Value::Nil => {}
286 Value::List(xs) => out.append(tatara_lisp_eval::List::clone(xs)),
287 other => return Err(EvalError::type_mismatch("list", other.type_name(), span)),
288 }
289 }
290 Ok(Value::List(std::sync::Arc::new(out)))
291 });
292
293 interp.register_fn("split", Arity::Exact(2), |a: &[Value], _h: &mut H, s| {
294 let text = as_str(&a[0], s)?;
295 let sep = as_str(&a[1], s)?;
296 let parts: Vec<Value> = if sep.is_empty() {
305 text.chars()
306 .map(|c| Value::Str(c.to_string().into()))
307 .collect()
308 } else {
309 text.split(sep.as_str())
310 .map(|p| Value::Str(p.into()))
311 .collect()
312 };
313 Ok(list(parts))
314 });
315
316 interp.register_fn("join", Arity::Exact(2), |a: &[Value], _h: &mut H, s| {
317 let sep = as_str(&a[1], s)?;
318 match &a[0] {
319 Value::List(items) => Ok(Value::Str(
320 items
321 .iter()
322 .map(render)
323 .collect::<Vec<_>>()
324 .join(&sep)
325 .into(),
326 )),
327 other => Err(EvalError::type_mismatch("a list", other.type_name(), s).into()),
328 }
329 });
330
331 interp.register_fn(
332 "contains?",
333 Arity::Exact(2),
334 |a: &[Value], _h: &mut H, s| {
335 Ok(Value::Bool(as_str(&a[0], s)?.contains(&as_str(&a[1], s)?)))
336 },
337 );
338
339 interp.register_fn(
340 "starts_with?",
341 Arity::Exact(2),
342 |a: &[Value], _h: &mut H, s| {
343 Ok(Value::Bool(
344 as_str(&a[0], s)?.starts_with(&as_str(&a[1], s)?),
345 ))
346 },
347 );
348
349 interp.register_fn(
350 "ends_with?",
351 Arity::Exact(2),
352 |a: &[Value], _h: &mut H, s| {
353 Ok(Value::Bool(as_str(&a[0], s)?.ends_with(&as_str(&a[1], s)?)))
354 },
355 );
356
357 interp.register_fn("replace", Arity::Exact(3), |a: &[Value], _h: &mut H, s| {
358 Ok(Value::Str(
359 as_str(&a[0], s)?
360 .replace(&as_str(&a[1], s)?, &as_str(&a[2], s)?)
361 .into(),
362 ))
363 });
364
365 interp.register_fn("reverse", Arity::Exact(1), |a: &[Value], _h: &mut H, s| {
366 match &a[0] {
367 Value::List(items) => Ok(Value::list(items.iter().rev().cloned())),
368 other => Ok(Value::Str(
371 as_str(other, s)?.chars().rev().collect::<String>().into(),
372 )),
373 }
374 });
375
376 interp.register_fn("chars", Arity::Exact(1), |a: &[Value], _h: &mut H, s| {
377 Ok(list(
378 as_str(&a[0], s)?
379 .chars()
380 .map(|c| Value::Str(c.to_string().into()))
381 .collect(),
382 ))
383 });
384
385 interp.register_fn(
392 "to_int",
393 Arity::Exact(1),
394 |a: &[Value], _h: &mut H, s| match &a[0] {
395 Value::Int(n) => Ok(Value::Int(*n)),
396 Value::Float(x) => Ok(float_to_int(*x).map_or(Value::Nil, Value::Int)),
400 other => Ok(as_str(other, s)?
401 .trim()
402 .parse::<i64>()
403 .map_or(Value::Nil, Value::Int)),
404 },
405 );
406
407 interp.register_fn(
408 "to_int!",
409 Arity::Exact(1),
410 |a: &[Value], _h: &mut H, s| match &a[0] {
411 Value::Int(n) => Ok(Value::Int(*n)),
412 Value::Float(x) => float_to_int(*x)
413 .map(Value::Int)
414 .ok_or(EvalError::IntegerOverflow {
415 op: "to_int!",
416 at: s,
417 }),
418 other => {
419 let text = as_str(other, s)?;
420 text.trim().parse::<i64>().map(Value::Int).map_err(|_| {
421 EvalError::native_fn(
422 "to_int!",
423 "`".to_string() + &text + "` is not an integer",
424 s,
425 )
426 .into()
427 })
428 }
429 },
430 );
431
432 interp.register_fn(
433 "to_float",
434 Arity::Exact(1),
435 |a: &[Value], _h: &mut H, s| match &a[0] {
436 Value::Float(x) => Ok(Value::Float(*x)),
437 Value::Int(n) => Ok(Value::Float(*n as f64)),
438 other => Ok(as_str(other, s)?
439 .trim()
440 .parse::<f64>()
441 .map_or(Value::Nil, Value::Float)),
442 },
443 );
444
445 interp.register_fn(
446 "abs",
447 Arity::Exact(1),
448 |a: &[Value], _h: &mut H, s| match &a[0] {
449 Value::Int(n) => n
452 .checked_abs()
453 .map(Value::Int)
454 .ok_or(EvalError::IntegerOverflow { op: "abs", at: s }),
455 Value::Float(x) => Ok(Value::Float(x.abs())),
456 other => Err(EvalError::type_mismatch("a number", other.type_name(), s).into()),
457 },
458 );
459
460 interp.register_fn("range", Arity::Range(1, 3), |a: &[Value], _h: &mut H, s| {
481 let (start, end, step) = match a.len() {
482 1 => (Value::Int(0), a[0].clone(), Value::Int(1)),
483 2 => (a[0].clone(), a[1].clone(), Value::Int(1)),
484 _ => (a[0].clone(), a[1].clone(), a[2].clone()),
485 };
486 range_list(start, &end, &step, s)
487 });
488
489 interp.register_fn(
496 "write_stdout",
497 Arity::Exact(1),
498 |a: &[Value], _h: &mut H, s| {
499 use std::io::Write;
500 let text = as_str(&a[0], s)?;
501 let mut out = std::io::stdout().lock();
502 out.write_all(text.as_bytes())
503 .and_then(|()| out.flush())
504 .map_err(|e| EvalError::native_fn("write_stdout", e.to_string(), s))?;
505 Ok(Value::Nil)
506 },
507 );
508 interp.register_fn(
509 "write_stderr",
510 Arity::Exact(1),
511 |a: &[Value], _h: &mut H, s| {
512 use std::io::Write;
513 let text = as_str(&a[0], s)?;
514 let mut err = std::io::stderr().lock();
515 err.write_all(text.as_bytes())
516 .and_then(|()| err.flush())
517 .map_err(|e| EvalError::native_fn("write_stderr", e.to_string(), s))?;
518 Ok(Value::Nil)
519 },
520 );
521
522 interp.register_higher_order_fn(
529 "sort_keyed",
530 Arity::Exact(2),
531 |a: &[Value], host: &mut H, caller: &tatara_lisp_eval::ffi::Caller<H>, s| {
532 let xs = match &a[1] {
533 Value::List(xs) => xs.to_vec(),
534 Value::Nil => Vec::new(),
535 other => {
536 return Err(EvalError::type_mismatch("a list", other.type_name(), s));
537 }
538 };
539 let mut keyed = Vec::with_capacity(xs.len());
540 for x in xs {
541 keyed.push((caller.apply_value(&a[0], vec![x.clone()], host, s)?, x));
542 }
543 Ok(list(tatara_lisp_eval::hof::sort_keyed_values(keyed, s)?))
544 },
545 );
546}
547
548fn range_num(v: &Value, s: tatara_lisp::Span) -> Result<f64, EvalError> {
550 match v {
551 Value::Int(n) => Ok(*n as f64),
552 Value::Float(x) => Ok(*x),
553 other => Err(EvalError::native_fn(
554 "range",
555 "expected a number, got ".to_string() + other.type_name(),
556 s,
557 )),
558 }
559}
560
561fn float_to_int(x: f64) -> Option<i64> {
564 const LIMIT: f64 = 9_223_372_036_854_775_808.0;
565 let t = x.trunc();
566 #[allow(clippy::cast_possible_truncation)]
567 (t.is_finite() && (-LIMIT..LIMIT).contains(&t)).then(|| t as i64)
568}
569
570fn range_add(a: &Value, b: &Value, s: tatara_lisp::Span) -> Result<Value, EvalError> {
573 match (a, b) {
574 (Value::Int(x), Value::Int(y)) => x.checked_add(*y).map(Value::Int).ok_or_else(|| {
575 EvalError::native_fn("range", "an element overflowed a 64-bit integer", s)
576 }),
577 _ => Ok(Value::Float(range_num(a, s)? + range_num(b, s)?)),
578 }
579}
580
581fn range_list(
582 start: Value,
583 end: &Value,
584 step: &Value,
585 s: tatara_lisp::Span,
586) -> Result<Value, EvalError> {
587 let end_f = range_num(end, s)?;
588 let step_f = range_num(step, s)?;
589 range_num(&start, s)?;
590 if step_f == 0.0 {
591 return Err(EvalError::native_fn(
592 "range",
593 "a step of zero never reaches the end",
594 s,
595 ));
596 }
597 let mut out = Vec::new();
598 let mut cur = start;
599 loop {
600 let c = range_num(&cur, s)?;
601 if (step_f > 0.0 && c >= end_f) || (step_f < 0.0 && c <= end_f) {
602 break;
603 }
604 let next = range_add(&cur, step, s)?;
605 out.push(cur);
606 cur = next;
607 }
608 Ok(list(out))
609}
610
611#[cfg(test)]
612mod tests {
613 use super::*;
614
615 #[test]
616 fn sort_keyed_is_stable_keyed_and_deep() {
617 assert_eq!(
619 ints("map(fn(p) last(p) end, sort_keyed(fn(p) first(p) end, [[2, 1], [1, 2], [2, 3], [1, 4]]))"),
620 vec![2, 4, 1, 3]
621 );
622 assert_eq!(
624 i("first(sort_keyed(fn(x) x end, reverse(range(0, 20000))))"),
625 0
626 );
627 assert_eq!(i("last(sort_keyed(fn(x) 0 - x end, range(0, 20000)))"), 0);
628 assert_eq!(ints("sort_keyed(fn(x) x end, [])"), Vec::<i64>::new());
630 assert!(crate::run("sort_keyed(fn(x) x end, [1, \"a\"])").is_err());
631 }
632
633 fn eval(src: &str) -> Value {
634 crate::run(src)
635 .unwrap_or_else(|e| panic!("{src:?}: {e}"))
636 .value
637 }
638
639 fn s(src: &str) -> String {
640 match eval(src) {
641 Value::Str(v) => v.to_string(),
642 other => panic!("{src:?} produced {other:?}"),
643 }
644 }
645
646 fn i(src: &str) -> i64 {
647 match eval(src) {
648 Value::Int(v) => v,
649 other => panic!("{src:?} produced {other:?}"),
650 }
651 }
652
653 fn ints(src: &str) -> Vec<i64> {
654 match eval(src) {
655 Value::List(xs) => xs
656 .iter()
657 .map(|v| match v {
658 Value::Int(n) => *n,
659 other => panic!("{src:?} produced a non-Int element {other:?}"),
660 })
661 .collect(),
662 other => panic!("{src:?} produced {other:?}"),
663 }
664 }
665
666 #[test]
669 fn range_answers_as_the_lisp_definition_did() {
670 assert_eq!(ints("range(5)"), vec![0, 1, 2, 3, 4]);
671 assert_eq!(ints("range(2, 6)"), vec![2, 3, 4, 5]);
672 assert_eq!(ints("range(0, 10, 2)"), vec![0, 2, 4, 6, 8]);
673 assert_eq!(ints("range(10, 0, 0 - 2)"), vec![10, 8, 6, 4, 2]);
674 assert!(ints("range(5, 5)").is_empty());
675 assert!(ints("range(5, 2)").is_empty());
676 assert!(ints("range(0)").is_empty());
677 match eval("range(0, 1, 0.5)") {
679 Value::List(xs) => {
680 assert!(matches!(xs[0], Value::Int(0)), "{xs:?}");
681 assert!(matches!(xs[1], Value::Float(x) if x == 0.5), "{xs:?}");
682 assert_eq!(xs.len(), 2);
683 }
684 other => panic!("{other:?}"),
685 }
686 }
687
688 #[test]
694 fn range_builds_long_lists_without_recursing() {
695 assert_eq!(i("length(range(0, 200000))"), 200_000);
696 assert_eq!(i("nth(199999, range(0, 200000))"), 199_999);
697 }
698
699 #[test]
702 fn range_refuses_a_zero_step_and_a_non_number() {
703 assert!(crate::run("range(0, 5, 0)").is_err());
704 assert!(crate::run("range(0, \"x\")").is_err());
705 }
706
707 #[test]
711 fn length_counts_characters_not_bytes() {
712 assert_eq!(i("length(\"hello\")"), 5);
713 assert_eq!(i("length(\"héllo\")"), 5, "must not be 6");
715 assert_eq!(i("length(\"😀\")"), 1, "must not be 4");
717 }
718
719 #[test]
722 fn a_combining_sequence_counts_scalars_not_graphemes() {
723 assert_eq!(
725 i("length(\"e\\u{301}\")"),
726 2,
727 "blue counts scalar values; Elixir's String.length would say 1"
728 );
729 }
730
731 #[test]
732 fn length_also_works_on_a_list() {
733 assert_eq!(i("length([1, 2, 3])"), 3);
734 }
735
736 #[test]
737 fn case_and_trim() {
738 assert_eq!(s("upcase(\"abc\")"), "ABC");
739 assert_eq!(s("downcase(\"ABC\")"), "abc");
740 assert_eq!(s("trim(\" hi \")"), "hi");
741 assert_eq!(s("upcase(\"é\")"), "É");
743 }
744
745 #[test]
746 fn concat_and_to_s() {
747 assert_eq!(s("concat(\"a\", \"b\")"), "ab");
748 assert_eq!(s("concat(\"n=\", 42)"), "n=42");
749 assert_eq!(s("to_s(42)"), "42");
750 assert_eq!(s("to_s(true)"), "true");
751 }
752
753 #[test]
757 fn plus_is_not_string_concatenation() {
758 assert!(
759 crate::run("\"a\" + \"b\"").is_err(),
760 "`+` must not silently concatenate — use concat"
761 );
762 }
763
764 #[test]
765 fn split_and_join() {
766 assert_eq!(i("length(split(\"a,b,c\", \",\"))"), 3);
767 assert_eq!(s("join(split(\"a,b,c\", \",\"), \"-\")"), "a-b-c");
768 assert_eq!(i("length(split(\"abc\", \"\"))"), 3);
770 }
771
772 #[test]
773 fn predicates() {
774 assert!(matches!(
775 eval("contains?(\"hello\", \"ell\")"),
776 Value::Bool(true)
777 ));
778 assert!(matches!(
779 eval("contains?(\"hello\", \"xyz\")"),
780 Value::Bool(false)
781 ));
782 assert!(matches!(
783 eval("starts_with?(\"hello\", \"he\")"),
784 Value::Bool(true)
785 ));
786 assert!(matches!(
787 eval("ends_with?(\"hello\", \"lo\")"),
788 Value::Bool(true)
789 ));
790 }
791
792 #[test]
793 fn replace_and_chars() {
794 assert_eq!(s("replace(\"a-b-c\", \"-\", \"+\")"), "a+b+c");
795 assert_eq!(i("length(chars(\"abc\"))"), 3);
796 }
797
798 #[test]
801 fn reverse_is_character_wise() {
802 assert_eq!(s("reverse(\"abc\")"), "cba");
803 assert_eq!(s("reverse(\"héllo\")"), "olléh", "must not corrupt the é");
804 }
805
806 #[test]
807 fn reverse_also_works_on_a_list() {
808 assert_eq!(s("join(reverse([1, 2, 3]), \",\")"), "3,2,1");
809 }
810
811 #[test]
816 fn to_int_is_nil_on_garbage_rather_than_zero() {
817 assert_eq!(i("to_int(\"42\")"), 42);
818 assert!(
819 matches!(eval("to_int(\"banana\")"), Value::Nil),
820 "Ruby would say 0 here; a falsy nil cannot be mistaken for a result"
821 );
822 assert!(
823 matches!(eval("to_int(\"0\")"), Value::Int(0)),
824 "and a real 0 is still a real 0 — the two must stay distinguishable"
825 );
826 }
827
828 #[test]
829 fn to_int_bang_raises_on_garbage() {
830 assert_eq!(i("to_int!(\"42\")"), 42);
831 let err = crate::run("to_int!(\"banana\")").expect_err("must raise");
832 assert!(err.to_string().contains("banana"), "must name it: {err}");
833 }
834
835 #[test]
836 fn numeric_conversions_and_abs() {
837 assert_eq!(i("to_int(3.9)"), 3);
838 assert_eq!(i("abs(0 - 5)"), 5);
839 assert!(matches!(eval("to_float(\"1.5\")"), Value::Float(_)));
840 assert!(matches!(eval("to_float(\"nope\")"), Value::Nil));
841 }
842
843 #[test]
845 fn a_non_string_argument_is_a_type_error() {
846 assert!(crate::run("upcase([1, 2])").is_err());
847 assert!(crate::run("join(\"not a list\", \",\")").is_err());
848 }
849}