add boolean cast function
This commit is contained in:
parent
001e35755d
commit
ce3dba470a
4 changed files with 133 additions and 2 deletions
|
|
@ -117,3 +117,29 @@ pub fn toggle_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, String> {
|
|||
syms.insert(var_name, sym);
|
||||
Ok(Ctr::None)
|
||||
}
|
||||
|
||||
|
||||
pub const BOOLCAST_DOCSTRING: &str = "takes one argument of any type.
|
||||
attempts to cast argument to a bool.
|
||||
Strings will cast to a bool if they are 'true' or 'false'.
|
||||
Integers and Floats will cast to true if they are 0 and false otherwise.";
|
||||
|
||||
pub fn boolcast_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, String> {
|
||||
match &*ast.car {
|
||||
Ctr::Bool(_) => Ok(*ast.car.clone()),
|
||||
Ctr::Symbol(_) => Err("not clear how to cast a symbol to a bool".to_string()),
|
||||
Ctr::String(s) => {
|
||||
if s == "true" {
|
||||
Ok(Ctr::Bool(true))
|
||||
} else if s == "false" {
|
||||
Ok(Ctr::Bool(false))
|
||||
} else {
|
||||
Err("string cannot be parsed as a bool".to_string())
|
||||
}
|
||||
},
|
||||
Ctr::Integer(i) => Ok(Ctr::Bool(*i == 0)),
|
||||
Ctr::Float(f) => Ok(Ctr::Bool(*f == 0.0)),
|
||||
Ctr::Seg(_) => Err("cannot convert list to a boolean".to_string()),
|
||||
Ctr::None => Err("Impossible task: cannot convert none to bool".to_string()),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue