2024-02-06 22:39:08 +00:00
|
|
|
/* Flesh: Flexible Shell
|
|
|
|
|
* Copyright (C) 2021 Ava Affine
|
2023-03-02 09:43:12 -08:00
|
|
|
*
|
|
|
|
|
* 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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-05-25 23:08:44 +00:00
|
|
|
use crate::segment::{Ctr, Seg, Type};
|
2023-05-23 22:06:11 +00:00
|
|
|
use crate::error::{Traceback, start_trace};
|
2023-05-25 23:08:44 +00:00
|
|
|
use crate::sym::{SymTable, ValueType, Args, Symbol};
|
|
|
|
|
use std::rc::Rc;
|
2023-03-02 09:43:12 -08:00
|
|
|
|
2023-05-25 23:08:44 +00:00
|
|
|
const AND_DOCSTRING: &str =
|
2023-03-06 15:52:09 -08:00
|
|
|
"traverses a list of N arguments, all of which are expected to be boolean.
|
2023-03-06 15:12:59 -08:00
|
|
|
starts with arg1 AND arg2, and then calculates prev_result AND next_arg.
|
|
|
|
|
returns final result.";
|
2023-05-25 23:08:44 +00:00
|
|
|
fn and_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback> {
|
2023-03-02 09:43:12 -08:00
|
|
|
let mut type_error = false;
|
2023-05-23 22:06:11 +00:00
|
|
|
let mut cursor = 0;
|
2023-03-02 09:43:12 -08:00
|
|
|
let result = ast.circuit(&mut |arg: &Ctr| -> bool {
|
|
|
|
|
if let Ctr::Bool(b) = *arg {
|
2023-05-23 22:06:11 +00:00
|
|
|
cursor += 1;
|
2023-03-02 09:43:12 -08:00
|
|
|
b
|
|
|
|
|
} else {
|
|
|
|
|
type_error = true;
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if type_error {
|
2023-05-23 22:06:11 +00:00
|
|
|
Err(start_trace(("and", format!("input {} not a boolean", cursor)).into()))
|
2023-03-02 09:43:12 -08:00
|
|
|
} else {
|
|
|
|
|
Ok(Ctr::Bool(result))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-25 23:08:44 +00:00
|
|
|
const OR_DOCSTRING: &str =
|
2023-03-06 15:52:09 -08:00
|
|
|
"traverses a list of N arguments, all of which are expected to be boolean.
|
2023-03-06 15:12:59 -08:00
|
|
|
starts with arg1 OR arg2, and then calculates prev_result OR next_arg.
|
|
|
|
|
returns final result.";
|
2023-05-25 23:08:44 +00:00
|
|
|
fn or_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback> {
|
2023-03-02 09:43:12 -08:00
|
|
|
let mut result = false;
|
2023-05-23 22:06:11 +00:00
|
|
|
let mut cursor = 0;
|
2023-03-02 09:43:12 -08:00
|
|
|
let correct_types = ast.circuit(&mut |arg: &Ctr| -> bool {
|
|
|
|
|
if let Ctr::Bool(b) = *arg {
|
2023-05-23 22:06:11 +00:00
|
|
|
cursor += 1;
|
2023-03-02 09:43:12 -08:00
|
|
|
result = result || b;
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if !correct_types {
|
2023-05-23 22:06:11 +00:00
|
|
|
Err(start_trace(("or", format!("input {} not a boolean", cursor)).into()))
|
2023-03-02 09:43:12 -08:00
|
|
|
} else {
|
|
|
|
|
Ok(Ctr::Bool(result))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-25 23:08:44 +00:00
|
|
|
const NOT_DOCSTRING: &str = "takes a single argument (expects a boolean).
|
2023-03-06 15:12:59 -08:00
|
|
|
returns false if arg is true or true if arg is false.";
|
2023-05-25 23:08:44 +00:00
|
|
|
fn not_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback> {
|
2023-03-02 09:43:12 -08:00
|
|
|
if let Ctr::Bool(b) = *ast.car {
|
|
|
|
|
Ok(Ctr::Bool(!b))
|
|
|
|
|
} else {
|
2023-05-23 22:06:11 +00:00
|
|
|
Err(start_trace(("not", "input is not a bool").into()))
|
2023-03-02 09:43:12 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-25 23:08:44 +00:00
|
|
|
const ISEQ_DOCSTRING: &str = "traverses a list of N arguments.
|
2023-03-06 15:12:59 -08:00
|
|
|
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";
|
2023-05-25 23:08:44 +00:00
|
|
|
fn iseq_callback(ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, Traceback> {
|
2023-03-02 12:48:26 -08:00
|
|
|
let head_ctr_ref = &*ast.car;
|
2023-03-03 14:29:53 -08:00
|
|
|
Ok(Ctr::Bool(
|
|
|
|
|
ast.circuit(&mut |arg: &Ctr| -> bool { arg == head_ctr_ref }),
|
|
|
|
|
))
|
2023-03-02 09:43:12 -08:00
|
|
|
}
|
|
|
|
|
|
2023-05-25 23:08:44 +00:00
|
|
|
const TOGGLE_DOCSTRING: &str = "switches a boolean symbol between true or false.
|
2023-03-06 15:12:59 -08:00
|
|
|
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.";
|
2023-05-25 23:08:44 +00:00
|
|
|
fn toggle_callback(ast: &Seg, syms: &mut SymTable) -> Result<Ctr, Traceback> {
|
2023-03-02 12:15:42 -08:00
|
|
|
let var_name: String;
|
2023-03-03 14:29:53 -08:00
|
|
|
if let Ctr::Symbol(ref s) = *ast.car {
|
|
|
|
|
var_name = s.clone();
|
2023-03-02 12:15:42 -08:00
|
|
|
} else {
|
2023-05-23 22:06:11 +00:00
|
|
|
return Err(start_trace(("toggle", "input must be a symbol").into()));
|
2023-03-02 12:15:42 -08:00
|
|
|
}
|
|
|
|
|
|
2023-03-03 14:29:53 -08:00
|
|
|
let mut sym = syms
|
|
|
|
|
.remove(&var_name)
|
|
|
|
|
.expect(&format!("symbol {var_name} is not defined"));
|
2023-03-02 12:15:42 -08:00
|
|
|
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);
|
2023-05-23 22:06:11 +00:00
|
|
|
return Err(start_trace(("toggle", "can only toggle a boolean").into()));
|
2023-03-02 12:15:42 -08:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
syms.insert(var_name, sym);
|
2023-05-23 22:06:11 +00:00
|
|
|
return Err(start_trace(("toggle", "cannot toggle a function").into()));
|
2023-03-02 12:15:42 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
syms.insert(var_name, sym);
|
|
|
|
|
Ok(Ctr::None)
|
2023-03-02 09:43:12 -08:00
|
|
|
}
|
2023-03-09 17:28:17 -08:00
|
|
|
|
2023-05-25 23:08:44 +00:00
|
|
|
const BOOLCAST_DOCSTRING: &str = "takes one argument of any type.
|
2023-03-09 17:28:17 -08:00
|
|
|
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.";
|
2023-05-25 23:08:44 +00:00
|
|
|
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 {
|
2023-05-23 22:06:11 +00:00
|
|
|
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)),
|
2023-05-23 22:06:11 +00:00
|
|
|
_ => Err(start_trace(("bool", format!("cannot convert a {} to a boolean",
|
|
|
|
|
ast.car.to_type())).into())),
|
2023-03-09 17:28:17 -08:00
|
|
|
}
|
|
|
|
|
}
|
2023-05-25 23:08:44 +00:00
|
|
|
|
|
|
|
|
pub fn add_bool_lib(syms: &mut SymTable) {
|
|
|
|
|
syms.insert(
|
|
|
|
|
"and".to_string(),
|
|
|
|
|
Symbol {
|
|
|
|
|
name: String::from("and"),
|
|
|
|
|
args: Args::Infinite,
|
|
|
|
|
conditional_branches: false,
|
|
|
|
|
docs: AND_DOCSTRING.to_string(),
|
|
|
|
|
value: ValueType::Internal(Rc::new(and_callback)),
|
2024-06-12 11:13:34 -07:00
|
|
|
optimizable: true,
|
2023-05-25 23:08:44 +00:00
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
syms.insert(
|
|
|
|
|
"bool".to_string(),
|
|
|
|
|
Symbol {
|
|
|
|
|
name: String::from("bool"),
|
|
|
|
|
args: Args::Infinite,
|
|
|
|
|
conditional_branches: false,
|
|
|
|
|
docs: BOOLCAST_DOCSTRING.to_string(),
|
|
|
|
|
value: ValueType::Internal(Rc::new(boolcast_callback)),
|
2024-06-12 11:13:34 -07:00
|
|
|
optimizable: true,
|
2023-05-25 23:08:44 +00:00
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
syms.insert(
|
|
|
|
|
"or".to_string(),
|
|
|
|
|
Symbol {
|
|
|
|
|
name: String::from("or"),
|
|
|
|
|
args: Args::Infinite,
|
|
|
|
|
conditional_branches: false,
|
|
|
|
|
docs: OR_DOCSTRING.to_string(),
|
|
|
|
|
value: ValueType::Internal(Rc::new(or_callback)),
|
2024-06-12 11:13:34 -07:00
|
|
|
optimizable: true,
|
2023-05-25 23:08:44 +00:00
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
syms.insert(
|
|
|
|
|
"not".to_string(),
|
|
|
|
|
Symbol {
|
|
|
|
|
name: String::from("not"),
|
|
|
|
|
args: Args::Strict(vec![Type::Bool]),
|
|
|
|
|
conditional_branches: false,
|
|
|
|
|
docs: NOT_DOCSTRING.to_string(),
|
|
|
|
|
value: ValueType::Internal(Rc::new(not_callback)),
|
2024-06-12 11:13:34 -07:00
|
|
|
optimizable: true,
|
2023-05-25 23:08:44 +00:00
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
syms.insert(
|
|
|
|
|
"eq?".to_string(),
|
|
|
|
|
Symbol {
|
|
|
|
|
name: String::from("eq?"),
|
|
|
|
|
args: Args::Infinite,
|
|
|
|
|
conditional_branches: false,
|
|
|
|
|
docs: ISEQ_DOCSTRING.to_string(),
|
|
|
|
|
value: ValueType::Internal(Rc::new(iseq_callback)),
|
2024-06-12 11:13:34 -07:00
|
|
|
optimizable: true,
|
2023-05-25 23:08:44 +00:00
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
syms.insert(
|
|
|
|
|
"toggle".to_string(),
|
|
|
|
|
Symbol {
|
|
|
|
|
name: String::from("toggle"),
|
|
|
|
|
args: Args::Lazy(1),
|
|
|
|
|
conditional_branches: true,
|
|
|
|
|
docs: TOGGLE_DOCSTRING.to_string(),
|
|
|
|
|
value: ValueType::Internal(Rc::new(toggle_callback)),
|
|
|
|
|
..Default::default()
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|