feat: eval/apply functions

This commit is contained in:
Roman Godmaire 2024-05-04 21:14:47 -04:00
parent 2d93c83b67
commit c596e00be8
2 changed files with 30 additions and 0 deletions

27
src/env/core.rs vendored
View file

@ -8,6 +8,33 @@ use crate::node::Node;
pub(super) fn core() -> HashMap<String, Node> {
[
// Meta
(
"eval",
Node::NativeFunc(|env, args| {
arg_count!(1, args.len());
let ast = args.into_iter().next().unwrap();
eval_node(env, ast)
}),
),
(
"apply",
Node::NativeFunc(|env, args| {
arg_count!(2, args.len());
let mut args = args.into_iter();
let operator = args.next().unwrap();
let args = match args.next().unwrap() {
Node::List(list) => list,
_ => Err(Error::ExpectedList)?,
};
let mut list = vec![operator];
list.extend(args);
eval_node(env, Node::List(list))
}),
),
// Arithmetic operations
(
"+",

View file

@ -92,6 +92,9 @@ mod test {
#[case(":owo", ":owo")]
#[case("()", "()")]
#[case("nil", "()")]
// Meta
#[case("(eval (list + 1 1))", "2")]
#[case("(apply + (list 1 2 3))", "6")]
// Arithmetic
#[case("(+ 1 2)", "3")]
#[case("(- 5 1)", "4")]