add some boolean operations, tests for
Signed-off-by: Ava Hahn <ava@aidanis.online>
This commit is contained in:
parent
c1d83a6285
commit
28e158f110
5 changed files with 292 additions and 4 deletions
61
src/stl.rs
61
src/stl.rs
|
|
@ -23,6 +23,7 @@ use std::rc::Rc;
|
|||
|
||||
pub mod append;
|
||||
pub mod control;
|
||||
pub mod boolean;
|
||||
//pub mod str;
|
||||
|
||||
/// static_stdlib
|
||||
|
|
@ -79,6 +80,66 @@ pub fn static_stdlib(syms: &mut SymTable) -> Result<(), String> {
|
|||
},
|
||||
);
|
||||
|
||||
syms.insert(
|
||||
"and".to_string(),
|
||||
Symbol {
|
||||
name: String::from("and"),
|
||||
args: Args::Infinite,
|
||||
conditional_branches: false,
|
||||
value: ValueType::Internal(Rc::new(boolean::bool_and_callback)),
|
||||
}
|
||||
);
|
||||
|
||||
syms.insert(
|
||||
"or".to_string(),
|
||||
Symbol {
|
||||
name: String::from("or"),
|
||||
args: Args::Infinite,
|
||||
conditional_branches: false,
|
||||
value: ValueType::Internal(Rc::new(boolean::bool_or_callback)),
|
||||
}
|
||||
);
|
||||
|
||||
syms.insert(
|
||||
"xor".to_string(),
|
||||
Symbol {
|
||||
name: String::from("xor"),
|
||||
args: Args::Infinite,
|
||||
conditional_branches: false,
|
||||
value: ValueType::Internal(Rc::new(boolean::bool_xor_callback)),
|
||||
}
|
||||
);
|
||||
|
||||
syms.insert(
|
||||
"not".to_string(),
|
||||
Symbol {
|
||||
name: String::from("not"),
|
||||
args: Args::Strict(vec![Type::Bool]),
|
||||
conditional_branches: false,
|
||||
value: ValueType::Internal(Rc::new(boolean::bool_not_callback)),
|
||||
}
|
||||
);
|
||||
|
||||
syms.insert(
|
||||
"eq?".to_string(),
|
||||
Symbol {
|
||||
name: String::from("eq?"),
|
||||
args: Args::Infinite,
|
||||
conditional_branches: false,
|
||||
value: ValueType::Internal(Rc::new(boolean::bool_iseq_callback)),
|
||||
}
|
||||
);
|
||||
|
||||
syms.insert(
|
||||
"toggle".to_string(),
|
||||
Symbol {
|
||||
name: String::from("toggle"),
|
||||
args: Args::Lazy(1),
|
||||
conditional_branches: true,
|
||||
value: ValueType::Internal(Rc::new(boolean::bool_toggle_callback)),
|
||||
}
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
|||
76
src/stl/boolean.rs
Normal file
76
src/stl/boolean.rs
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
/* 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::sym::SymTable;
|
||||
|
||||
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!()
|
||||
}
|
||||
|
||||
pub fn bool_toggle_callback(_ast: &Seg, _syms: &mut SymTable) -> Result<Ctr, String> {
|
||||
todo!()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue