Fully fledged lambdas, along with efficiency tweaks across the ast

This commit is contained in:
Ava Apples Affine 2023-03-13 15:02:19 -07:00
parent b0bd369c1d
commit 8efa1dbaad
Signed by: affine
GPG key ID: 3A4645B8CF806069
10 changed files with 264 additions and 70 deletions

View file

@ -1,4 +1,4 @@
mod var_lib_tests {
mod decl_lib_tests {
use relish::ast::{eval, lex, Ctr, SymTable};
use relish::stdlib::{dynamic_stdlib, static_stdlib};
@ -297,4 +297,86 @@ mod var_lib_tests {
result.to_string(),
);
}
#[test]
fn test_lambda_str_equivalency_list() {
let document = "(lambda (x y) (add x y))";
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
document.to_string(),
);
}
#[test]
fn test_lambda_str_equivalency_no_args() {
let document = "(lambda () (add 1 2))";
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
document.to_string(),
);
}
#[test]
fn test_lambda_inline_call() {
let document = "((lambda (x y) (add x y)) 1 2)";
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
let it = *eval(
&lex(&document.to_string()).unwrap(),
&mut syms).unwrap();
println!("final return: {}", it);
if let Ctr::Integer(i) = it {
assert_eq!(i, 3)
} else {
panic!()
}
}
#[test]
fn test_lambda_let_call() {
let document = "(let ((adder (lambda (x y) (add x y))))
(adder 1 2))";
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
let it = *eval(
&lex(&document.to_string()).unwrap(),
&mut syms).unwrap();
println!("{}", it);
if let Ctr::Integer(i) = it {
assert_eq!(i, 3)
} else {
panic!()
}
}
#[test]
fn test_lambda_var_bound_call() {
let document = "(let (())
(def adder 'my adder' (lambda (x y) (add x y)))
(adder 1 2))";
let mut syms = SymTable::new();
static_stdlib(&mut syms).unwrap();
dynamic_stdlib(&mut syms).unwrap();
let it = *eval(
&lex(&document.to_string()).unwrap(),
&mut syms).unwrap();
println!("{}", it);
if let Ctr::Integer(i) = it {
assert_eq!(i, 3)
} else {
panic!()
}
}
}