add list type check

This commit is contained in:
Ava Apples Affine 2023-07-30 12:26:02 -07:00
parent e3875d4652
commit 0babc1986a
4 changed files with 56 additions and 2 deletions

View file

@ -155,7 +155,6 @@ Note: this section only tracks the state of incomplete TODO items. Having everyt
** DONE Beta tasks
(See tag: v0.3.0)
** TODO v1.0 tasks
- islist type query
- set library
- Can pass args to relish scripts (via interpreter)
- Can pass args to relish scripts (via command line)

View file

@ -299,7 +299,7 @@ The following table is up to date as of Relish 0.3.0. For latest information try
|----------------+---------------+----------------+---------+--------+-----------+-----------+-----------+--------+-------------|
| | | | pop | gte? | string | | cond | | |
|----------------+---------------+----------------+---------+--------+-----------+-----------+-----------+--------+-------------|
| | | | | int | | | | | |
| | | | list? | int | | | | | |
|----------------+---------------+----------------+---------+--------+-----------+-----------+-----------+--------+-------------|
| | | | | mod | | | | | |
|----------------+---------------+----------------+---------+--------+-----------+-----------+-----------+--------+-------------|

View file

@ -180,6 +180,15 @@ fn reverse_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback> {
}
}
const ISLIST_DOCSTRING: &str = "takes a single argument, returns true if argument is a list";
fn islist_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback> {
if let Ctr::Seg(_) = *ast.car {
Ok(Ctr::Bool(true))
} else {
Ok(Ctr::Bool(false))
}
}
pub fn add_list_lib(syms: &mut SymTable) {
syms.insert(
"cons".to_string(),
@ -264,4 +273,16 @@ pub fn add_list_lib(syms: &mut SymTable) {
..Default::default()
},
);
syms.insert(
"list?".to_string(),
Symbol {
name: String::from("list?"),
args: Args::Lazy(1),
conditional_branches: false,
docs: ISLIST_DOCSTRING.to_string(),
value: ValueType::Internal(Rc::new(islist_callback)),
..Default::default()
},
);
}

View file

@ -328,4 +328,38 @@ mod append_lib_tests {
result.to_string(),
);
}
#[test]
fn test_islist_t() {
let document = "(list? ())";
let result = "true";
let mut syms = SymTable::new();
static_stdlib(&mut syms);
dynamic_stdlib(&mut syms, None);
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
#[test]
fn test_islist_f() {
let document = "(list? 1223)";
let result = "false";
let mut syms = SymTable::new();
static_stdlib(&mut syms);
dynamic_stdlib(&mut syms, None);
assert_eq!(
*eval(&lex(&document.to_string()).unwrap(), &mut syms)
.unwrap()
.to_string(),
result.to_string(),
);
}
}