Skip to content

Commit 9a5cf6d

Browse files
committed
Merge branch 'develop' of github.com:JanDotNet/ThinkSharp.FormulaParser into develop
2 parents bd1fd21 + 3ffb18d commit 9a5cf6d

1 file changed

Lines changed: 71 additions & 63 deletions

File tree

README.md

Lines changed: 71 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -29,80 +29,88 @@ ThinkSharp.FormulaParser can be installed via [Nuget](https://www.nuget.org/pack
2929

3030
### Evaluating formulas
3131

32-
// The simples way to create a formula parser is the static method 'Create'.
33-
var parser = FormulaParser.Create();
32+
```csharp
33+
// The simples way to create a formula parser is the static method 'Create'.
34+
var parser = FormulaParser.Create();
3435

35-
// Parsing a simple mathematical formula
36-
var result1 = parser.Evaluate("1+1").Value; // result1 = 2.0
36+
// Parsing a simple mathematical formula
37+
var result1 = parser.Evaluate("1+1").Value; // result1 = 2.0
3738
38-
// Usage of variables
39-
var variables = new Dictionary<string, double> { ["x"] = 2.0 };
40-
var result2 = parser.Evaluate("1+x", variables).Value; // result2 = 3.0
39+
// Usage of variables
40+
var variables = new Dictionary<string, double> { ["x"] = 2.0 };
41+
var result2 = parser.Evaluate("1+x", variables).Value; // result2 = 3.0
4142
42-
// Usage of functions
43-
var result3 = parser.Evaluate("1+min(3,4)").Value; // result2 = 4.0
43+
// Usage of functions
44+
var result3 = parser.Evaluate("1+min(3,4)").Value; // result2 = 4.0
4445
45-
// Handle errors
46-
var parsingResult = parser.Evaluate("2*?");
47-
if (!parsingResult.Success) Console.WriteLine(parsingResult.Error); // "column 2: token recognition error at: '?'"
46+
// Handle errors
47+
var parsingResult = parser.Evaluate("2*?");
48+
if (!parsingResult.Success)
49+
{
50+
Console.WriteLine(parsingResult.Error); // "column 2: token recognition error at: '?'"
51+
}
52+
```
4853

4954
#### Creating and evaluating a parsing tree
5055

51-
var parser = FormulaParser.Create();
52-
var variables = new Dictionary<string, double> { ["x"] = 2.0 };
53-
54-
// Parsing a simple mathematical formula
55-
var node1 = parser.Parse("1+x", variables).Value;
56-
// |FormulaNode("1+x")
57-
// |- Child: BinaryOperatorNode(+)
58-
// |- LeftNode: NumericNode(1.0)
59-
// |- RightNode: VariableNode("x")
60-
var result1 = parser.Evaluate(node1, variables).Value; // result = 3.0
61-
62-
63-
var node2 = parser.Parse("pi * sqrt(3*x)", variables).Value;
64-
// |FormulaNode("pi * sqrt(3*x)")
65-
// |- Child: BinaryOperatorNode(*)
66-
// |- LeftNode: ConstantNode("pi")
67-
// |- RightNode: FunctionNode("sqrt")
68-
// |- Parameters: [BinaryOperationNode(*)]
69-
// |- LeftNode: Numeric(3.0)
70-
// |- RightNode: VariableNode("x")
71-
var result2 = parser.Evaluate(node2, variables).Value; // result = 7.695...
72-
56+
```csharp
57+
var parser = FormulaParser.Create();
58+
var variables = new Dictionary<string, double> { ["x"] = 2.0 };
59+
60+
// Parsing a simple mathematical formula
61+
var node1 = parser.Parse("1+x", variables).Value;
62+
// |FormulaNode("1+x")
63+
// |- Child: BinaryOperatorNode(+)
64+
// |- LeftNode: NumericNode(1.0)
65+
// |- RightNode: VariableNode("x")
66+
var result1 = parser.Evaluate(node1, variables).Value; // result = 3.0
67+
68+
69+
var node2 = parser.Parse("pi * sqrt(3*x)", variables).Value;
70+
// |FormulaNode("pi * sqrt(3*x)")
71+
// |- Child: BinaryOperatorNode(*)
72+
// |- LeftNode: ConstantNode("pi")
73+
// |- RightNode: FunctionNode("sqrt")
74+
// |- Parameters: [BinaryOperationNode(*)]
75+
// |- LeftNode: Numeric(3.0)
76+
// |- RightNode: VariableNode("x")
77+
var result2 = parser.Evaluate(node2, variables).Value; // result = 7.695...
78+
```
79+
7380
#### Configure custom functions / constants
7481

75-
var parser = FormulaParser
76-
.CreateBuilder()
77-
.ConfigureConstats(constants =>
78-
{
79-
// constants.RemoveAll() for removing all default constants
80-
// constants.Remove("pi") for removing constants by name
82+
```csharp
83+
var parser = FormulaParser
84+
.CreateBuilder()
85+
.ConfigureConstats(constants =>
86+
{
87+
// constants.RemoveAll() for removing all default constants
88+
// constants.Remove("pi") for removing constants by name
8189
82-
constants.Add("h", 6.62607015e-34);
83-
})
84-
.ConfigureFunctions(functions =>
85-
{
86-
// functions.RemoveAll() for removing all default functions
87-
// functions.Remove("sum") for removing function by name
88-
89-
// define function with 2 to n number of parameters (typeof(nums) = double[])
90-
functions.Add("product", nums => nums.Aggregate((p1, p2) => p1 * p2));
91-
92-
// define functions with specified number of parameters (1-5 parameters are supported)
93-
functions.Add("celsiusToFarenheit", celsius => celsius * 1.8 + 32);
94-
functions.Add("fahrenheitToCelsius", fahrenheit => (fahrenheit - 32) * 5 / 9);
95-
functions.Add("p1_plus_p2_plus_p3", (p1, p2, p3) => p1 + p2 + p3);
96-
}).Build();
97-
98-
var poolTemperatureInCelsius = parser.Evaluate("celsiusToFarenheit(fahrenheitToCelsius(30))").Value; // poolTemperatureInCelsius = 30
99-
var result2 = parser.Evaluate("product(2, 2, 2, 2, 2, 2, 2)").Value; // result2 = 2^6 = 128
100-
var result3 = parser.Evaluate("p1_plus_p2_plus_p3(1, 2, 3)").Value; // result3 = 6
101-
102-
string error1 = parser.Evaluate("celsiusToFarenheit(1, 2)").Error; // column 0: There is no function 'celsiusToFarenheit' that takes 2 argument(s).
90+
constants.Add("h", 6.62607015e-34);
91+
})
92+
.ConfigureFunctions(functions =>
93+
{
94+
// functions.RemoveAll() for removing all default functions
95+
// functions.Remove("sum") for removing function by name
96+
// define function with 2 to n number of parameters (typeof(nums) = double[])
97+
functions.Add("product", nums => nums.Aggregate((p1, p2) => p1 * p2));
98+
99+
// define functions with specified number of parameters (1-5 parameters are supported)
100+
functions.Add("celsiusToFarenheit", celsius => celsius * 1.8 + 32);
101+
functions.Add("fahrenheitToCelsius", fahrenheit => (fahrenheit - 32) * 5 / 9);
102+
functions.Add("p1_plus_p2_plus_p3", (p1, p2, p3) => p1 + p2 + p3);
103+
}).Build();
104+
105+
var poolTemperatureInCelsius = parser.Evaluate("celsiusToFarenheit(fahrenheitToCelsius(30))").Value; // poolTemperatureInCelsius = 30
106+
var result2 = parser.Evaluate("product(2, 2, 2, 2, 2, 2, 2)").Value; // result2 = 2^6 = 128
107+
var result3 = parser.Evaluate("p1_plus_p2_plus_p3(1, 2, 3)").Value; // result3 = 6
108+
109+
string error1 = parser.Evaluate("celsiusToFarenheit(1, 2)").Error; // column 0: There is no function 'celsiusToFarenheit' that takes 2 argument(s).
103110
string error2 = parser.Evaluate("product()").Error; // column 0: There is no function 'product' that takes 0 argument(s).
104-
string error3 = parser.Evaluate("p1_plus_p2_plus_p3(1, 2)").Error; // column 0: There is no function 'p1_plus_p2_plus_p3' that takes 2 argument(s).
105-
111+
string error3 = parser.Evaluate("p1_plus_p2_plus_p3(1, 2)").Error; // column 0: There is no function 'p1_plus_p2_plus_p3' that takes 2 argument(s).
112+
```
113+
106114
## License
107115

108116
ThinkSharp.FormulaParser is released under [The MIT license (MIT)](LICENSE.TXT)

0 commit comments

Comments
 (0)