feat: add list constructor

This commit is contained in:
Roman Godmaire 2024-05-04 16:52:55 -04:00
parent c38576b667
commit 769cc61d48
2 changed files with 22 additions and 2 deletions

13
src/env/core.rs vendored
View file

@ -66,6 +66,19 @@ pub(super) fn core() -> HashMap<String, Node> {
}), }),
), ),
// Collections // Collections
("list", Node::NativeFunc(|_env, args| Ok(Node::List(args)))),
(
"list?",
Node::NativeFunc(|_env, args| {
arg_count!(1, args.len());
if let Node::List(_list) = args[0].borrow() {
return Ok(Node::Boolean(true));
}
Ok(Node::Boolean(false))
}),
),
( (
"vector", "vector",
Node::NativeFunc(|_env, args| Ok(Node::Vector(args))), Node::NativeFunc(|_env, args| Ok(Node::Vector(args))),

View file

@ -93,21 +93,28 @@ mod test {
#[case("(- 5 1)", "4")] #[case("(- 5 1)", "4")]
#[case("(* 8 9)", "72")] #[case("(* 8 9)", "72")]
#[case("(/ 86 2)", "43")] #[case("(/ 86 2)", "43")]
// Native functions
#[case("(+ 1 2 (- 3 4))", "2")] #[case("(+ 1 2 (- 3 4))", "2")]
#[case("(vector 1 2 3)", "[1 2 3]")]
// Native functions defaults // Native functions defaults
#[case("(+)", "0")] #[case("(+)", "0")]
#[case("(-)", "0")] #[case("(-)", "0")]
#[case("(*)", "0")] #[case("(*)", "0")]
#[case("(/)", "0")] #[case("(/)", "0")]
#[case("(vector)", "[]")] #[case("(vector)", "[]")]
#[case("(hashmap)", "{}")]
#[case("(list)", "()")]
// Collections // Collections
#[case("[]", "[]")] #[case("[]", "[]")]
#[case("[1 2]", "[1 2]")] #[case("[1 2]", "[1 2]")]
#[case("[1 (+ 1 2)]", "[1 3]")] #[case("[1 (+ 1 2)]", "[1 3]")]
#[case("{}", "{}")] #[case("{}", "{}")]
#[case("{:a \"uwu\"}", "{:a: uwu}")] #[case("{:a \"uwu\"}", "{:a: uwu}")]
#[case("(list 1 2 3)", "(1 2 3)")]
#[case("(list? (list))", "true")]
#[case("(vector 1 2 3)", "[1 2 3]")]
#[case("(vector? [])", "true")]
// Unsure how to test this due to hashmaps being unordered
// #[case("(hashmap :a 1 :b 2)", "{:a: 1, :b: 2}")]
#[case("(hashmap? {})", "true")]
// Environment manipulation // Environment manipulation
#[case("(define! asdf (+ 2 2)) (+ asdf 2)", "4\n6")] #[case("(define! asdf (+ 2 2)) (+ asdf 2)", "4\n6")]
#[case("(let* (a 2) (+ a 2))", "4")] #[case("(let* (a 2) (+ a 2))", "4")]