Skip to content

Commit 3d0907f

Browse files
committed
Compile function once, during the registration.
1 parent 9d67c08 commit 3d0907f

File tree

4 files changed

+22
-12
lines changed

4 files changed

+22
-12
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
Sprache.Calc
22
============
33

4+
[![Appveyor build status](https://ci.appveyor.com/api/projects/status/qswh0wj2uv3w502v?svg=true)](https://ci.appveyor.com/project/yallie/sprache-calc)
5+
[![Tests](https://img.shields.io/appveyor/tests/yallie/sprache-calc.svg)](https://ci.appveyor.com/project/yallie/sprache-calc/build/tests)
6+
47
This library provides easy to use extensible expression evaluator based on [LinqyCalculator sample](https://github.com/sprache/Sprache/blob/master/samples/LinqyCalculator/ExpressionParser.cs).
58
The evaluator supports arithmetic operations, custom functions and parameters. It takes string
69
representation of an expression and converts it to a structured LINQ expression instance

Sprache.Calc.Tests/XtensibleCalculatorFacts.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,9 @@ protected override object[] LambdaArguments
3232
public void UserFunctionCanWork()
3333
{
3434
var calc = (XtensibleCalculator) CreateCalculator();
35-
3635
calc.RegisterFunction("User", (aU,bU,cU) => calc.ParseExpression("Min(a,b,c)", new Dictionary<string,double>(){{"a",aU},{"b",bU},{"c",cU}}).Compile().Invoke());
3736

38-
3937
var func = calc.ParseExpression("User(a,b,c)", new Dictionary<string,double>(){{"a",1},{"b",2},{"c",3}}).Compile();
40-
4138
Assert.Equal(1, func());
4239
}
4340

@@ -48,7 +45,7 @@ public void StringUserFunctionCanWork()
4845
calc.RegisterFunction("UserB", "Min(a,b,c)", "a", "b", "c");
4946
var func = calc.ParseExpression("UserB(a,b,c)",
5047
new Dictionary<string, double>() {{"a", 1}, {"b", 2}, {"c", 3}}).Compile();
51-
48+
5249
Assert.Equal(1, func());
5350
}
5451

Sprache.Calc/Sprache.Calc.csproj

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard1.6</TargetFramework>
4+
<TargetFrameworks>netstandard1.6;netstandard2.0</TargetFrameworks>
55
<PackageVersion></PackageVersion>
66
<Title>Sprache Calculator</Title>
77
<Authors>Alexey Yakovlev, Andrey Leskov</Authors>
@@ -13,11 +13,14 @@
1313
<RepositoryUrl>https://github.com/yallie/Sprache.Calc</RepositoryUrl>
1414
<RepositoryType>git</RepositoryType>
1515
<PackageTags>sprache expression expressions evaluator calculator parser parsers linq monad</PackageTags>
16-
<PackageReleaseNotes>Sprache toolkit grammar inheritance sample. Updated for .Net Standart 1.6 and added functionality to add user-defined functions from strings. For more details, see https://github.com/yallie/Sprache.Calc</PackageReleaseNotes>
16+
<PackageReleaseNotes>Sprache toolkit grammar inheritance sample. Updated for .Net Standard 1.6 and added functionality to add user-defined functions from strings. For more details, see https://github.com/yallie/Sprache.Calc</PackageReleaseNotes>
1717
</PropertyGroup>
1818

1919
<ItemGroup>
2020
<PackageReference Include="Sprache" Version="2.2.0" />
21+
</ItemGroup>
22+
23+
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.6' ">
2124
<PackageReference Include="System.Reflection.TypeExtensions" Version="4.5.1" />
2225
</ItemGroup>
2326

Sprache.Calc/XtensibleCalculator.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,18 +151,25 @@ public XtensibleCalculator RegisterFunction(string name, Func<double, double, do
151151
CustomFuctions[MangleName(name, 5)] = x => function(x[0], x[1], x[2], x[3], x[4]);
152152
return this;
153153
}
154-
154+
155155
public XtensibleCalculator RegisterFunction(string name, string functionExpression, params string[] parameters)
156156
{
157+
// Func<Dictionary, double>
158+
var compiledFunction = ParseFunction(functionExpression).Compile();
159+
157160
//syntactic sugar: ParseExpression("a + b / c", "a","b","c")
158161
CustomFuctions[MangleName(name, parameters.Length)] = x =>
159162
{
160-
var parametersDictionary = new Dictionary<string,double>();
161-
for(int paramSeq = 0;paramSeq < parameters.Length; paramSeq++)
162-
parametersDictionary.Add(parameters[paramSeq],x[paramSeq]);
163-
164-
return this.ParseExpression(functionExpression,parametersDictionary).Compile().Invoke();
163+
// convert double[] to Dictionary
164+
var parametersDictionary = new Dictionary<string, double>();
165+
for (int paramSeq = 0; paramSeq < parameters.Length; paramSeq++)
166+
{
167+
parametersDictionary.Add(parameters[paramSeq], x[paramSeq]);
168+
}
169+
170+
return compiledFunction(parametersDictionary);
165171
};
172+
166173
return this;
167174
}
168175
}

0 commit comments

Comments
 (0)