Lines
100 %
Functions
Branches
//! `QUOTE`'s evaluation is the reader's `'x`: one `Expr::Quote` value.
use crate::ast::Expr;
use crate::compiler::expr::eval_value;
use crate::error::Error;
use crate::reader::Reader;
use crate::runtime::SymbolTable;
fn form(source: &str) -> Expr {
Reader::parse(source)
.ok()
.and_then(|program| program.exprs.into_iter().next())
.unwrap_or(Expr::Nil)
}
fn eval(source: &str) -> Result<Expr, Error> {
eval_value(&mut SymbolTable::with_builtins_for_wasm(), &form(source))
#[test]
fn a_quote_form_evaluates_like_the_reader_sugar() {
assert_eq!(eval("(quote b)").ok(), eval("'b").ok());
assert_eq!(eval("(quote (1 2))").ok(), eval("'(1 2)").ok());
fn a_quote_form_evaluates_to_a_quote_value() {
assert!(matches!(eval("(quote b)"), Ok(Expr::Datum(_))));
fn a_quoted_number_or_string_is_the_atom() {
for atom in ["1", "1/2", "\"a\""] {
let bare = eval(atom).ok();
assert_eq!(
eval(&format!("(quote {atom})")).ok(),
bare,
"(quote {atom})"
);
assert_eq!(eval(&format!("'{atom}")).ok(), bare, "'{atom}");
assert!(!matches!(bare, Some(Expr::Datum(_))), "{atom}");
fn a_quoted_keyword_is_the_keyword() {
assert_eq!(eval("(quote :k)").ok(), Some(Expr::Keyword("K".into())));
assert_eq!(eval("(quote :k)").ok(), eval("':k").ok());
fn a_quote_form_with_two_operands_is_an_arity_error() {
assert!(matches!(eval("(quote a b)"), Err(Error::Arity { .. })));
fn a_quoted_constant_is_the_constant_the_reader_folds() {
for datum in ["nil", "t", "()", "b", "(1 2)"] {
eval(&format!("(quote {datum})")).ok(),
eval(&format!("'{datum}")).ok(),
"(quote {datum})"
assert_eq!(eval("(quote nil)").ok(), Some(Expr::Nil));
assert_eq!(eval("(quote ())").ok(), Some(Expr::Nil));
assert_eq!(eval("(quote t)").ok(), Some(Expr::T));