Fully fledged lambdas, along with efficiency tweaks across the ast
This commit is contained in:
parent
b0bd369c1d
commit
8efa1dbaad
10 changed files with 264 additions and 70 deletions
|
|
@ -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!()
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue