-
Notifications
You must be signed in to change notification settings - Fork 98
Getting Started
Coding Seb edited this page Jan 22, 2021
·
10 revisions
Install the following nuget package :
Install-Package CodingSeb.ExpressionEvaluator
or copy the CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs file in your project :
using CodingSeb.ExpressionEvaluator;
//...
string expression;
//...
ExpressionEvaluator evaluator = new ExpressionEvaluator();
Console.WriteLine(expression);
Console.WriteLine(evaluator.Evaluate(expression));
Results with some expressions :
Live examples here
1+1
2
2 + 3 * 2
8
(2 + 3) * 2
10
Pi
3.14159265358979
Pow(2, 4)
16
Sqrt(2) / 3
0.471404520791032
"Hello" + " " + "World"
Hello World
Max(1+1, 2+3, 2*6, Pow(2, 3))
12
Array(2, $"Test { 2 + 2 } U", true).Length
3
Array(2, $"Test { 2 + 2 } U", true)[1]
Test 4 U
Array(2, $"Test { 2 + 2 } U", true)[2] ? "yes" : "no"
yes
false ? "yes" : "no"
no
"Hello\nWorld"
Hello
World
@"Hello\nWorld"
Hello\nWorld
Regex.Match("Test 34 Hello/-World", @"\d+").Value
34
int.Parse(Regex.Match("Test 34 Hello/-World", @"\d+").Value) + 2
36
3 / 2
1
3d / 2d
1.5
(float)3 / (float)2
1.5
// use new as a function
new(Random).Next(1,10)
4 // or a random value between 1 and 9
// or as a keyword
new Regex(@"\w*[n]\w*").Match("Which word contains the desired letter ?").Value
contains
List("Test", "Hello", "Bye", "How are you?").Find(t => t.Length < 4)
Bye
new List<string>(){ "Test", "Hello", "Bye", "How are you?" }.Find(t => t.Length < 4);
Bye
Enumerable.Range(1,4).Cast().Sum(x =>(int)x)
10
Enumerable.Repeat(3,6).Cast().ToList().Count
6
Enumerable.Repeat(3,6).Cast().ToList()[4]
3
((x, y) => x * y)(4, 2)
8
"Hello"[2] == 'l'
true
"Hello,Test,What".Split(ArrayOfType(typeof(char), ',')).Length
3
"Hello,Test,What".Split(ArrayOfType(typeof(char), ','))[0]
Hello
"Hello,Test,What".Split(ArrayOfType(typeof(char), ','))[1]
Test
"Hello,Test,What".Split(ArrayOfType(typeof(char), ','))[2]
What
"Hello,Test,What".Split(new char[] {','}).Length
3
"Hello,Test,What".Split(new char[] {','})[0]
Hello
"Hello,Test,What".Split(new char[] {','})[1]
Test
"Hello,Test,What".Split(new char[] {','})[2]
What
new System.Xml.XmlDocument().FluidLoadXml("<root><element id='MyElement'>Xml Content</element></root>").SelectSingleNode("//element[@id='MyElement']").InnerXml
Xml Content
// if System.Net Assembly is referenced in your project.
$"https://www.google.ch/search?q={System.Net.WebUtility.UrlEncode("request with url encode() ?")}"
https://www.google.ch/search?q=request+with+url+encode()+%3F
using CodingSeb.ExpressionEvaluator;
//...
string script;
//...
ExpressionEvaluator evaluator = new ExpressionEvaluator();
Console.WriteLine("--------------------------------------------");
Console.WriteLine(script);
Console.WriteLine("---------------- Result --------------------");
Console.WriteLine(evaluator.ScriptEvaluate(script));
Results with some scripts :
Live examples here
--------------------------------------------
x = 0;
result = "";
while(x < 5)
{
result += $"{x},";
x++;
}
result.Remove(result.Length - 1);
---------------- Result --------------------
0,1,2,3,4
--------------------------------------------
result = "";
for(x = 0; x < 5;x++)
{
result += $"{x},";
}
result.Remove(result.Length - 1);
---------------- Result --------------------
0,1,2,3,4
--------------------------------------------
x = 0;
y = 1;
result = 0;
if(y != 0)
{
return 1;
}
else if(x == 0)
{
return 2;
}
else if(x < 0)
{
return 3;
}
else
{
return 4;
}
---------------- Result --------------------
1
--------------------------------------------
x = 0;
y = 0;
result = 0;
if(y != 0)
{
return 1;
}
else if(x == 0)
{
return 2;
}
else if(x < 0)
{
return 3;
}
else
{
return 4;
}
---------------- Result --------------------
2
--------------------------------------------
x = 5;
y = 0;
result = 0;
if(y != 0)
{
return 1;
}
else if(x == 0)
{
return 2;
}
else if(x < 0)
{
return 3;
}
else
{
return 4;
}
---------------- Result --------------------
4
--------------------------------------------
Add = (x, y) => x + y;
return Add(3, 4);
---------------- Result --------------------
7
--------------------------------------------
Add = (x, y) =>
{
var result = x + y;
return result;
};
return Add(3, 4);
---------------- Result --------------------
7
To see more scripts examples see scripts uses for tests in sub directories CodingSeb.ExpressionEvaluator.Tests/Resources
- Getting Started
- Variables and Functions
- Operators and Keywords
- C# Types Management
- ExpandoObject
- Code Comments Management
- Advanced Customization and Hacking
- Caching
-
Options
- CultureInfoForNumberParsing
- OptionCaseSensitiveEvaluationActive
- OptionVariablesPersistenceCustomComparer
- OptionFluidPrefixingActive
- OptionForceIntegerNumbersEvaluationsAsDoubleByDefault
- OptionNumberParsingDecimalSeparator
- OptionNumberParsingThousandSeparator
- OptionFunctionArgumentsSeparator
- OptionInitializersSeparator
- OptionInlineNamespacesEvaluationRule
- OptionNewFunctionEvaluationActive
- OptionNewKeywordEvaluationActive
- OptionStaticMethodsCallActive
- OptionStaticProperiesGetActive
- OptionInstanceMethodsCallActive
- OptionInstanceProperiesGetActive
- OptionIndexingActive
- OptionStringEvaluationActive
- OptionCharEvaluationActive
- OptionEvaluateFunctionActive
- OptionVariableAssignationActive
- OptionPropertyOrFieldSetActive
- OptionIndexingAssignationActive
- OptionScriptEvaluateFunctionActive
- OptionOnNoReturnKeywordFoundInScriptAction
- OptionScriptNeedSemicolonAtTheEndOfLastExpression
- OptionAllowNonPublicMembersAccess
- Todo List