flesh/src/stl/boolean.rs

148 lines
5 KiB
Rust
Raw Normal View History

/* Copyright (C) 2021 Aidan Hahn
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
use crate::segment::{Ctr, Seg};
use crate::error::{Traceback, start_trace};
use crate::sym::{SymTable, ValueType};
pub const AND_DOCSTRING: &str =
"traverses a list of N arguments, all of which are expected to be boolean.
starts with arg1 AND arg2, and then calculates prev_result AND next_arg.
returns final result.";
pub fn and_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback> {
let mut type_error = false;
let mut cursor = 0;
let result = ast.circuit(&mut |arg: &Ctr| -> bool {
if let Ctr::Bool(b) = *arg {
cursor += 1;
b
} else {
type_error = true;
false
}
});
if type_error {
Err(start_trace(("and", format!("input {} not a boolean", cursor)).into()))
} else {
Ok(Ctr::Bool(result))
}
}
pub const OR_DOCSTRING: &str =
"traverses a list of N arguments, all of which are expected to be boolean.
starts with arg1 OR arg2, and then calculates prev_result OR next_arg.
returns final result.";
pub fn or_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback> {
let mut result = false;
let mut cursor = 0;
let correct_types = ast.circuit(&mut |arg: &Ctr| -> bool {
if let Ctr::Bool(b) = *arg {
cursor += 1;
result = result || b;
true
} else {
false
}
});
if !correct_types {
Err(start_trace(("or", format!("input {} not a boolean", cursor)).into()))
} else {
Ok(Ctr::Bool(result))
}
}
pub const NOT_DOCSTRING: &str = "takes a single argument (expects a boolean).
returns false if arg is true or true if arg is false.";
pub fn not_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback> {
if let Ctr::Bool(b) = *ast.car {
Ok(Ctr::Bool(!b))
} else {
Err(start_trace(("not", "input is not a bool").into()))
}
}
pub const ISEQ_DOCSTRING: &str = "traverses a list of N arguments.
returns true if all arguments hold the same value.
NOTE: 1 and 1.0 are the same, but '1' 'one' or one (symbol) aren't";
pub fn iseq_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback> {
let head_ctr_ref = &*ast.car;
Ok(Ctr::Bool(
ast.circuit(&mut |arg: &Ctr| -> bool { arg == head_ctr_ref }),
))
}
pub const TOGGLE_DOCSTRING: &str = "switches a boolean symbol between true or false.
Takes a single argument (a symbol). Looks it up in the variable table.
Either sets the symbol to true if it is currently false, or vice versa.";
pub fn toggle_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, Traceback> {
let var_name: String;
if let Ctr::Symbol(ref s) = *ast.car {
var_name = s.clone();
} else {
return Err(start_trace(("toggle", "input must be a symbol").into()));
}
let mut sym = syms
.remove(&var_name)
.expect(&format!("symbol {var_name} is not defined"));
if let ValueType::VarForm(ref var) = sym.value {
if let Ctr::Bool(ref b) = **var {
sym.value = ValueType::VarForm(Box::new(Ctr::Bool(!b)));
} else {
syms.insert(var_name, sym);
return Err(start_trace(("toggle", "can only toggle a boolean").into()));
}
} else {
syms.insert(var_name, sym);
return Err(start_trace(("toggle", "cannot toggle a function").into()));
}
syms.insert(var_name, sym);
Ok(Ctr::None)
}
2023-03-09 17:28:17 -08:00
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, Traceback> {
2023-03-09 17:28:17 -08:00
match &*ast.car {
Ctr::Bool(_) => Ok(*ast.car.clone()),
Ctr::String(s) => {
if s == "true" {
Ok(Ctr::Bool(true))
} else if s == "false" {
Ok(Ctr::Bool(false))
} else {
Err(start_trace(("bool", "string cannot be parsed as a bool").into()))
2023-03-09 17:28:17 -08:00
}
},
Ctr::Integer(i) => Ok(Ctr::Bool(*i == 0)),
Ctr::Float(f) => Ok(Ctr::Bool(*f == 0.0)),
_ => Err(start_trace(("bool", format!("cannot convert a {} to a boolean",
ast.car.to_type())).into())),
2023-03-09 17:28:17 -08:00
}
}