Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,7 @@ Chap has 49 builtin function(version 2.0.0) (less than Java's keywords)
| not_equal, neq | any, any | bool | true if 1st and 2nd are not equal and false if they are |
| and | bool, bool | bool | and logical gate |
| or | bool, bool | bool | or logical gate |
| xor | bool, bool | bool | xor logical gate |
| not | bool | bool | not logical gate |
| greater_than, gt | num, num | bool | true if 1st param is bigger than 2nd param 3,2 -> true |
| less_than, lt | num, num | bool | true if 1st param is less than 2nd param 3,2 -> false |
Expand Down
1 change: 1 addition & 0 deletions src/builtin_function/bools/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ pub mod less_than_equal;
pub mod not;
pub mod not_equal;
pub mod or;
pub mod xor;
22 changes: 22 additions & 0 deletions src/builtin_function/bools/xor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use crate::builtin_function::utils::{param_to_datatype, returns};
use crate::common::data_type::DataType;
use crate::common::errors::{ChapError, Result};
use crate::{common::executable::ExecutableLine, runtime::Runtime};

pub fn xor(runtime: &mut Runtime, executable: &ExecutableLine) -> Result<()> {
let mut result = false;
for param in &executable.params {
let dt = param_to_datatype(runtime, Some(param), executable.line_number)?;
match dt {
DataType::Bool(x) => result ^= *x,
_ => {
return Err(ChapError::runtime_with_msg(
executable.line_number,
"xor function needs bool params".to_string(),
));
}
}
}

returns(runtime, executable, DataType::Bool(result))
}
1 change: 1 addition & 0 deletions src/builtin_function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ pub fn function_match(function_name: &str) -> Option<BuiltinFunction> {
"notequal" | "neq" => Some(bools::not_equal::not_equal),
"and" => Some(bools::and::and),
"or" => Some(bools::or::or),
"xor" => Some(bools::xor::xor),
"not" => Some(bools::not::not),
"gt" | "greaterthan" => Some(bools::greater_than::greater_than),
"gte" | "greaterthanequal" => Some(bools::greater_than_equal::greater_than_equal),
Expand Down