2023-03-02 09:43:12 -08:00
|
|
|
/* 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};
|
2023-03-02 12:15:42 -08:00
|
|
|
use crate::sym::{SymTable, ValueType};
|
2023-03-02 09:43:12 -08:00
|
|
|
|
|
|
|
|
pub fn bool_and_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, String> {
|
|
|
|
|
let mut type_error = false;
|
|
|
|
|
let result = ast.circuit(&mut |arg: &Ctr| -> bool {
|
|
|
|
|
if let Ctr::Bool(b) = *arg {
|
|
|
|
|
b
|
|
|
|
|
} else {
|
|
|
|
|
eprintln!("{} is not a boolean", arg);
|
|
|
|
|
type_error = true;
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if type_error {
|
|
|
|
|
Err("all arguments to and must evaluate to boolean".to_string())
|
|
|
|
|
} else {
|
|
|
|
|
Ok(Ctr::Bool(result))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn bool_or_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, String> {
|
|
|
|
|
let mut result = false;
|
|
|
|
|
let correct_types = ast.circuit(&mut |arg: &Ctr| -> bool {
|
|
|
|
|
if let Ctr::Bool(b) = *arg {
|
|
|
|
|
result = result || b;
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
eprintln!("{} is not a boolean", arg);
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if !correct_types {
|
|
|
|
|
Err("all arguments to 'or' must evaluate to boolean".to_string())
|
|
|
|
|
} else {
|
|
|
|
|
Ok(Ctr::Bool(result))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn bool_xor_callback(_ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, String> {
|
|
|
|
|
todo!()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn bool_not_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, String> {
|
|
|
|
|
if let Ctr::Bool(b) = *ast.car {
|
|
|
|
|
Ok(Ctr::Bool(!b))
|
|
|
|
|
} else {
|
|
|
|
|
Err("impossible state: non bool given to not".to_string())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn bool_iseq_callback(_ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, String> {
|
|
|
|
|
todo!()
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-02 12:15:42 -08:00
|
|
|
pub fn bool_toggle_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, String> {
|
|
|
|
|
let var_name: String;
|
|
|
|
|
if let Ctr::Symbol(ref s) = *ast.car {
|
|
|
|
|
var_name = s.clone();
|
|
|
|
|
} else {
|
|
|
|
|
return Err("argument to toggle should be a symbol".to_string())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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("can only toggle a boolean".to_string())
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
syms.insert(var_name, sym);
|
|
|
|
|
return Err("cannot toggle a function".to_string())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
syms.insert(var_name, sym);
|
|
|
|
|
Ok(Ctr::None)
|
2023-03-02 09:43:12 -08:00
|
|
|
}
|