You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+236-9Lines changed: 236 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,8 @@
2
2
3
3
A Simple Math and Pseudo C# Expression Evaluator in One C# File.
4
4
5
+
And now can execute small C# like scripts
6
+
5
7
It is largely based on and inspired by the following resources [this post on stackoverflow](http://stackoverflow.com/questions/333737/evaluating-string-342-yield-int-18/333749), [NCalc](https://ncalc.codeplex.com/) and [C# Operators](https://msdn.microsoft.com/en-us/library/6a71f45d.aspx)
6
8
7
9
## Status
@@ -23,10 +25,17 @@ It is largely based on and inspired by the following resources [this post on st
23
25
* You can call Methods and/or Properties on your own classes (just pass a object as custom variables)
24
26
*[C# primary types](#primary-types)
25
27
* Use strings as in C# (@"", $"", $@"" available)
26
-
*Linq, generics and lambda expressions
28
+
*Lambda expressions
27
29
* Classes like File, Directory, Regex, List ... available ([You can extend the list of Namespaces](#namespaces))
28
30
* Create instance with [new(MyClassName, constructorArgs)](#standard-functions) or [new MyClassName(constructorArgs)](#operators)
29
31
*[Call void methods with fluid prefix convention to chain operations](#go-fluid-with-a-simple-methods-prefixing-convention)
32
+
* Manage now assignation operators like =, +=, -=, *= ... (On variables and sub properties)
33
+
* Manage now postfix operators ++ and -- (On variables and sub properties)
34
+
35
+
## And with [ScriptEvaluate](#scripts)
36
+
* Small C# like script evaluation (Multi expressions separated by ;)
37
+
* Some conditional and loop blocks [keywords](#script-keywords) (if, while, for ...)
Console.WriteLine("---------------- Result --------------------");
166
+
Console.WriteLine(evaluator.Evaluate(script));
167
+
```
168
+
Results with some scripts :
169
+
170
+
```
171
+
--------------------------------------------
172
+
x = 0;
173
+
result = "";
174
+
175
+
while(x < 5)
176
+
{
177
+
result += $"{x},";
178
+
x++;
179
+
}
180
+
181
+
result.Remove(result.Length - 1);
182
+
---------------- Result --------------------
183
+
0,1,2,3,4
184
+
185
+
--------------------------------------------
186
+
result = "";
187
+
188
+
for(x = 0; x < 5;x++)
189
+
{
190
+
result += $"{x},";
191
+
}
192
+
193
+
result.Remove(result.Length - 1);
194
+
---------------- Result --------------------
195
+
0,1,2,3,4
196
+
197
+
--------------------------------------------
198
+
x = 0;
199
+
y = 1;
200
+
result = 0;
201
+
202
+
if(y != 0)
203
+
{
204
+
return 1;
205
+
}
206
+
else if(x == 0)
207
+
{
208
+
return 2;
209
+
}
210
+
else if(x < 0)
211
+
{
212
+
return 3;
213
+
}
214
+
else
215
+
{
216
+
return 4;
217
+
}
218
+
---------------- Result --------------------
219
+
1
220
+
221
+
--------------------------------------------
222
+
x = 0;
223
+
y = 0;
224
+
result = 0;
225
+
226
+
if(y != 0)
227
+
{
228
+
return 1;
229
+
}
230
+
else if(x == 0)
231
+
{
232
+
return 2;
233
+
}
234
+
else if(x < 0)
235
+
{
236
+
return 3;
237
+
}
238
+
else
239
+
{
240
+
return 4;
241
+
}
242
+
---------------- Result --------------------
243
+
2
244
+
245
+
--------------------------------------------
246
+
x = 5;
247
+
y = 0;
248
+
result = 0;
249
+
250
+
if(y != 0)
251
+
{
252
+
return 1;
253
+
}
254
+
else if(x == 0)
255
+
{
256
+
return 2;
257
+
}
258
+
else if(x < 0)
259
+
{
260
+
return 3;
261
+
}
262
+
else
263
+
{
264
+
return 4;
265
+
}
266
+
---------------- Result --------------------
267
+
4
268
+
```
269
+
270
+
To see more scripts examples see scripts uses for tests in sub directories [CodingSeb.ExpressionEvaluator.Tests/Resources](./CodingSeb.ExpressionEvaluator.Tests/Resources)
271
+
144
272
## Standard constants (variables)
145
273
146
274
The evaluation of variables name is case insensitive so you can write it as you want.
@@ -206,7 +334,6 @@ The following functions are internally defined. (Most of these are [System.Math
206
334
|**[Exp](https://msdn.microsoft.com/en-us/library/system.math.exp(v=vs.110).aspx)**(double d)|Return a double value that is e raised to the specified d power|`Exp(3d)`|`20.0855369231877d`|
207
335
|**[Floor](https://msdn.microsoft.com/en-us/library/e0b5f0xb(v=vs.110).aspx)**(double d)|Return a double value that is the largest integer less than or equal to the specified d argument|`Floor(4.23d)`|`4d`|
208
336
|**[IEEERemainder](https://msdn.microsoft.com/en-us/library/system.math.ieeeremainder(v=vs.110).aspx)**(double x, double y)|Return a double value that is the remainder resulting from the division of x by y|`IEEERemainder(9, 8)`|`1d`|
209
-
|**if**(boolcondition, objectyes, objectno)|Returntheyesobjectvalueifconditionistrue.<br/>Returnthenoobjectifconditionisfalse|`if(1>2, "It is true", "It is false")`|`"It is false"`|
|**[Log](https://msdn.microsoft.com/en-us/library/system.math.log(v=vs.110).aspx)**(double a, double base)|Return a double value that is the logarithm of a in the specified base|`Log(64d, 2d)`|`6d`|
@@ -224,6 +351,9 @@ The following functions are internally defined. (Most of these are [System.Math
224
351
|**[Tanh](https://msdn.microsoft.com/en-us/library/system.math.tanh(v=vs.110).aspx)**(double angle)|Return a double value that is the hyperbolic tangent of the specified angle in radian|`Tanh(2d)`|`0.964027580075817d`|
225
352
|**[Truncate](https://msdn.microsoft.com/en-us/library/c2eabd70(v=vs.110).aspx)**(double d)|Return a double value that is the integer part of the specified d value|`Truncate(2.45d)`|`2d`|
|Primary|[x++](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/increment-operator)|Supported **Warning change the state of the postfixed element**|
470
+
|Primary|[x--](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/decrement-operator)|Supported **Warning change the state of the postfixed element**|
341
471
|Primary|[new](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/new-operator)|Supported you can also use [new() function](#standard-functions)|
@@ -380,7 +510,88 @@ Here is a list of which operators are supported in ExpressionEvaluator or not
380
510
|Conditional|[t ? x : y](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator)|Supported equivalent to the [if() function](#standard-functions)|
|[=](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/assignment-operator)|Supported (Can be use to declare a new variable that will be injected in the Variables dictionary)|
|Jump|[break](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/break)|Supported in do, for and while blocks|
578
+
|Jump|[continue](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/continue)|Supported in do, for and while blocks|
579
+
|Jump|[goto](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/goto)|Not supported (But if you looked after it -> Booo !!! Bad code)|
* [CS-Script](https://github.com/oleg-shilo/cs-script) Best alternative (I use it some times) -> Real C# scripts better than ExpressionEvaluator (But everything is compiled. Read the doc. Execution is faster but compilation can make it very slow. And if not done the right way, it can lead to memory leaks)
0 commit comments