From d6fafc171dac69038ce8064b0cdb6e5bb7daf455 Mon Sep 17 00:00:00 2001 From: Diane Sparks Date: Sun, 6 Mar 2022 10:17:37 -0800 Subject: [PATCH 1/3] Add bitwise shift functions --- src/function/builtin.rs | 4 +++- src/lib.rs | 2 ++ tests/integration.rs | 4 ++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/function/builtin.rs b/src/function/builtin.rs index fd65e8e..b4d6cab 100644 --- a/src/function/builtin.rs +++ b/src/function/builtin.rs @@ -5,7 +5,7 @@ use crate::{ value::{FloatType, IntType}, EvalexprError, Function, Value, ValueType, }; -use std::ops::{BitAnd, BitOr, BitXor, Not}; +use std::ops::{BitAnd, BitOr, BitXor, Not, Shl, Shr}; macro_rules! simple_math { ($func:ident) => { @@ -185,6 +185,8 @@ pub fn builtin_function(identifier: &str) -> Option { "bitor" => int_function!(bitor, 2), "bitxor" => int_function!(bitxor, 2), "bitnot" => int_function!(not), + "shl" => int_function!(shl, 2), + "shr" => int_function!(shr, 2), _ => None, } } diff --git a/src/lib.rs b/src/lib.rs index f931cfa..967a48c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -360,6 +360,8 @@ //! | `bitor` | 2 | Int | Computes the bitwise or 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 | +//! | `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. //! If the maximum or minimum is an integer, then an integer is returned. diff --git a/tests/integration.rs b/tests/integration.rs index a6f244c..4f0b27b 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -403,6 +403,10 @@ fn test_builtin_functions() { assert_eq!(eval("bitxor(6, 5)"), Ok(Value::Int(3))); assert_eq!(eval("bitnot(5)"), Ok(Value::Int(-6))); 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] From 284ddfd567a9fe02827ad20377ccbba250e398e6 Mon Sep 17 00:00:00 2001 From: Diane Sparks Date: Sun, 6 Mar 2022 10:20:40 -0800 Subject: [PATCH 2/3] Add comment separator for bitwise tests --- tests/integration.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/integration.rs b/tests/integration.rs index 4f0b27b..d93c235 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -395,6 +395,7 @@ fn test_builtin_functions() { Ok(Value::String(String::from("(1, 2, 3)"))) ); 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(6, 5)"), Ok(Value::Int(4))); assert_eq!(eval("bitor(5, -1)"), Ok(Value::Int(-1))); From 680ff4ca9816f8a1ca08dcdbbdf3adcdb9a94c64 Mon Sep 17 00:00:00 2001 From: Diane Sparks Date: Sun, 6 Mar 2022 10:28:44 -0800 Subject: [PATCH 3/3] Sync readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 1956bfb..d83d5f7 100644 --- a/README.md +++ b/README.md @@ -377,6 +377,8 @@ This crate offers a set of builtin functions. | `bitor` | 2 | Int | Computes the bitwise or 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 | +| `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. If the maximum or minimum is an integer, then an integer is returned.