Lines
100 %
Functions
Branches
use super::FORMS;
use crate::ast::{Expr, WasmType};
use crate::compiler::context::CompileContext;
use crate::compiler::emit::FunctionEmitter;
use crate::error::{Error, Result};
use crate::reader::Reader;
use crate::runtime::SymbolTable;
fn form(name: &str) -> &'static crate::compiler::special::SpecialFormSpec {
FORMS
.iter()
.find(|spec| spec.name == name)
.expect("registered case form")
}
fn args(sources: &[&str]) -> Vec<Expr> {
sources
.map(|s| Reader::parse_expr(s).unwrap())
.collect()
fn eval(name: &str, sources: &[&str]) -> Result<Expr> {
let mut symbols = SymbolTable::with_builtins();
(form(name).eval)(&mut symbols, &args(sources))
fn value(source: &str) -> Expr {
Reader::parse_expr(source).unwrap()
#[test]
fn case_answers_the_first_matching_clause() {
assert_eq!(
eval("CASE", &["2", "(1 10)", "(2 20)", "(2 30)"]).unwrap(),
value("20")
);
fn case_matches_any_key_of_a_key_list() {
eval("CASE", &[":b", "((:a :b) 1)", "(t 9)"]).unwrap(),
value("1")
fn case_keys_are_data_not_variables() {
assert_eq!(eval("CASE", &["'a", "(a 1)", "(t 9)"]).unwrap(), value("1"));
fn case_falls_to_t_or_otherwise_then_to_nil() {
eval("CASE", &["5", "(1 10)", "(otherwise 9)"]).unwrap(),
value("9")
assert_eq!(eval("CASE", &["5", "(1 10)"]).unwrap(), Expr::Nil);
fn a_clause_without_forms_answers_nil() {
assert_eq!(eval("CASE", &["1", "(1)"]).unwrap(), Expr::Nil);
fn a_default_clause_must_be_last_and_not_in_ecase() {
for (name, clauses) in [
("CASE", ["(t 1)", "(2 3)"]),
("ECASE", ["(1 2)", "(t 3)"]),
("ECASE", ["(otherwise 3)", "(1 2)"]),
] {
let err = eval(name, &["1", clauses[0], clauses[1]]);
assert!(
matches!(err, Err(Error::Compile(ref m)) if m.contains("last clause")),
"{name} {clauses:?}"
fn a_malformed_form_is_refused() {
assert!(matches!(
eval("CASE", &[]),
Err(Error::Arity { expected: 1, .. })
));
eval("ECASE", &["1", "2"]),
Err(Error::Compile(ref m)) if m.contains("clause must be")
fn ecase_without_a_match_is_a_raise_not_a_value() {
let raised = eval("ECASE", &["5", "(1 1)"]).unwrap();
assert!(raised.is_wasm_runtime());
fn ecase_with_a_match_answers_the_clause() {
eval("ECASE", &["2", "(1 10)", "((2 3) 20)"]).unwrap(),
fn lower(name: &str, sources: &[&str]) -> Result<WasmType> {
let mut ctx = CompileContext::new_with_host_fns(&[])?;
let mut emit = FunctionEmitter::new();
let stack = form(name).stack.expect("case forms lower to the stack");
let ty = stack(&mut ctx, &mut emit, &mut symbols, &args(sources))?;
emit.finish(&[])?;
Ok(ty)
fn a_runtime_key_lowers_to_the_clause_type() {
lower("CASE", &["(progn (print 1) :a)", "(:a 1)", "(t 2)"]).unwrap(),
WasmType::I32
lower("ECASE", &["(progn (print 1) :a)", "(:a 1)"]).unwrap(),
fn the_effect_and_compile_paths_lower_too() {
for name in ["CASE", "ECASE"] {
for path in [form(name).compile, form(name).effect.expect("effect path")] {
let mut ctx = CompileContext::new_with_host_fns(&[]).unwrap();
path(&mut ctx, &mut emit, &mut symbols, &args(&["1", "(1 2)"])).unwrap();
emit.finish(&[]).unwrap();