feat: basic quoting

...  I think this is right?
This commit is contained in:
Roman Godmaire 2024-05-04 21:55:17 -04:00
parent c596e00be8
commit 00ac922bd0
2 changed files with 13 additions and 0 deletions

8
src/env/core.rs vendored
View file

@ -35,6 +35,14 @@ pub(super) fn core() -> HashMap<String, Node> {
eval_node(env, Node::List(list)) eval_node(env, Node::List(list))
}), }),
), ),
(
"quote",
Node::Special(|_env, args| {
arg_count!(1, args.len());
let val = args.into_iter().next().unwrap();
Ok(val)
}),
),
// Arithmetic operations // Arithmetic operations
( (
"+", "+",

View file

@ -69,6 +69,7 @@ pub fn eval_node(env: &Environment, ast_node: Node) -> Result<Node> {
_ => Err(Error::InvalidOperator)?, _ => Err(Error::InvalidOperator)?,
} }
} }
Node::Symbol(sym) => env.get(&sym).ok_or(Error::NotInEnv(sym))?.to_owned(), Node::Symbol(sym) => env.get(&sym).ok_or(Error::NotInEnv(sym))?.to_owned(),
val => val, val => val,
@ -95,6 +96,10 @@ mod test {
// Meta // Meta
#[case("(eval (list + 1 1))", "2")] #[case("(eval (list + 1 1))", "2")]
#[case("(apply + (list 1 2 3))", "6")] #[case("(apply + (list 1 2 3))", "6")]
#[case("'(1 2 3)", "(1 2 3)")]
#[case("(quote (1 2 3))", "(1 2 3)")]
#[case("'(1 2 (4 5) 6 7)", "(1 2 (4 5) 6 7)")]
#[case("(quote (1 2 (4 5) 6 7))", "(1 2 (4 5) 6 7)")]
// Arithmetic // Arithmetic
#[case("(+ 1 2)", "3")] #[case("(+ 1 2)", "3")]
#[case("(- 5 1)", "4")] #[case("(- 5 1)", "4")]