fix append func
Signed-off-by: Ava Hahn <ava@aidanis.online>
This commit is contained in:
parent
09e3546ba6
commit
452cb7a654
2 changed files with 27 additions and 5 deletions
|
|
@ -15,15 +15,19 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use crate::segment::{Ctr, Seg};
|
use crate::segment::{Ctr, Seg};
|
||||||
use crate::sym::{SymTable};
|
use crate::sym::SymTable;
|
||||||
|
|
||||||
// Some essential operations below
|
|
||||||
pub fn append_callback (ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, String> {
|
pub fn append_callback (ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, String> {
|
||||||
// if car is a list, append cdr
|
|
||||||
// otherwise create a list out of all arguments
|
|
||||||
if let Ctr::Seg(ref s) = *ast.car {
|
if let Ctr::Seg(ref s) = *ast.car {
|
||||||
let mut temp = s.clone();
|
let mut temp = s.clone();
|
||||||
temp.append(ast.cdr.clone());
|
if let Ctr::Seg(ref list) = *ast.cdr {
|
||||||
|
list.circuit(&mut |c: &Ctr| -> bool {
|
||||||
|
temp.append(Box::new(c.clone()));
|
||||||
|
true
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
temp.append(Box::new(*ast.cdr.clone()));
|
||||||
|
}
|
||||||
Ok(Ctr::Seg(temp))
|
Ok(Ctr::Seg(temp))
|
||||||
} else {
|
} else {
|
||||||
let mut temp = Seg::new();
|
let mut temp = Seg::new();
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,24 @@ mod append_lib_tests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_multi_append_to_empty_list() {
|
||||||
|
let document = "(append () 1 'two' 3.4)";
|
||||||
|
let result = "(1 'two' 3.4)";
|
||||||
|
|
||||||
|
let mut syms = SymTable::new();
|
||||||
|
static_stdlib(&mut syms).unwrap();
|
||||||
|
dynamic_stdlib(false, &mut syms).unwrap();
|
||||||
|
|
||||||
|
if let Ok(tree) = lex(&document.to_string()) {
|
||||||
|
if let Ctr::Seg(ref s) = *eval(&tree, &mut syms).unwrap() {
|
||||||
|
assert_eq!(s.to_string(), result);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
assert!(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_append_to_full_list() {
|
fn test_append_to_full_list() {
|
||||||
let document = "(append (1 2) 3)";
|
let document = "(append (1 2) 3)";
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue