2024-11-11 22:50:58 +00:00
|
|
|
use crate::value::*;
|
2024-12-05 16:51:42 +00:00
|
|
|
use imbl::*;
|
2024-12-06 19:08:34 +00:00
|
|
|
use ran::ran_f64;
|
2024-12-04 20:03:09 +00:00
|
|
|
use std::rc::Rc;
|
2024-11-11 22:50:58 +00:00
|
|
|
|
2024-11-15 02:30:42 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2025-06-03 22:54:33 +00:00
|
|
|
pub enum BaseFn {
|
|
|
|
Nullary(fn() -> Value),
|
|
|
|
Unary(fn(&Value) -> Value),
|
|
|
|
Binary(fn(&Value, &Value) -> Value),
|
|
|
|
Ternary(fn(&Value, &Value, &Value) -> Value),
|
2024-11-11 22:50:58 +00:00
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn eq(x: &Value, y: &Value) -> Value {
|
|
|
|
if x == y {
|
|
|
|
Value::True
|
|
|
|
} else {
|
|
|
|
Value::False
|
|
|
|
}
|
2024-11-15 02:30:42 +00:00
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn add(x: &Value, y: &Value) -> Value {
|
2024-11-15 02:30:42 +00:00
|
|
|
match (x, y) {
|
2024-12-05 01:19:41 +00:00
|
|
|
(Value::Number(x), Value::Number(y)) => Value::Number(x + y),
|
|
|
|
_ => unreachable!("internal Ludus error"),
|
2024-11-15 02:30:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn sub(x: &Value, y: &Value) -> Value {
|
2024-11-15 02:30:42 +00:00
|
|
|
match (x, y) {
|
2024-12-05 01:19:41 +00:00
|
|
|
(Value::Number(x), Value::Number(y)) => Value::Number(x - y),
|
|
|
|
_ => unreachable!("internal Ludus error"),
|
2024-11-15 02:30:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn unbox(x: &Value) -> Value {
|
2024-11-15 02:30:42 +00:00
|
|
|
match x {
|
2025-06-03 22:54:33 +00:00
|
|
|
Value::Box(cell) => cell.as_ref().borrow().clone(),
|
2024-12-05 01:19:41 +00:00
|
|
|
_ => unreachable!("internal Ludus error"),
|
2024-11-15 02:30:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn store(b: &Value, val: &Value) -> Value {
|
|
|
|
if let Value::Box(cell) = b {
|
2024-11-15 02:30:42 +00:00
|
|
|
cell.replace(val.clone());
|
2024-12-05 01:19:41 +00:00
|
|
|
val.clone()
|
2024-11-15 02:30:42 +00:00
|
|
|
} else {
|
2024-12-05 01:19:41 +00:00
|
|
|
unreachable!("internal Ludus error")
|
2024-11-11 22:50:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-04 20:03:09 +00:00
|
|
|
// TODO: do better than returning just the docstr
|
|
|
|
// name, patterns, AND docstring
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn doc(f: &Value) -> Value {
|
2024-12-04 20:03:09 +00:00
|
|
|
match f {
|
2025-06-04 21:53:38 +00:00
|
|
|
Value::Fn(f) => f.as_ref().doc(),
|
2025-06-03 22:54:33 +00:00
|
|
|
_ => Value::Interned("no documentation found"),
|
2024-12-04 20:03:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn assoc(dict: &Value, key: &Value, value: &Value) -> Value {
|
2024-12-05 01:19:41 +00:00
|
|
|
match (dict, key) {
|
2025-06-03 22:54:33 +00:00
|
|
|
(Value::Dict(d), Value::Keyword(k)) => Value::Dict(Box::new(d.update(k, value.clone()))),
|
2024-12-05 01:19:41 +00:00
|
|
|
_ => unreachable!("internal Ludus error"),
|
|
|
|
}
|
|
|
|
}
|
2024-11-15 02:30:42 +00:00
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn r#bool(x: &Value) -> Value {
|
|
|
|
match x {
|
|
|
|
Value::Nil | Value::False => Value::False,
|
|
|
|
_ => Value::True,
|
|
|
|
}
|
2024-12-05 01:19:41 +00:00
|
|
|
}
|
2024-11-11 22:50:58 +00:00
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn chars(x: &Value) -> Value {
|
2024-12-05 01:19:41 +00:00
|
|
|
match x {
|
2025-06-03 22:54:33 +00:00
|
|
|
Value::Interned(s) => {
|
2024-12-05 01:19:41 +00:00
|
|
|
let chars = s.chars();
|
|
|
|
|
2024-12-05 16:51:42 +00:00
|
|
|
let mut charlist = vector![];
|
2024-12-05 01:19:41 +00:00
|
|
|
for char in chars {
|
|
|
|
if char.is_ascii() {
|
2025-06-03 22:54:33 +00:00
|
|
|
charlist.push_back(Value::String(Rc::new(char.to_string())))
|
2024-12-05 01:19:41 +00:00
|
|
|
} else {
|
|
|
|
return Value::Tuple(Rc::new(vec![
|
|
|
|
Value::Keyword("err"),
|
2025-06-03 22:54:33 +00:00
|
|
|
Value::String(Rc::new(format!("{char} is not an ascii character"))),
|
2024-12-05 01:19:41 +00:00
|
|
|
]));
|
|
|
|
}
|
|
|
|
}
|
2025-06-03 22:54:33 +00:00
|
|
|
Value::Tuple(Rc::new(vec![
|
|
|
|
Value::Keyword("ok"),
|
|
|
|
Value::List(Box::new(charlist)),
|
|
|
|
]))
|
2024-12-05 01:19:41 +00:00
|
|
|
}
|
2025-06-03 22:54:33 +00:00
|
|
|
Value::String(s) => {
|
2024-12-05 01:19:41 +00:00
|
|
|
let chars = s.chars();
|
|
|
|
|
2024-12-05 16:51:42 +00:00
|
|
|
let mut charlist = vector![];
|
2024-12-05 01:19:41 +00:00
|
|
|
for char in chars {
|
|
|
|
if char.is_ascii() {
|
2025-06-03 22:54:33 +00:00
|
|
|
charlist.push_back(Value::String(Rc::new(char.to_string())))
|
2024-12-05 01:19:41 +00:00
|
|
|
} else {
|
|
|
|
return Value::Tuple(Rc::new(vec![
|
|
|
|
Value::Keyword("err"),
|
2025-06-03 22:54:33 +00:00
|
|
|
Value::String(Rc::new(format!("{char} is not an ascii character"))),
|
2024-12-05 01:19:41 +00:00
|
|
|
]));
|
|
|
|
}
|
|
|
|
}
|
2025-06-03 22:54:33 +00:00
|
|
|
Value::Tuple(Rc::new(vec![
|
|
|
|
Value::Keyword("ok"),
|
|
|
|
Value::List(Box::new(charlist)),
|
|
|
|
]))
|
2024-12-05 01:19:41 +00:00
|
|
|
}
|
|
|
|
_ => unreachable!("internal Ludus error"),
|
|
|
|
}
|
|
|
|
}
|
2024-12-04 20:03:09 +00:00
|
|
|
|
2024-12-05 16:51:42 +00:00
|
|
|
// TODO: figure out how to get to opportunistic mutation here
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn concat(x: &Value, y: &Value) -> Value {
|
2024-12-05 16:51:42 +00:00
|
|
|
match (x, y) {
|
2025-06-03 22:54:33 +00:00
|
|
|
(Value::Interned(x), Value::Interned(y)) => Value::String(Rc::new(format!("{x}{y}"))),
|
|
|
|
(Value::String(x), Value::String(y)) => Value::String(Rc::new(format!("{x}{y}"))),
|
|
|
|
(Value::String(x), Value::Interned(y)) => Value::String(Rc::new(format!("{x}{y}"))),
|
|
|
|
(Value::Interned(x), Value::String(y)) => Value::String(Rc::new(format!("{x}{y}"))),
|
2024-12-05 16:51:42 +00:00
|
|
|
(Value::List(x), Value::List(y)) => {
|
2025-06-03 22:54:33 +00:00
|
|
|
let mut newlist = *x.clone();
|
|
|
|
newlist.append(*y.clone());
|
|
|
|
Value::List(Box::new(newlist))
|
2024-12-05 16:51:42 +00:00
|
|
|
}
|
|
|
|
_ => unreachable!("internal Ludus error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn append(x: &Value, y: &Value) -> Value {
|
2024-12-06 18:21:25 +00:00
|
|
|
match x {
|
|
|
|
Value::List(list) => {
|
|
|
|
let mut newlist = list.clone();
|
|
|
|
newlist.push_back(y.clone());
|
|
|
|
Value::List(newlist)
|
|
|
|
}
|
|
|
|
_ => unreachable!("internal Ludus error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn dec(x: &Value) -> Value {
|
2024-12-06 18:21:25 +00:00
|
|
|
match x {
|
|
|
|
Value::Number(n) => Value::Number(n - 1.0),
|
|
|
|
_ => unreachable!("internal Ludus error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn inc(x: &Value) -> Value {
|
2024-12-06 18:21:25 +00:00
|
|
|
match x {
|
|
|
|
Value::Number(n) => Value::Number(n + 1.0),
|
|
|
|
_ => unreachable!("internal Ludus error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn div(x: &Value, y: &Value) -> Value {
|
2024-12-06 18:21:25 +00:00
|
|
|
match (x, y) {
|
|
|
|
(Value::Number(x), Value::Number(y)) => Value::Number(x / y),
|
|
|
|
_ => unreachable!("internal Ludus error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn mult(x: &Value, y: &Value) -> Value {
|
2024-12-06 18:21:25 +00:00
|
|
|
match (x, y) {
|
|
|
|
(Value::Number(x), Value::Number(y)) => Value::Number(x * y),
|
|
|
|
_ => unreachable!("internal Ludus error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn dissoc(dict: &Value, key: &Value) -> Value {
|
2024-12-06 18:21:25 +00:00
|
|
|
match (dict, key) {
|
|
|
|
(Value::Dict(dict), Value::Keyword(key)) => {
|
|
|
|
let mut new = dict.clone();
|
|
|
|
new.remove(key);
|
|
|
|
Value::Dict(new)
|
|
|
|
}
|
|
|
|
_ => unreachable!("internal Ludus error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn first(ordered: &Value) -> Value {
|
2024-12-06 18:21:25 +00:00
|
|
|
match ordered {
|
|
|
|
Value::List(list) => match list.front() {
|
|
|
|
Some(n) => n.clone(),
|
|
|
|
None => Value::Nil,
|
|
|
|
},
|
|
|
|
Value::Tuple(tuple) => match tuple.first() {
|
|
|
|
Some(n) => n.clone(),
|
|
|
|
None => Value::Nil,
|
|
|
|
},
|
|
|
|
_ => unreachable!("internal Ludus error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: figure out how to handle negative numbers
|
|
|
|
// the cast from f64 to usize discards sign info
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn at(ordered: &Value, i: &Value) -> Value {
|
2024-12-06 18:21:25 +00:00
|
|
|
match (ordered, i) {
|
|
|
|
(Value::List(list), Value::Number(n)) => {
|
|
|
|
let i = *n as usize;
|
|
|
|
match list.get(i) {
|
|
|
|
Some(n) => n.clone(),
|
|
|
|
None => Value::Nil,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(Value::Tuple(tuple), Value::Number(n)) => {
|
|
|
|
let i = *n as usize;
|
|
|
|
match tuple.get(i) {
|
|
|
|
Some(n) => n.clone(),
|
|
|
|
None => Value::Nil,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => unreachable!("internal Ludus error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn get(dict: &Value, key: &Value) -> Value {
|
2024-12-06 18:21:25 +00:00
|
|
|
match (dict, key) {
|
|
|
|
(Value::Dict(dict), Value::Keyword(key)) => match dict.get(key) {
|
|
|
|
Some(x) => x.clone(),
|
|
|
|
None => Value::Nil,
|
|
|
|
},
|
|
|
|
_ => unreachable!("internal Ludus error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn last(ordered: &Value) -> Value {
|
2024-12-06 18:21:25 +00:00
|
|
|
match ordered {
|
|
|
|
Value::List(list) => match list.last() {
|
|
|
|
Some(x) => x.clone(),
|
|
|
|
None => Value::Nil,
|
|
|
|
},
|
|
|
|
Value::Tuple(tuple) => match tuple.last() {
|
|
|
|
Some(x) => x.clone(),
|
|
|
|
None => Value::Nil,
|
|
|
|
},
|
|
|
|
_ => unreachable!("internal Ludus error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-13 00:43:56 +00:00
|
|
|
// TODO: fix this: x is a list of all the args passed to Ludus's print!
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn print(x: &Value) -> Value {
|
2024-12-13 18:47:03 +00:00
|
|
|
let Value::List(args) = x else {
|
|
|
|
unreachable!("internal Ludus error")
|
|
|
|
};
|
|
|
|
let out = args
|
|
|
|
.iter()
|
|
|
|
.map(|val| format!("{val}"))
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join("");
|
|
|
|
println!("{out}");
|
2024-12-06 18:21:25 +00:00
|
|
|
Value::Keyword("ok")
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn show(x: &Value) -> Value {
|
|
|
|
Value::String(Rc::new(format!("{x}")))
|
2024-12-06 18:21:25 +00:00
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn rest(ordered: &Value) -> Value {
|
2024-12-06 18:21:25 +00:00
|
|
|
match ordered {
|
2025-06-03 22:54:33 +00:00
|
|
|
Value::List(list) => Value::List(Box::new(list.clone().split_at(1).1)),
|
|
|
|
Value::Tuple(tuple) => {
|
|
|
|
Value::List(Box::new(Vector::from_iter(tuple.iter().next().cloned())))
|
|
|
|
}
|
2024-12-06 18:21:25 +00:00
|
|
|
_ => unreachable!("internal Ludus error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn count(coll: &Value) -> Value {
|
2024-12-06 18:21:25 +00:00
|
|
|
match coll {
|
|
|
|
Value::Dict(d) => Value::Number(d.len() as f64),
|
|
|
|
Value::List(l) => Value::Number(l.len() as f64),
|
|
|
|
Value::Tuple(t) => Value::Number(t.len() as f64),
|
2025-06-03 22:54:33 +00:00
|
|
|
Value::String(s) => Value::Number(s.len() as f64),
|
|
|
|
Value::Interned(s) => Value::Number(s.len() as f64),
|
2024-12-06 18:21:25 +00:00
|
|
|
_ => unreachable!("internal Ludus error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn range(start: &Value, end: &Value) -> Value {
|
2024-12-06 18:21:25 +00:00
|
|
|
match (start, end) {
|
|
|
|
(Value::Number(start), Value::Number(end)) => {
|
|
|
|
let start = *start as isize;
|
|
|
|
let end = *end as isize;
|
|
|
|
let mut range = Vector::new();
|
|
|
|
for n in start..end {
|
|
|
|
range.push_back(Value::Number(n as f64))
|
|
|
|
}
|
2025-06-03 22:54:33 +00:00
|
|
|
Value::List(Box::new(range))
|
2024-12-06 18:21:25 +00:00
|
|
|
}
|
|
|
|
_ => unreachable!("internal Ludus error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn slice(ordered: &Value, start: &Value, end: &Value) -> Value {
|
2024-12-06 18:21:25 +00:00
|
|
|
match (ordered, start, end) {
|
|
|
|
(Value::List(list), Value::Number(start), Value::Number(end)) => {
|
|
|
|
let mut newlist = list.clone();
|
|
|
|
let start = std::cmp::max(*start as usize, 0);
|
|
|
|
let end = std::cmp::min(*end as usize, list.len());
|
2025-06-03 22:54:33 +00:00
|
|
|
Value::List(Box::new(newlist.slice(start..end)))
|
2024-12-06 18:21:25 +00:00
|
|
|
}
|
|
|
|
// TODO: figure out something better to do than return an empty string on a bad slice
|
2025-06-03 22:54:33 +00:00
|
|
|
(Value::String(string), Value::Number(start), Value::Number(end)) => {
|
2024-12-06 18:21:25 +00:00
|
|
|
let start = std::cmp::max(*start as usize, 0);
|
|
|
|
let end = std::cmp::min(*end as usize, string.len());
|
2025-06-03 22:54:33 +00:00
|
|
|
Value::String(Rc::new(
|
2024-12-06 18:21:25 +00:00
|
|
|
string
|
|
|
|
.clone()
|
|
|
|
.as_str()
|
|
|
|
.get(start..end)
|
|
|
|
.unwrap_or("")
|
|
|
|
.to_string(),
|
|
|
|
))
|
|
|
|
}
|
2025-06-03 22:54:33 +00:00
|
|
|
(Value::Interned(string), Value::Number(start), Value::Number(end)) => {
|
2024-12-06 18:21:25 +00:00
|
|
|
let start = std::cmp::max(*start as usize, 0);
|
|
|
|
let end = std::cmp::min(*end as usize, string.len());
|
2025-06-03 22:54:33 +00:00
|
|
|
Value::String(Rc::new(string.get(start..end).unwrap_or("").to_string()))
|
2024-12-06 18:21:25 +00:00
|
|
|
}
|
|
|
|
_ => unreachable!("internal Ludus error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn list(x: &Value) -> Value {
|
2024-12-06 18:21:25 +00:00
|
|
|
match x {
|
|
|
|
Value::List(_) => x.clone(),
|
2025-06-03 22:54:33 +00:00
|
|
|
Value::Tuple(t) => Value::List(Box::new(Vector::from_iter(t.iter().cloned()))),
|
2024-12-06 18:21:25 +00:00
|
|
|
Value::Dict(d) => {
|
|
|
|
let kvs = d.iter();
|
|
|
|
let mut list = vector![];
|
|
|
|
for (key, value) in kvs {
|
|
|
|
let kv = Value::Tuple(Rc::new(vec![Value::Keyword(key), value.clone()]));
|
|
|
|
list.push_back(kv);
|
|
|
|
}
|
2025-06-03 22:54:33 +00:00
|
|
|
Value::List(Box::new(list))
|
2024-12-06 18:21:25 +00:00
|
|
|
}
|
2025-06-03 22:54:33 +00:00
|
|
|
_ => Value::List(Box::new(vector![x.clone()])),
|
2024-12-06 18:21:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn number(x: &Value) -> Value {
|
2024-12-06 18:21:25 +00:00
|
|
|
match x {
|
2025-06-03 22:54:33 +00:00
|
|
|
Value::Interned(string) => match string.parse::<f64>() {
|
2024-12-06 18:21:25 +00:00
|
|
|
Ok(n) => Value::Tuple(Rc::new(vec![Value::Keyword("ok"), Value::Number(n)])),
|
|
|
|
Err(_) => Value::Tuple(Rc::new(vec![
|
|
|
|
Value::Keyword("err"),
|
2025-06-03 22:54:33 +00:00
|
|
|
Value::String(Rc::new(format!("could not parse `{string}` as a number"))),
|
2024-12-06 18:21:25 +00:00
|
|
|
])),
|
|
|
|
},
|
2025-06-03 22:54:33 +00:00
|
|
|
Value::String(string) => match string.parse::<f64>() {
|
2024-12-06 18:21:25 +00:00
|
|
|
Ok(n) => Value::Tuple(Rc::new(vec![Value::Keyword("ok"), Value::Number(n)])),
|
|
|
|
Err(_) => Value::Tuple(Rc::new(vec![
|
|
|
|
Value::Keyword("err"),
|
2025-06-03 22:54:33 +00:00
|
|
|
Value::String(Rc::new(format!("could not parse `{string}` as a number"))),
|
2024-12-06 18:21:25 +00:00
|
|
|
])),
|
|
|
|
},
|
|
|
|
_ => unreachable!("internal Ludus error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn r#type(x: &Value) -> Value {
|
2024-12-06 18:21:25 +00:00
|
|
|
match x {
|
|
|
|
Value::Nil => Value::Keyword("nil"),
|
|
|
|
Value::Number(_) => Value::Keyword("number"),
|
2025-06-03 22:54:33 +00:00
|
|
|
Value::True | Value::False => Value::Keyword("bool"),
|
2024-12-06 18:21:25 +00:00
|
|
|
Value::Keyword(_) => Value::Keyword("keyword"),
|
|
|
|
Value::Tuple(_) => Value::Keyword("tuple"),
|
2025-06-03 22:54:33 +00:00
|
|
|
Value::Interned(_) => Value::Keyword("string"),
|
|
|
|
Value::String(_) => Value::Keyword("string"),
|
2024-12-06 18:21:25 +00:00
|
|
|
Value::List(_) => Value::Keyword("list"),
|
|
|
|
Value::Dict(_) => Value::Keyword("dict"),
|
|
|
|
Value::Fn(_) => Value::Keyword("fn"),
|
2025-06-03 22:54:33 +00:00
|
|
|
Value::Box(_) => Value::Keyword("box"),
|
|
|
|
Value::BaseFn(_) => Value::Keyword("fn"),
|
|
|
|
Value::Nothing => unreachable!(),
|
2024-12-06 18:21:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn split(source: &Value, splitter: &Value) -> Value {
|
2024-12-06 19:08:34 +00:00
|
|
|
match (source, splitter) {
|
2025-06-03 22:54:33 +00:00
|
|
|
(Value::String(source), Value::String(splitter)) => {
|
2024-12-06 19:08:34 +00:00
|
|
|
let parts = source.split_terminator(splitter.as_str());
|
|
|
|
let mut list = vector![];
|
|
|
|
for part in parts {
|
2025-06-03 22:54:33 +00:00
|
|
|
list.push_back(Value::String(Rc::new(part.to_string())));
|
2024-12-06 19:08:34 +00:00
|
|
|
}
|
2025-06-03 22:54:33 +00:00
|
|
|
Value::List(Box::new(list))
|
2024-12-06 19:08:34 +00:00
|
|
|
}
|
2025-06-03 22:54:33 +00:00
|
|
|
(Value::String(source), Value::Interned(splitter)) => {
|
2024-12-06 19:08:34 +00:00
|
|
|
let parts = source.split_terminator(splitter);
|
|
|
|
let mut list = vector![];
|
|
|
|
for part in parts {
|
2025-06-03 22:54:33 +00:00
|
|
|
list.push_back(Value::String(Rc::new(part.to_string())));
|
2024-12-06 19:08:34 +00:00
|
|
|
}
|
2025-06-03 22:54:33 +00:00
|
|
|
Value::List(Box::new(list))
|
2024-12-06 19:08:34 +00:00
|
|
|
}
|
2025-06-03 22:54:33 +00:00
|
|
|
(Value::Interned(source), Value::String(splitter)) => {
|
2024-12-06 19:08:34 +00:00
|
|
|
let parts = source.split_terminator(splitter.as_str());
|
|
|
|
let mut list = vector![];
|
|
|
|
for part in parts {
|
2025-06-03 22:54:33 +00:00
|
|
|
list.push_back(Value::String(Rc::new(part.to_string())));
|
2024-12-06 19:08:34 +00:00
|
|
|
}
|
2025-06-03 22:54:33 +00:00
|
|
|
Value::List(Box::new(list))
|
2024-12-06 19:08:34 +00:00
|
|
|
}
|
2025-06-03 22:54:33 +00:00
|
|
|
(Value::Interned(source), Value::Interned(splitter)) => {
|
2024-12-06 19:08:34 +00:00
|
|
|
let parts = source.split_terminator(splitter);
|
|
|
|
let mut list = vector![];
|
|
|
|
for part in parts {
|
2025-06-03 22:54:33 +00:00
|
|
|
list.push_back(Value::String(Rc::new(part.to_string())));
|
2024-12-06 19:08:34 +00:00
|
|
|
}
|
2025-06-03 22:54:33 +00:00
|
|
|
Value::List(Box::new(list))
|
2024-12-06 19:08:34 +00:00
|
|
|
}
|
|
|
|
_ => unreachable!("internal Ludus error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn upcase(string: &Value) -> Value {
|
2024-12-06 19:08:34 +00:00
|
|
|
match string {
|
2025-06-03 22:54:33 +00:00
|
|
|
Value::String(string) => Value::String(Rc::new(string.to_uppercase())),
|
|
|
|
Value::Interned(string) => Value::String(Rc::new(string.to_uppercase())),
|
2024-12-06 19:08:34 +00:00
|
|
|
_ => unreachable!("internal Ludus error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn downcase(string: &Value) -> Value {
|
2024-12-06 19:08:34 +00:00
|
|
|
match string {
|
2025-06-03 22:54:33 +00:00
|
|
|
Value::String(string) => Value::String(Rc::new(string.to_lowercase())),
|
|
|
|
Value::Interned(string) => Value::String(Rc::new(string.to_lowercase())),
|
2024-12-06 19:08:34 +00:00
|
|
|
_ => unreachable!("internal Ludus error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn trim(string: &Value) -> Value {
|
2024-12-06 19:08:34 +00:00
|
|
|
match string {
|
2025-06-03 22:54:33 +00:00
|
|
|
Value::String(string) => Value::String(Rc::new(string.trim().to_string())),
|
|
|
|
Value::Interned(string) => Value::String(Rc::new(string.trim().to_string())),
|
2024-12-06 19:08:34 +00:00
|
|
|
_ => unreachable!("internal Ludus error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn triml(string: &Value) -> Value {
|
2024-12-06 19:08:34 +00:00
|
|
|
match string {
|
2025-06-03 22:54:33 +00:00
|
|
|
Value::String(string) => Value::String(Rc::new(string.trim_start().to_string())),
|
|
|
|
Value::Interned(string) => Value::String(Rc::new(string.trim_start().to_string())),
|
2024-12-06 19:08:34 +00:00
|
|
|
_ => unreachable!("internal Ludus error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn trimr(string: &Value) -> Value {
|
2024-12-06 19:08:34 +00:00
|
|
|
match string {
|
2025-06-03 22:54:33 +00:00
|
|
|
Value::String(string) => Value::String(Rc::new(string.trim_end().to_string())),
|
|
|
|
Value::Interned(string) => Value::String(Rc::new(string.trim_end().to_string())),
|
2024-12-06 19:08:34 +00:00
|
|
|
_ => unreachable!("internal Ludus error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn atan_2(x: &Value, y: &Value) -> Value {
|
2024-12-06 19:08:34 +00:00
|
|
|
match (x, y) {
|
|
|
|
(Value::Number(x), Value::Number(y)) => Value::Number(x.atan2(*y)),
|
|
|
|
_ => unreachable!("internal Ludus error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn ceil(x: &Value) -> Value {
|
2024-12-06 19:08:34 +00:00
|
|
|
match x {
|
|
|
|
Value::Number(x) => Value::Number(x.ceil()),
|
|
|
|
_ => unreachable!("internal Ludus error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn cos(x: &Value) -> Value {
|
2024-12-06 19:08:34 +00:00
|
|
|
match x {
|
|
|
|
Value::Number(x) => Value::Number(x.cos()),
|
|
|
|
_ => unreachable!("internal Ludus error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn floor(x: &Value) -> Value {
|
2024-12-06 19:08:34 +00:00
|
|
|
match x {
|
|
|
|
Value::Number(x) => Value::Number(x.floor()),
|
|
|
|
_ => unreachable!("internal Ludus error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn random() -> Value {
|
2024-12-06 19:08:34 +00:00
|
|
|
Value::Number(ran_f64())
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn round(x: &Value) -> Value {
|
2024-12-06 19:08:34 +00:00
|
|
|
match x {
|
|
|
|
Value::Number(x) => Value::Number(x.round()),
|
|
|
|
_ => unreachable!("internal Ludus error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn sin(x: &Value) -> Value {
|
2024-12-06 19:08:34 +00:00
|
|
|
match x {
|
|
|
|
Value::Number(x) => Value::Number(x.sin()),
|
|
|
|
_ => unreachable!("internal Ludus error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn sqrt(x: &Value) -> Value {
|
2024-12-06 19:08:34 +00:00
|
|
|
match x {
|
|
|
|
Value::Number(x) => Value::Number(x.sqrt()),
|
|
|
|
_ => unreachable!("internal Ludus error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn tan(x: &Value) -> Value {
|
2024-12-06 19:08:34 +00:00
|
|
|
match x {
|
|
|
|
Value::Number(x) => Value::Number(x.tan()),
|
|
|
|
_ => unreachable!("internal Ludus error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn gt(x: &Value, y: &Value) -> Value {
|
2024-12-06 19:08:34 +00:00
|
|
|
match (x, y) {
|
2025-06-03 22:54:33 +00:00
|
|
|
(Value::Number(x), Value::Number(y)) => {
|
|
|
|
if x > y {
|
|
|
|
Value::True
|
|
|
|
} else {
|
|
|
|
Value::False
|
|
|
|
}
|
|
|
|
}
|
2024-12-06 19:08:34 +00:00
|
|
|
_ => unreachable!("internal Ludus error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn gte(x: &Value, y: &Value) -> Value {
|
2024-12-06 19:08:34 +00:00
|
|
|
match (x, y) {
|
2025-06-03 22:54:33 +00:00
|
|
|
(Value::Number(x), Value::Number(y)) => {
|
|
|
|
if x >= y {
|
|
|
|
Value::True
|
|
|
|
} else {
|
|
|
|
Value::False
|
|
|
|
}
|
|
|
|
}
|
2024-12-06 19:08:34 +00:00
|
|
|
_ => unreachable!("internal Ludus error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn lt(x: &Value, y: &Value) -> Value {
|
2024-12-06 19:08:34 +00:00
|
|
|
match (x, y) {
|
2025-06-03 22:54:33 +00:00
|
|
|
(Value::Number(x), Value::Number(y)) => {
|
|
|
|
if x < y {
|
|
|
|
Value::True
|
|
|
|
} else {
|
|
|
|
Value::False
|
|
|
|
}
|
|
|
|
}
|
2024-12-06 19:08:34 +00:00
|
|
|
_ => unreachable!("internal Ludus error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn lte(x: &Value, y: &Value) -> Value {
|
2024-12-06 19:08:34 +00:00
|
|
|
match (x, y) {
|
2025-06-03 22:54:33 +00:00
|
|
|
(Value::Number(x), Value::Number(y)) => {
|
|
|
|
if x <= y {
|
|
|
|
Value::True
|
|
|
|
} else {
|
|
|
|
Value::False
|
|
|
|
}
|
|
|
|
}
|
2024-12-06 19:08:34 +00:00
|
|
|
_ => unreachable!("internal Ludus error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn r#mod(x: &Value, y: &Value) -> Value {
|
2024-12-06 19:08:34 +00:00
|
|
|
match (x, y) {
|
|
|
|
(Value::Number(x), Value::Number(y)) => Value::Number(x % y),
|
|
|
|
_ => unreachable!("internal Ludus error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-03 22:54:33 +00:00
|
|
|
pub fn base() -> Value {
|
2024-12-09 04:33:02 +00:00
|
|
|
let members = vec![
|
2025-06-03 22:54:33 +00:00
|
|
|
("add", Value::BaseFn(BaseFn::Binary(add))),
|
|
|
|
("append", Value::BaseFn(BaseFn::Binary(append))),
|
|
|
|
("assoc", Value::BaseFn(BaseFn::Ternary(assoc))),
|
|
|
|
("at", Value::BaseFn(BaseFn::Binary(at))),
|
|
|
|
("atan_2", Value::BaseFn(BaseFn::Binary(atan_2))),
|
|
|
|
("bool", Value::BaseFn(BaseFn::Unary(r#bool))),
|
|
|
|
("ceil", Value::BaseFn(BaseFn::Unary(ceil))),
|
|
|
|
("chars", Value::BaseFn(BaseFn::Unary(chars))),
|
|
|
|
("concat", Value::BaseFn(BaseFn::Binary(concat))),
|
|
|
|
("cos", Value::BaseFn(BaseFn::Unary(cos))),
|
|
|
|
("count", Value::BaseFn(BaseFn::Unary(count))),
|
|
|
|
("dec", Value::BaseFn(BaseFn::Unary(dec))),
|
|
|
|
("dissoc", Value::BaseFn(BaseFn::Binary(dissoc))),
|
|
|
|
("div", Value::BaseFn(BaseFn::Binary(div))),
|
|
|
|
("doc!", Value::BaseFn(BaseFn::Unary(doc))),
|
|
|
|
("downcase", Value::BaseFn(BaseFn::Unary(downcase))),
|
|
|
|
("eq?", Value::BaseFn(BaseFn::Binary(eq))),
|
|
|
|
("first", Value::BaseFn(BaseFn::Unary(first))),
|
|
|
|
("floor", Value::BaseFn(BaseFn::Unary(floor))),
|
|
|
|
("get", Value::BaseFn(BaseFn::Binary(get))),
|
|
|
|
("gt?", Value::BaseFn(BaseFn::Binary(gt))),
|
|
|
|
("gte?", Value::BaseFn(BaseFn::Binary(gte))),
|
|
|
|
("inc", Value::BaseFn(BaseFn::Unary(inc))),
|
|
|
|
("last", Value::BaseFn(BaseFn::Unary(last))),
|
|
|
|
("list", Value::BaseFn(BaseFn::Unary(list))),
|
|
|
|
("lt?", Value::BaseFn(BaseFn::Binary(lt))),
|
|
|
|
("lte?", Value::BaseFn(BaseFn::Binary(lte))),
|
|
|
|
("mod", Value::BaseFn(BaseFn::Binary(r#mod))),
|
|
|
|
("mult", Value::BaseFn(BaseFn::Binary(mult))),
|
|
|
|
("number", Value::BaseFn(BaseFn::Unary(number))),
|
2024-12-09 04:33:02 +00:00
|
|
|
("pi", Value::Number(std::f64::consts::PI)),
|
2025-06-03 22:54:33 +00:00
|
|
|
("print!", Value::BaseFn(BaseFn::Unary(print))),
|
|
|
|
("random", Value::BaseFn(BaseFn::Nullary(random))),
|
|
|
|
("range", Value::BaseFn(BaseFn::Binary(range))),
|
|
|
|
("rest", Value::BaseFn(BaseFn::Unary(rest))),
|
|
|
|
("round", Value::BaseFn(BaseFn::Unary(round))),
|
|
|
|
("show", Value::BaseFn(BaseFn::Unary(show))),
|
|
|
|
("sin", Value::BaseFn(BaseFn::Unary(sin))),
|
|
|
|
("slice", Value::BaseFn(BaseFn::Ternary(slice))),
|
|
|
|
("split", Value::BaseFn(BaseFn::Binary(split))),
|
|
|
|
("sqrt", Value::BaseFn(BaseFn::Unary(sqrt))),
|
2024-12-09 04:33:02 +00:00
|
|
|
("sqrt_2", Value::Number(std::f64::consts::SQRT_2)),
|
2025-06-03 22:54:33 +00:00
|
|
|
("store!", Value::BaseFn(BaseFn::Binary(store))),
|
|
|
|
("sub", Value::BaseFn(BaseFn::Binary(sub))),
|
|
|
|
("tan", Value::BaseFn(BaseFn::Unary(tan))),
|
|
|
|
("trim", Value::BaseFn(BaseFn::Unary(trim))),
|
|
|
|
("triml", Value::BaseFn(BaseFn::Unary(triml))),
|
|
|
|
("trimr", Value::BaseFn(BaseFn::Unary(trimr))),
|
|
|
|
("type", Value::BaseFn(BaseFn::Unary(r#type))),
|
|
|
|
("unbox", Value::BaseFn(BaseFn::Unary(unbox))),
|
|
|
|
("upcase", Value::BaseFn(BaseFn::Unary(upcase))),
|
2024-12-09 04:33:02 +00:00
|
|
|
];
|
2025-06-03 22:54:33 +00:00
|
|
|
Value::Dict(Box::new(HashMap::from(members)))
|
2024-11-11 22:50:58 +00:00
|
|
|
}
|