use std::cmp::Ordering;
use std::rc::Rc;
use crate::globals::Globals;
use crate::value::{
Builtin, BuiltinFn, HostBuiltin, HostFn, Input, NativeFn, Output, System, SystemFn, Value,
};
pub fn register(globals: &mut Globals) {
define(globals, "print", print);
define(globals, "eprint", eprint);
define(globals, "exit", exit);
define(globals, "len", len);
define(globals, "push", push);
define(globals, "str", to_str);
define(globals, "type", type_of);
define(globals, "is_error", is_error);
define(globals, "range", range);
define(globals, "keys", keys);
define(globals, "values", values);
define(globals, "has", has);
define(globals, "remove", remove);
define(globals, "upper", upper);
define(globals, "lower", lower);
define(globals, "trim", trim);
define(globals, "replace", replace);
define(globals, "split", split);
define(globals, "join", join);
define(globals, "contains", contains);
define(globals, "find", find);
define(globals, "pop", pop);
define(globals, "index_of", index_of);
define(globals, "slice", slice);
define(globals, "sort", sort);
define(globals, "reverse", reverse);
define(globals, "abs", abs);
define(globals, "min", min);
define(globals, "max", max);
define(globals, "floor", floor);
define(globals, "ceil", ceil);
define(globals, "round", round);
define(globals, "sqrt", sqrt);
define(globals, "pow", pow);
define(globals, "int", int);
define(globals, "float", float);
define(globals, "input", input);
define_system(globals, "read_file", read_file);
define_system(globals, "write_file", write_file);
define_system(globals, "file_exists", file_exists);
define_system(globals, "args", args);
define_host(globals, "map", map);
define_host(globals, "filter", filter);
define_host(globals, "reduce", reduce);
}
pub fn accepts_error(name: &str) -> bool {
matches!(name, "type" | "is_error")
}
fn define(globals: &mut Globals, name: &'static str, func: BuiltinFn) {
let slot = globals
.slot_for_builtin(name)
.expect("room for the builtins");
let func = NativeFn::Plain(func);
globals.define(slot, Value::Builtin(Builtin { name, func }));
}
fn define_system(globals: &mut Globals, name: &'static str, func: SystemFn) {
let slot = globals
.slot_for_builtin(name)
.expect("room for the builtins");
let func = NativeFn::System(func);
globals.define(slot, Value::Builtin(Builtin { name, func }));
}
fn define_host(globals: &mut Globals, name: &'static str, func: HostFn) {
let slot = globals
.slot_for_builtin(name)
.expect("room for the builtins");
globals.define(slot, Value::HostBuiltin(HostBuiltin { name, func }));
}
fn check_arity(name: &str, args: &[Value], expected: usize) -> Result<(), String> {
if args.len() == expected {
Ok(())
} else {
Err(format!(
"{name} expects {expected} argument(s) but got {}",
args.len()
))
}
}
fn print(out: &mut dyn Output, _input: &mut dyn Input, args: Vec<Value>) -> Result<Value, String> {
let parts: Vec<String> = args.iter().map(Value::display).collect();
out.write(&parts.join(" "));
out.write("\n");
Ok(Value::Nil)
}
fn eprint(out: &mut dyn Output, _input: &mut dyn Input, args: Vec<Value>) -> Result<Value, String> {
let parts: Vec<String> = args.iter().map(Value::display).collect();
out.write_error(&parts.join(" "));
out.write_error("\n");
Ok(Value::Nil)
}
fn exit(out: &mut dyn Output, _input: &mut dyn Input, args: Vec<Value>) -> Result<Value, String> {
check_arity("exit", &args, 1)?;
let code = match &args[0] {
Value::Int(n) => *n,
other => {
return Err(format!(
"exit expects an int but got a {}",
other.type_name()
))
}
};
if !(0..=255).contains(&code) {
return Err(format!("exit code must be from 0 to 255 but got {code}"));
}
out.request_exit(code as i32);
Err("the program called exit".to_string())
}
fn len(_out: &mut dyn Output, _input: &mut dyn Input, args: Vec<Value>) -> Result<Value, String> {
check_arity("len", &args, 1)?;
match &args[0] {
Value::Str(s) => Ok(Value::Int(s.chars().count() as i64)),
Value::Array(items) => Ok(Value::Int(items.borrow().len() as i64)),
Value::Map(entries) => Ok(Value::Int(entries.borrow().len() as i64)),
other => Err(format!(
"len expects a string, array, or map but got a {}",
other.type_name()
)),
}
}
fn push(_out: &mut dyn Output, _input: &mut dyn Input, args: Vec<Value>) -> Result<Value, String> {
check_arity("push", &args, 2)?;
match &args[0] {
Value::Array(items) => {
items.borrow_mut().push(args[1].clone());
Ok(args[0].clone())
}
other => Err(format!(
"push expects an array as its first argument but got a {}",
other.type_name()
)),
}
}
fn to_str(
_out: &mut dyn Output,
_input: &mut dyn Input,
args: Vec<Value>,
) -> Result<Value, String> {
check_arity("str", &args, 1)?;
Ok(Value::Str(Rc::new(args[0].display())))
}
fn type_of(
_out: &mut dyn Output,
_input: &mut dyn Input,
args: Vec<Value>,
) -> Result<Value, String> {
check_arity("type", &args, 1)?;
Ok(Value::Str(Rc::new(args[0].type_name().to_string())))
}
fn is_error(
_out: &mut dyn Output,
_input: &mut dyn Input,
args: Vec<Value>,
) -> Result<Value, String> {
check_arity("is_error", &args, 1)?;
Ok(Value::Bool(matches!(args[0], Value::Error(_))))
}
fn range(_out: &mut dyn Output, _input: &mut dyn Input, args: Vec<Value>) -> Result<Value, String> {
let (start, end) = match args.as_slice() {
[Value::Int(end)] => (0i64, *end),
[Value::Int(start), Value::Int(end)] => (*start, *end),
[_] | [_, _] => return Err("range expects integer arguments".to_string()),
_ => {
return Err(format!(
"range expects 1 or 2 arguments but got {}",
args.len()
))
}
};
let mut items = Vec::new();
let mut current = start;
while current < end {
items.push(Value::Int(current));
current += 1;
}
Ok(Value::array(items))
}
fn keys(_out: &mut dyn Output, _input: &mut dyn Input, args: Vec<Value>) -> Result<Value, String> {
check_arity("keys", &args, 1)?;
match &args[0] {
Value::Map(entries) => {
let items: Vec<Value> = entries
.borrow()
.keys()
.map(|key| Value::Str(Rc::new(key.clone())))
.collect();
Ok(Value::array(items))
}
other => Err(format!(
"keys expects a map but got a {}",
other.type_name()
)),
}
}
fn values(
_out: &mut dyn Output,
_input: &mut dyn Input,
args: Vec<Value>,
) -> Result<Value, String> {
check_arity("values", &args, 1)?;
match &args[0] {
Value::Map(entries) => {
let items: Vec<Value> = entries.borrow().values().cloned().collect();
Ok(Value::array(items))
}
other => Err(format!(
"values expects a map but got a {}",
other.type_name()
)),
}
}
fn has(_out: &mut dyn Output, _input: &mut dyn Input, args: Vec<Value>) -> Result<Value, String> {
check_arity("has", &args, 2)?;
let key = match &args[1] {
Value::Str(s) => s.to_string(),
other => {
return Err(format!(
"has expects a string key but got a {}",
other.type_name()
))
}
};
match &args[0] {
Value::Map(entries) => Ok(Value::Bool(entries.borrow().contains_key(&key))),
other => Err(format!("has expects a map but got a {}", other.type_name())),
}
}
fn remove(
_out: &mut dyn Output,
_input: &mut dyn Input,
args: Vec<Value>,
) -> Result<Value, String> {
check_arity("remove", &args, 2)?;
let key = match &args[1] {
Value::Str(s) => s.as_str(),
other => {
return Err(format!(
"remove expects a string key but got a {}",
other.type_name()
))
}
};
match &args[0] {
Value::Map(entries) => Ok(entries.borrow_mut().remove(key).unwrap_or(Value::Nil)),
other => Err(format!(
"remove expects a map but got a {}",
other.type_name()
)),
}
}
fn upper(_out: &mut dyn Output, _input: &mut dyn Input, args: Vec<Value>) -> Result<Value, String> {
check_arity("upper", &args, 1)?;
match &args[0] {
Value::Str(s) => Ok(Value::Str(Rc::new(s.to_uppercase()))),
other => Err(format!(
"upper expects a string but got a {}",
other.type_name()
)),
}
}
fn lower(_out: &mut dyn Output, _input: &mut dyn Input, args: Vec<Value>) -> Result<Value, String> {
check_arity("lower", &args, 1)?;
match &args[0] {
Value::Str(s) => Ok(Value::Str(Rc::new(s.to_lowercase()))),
other => Err(format!(
"lower expects a string but got a {}",
other.type_name()
)),
}
}
fn trim(_out: &mut dyn Output, _input: &mut dyn Input, args: Vec<Value>) -> Result<Value, String> {
check_arity("trim", &args, 1)?;
match &args[0] {
Value::Str(s) => Ok(Value::Str(Rc::new(s.trim().to_string()))),
other => Err(format!(
"trim expects a string but got a {}",
other.type_name()
)),
}
}
fn replace(
_out: &mut dyn Output,
_input: &mut dyn Input,
args: Vec<Value>,
) -> Result<Value, String> {
check_arity("replace", &args, 3)?;
match (&args[0], &args[1], &args[2]) {
(Value::Str(s), Value::Str(from), Value::Str(to)) => {
Ok(Value::Str(Rc::new(s.replace(from.as_str(), to.as_str()))))
}
_ => Err("replace expects three string arguments".to_string()),
}
}
fn split(_out: &mut dyn Output, _input: &mut dyn Input, args: Vec<Value>) -> Result<Value, String> {
check_arity("split", &args, 2)?;
match (&args[0], &args[1]) {
(Value::Str(s), Value::Str(sep)) => {
let parts: Vec<Value> = if sep.is_empty() {
s.chars()
.map(|c| Value::Str(Rc::new(c.to_string())))
.collect()
} else {
s.split(sep.as_str())
.map(|part| Value::Str(Rc::new(part.to_string())))
.collect()
};
Ok(Value::array(parts))
}
_ => Err("split expects two string arguments".to_string()),
}
}
fn join(_out: &mut dyn Output, _input: &mut dyn Input, args: Vec<Value>) -> Result<Value, String> {
check_arity("join", &args, 2)?;
match (&args[0], &args[1]) {
(Value::Array(items), Value::Str(sep)) => {
let parts: Vec<String> = items.borrow().iter().map(Value::display).collect();
Ok(Value::Str(Rc::new(parts.join(sep.as_str()))))
}
_ => Err("join expects an array and a string separator".to_string()),
}
}
fn contains(
_out: &mut dyn Output,
_input: &mut dyn Input,
args: Vec<Value>,
) -> Result<Value, String> {
check_arity("contains", &args, 2)?;
match &args[0] {
Value::Str(s) => match &args[1] {
Value::Str(sub) => Ok(Value::Bool(s.contains(sub.as_str()))),
other => Err(format!(
"contains on a string expects a string but got a {}",
other.type_name()
)),
},
Value::Array(items) => Ok(Value::Bool({
let items = items.borrow();
let mut found = false;
for item in items.iter() {
if item.equals(&args[1])? {
found = true;
break;
}
}
found
})),
other => Err(format!(
"contains expects a string or array but got a {}",
other.type_name()
)),
}
}
fn find(_out: &mut dyn Output, _input: &mut dyn Input, args: Vec<Value>) -> Result<Value, String> {
check_arity("find", &args, 2)?;
match (&args[0], &args[1]) {
(Value::Str(s), Value::Str(sub)) => {
let index = match s.find(sub.as_str()) {
Some(byte_index) => s[..byte_index].chars().count() as i64,
None => -1,
};
Ok(Value::Int(index))
}
_ => Err("find expects two string arguments".to_string()),
}
}
fn pop(_out: &mut dyn Output, _input: &mut dyn Input, args: Vec<Value>) -> Result<Value, String> {
check_arity("pop", &args, 1)?;
match &args[0] {
Value::Array(items) => items
.borrow_mut()
.pop()
.ok_or_else(|| "pop from an empty array".to_string()),
other => Err(format!(
"pop expects an array but got a {}",
other.type_name()
)),
}
}
fn index_of(
_out: &mut dyn Output,
_input: &mut dyn Input,
args: Vec<Value>,
) -> Result<Value, String> {
check_arity("index_of", &args, 2)?;
match &args[0] {
Value::Array(items) => {
let items = items.borrow();
for (index, item) in items.iter().enumerate() {
if item.equals(&args[1])? {
return Ok(Value::Int(index as i64));
}
}
Ok(Value::Int(-1))
}
other => Err(format!(
"index_of expects an array but got a {}",
other.type_name()
)),
}
}
fn clamp_range(start: i64, end: i64, len: usize) -> (usize, usize) {
let len = len as i64;
let lo = start.clamp(0, len) as usize;
let hi = end.clamp(0, len) as usize;
(lo, hi.max(lo))
}
fn slice(_out: &mut dyn Output, _input: &mut dyn Input, args: Vec<Value>) -> Result<Value, String> {
check_arity("slice", &args, 3)?;
let start = match &args[1] {
Value::Int(n) => *n,
other => {
return Err(format!(
"slice expects an integer start but got a {}",
other.type_name()
))
}
};
let end = match &args[2] {
Value::Int(n) => *n,
other => {
return Err(format!(
"slice expects an integer end but got a {}",
other.type_name()
))
}
};
match &args[0] {
Value::Array(items) => {
let items = items.borrow();
let (lo, hi) = clamp_range(start, end, items.len());
Ok(Value::array(items[lo..hi].to_vec()))
}
Value::Str(s) => {
let chars: Vec<char> = s.chars().collect();
let (lo, hi) = clamp_range(start, end, chars.len());
Ok(Value::Str(Rc::new(chars[lo..hi].iter().collect())))
}
other => Err(format!(
"slice expects an array or string but got a {}",
other.type_name()
)),
}
}
fn number_as_f64(value: &Value) -> f64 {
match value {
Value::Int(n) => *n as f64,
Value::Float(f) => *f,
_ => 0.0,
}
}
fn sort(_out: &mut dyn Output, _input: &mut dyn Input, args: Vec<Value>) -> Result<Value, String> {
check_arity("sort", &args, 1)?;
let mut sorted = match &args[0] {
Value::Array(items) => items.borrow().clone(),
other => {
return Err(format!(
"sort expects an array but got a {}",
other.type_name()
))
}
};
if sorted.iter().all(|v| matches!(v, Value::Int(_))) {
sorted.sort_by_key(|v| match v {
Value::Int(n) => *n,
_ => 0,
});
} else if sorted.iter().all(|v| matches!(v, Value::Str(_))) {
sorted.sort_by(|a, b| match (a, b) {
(Value::Str(x), Value::Str(y)) => x.cmp(y),
_ => Ordering::Equal,
});
} else if sorted
.iter()
.all(|v| matches!(v, Value::Int(_) | Value::Float(_)))
{
if sorted
.iter()
.any(|v| matches!(v, Value::Float(f) if f.is_nan()))
{
return Err("sort cannot order NaN".to_string());
}
sorted.sort_by(|a, b| {
number_as_f64(a)
.partial_cmp(&number_as_f64(b))
.unwrap_or(Ordering::Equal)
});
} else {
return Err("sort expects an array of all numbers or all strings".to_string());
}
Ok(Value::array(sorted))
}
fn reverse(
_out: &mut dyn Output,
_input: &mut dyn Input,
args: Vec<Value>,
) -> Result<Value, String> {
check_arity("reverse", &args, 1)?;
match &args[0] {
Value::Array(items) => {
let mut copy = items.borrow().clone();
copy.reverse();
Ok(Value::array(copy))
}
Value::Str(s) => Ok(Value::Str(Rc::new(s.chars().rev().collect()))),
other => Err(format!(
"reverse expects an array or string but got a {}",
other.type_name()
)),
}
}
fn abs(_out: &mut dyn Output, _input: &mut dyn Input, args: Vec<Value>) -> Result<Value, String> {
check_arity("abs", &args, 1)?;
match &args[0] {
Value::Int(n) => n
.checked_abs()
.map(Value::Int)
.ok_or_else(|| "integer overflow in abs".to_string()),
Value::Float(f) => Ok(Value::Float(f.abs())),
other => Err(format!(
"abs expects a number but got a {}",
other.type_name()
)),
}
}
fn extreme(name: &str, args: Vec<Value>, want: Ordering) -> Result<Value, String> {
if args.is_empty() {
return Err(format!("{name} expects at least one argument"));
}
for value in &args {
if !matches!(value, Value::Int(_) | Value::Float(_)) {
return Err(format!(
"{name} expects numbers but got a {}",
value.type_name()
));
}
if matches!(value, Value::Float(f) if f.is_nan()) {
return Err(format!("{name} cannot compare NaN"));
}
}
let mut best = args[0].clone();
for value in &args[1..] {
let ordering = number_as_f64(value)
.partial_cmp(&number_as_f64(&best))
.unwrap_or(Ordering::Equal);
if ordering == want {
best = value.clone();
}
}
Ok(best)
}
fn min(_out: &mut dyn Output, _input: &mut dyn Input, args: Vec<Value>) -> Result<Value, String> {
extreme("min", args, Ordering::Less)
}
fn max(_out: &mut dyn Output, _input: &mut dyn Input, args: Vec<Value>) -> Result<Value, String> {
extreme("max", args, Ordering::Greater)
}
fn round_like(name: &str, args: Vec<Value>, apply: fn(f64) -> f64) -> Result<Value, String> {
check_arity(name, &args, 1)?;
match &args[0] {
Value::Int(n) => Ok(Value::Int(*n)),
Value::Float(x) => {
let rounded = apply(*x);
if rounded.is_finite() {
Ok(Value::Int(rounded as i64))
} else {
Err(format!("{name} of a non-finite number"))
}
}
other => Err(format!(
"{name} expects a number but got a {}",
other.type_name()
)),
}
}
fn floor(_out: &mut dyn Output, _input: &mut dyn Input, args: Vec<Value>) -> Result<Value, String> {
round_like("floor", args, f64::floor)
}
fn ceil(_out: &mut dyn Output, _input: &mut dyn Input, args: Vec<Value>) -> Result<Value, String> {
round_like("ceil", args, f64::ceil)
}
fn round(_out: &mut dyn Output, _input: &mut dyn Input, args: Vec<Value>) -> Result<Value, String> {
round_like("round", args, f64::round)
}
fn number_arg(name: &str, value: &Value) -> Result<f64, String> {
match value {
Value::Int(n) => Ok(*n as f64),
Value::Float(f) => Ok(*f),
other => Err(format!(
"{name} expects numbers but got a {}",
other.type_name()
)),
}
}
fn sqrt(_out: &mut dyn Output, _input: &mut dyn Input, args: Vec<Value>) -> Result<Value, String> {
check_arity("sqrt", &args, 1)?;
let x = number_arg("sqrt", &args[0])?;
if x < 0.0 {
return Err("sqrt of a negative number".to_string());
}
Ok(Value::Float(x.sqrt()))
}
fn pow(_out: &mut dyn Output, _input: &mut dyn Input, args: Vec<Value>) -> Result<Value, String> {
check_arity("pow", &args, 2)?;
match (&args[0], &args[1]) {
(Value::Int(base), Value::Int(exp)) if *exp >= 0 => {
let exp = u32::try_from(*exp).map_err(|_| "pow exponent is too large".to_string())?;
base.checked_pow(exp)
.map(Value::Int)
.ok_or_else(|| "integer overflow in pow".to_string())
}
_ => {
let base = number_arg("pow", &args[0])?;
let exp = number_arg("pow", &args[1])?;
Ok(Value::Float(base.powf(exp)))
}
}
}
fn int(_out: &mut dyn Output, _input: &mut dyn Input, args: Vec<Value>) -> Result<Value, String> {
check_arity("int", &args, 1)?;
match &args[0] {
Value::Int(n) => Ok(Value::Int(*n)),
Value::Float(f) => {
if f.is_finite() {
Ok(Value::Int(*f as i64))
} else {
Err("int of a non-finite number".to_string())
}
}
Value::Str(s) => s
.trim()
.parse::<i64>()
.map(Value::Int)
.map_err(|_| format!("cannot convert \"{s}\" to an int")),
other => Err(format!("int cannot convert a {}", other.type_name())),
}
}
fn float(_out: &mut dyn Output, _input: &mut dyn Input, args: Vec<Value>) -> Result<Value, String> {
check_arity("float", &args, 1)?;
match &args[0] {
Value::Int(n) => Ok(Value::Float(*n as f64)),
Value::Float(f) => Ok(Value::Float(*f)),
Value::Str(s) => s
.trim()
.parse::<f64>()
.map(Value::Float)
.map_err(|_| format!("cannot convert \"{s}\" to a float")),
other => Err(format!("float cannot convert a {}", other.type_name())),
}
}
fn args(system: &mut dyn System, args: Vec<Value>) -> Result<Value, String> {
check_arity("args", &args, 0)?;
let items = system
.arguments()
.into_iter()
.map(|arg| Value::Str(Rc::new(arg)))
.collect();
Ok(Value::array(items))
}
fn read_file(system: &mut dyn System, args: Vec<Value>) -> Result<Value, String> {
check_arity("read_file", &args, 1)?;
match &args[0] {
Value::Str(path) => system.read_file(path).map(|text| Value::Str(Rc::new(text))),
other => Err(format!(
"read_file expects a string path but got a {}",
other.type_name()
)),
}
}
fn write_file(system: &mut dyn System, args: Vec<Value>) -> Result<Value, String> {
check_arity("write_file", &args, 2)?;
match (&args[0], &args[1]) {
(Value::Str(path), Value::Str(contents)) => {
system.write_file(path, contents).map(|()| Value::Nil)
}
(Value::Str(_), other) => Err(format!(
"write_file expects a string to write but got a {}",
other.type_name()
)),
(other, _) => Err(format!(
"write_file expects a string path but got a {}",
other.type_name()
)),
}
}
fn file_exists(system: &mut dyn System, args: Vec<Value>) -> Result<Value, String> {
check_arity("file_exists", &args, 1)?;
match &args[0] {
Value::Str(path) => Ok(Value::Bool(system.file_exists(path))),
other => Err(format!(
"file_exists expects a string path but got a {}",
other.type_name()
)),
}
}
fn input(out: &mut dyn Output, input: &mut dyn Input, args: Vec<Value>) -> Result<Value, String> {
match args.as_slice() {
[] => {}
[Value::Str(prompt)] => out.write(prompt.as_str()),
[other] => {
return Err(format!(
"input expects a string prompt but got a {}",
other.type_name()
))
}
_ => {
return Err(format!(
"input expects 0 or 1 arguments but got {}",
args.len()
))
}
}
match input.read_line() {
Some(line) => Ok(Value::Str(Rc::new(line))),
None => Ok(Value::Nil),
}
}
pub enum Args {
One(Value),
Two(Value, Value),
}
impl Args {
pub fn count(&self) -> usize {
match self {
Args::One(_) => 1,
Args::Two(_, _) => 2,
}
}
pub fn push_onto(self, stack: &mut Vec<Value>) {
match self {
Args::One(a) => stack.push(a),
Args::Two(a, b) => {
stack.push(a);
stack.push(b);
}
}
}
pub fn into_vec(self) -> Vec<Value> {
match self {
Args::One(a) => vec![a],
Args::Two(a, b) => vec![a, b],
}
}
}
pub enum Step {
Call(Args),
Done(Value),
}
pub enum HostTask {
Map {
func: Value,
items: Vec<Value>,
next: usize,
out: Vec<Value>,
},
Filter {
func: Value,
items: Vec<Value>,
next: usize,
out: Vec<Value>,
pending: Option<Value>,
},
Reduce {
func: Value,
items: Vec<Value>,
next: usize,
acc: Value,
},
}
impl HostTask {
pub fn callback(&self) -> &Value {
match self {
HostTask::Map { func, .. }
| HostTask::Filter { func, .. }
| HostTask::Reduce { func, .. } => func,
}
}
pub fn resume(&mut self, last: Option<Value>) -> Result<Step, String> {
match self {
HostTask::Map {
items, next, out, ..
} => {
if let Some(value) = last {
out.push(value);
}
match take_next(items, next) {
None => Ok(Step::Done(array(std::mem::take(out)))),
Some(item) => Ok(Step::Call(Args::One(item))),
}
}
HostTask::Filter {
items,
next,
out,
pending,
..
} => {
if let Some(value) = last {
let item = pending.take().expect("an element under decision");
if value.condition()? {
out.push(item);
}
}
match take_next(items, next) {
None => Ok(Step::Done(array(std::mem::take(out)))),
Some(item) => {
*pending = Some(item.clone());
Ok(Step::Call(Args::One(item)))
}
}
}
HostTask::Reduce {
items, next, acc, ..
} => {
if let Some(value) = last {
*acc = value;
}
match take_next(items, next) {
None => Ok(Step::Done(std::mem::replace(acc, Value::Nil))),
Some(item) => {
let carried = std::mem::replace(acc, Value::Nil);
Ok(Step::Call(Args::Two(carried, item)))
}
}
}
}
}
}
fn take_next(items: &mut [Value], next: &mut usize) -> Option<Value> {
let slot = items.get_mut(*next)?;
*next += 1;
Some(std::mem::replace(slot, Value::Nil))
}
fn array(items: Vec<Value>) -> Value {
Value::array(items)
}
fn array_and_function(name: &str, args: Vec<Value>) -> Result<(Vec<Value>, Value), String> {
if args.len() != 2 {
return Err(format!(
"{name} expects 2 argument(s) but got {}",
args.len()
));
}
let mut args = args.into_iter();
let items = match args.next().expect("two arguments") {
Value::Array(items) => items.borrow().clone(),
other => {
return Err(format!(
"{name} expects an array but got a {}",
other.type_name()
))
}
};
Ok((items, args.next().expect("two arguments")))
}
fn map(args: Vec<Value>) -> Result<HostTask, String> {
let (items, func) = array_and_function("map", args)?;
Ok(HostTask::Map {
out: Vec::with_capacity(items.len()),
func,
items,
next: 0,
})
}
fn filter(args: Vec<Value>) -> Result<HostTask, String> {
let (items, func) = array_and_function("filter", args)?;
Ok(HostTask::Filter {
func,
items,
next: 0,
out: Vec::new(),
pending: None,
})
}
fn reduce(args: Vec<Value>) -> Result<HostTask, String> {
if args.len() != 3 {
return Err(format!(
"reduce expects 3 argument(s) but got {}",
args.len()
));
}
let mut args = args.into_iter();
let items = match args.next().expect("three arguments") {
Value::Array(items) => items.borrow().clone(),
other => {
return Err(format!(
"reduce expects an array but got a {}",
other.type_name()
))
}
};
Ok(HostTask::Reduce {
func: args.next().expect("three arguments"),
items,
next: 0,
acc: args.next().expect("three arguments"),
})
}
#[cfg(test)]
mod task_tests {
use super::{Args, HostTask, Step};
use crate::value::Value;
fn asked_for(step: &Result<Step, String>) -> String {
match step {
Ok(Step::Call(Args::One(v))) => v.repr(),
Ok(Step::Call(Args::Two(a, b))) => format!("two: {} {}", a.repr(), b.repr()),
Ok(Step::Done(v)) => format!("done: {}", v.repr()),
Err(message) => format!("refused: {message}"),
}
}
fn ints(values: &[i64]) -> Vec<Value> {
values.iter().map(|n| Value::Int(*n)).collect()
}
fn callback() -> Value {
Value::Nil
}
#[test]
fn a_map_task_asks_for_each_element_then_yields_the_results() {
let mut task = HostTask::Map {
func: callback(),
items: ints(&[1, 2]),
next: 0,
out: Vec::new(),
};
assert_eq!(asked_for(&task.resume(None)), "1");
assert_eq!(asked_for(&task.resume(Some(Value::Int(10)))), "2");
assert_eq!(
asked_for(&task.resume(Some(Value::Int(20)))),
"done: [10, 20]"
);
}
#[test]
fn a_map_task_over_nothing_is_done_at_once() {
let mut task = HostTask::Map {
func: callback(),
items: Vec::new(),
next: 0,
out: Vec::new(),
};
assert_eq!(asked_for(&task.resume(None)), "done: []");
}
#[test]
fn a_filter_task_keeps_only_what_the_answer_was_truthy_for() {
let mut task = HostTask::Filter {
func: callback(),
items: ints(&[1, 2, 3]),
next: 0,
out: Vec::new(),
pending: None,
};
assert_eq!(asked_for(&task.resume(None)), "1");
assert_eq!(asked_for(&task.resume(Some(Value::Bool(false)))), "2");
assert_eq!(asked_for(&task.resume(Some(Value::Bool(true)))), "3");
assert_eq!(asked_for(&task.resume(Some(Value::Nil))), "done: [2]");
}
#[test]
fn a_filter_task_yields_the_element_and_not_the_answer() {
let mut task = HostTask::Filter {
func: callback(),
items: ints(&[7]),
next: 0,
out: Vec::new(),
pending: None,
};
assert_eq!(asked_for(&task.resume(None)), "7");
assert_eq!(asked_for(&task.resume(Some(Value::Int(999)))), "done: [7]");
}
#[test]
fn a_reduce_task_carries_the_accumulator_through() {
let mut task = HostTask::Reduce {
func: callback(),
items: ints(&[1, 2]),
next: 0,
acc: Value::Int(0),
};
assert_eq!(asked_for(&task.resume(None)), "two: 0 1");
assert_eq!(asked_for(&task.resume(Some(Value::Int(1)))), "two: 1 2");
assert_eq!(asked_for(&task.resume(Some(Value::Int(3)))), "done: 3");
}
#[test]
fn a_reduce_task_over_nothing_yields_its_initial_value() {
let mut task = HostTask::Reduce {
func: callback(),
items: Vec::new(),
next: 0,
acc: Value::Int(42),
};
assert_eq!(asked_for(&task.resume(None)), "done: 42");
}
}
#[cfg(test)]
mod tests {
use crate::run_capture;
fn out(source: &str) -> String {
run_capture(source).expect("program should run")
}
fn err(source: &str) -> String {
run_capture(source)
.expect_err("program should fail")
.message
}
#[test]
fn eprint_writes_to_the_diagnostic_stream() {
let captured =
crate::run_capture_all("print(\"result\")\neprint(\"warning\")").expect("it runs");
assert_eq!(captured.out, "result\n");
assert_eq!(captured.err, "warning\n");
assert_eq!(captured.code, 0);
}
#[test]
fn eprint_formats_exactly_as_print_does() {
for source in [
"eprint()",
"eprint(1)",
"eprint(1, \"a\", true, nil)",
"eprint([1, 2], {\"k\": 1})",
] {
let printed = crate::run_capture_all(&source.replace("eprint", "print")).expect("runs");
let complained = crate::run_capture_all(source).expect("runs");
assert_eq!(
complained.err, printed.out,
"eprint and print disagree on {source}"
);
}
}
#[test]
fn exit_carries_a_code_and_keeps_what_was_printed() {
let captured =
crate::run_capture_all("print(\"done\")\neprint(\"why\")\nexit(2)").expect("it runs");
assert_eq!(captured.code, 2);
assert_eq!(captured.out, "done\n");
assert_eq!(captured.err, "why\n");
}
#[test]
fn a_program_that_never_calls_exit_reports_zero() {
assert_eq!(crate::run_capture_all("print(1)").expect("runs").code, 0);
}
#[test]
fn exit_refuses_a_code_an_operating_system_cannot_carry() {
assert_eq!(
err("exit(256)"),
"exit code must be from 0 to 255 but got 256"
);
assert_eq!(
err("exit(-1)"),
"exit code must be from 0 to 255 but got -1"
);
assert_eq!(err("exit(1.5)"), "exit expects an int but got a float");
}
#[test]
fn upper_and_lower() {
assert_eq!(out("print(upper(\"aBc\"), lower(\"aBc\"))"), "ABC abc\n");
}
#[test]
fn upper_rejects_non_strings() {
assert!(err("upper(1)").contains("expects a string"));
}
#[test]
fn trim_strips_surrounding_whitespace() {
assert_eq!(out("print(trim(\" hi \"))"), "hi\n");
}
#[test]
fn replace_swaps_every_occurrence() {
assert_eq!(out("print(replace(\"a.b.c\", \".\", \"-\"))"), "a-b-c\n");
}
#[test]
fn split_breaks_on_separator() {
assert_eq!(
out("print(split(\"a,b,c\", \",\"))"),
"[\"a\", \"b\", \"c\"]\n"
);
}
#[test]
fn split_on_empty_separator_yields_characters() {
assert_eq!(out("print(split(\"hi\", \"\"))"), "[\"h\", \"i\"]\n");
}
#[test]
fn join_concatenates_with_separator() {
assert_eq!(out("print(join([1, 2, 3], \"-\"))"), "1-2-3\n");
}
#[test]
fn contains_checks_substrings_and_membership() {
assert_eq!(
out("print(contains(\"hello\", \"ell\"), contains([1, 2], 2), contains([1, 2], 9))"),
"true true false\n"
);
}
#[test]
fn find_returns_char_index_or_negative_one() {
assert_eq!(
out("print(find(\"hello\", \"l\"), find(\"hello\", \"z\"))"),
"2 -1\n"
);
}
#[test]
fn pop_removes_and_returns_last() {
assert_eq!(out("let a = [1, 2, 3]\nprint(pop(a), a)"), "3 [1, 2]\n");
}
#[test]
fn pop_on_empty_array_errors() {
assert!(err("pop([])").contains("empty array"));
}
#[test]
fn index_of_finds_first_match_or_negative_one() {
assert_eq!(
out("print(index_of([10, 20, 30], 20), index_of([1], 9))"),
"1 -1\n"
);
}
#[test]
fn slice_on_arrays_and_strings() {
assert_eq!(out("print(slice([1, 2, 3, 4], 1, 3))"), "[2, 3]\n");
assert_eq!(out("print(slice(\"hello\", 1, 4))"), "ell\n");
}
#[test]
fn slice_clamps_out_of_range_bounds() {
assert_eq!(
out("print(slice([1, 2], 0, 99), slice([1, 2], 5, 9))"),
"[1, 2] []\n"
);
}
#[test]
fn sort_orders_numbers_and_strings() {
assert_eq!(out("print(sort([3, 1, 2]))"), "[1, 2, 3]\n");
assert_eq!(
out("print(sort([\"c\", \"a\", \"b\"]))"),
"[\"a\", \"b\", \"c\"]\n"
);
}
#[test]
fn sort_rejects_mixed_types() {
assert!(err("sort([1, \"a\"])").contains("all numbers or all strings"));
}
#[test]
fn reverse_flips_arrays_and_strings() {
assert_eq!(
out("print(reverse([1, 2, 3]), reverse(\"abc\"))"),
"[3, 2, 1] cba\n"
);
}
#[test]
fn abs_of_ints_and_floats() {
assert_eq!(out("print(abs(-3), abs(-2.5), abs(4))"), "3 2.5 4\n");
}
#[test]
fn min_and_max_over_numbers() {
assert_eq!(out("print(min(3, 1, 2), max(3, 1, 2))"), "1 3\n");
assert_eq!(out("print(min(2, 1.5), max(2, 1.5))"), "1.5 2\n");
}
#[test]
fn min_requires_arguments() {
assert!(err("min()").contains("at least one"));
}
#[test]
fn floor_ceil_round_return_ints() {
assert_eq!(
out("print(floor(2.7), ceil(2.1), round(2.5), round(2.4))"),
"2 3 3 2\n"
);
}
#[test]
fn rounding_passes_ints_through() {
assert_eq!(out("print(floor(5), ceil(5), round(5))"), "5 5 5\n");
}
#[test]
fn sqrt_returns_a_float() {
assert_eq!(out("print(sqrt(9), sqrt(16.0))"), "3.0 4.0\n");
}
#[test]
fn sqrt_rejects_negatives() {
assert!(err("sqrt(-1)").contains("negative"));
}
#[test]
fn pow_is_int_for_int_base_and_nonneg_exp() {
assert_eq!(
out("print(pow(2, 10), pow(2, -1), pow(2.0, 3))"),
"1024 0.5 8.0\n"
);
}
#[test]
fn int_converts_floats_and_strings() {
assert_eq!(out("print(int(2.9), int(\"42\"), int(7))"), "2 42 7\n");
}
#[test]
fn float_converts_ints_and_strings() {
assert_eq!(out("print(float(3), float(\"1.5\"))"), "3.0 1.5\n");
}
#[test]
fn int_rejects_unparseable_strings() {
assert!(err("int(\"abc\")").contains("cannot convert"));
}
#[test]
fn input_reads_a_line() {
let output =
crate::run_capture_with_input("let name = input()\nprint(\"hi\", name)", &["Aiko"])
.expect("program should run");
assert_eq!(output, "hi Aiko\n");
}
#[test]
fn input_writes_the_prompt() {
let output = crate::run_capture_with_input("let x = input(\"name? \")\nprint(x)", &["Bo"])
.expect("program should run");
assert_eq!(output, "name? Bo\n");
}
#[test]
fn input_returns_nil_at_end_of_input() {
let output =
crate::run_capture_with_input("print(input())", &[]).expect("program should run");
assert_eq!(output, "nil\n");
}
#[test]
fn map_applies_a_function_to_each_element() {
assert_eq!(
out("print(map([1, 2, 3], fn(x) { return x * 2 }))"),
"[2, 4, 6]\n"
);
}
#[test]
fn map_over_an_empty_array_is_empty() {
assert_eq!(out("print(map([], fn(x) { return x }))"), "[]\n");
}
#[test]
fn map_can_apply_a_closure() {
assert_eq!(
out("let n = 10\nlet add = fn(x) { return x + n }\nprint(map([1, 2, 3], add))"),
"[11, 12, 13]\n"
);
}
#[test]
fn map_can_apply_a_builtin() {
assert_eq!(out("print(map([-1, -2, 3], abs))"), "[1, 2, 3]\n");
}
#[test]
fn map_rejects_non_arrays() {
assert!(err("map(5, fn(x) { return x })").contains("expects an array"));
}
#[test]
fn map_rejects_a_non_callable_function() {
assert!(err("map([1, 2], 3)").contains("not callable"));
}
#[test]
fn map_checks_arity() {
assert!(err("map([1, 2])").contains("2 argument(s)"));
}
#[test]
fn filter_keeps_elements_where_the_predicate_is_truthy() {
assert_eq!(
out("print(filter([1, 2, 3, 4], fn(x) { return x % 2 == 0 }))"),
"[2, 4]\n"
);
}
#[test]
fn filter_can_drop_every_element() {
assert_eq!(out("print(filter([1, 2], fn(x) { return false }))"), "[]\n");
}
#[test]
fn filter_treats_nil_and_false_as_falsy() {
assert_eq!(
out("print(filter([1, 2, 3], fn(x) { if x == 2 { return nil } return true }))"),
"[1, 3]\n"
);
}
#[test]
fn filter_rejects_non_arrays() {
assert!(err("filter(5, fn(x) { return true })").contains("expects an array"));
}
#[test]
fn filter_rejects_a_non_callable_predicate() {
assert!(err("filter([1, 2], 3)").contains("not callable"));
}
#[test]
fn filter_checks_arity() {
assert!(err("filter([1, 2])").contains("2 argument(s)"));
}
#[test]
fn reduce_folds_from_the_left() {
assert_eq!(
out("print(reduce([1, 2, 3, 4], fn(acc, x) { return acc + x }, 0))"),
"10\n"
);
}
#[test]
fn reduce_returns_the_initial_value_for_an_empty_array() {
assert_eq!(
out("print(reduce([], fn(acc, x) { return acc + x }, 42))"),
"42\n"
);
}
#[test]
fn reduce_can_build_up_a_non_number() {
assert_eq!(
out("print(reduce([\"a\", \"b\", \"c\"], fn(acc, x) { return acc + x }, \"\"))"),
"abc\n"
);
}
#[test]
fn reduce_rejects_non_arrays() {
assert!(err("reduce(5, fn(acc, x) { return acc }, 0)").contains("expects an array"));
}
#[test]
fn reduce_rejects_a_non_callable_function() {
assert!(err("reduce([1, 2], 3, 0)").contains("not callable"));
}
#[test]
fn reduce_checks_arity() {
assert!(err("reduce([1, 2], fn(acc, x) { return acc })").contains("3 argument(s)"));
}
}
#[cfg(test)]
mod count {
use super::*;
#[test]
fn the_pinned_list_holds_every_builtin_that_is_registered() {
let mut globals = Globals::new();
register(&mut globals);
for name in BUILTIN_NAMES {
assert!(
globals.contains(name),
"'{name}' is listed but not registered"
);
}
assert_eq!(
globals.builtin_count(),
BUILTIN_NAMES.len(),
"register defines {} builtins but the list names {}",
globals.builtin_count(),
BUILTIN_NAMES.len()
);
}
#[test]
fn builtin_kind_counts_match_the_comments_that_quote_them() {
let mut globals = Globals::new();
register(&mut globals);
let (mut plain, mut system, mut host) = (0, 0, 0);
for slot in 0..globals.builtin_count() {
match globals.get(slot as u16) {
Some(Value::Builtin(builtin)) => match builtin.func {
NativeFn::Plain(_) => plain += 1,
NativeFn::System(_) => system += 1,
},
Some(Value::HostBuiltin(_)) => host += 1,
Some(other) => {
panic!(
"slot {slot} holds a {} rather than a builtin",
other.type_name()
)
}
None => panic!("slot {slot} is empty but is inside the builtin range"),
}
}
assert_eq!(
plain, 37,
"{plain} builtins take a BuiltinFn, not 37. Quoted by `Args::into_vec` \
above and by the trampoline section of docs/architecture.md."
);
assert_eq!(
plain + system,
41,
"{} builtins reach `call_native`, not 41. Quoted by the caught-error \
guard in `Vm::call_native`.",
plain + system
);
assert_eq!(
host, 3,
"{host} builtins are higher-order, not 3. Quoted by the same two \
places, which say the other seven take a different signature."
);
assert_eq!(
plain + system + host,
BUILTIN_NAMES.len(),
"the three kinds do not add up to the pinned list"
);
}
}
pub const BUILTIN_NAMES: [&str; 44] = [
"print",
"eprint",
"exit",
"len",
"push",
"str",
"type",
"is_error",
"range",
"keys",
"values",
"has",
"remove",
"upper",
"lower",
"trim",
"replace",
"split",
"join",
"contains",
"find",
"pop",
"index_of",
"slice",
"sort",
"reverse",
"abs",
"min",
"max",
"floor",
"ceil",
"round",
"sqrt",
"pow",
"int",
"float",
"input",
"read_file",
"write_file",
"file_exists",
"args",
"map",
"filter",
"reduce",
];