Merge pull request #98 from FractalDiane/main
Implement bitwise shift functions
This commit is contained in:
commit
6de3a0a925
@ -377,6 +377,8 @@ This crate offers a set of builtin functions.
|
|||||||
| `bitor` | 2 | Int | Computes the bitwise or of the given integers |
|
| `bitor` | 2 | Int | Computes the bitwise or of the given integers |
|
||||||
| `bitxor` | 2 | Int | Computes the bitwise xor of the given integers |
|
| `bitxor` | 2 | Int | Computes the bitwise xor of the given integers |
|
||||||
| `bitnot` | 1 | Int | Computes the bitwise not of the given integer |
|
| `bitnot` | 1 | Int | Computes the bitwise not of the given integer |
|
||||||
|
| `shl` | 2 | Int | Computes the given integer bitwise shifted left by the other given integer |
|
||||||
|
| `shr` | 2 | Int | Computes the given integer bitwise shifted right by the other given integer |
|
||||||
|
|
||||||
The `min` and `max` functions can deal with a mixture of integer and floating point arguments.
|
The `min` and `max` functions can deal with a mixture of integer and floating point arguments.
|
||||||
If the maximum or minimum is an integer, then an integer is returned.
|
If the maximum or minimum is an integer, then an integer is returned.
|
||||||
|
@ -5,7 +5,7 @@ use crate::{
|
|||||||
value::{FloatType, IntType},
|
value::{FloatType, IntType},
|
||||||
EvalexprError, Function, Value, ValueType,
|
EvalexprError, Function, Value, ValueType,
|
||||||
};
|
};
|
||||||
use std::ops::{BitAnd, BitOr, BitXor, Not};
|
use std::ops::{BitAnd, BitOr, BitXor, Not, Shl, Shr};
|
||||||
|
|
||||||
macro_rules! simple_math {
|
macro_rules! simple_math {
|
||||||
($func:ident) => {
|
($func:ident) => {
|
||||||
@ -185,6 +185,8 @@ pub fn builtin_function(identifier: &str) -> Option<Function> {
|
|||||||
"bitor" => int_function!(bitor, 2),
|
"bitor" => int_function!(bitor, 2),
|
||||||
"bitxor" => int_function!(bitxor, 2),
|
"bitxor" => int_function!(bitxor, 2),
|
||||||
"bitnot" => int_function!(not),
|
"bitnot" => int_function!(not),
|
||||||
|
"shl" => int_function!(shl, 2),
|
||||||
|
"shr" => int_function!(shr, 2),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -360,6 +360,8 @@
|
|||||||
//! | `bitor` | 2 | Int | Computes the bitwise or of the given integers |
|
//! | `bitor` | 2 | Int | Computes the bitwise or of the given integers |
|
||||||
//! | `bitxor` | 2 | Int | Computes the bitwise xor of the given integers |
|
//! | `bitxor` | 2 | Int | Computes the bitwise xor of the given integers |
|
||||||
//! | `bitnot` | 1 | Int | Computes the bitwise not of the given integer |
|
//! | `bitnot` | 1 | Int | Computes the bitwise not of the given integer |
|
||||||
|
//! | `shl` | 2 | Int | Computes the given integer bitwise shifted left by the other given integer |
|
||||||
|
//! | `shr` | 2 | Int | Computes the given integer bitwise shifted right by the other given integer |
|
||||||
//!
|
//!
|
||||||
//! The `min` and `max` functions can deal with a mixture of integer and floating point arguments.
|
//! The `min` and `max` functions can deal with a mixture of integer and floating point arguments.
|
||||||
//! If the maximum or minimum is an integer, then an integer is returned.
|
//! If the maximum or minimum is an integer, then an integer is returned.
|
||||||
|
@ -395,6 +395,7 @@ fn test_builtin_functions() {
|
|||||||
Ok(Value::String(String::from("(1, 2, 3)")))
|
Ok(Value::String(String::from("(1, 2, 3)")))
|
||||||
);
|
);
|
||||||
assert_eq!(eval("str::from()"), Ok(Value::String(String::from("()"))));
|
assert_eq!(eval("str::from()"), Ok(Value::String(String::from("()"))));
|
||||||
|
// Bitwise
|
||||||
assert_eq!(eval("bitand(5, -1)"), Ok(Value::Int(5)));
|
assert_eq!(eval("bitand(5, -1)"), Ok(Value::Int(5)));
|
||||||
assert_eq!(eval("bitand(6, 5)"), Ok(Value::Int(4)));
|
assert_eq!(eval("bitand(6, 5)"), Ok(Value::Int(4)));
|
||||||
assert_eq!(eval("bitor(5, -1)"), Ok(Value::Int(-1)));
|
assert_eq!(eval("bitor(5, -1)"), Ok(Value::Int(-1)));
|
||||||
@ -403,6 +404,10 @@ fn test_builtin_functions() {
|
|||||||
assert_eq!(eval("bitxor(6, 5)"), Ok(Value::Int(3)));
|
assert_eq!(eval("bitxor(6, 5)"), Ok(Value::Int(3)));
|
||||||
assert_eq!(eval("bitnot(5)"), Ok(Value::Int(-6)));
|
assert_eq!(eval("bitnot(5)"), Ok(Value::Int(-6)));
|
||||||
assert_eq!(eval("bitnot(-1)"), Ok(Value::Int(0)));
|
assert_eq!(eval("bitnot(-1)"), Ok(Value::Int(0)));
|
||||||
|
assert_eq!(eval("shl(5, 1)"), Ok(Value::Int(10)));
|
||||||
|
assert_eq!(eval("shl(-6, 5)"), Ok(Value::Int(-192)));
|
||||||
|
assert_eq!(eval("shr(5, 1)"), Ok(Value::Int(2)));
|
||||||
|
assert_eq!(eval("shr(-6, 5)"), Ok(Value::Int(-1)));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
Reference in New Issue
Block a user