Skip to content

Commit 4323a47

Browse files
author
Sébastien Geiser
committed
Add Bitwise Complement Operator with ~
Version unstable
1 parent 682addc commit 4323a47

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,16 @@ public void TypeTesting(string expression, Type type)
391391
[TestCase("3 --(2 *+(5 - 3 - +(-Abs(-5) - 6)))", TestOf = typeof(double), ExpectedResult = 29, Category = "ParenthesisPriority")]
392392
#endregion
393393

394+
#region BitwiseComplement
395+
[TestCase("~-10", TestOf = typeof(int), ExpectedResult = 9, Category = "BitwiseComplement")]
396+
[TestCase("~-2", TestOf = typeof(int), ExpectedResult = 1, Category = "BitwiseComplement")]
397+
[TestCase("~-1", TestOf = typeof(int), ExpectedResult = 0, Category = "BitwiseComplement")]
398+
[TestCase("~0", TestOf = typeof(int), ExpectedResult = -1, Category = "BitwiseComplement")]
399+
[TestCase("~1", TestOf = typeof(int), ExpectedResult = -2, Category = "BitwiseComplement")]
400+
[TestCase("~2", TestOf = typeof(int), ExpectedResult = -3, Category = "BitwiseComplement")]
401+
[TestCase("~10", TestOf = typeof(int), ExpectedResult = -11, Category = "BitwiseComplement")]
402+
#endregion
403+
394404
#region SimpleModulo
395405
[TestCase("-4 % 2", TestOf = typeof(int), ExpectedResult = 0, Category = "SimpleModulo")]
396406
[TestCase("-3 % 2", TestOf = typeof(int), ExpectedResult = -1, Category = "SimpleModulo")]

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/******************************************************************************************************
22
Title : ExpressionEvaluator (https://github.com/codingseb/ExpressionEvaluator)
3-
Version : 1.3.3.0
3+
Version : 1.3.3.1
44
(if last digit (the forth) is not a zero, the version is an intermediate version and can be unstable)
55
66
Author : Coding Seb
@@ -97,6 +97,7 @@ private enum ExpressionOperator
9797
Is,
9898
NotEqual,
9999
LogicalNegation,
100+
BitwiseComplement,
100101
ConditionalAnd,
101102
ConditionalOr,
102103
LogicalAnd,
@@ -216,6 +217,7 @@ private enum TryBlockEvaluatedState
216217
{ "&&", ExpressionOperator.ConditionalAnd },
217218
{ "||", ExpressionOperator.ConditionalOr },
218219
{ "!", ExpressionOperator.LogicalNegation },
220+
{ "~", ExpressionOperator.BitwiseComplement },
219221
{ "&", ExpressionOperator.LogicalAnd },
220222
{ "|", ExpressionOperator.LogicalOr },
221223
{ "^", ExpressionOperator.LogicalXor },
@@ -231,6 +233,7 @@ private enum TryBlockEvaluatedState
231233
private static Dictionary<ExpressionOperator, bool> rightOperandOnlyOperatorsEvaluationDictionary = new Dictionary<ExpressionOperator, bool>()
232234
{
233235
{ExpressionOperator.LogicalNegation, true },
236+
{ExpressionOperator.BitwiseComplement, true },
234237
{ExpressionOperator.UnaryPlus, true },
235238
{ExpressionOperator.UnaryMinus, true }
236239
};
@@ -262,6 +265,7 @@ private enum TryBlockEvaluatedState
262265
{ExpressionOperator.UnaryPlus, (dynamic left, dynamic right) => +right },
263266
{ExpressionOperator.UnaryMinus, (dynamic left, dynamic right) => -right },
264267
{ExpressionOperator.LogicalNegation, (dynamic left, dynamic right) => !right },
268+
{ExpressionOperator.BitwiseComplement, (dynamic left, dynamic right) => ~right },
265269
{ExpressionOperator.Cast, (dynamic left, dynamic right) => ChangeType(right, left) },
266270
},
267271
new Dictionary<ExpressionOperator, Func<dynamic, dynamic, object>>()

0 commit comments

Comments
 (0)