|
3 | 3 |
|
4 | 4 | Mathos Parser |
5 | 5 | ============= |
6 | | -**Mathos Parser** is a mathematical expression parser targeting the .NET Framework and .NET Standard that parses all kinds of mathematical expressions with the ability to use custom functions, operators, and variables. |
| 6 | +**Mathos Parser** is a mathematical expression parser for the .NET Framework and .NET Standard. It can parse all kinds of mathematical expressions with the ability to use custom functions, operators, and variables. |
7 | 7 |
|
8 | 8 | * The CIL version (compiles expressions into IL code): https://github.com/MathosProject/Mathos-Parser-CIL |
9 | 9 |
|
10 | 10 | You can find documentation and examples on the [wiki](https://github.com/MathosProject/Mathos-Parser/wiki). |
11 | 11 |
|
12 | 12 | ## Features |
13 | 13 |
|
14 | | -* Parse mathematical expressions. |
15 | | -* Add conditional statements. |
| 14 | +* Parse and execute mathematical expressions. |
16 | 15 | * Customize and override existing operators, functions, and variables. |
17 | 16 | * Supports common mathematical functions, such as pow, round, sqrt, rem, and abs. |
18 | 17 | * Culture independent. |
19 | 18 | * And much more! |
20 | 19 |
|
21 | 20 | ## Introduction |
22 | 21 |
|
23 | | -Mathos Parser is a part of Mathos Project, a project that aims to provide useful methods, structures, and utilities, to make life easier! This math parser is fully independent of the Mathos Core Library, so you can use this library to achieve powerful math parsing without external dependencies. |
| 22 | +Mathos Parser is a part of the Mathos Project, a project that aims to provide useful methods, structures, and utilities to make life easier! This math parser is fully independent of the Mathos Core Library, so you can use this library to achieve powerful math parsing without external dependencies. |
24 | 23 |
|
25 | 24 | ## How to use |
26 | 25 |
|
27 | 26 | It's really easy to use and understand. In this topic I will try to show you some key features of the library. |
28 | 27 |
|
| 28 | +* Custom variables |
29 | 29 | * Custom operators |
30 | 30 | * Custom functions |
31 | | -* Multi-argument functions |
32 | | -* Custom variables |
33 | 31 | * Variables through parsing |
| 32 | +* Multi-argument functions |
34 | 33 |
|
35 | | -### Custom Operators |
36 | | - |
| 34 | +### Custom Variables |
37 | 35 | ```csharp |
38 | 36 | // Create a parser. |
39 | 37 | MathParser parser = new MathParser(); |
40 | 38 |
|
41 | | -// Add the operator to the operator list. |
42 | | -parser.OperatorList.Add("^"); |
| 39 | +// Add a variable. |
| 40 | +parser.LocalVariables.Add("a", 25); |
43 | 41 |
|
44 | | -// Add an action for the newly added operator. |
45 | | -parser.OperatorAction.Add("^", delegate(double numA, double numB) |
46 | | -{ |
47 | | - return (decimal) Math.Pow((double) numA, (double) numB); |
48 | | -}); |
| 42 | +// How about another. |
| 43 | +parser.LocalVariables.Add("猫", 5); |
49 | 44 |
|
50 | 45 | // Parsing |
51 | | -Assert.IsTrue(parser.Parse("3^2") == Math.Pow(3,2)); |
| 46 | +Assert.AreEqual(30, parser.Parse("a + 猫")); |
52 | 47 | ``` |
53 | 48 |
|
54 | | -### Custom Functions |
| 49 | +### Custom Operators |
55 | 50 | ```csharp |
56 | 51 | // Create a parser. |
57 | 52 | MathParser parser = new MathParser(); |
58 | 53 |
|
59 | | -// Add the function and its contents. |
60 | | -parser.LocalFunctions.Add("timesTwo", inputs => inputs[0] * 2); |
| 54 | +// Add a custom operator |
| 55 | +parser.Operators.Add("λ", (left, right) => Math.Pow(left, right)); |
61 | 56 |
|
62 | 57 | // Parsing |
63 | | -Assert.IsTrue(parser.Parse("timesTwo(4)") == 8); |
| 58 | +Assert.AreEqual(Math.Pow(3, 2), parser.Parse("3 λ 2")); |
64 | 59 | ``` |
65 | 60 |
|
66 | | -### Multi-Argument Functions |
| 61 | +### Custom Functions |
67 | 62 | ```csharp |
68 | 63 | // Create a parser. |
69 | 64 | MathParser parser = new MathParser(); |
70 | 65 |
|
71 | | -// Add the function and its contents. |
72 | | -parser.LocalFunctions.Add("log", delegate(double[] inputs) |
73 | | -{ |
74 | | - // inputs[0] is the number. |
75 | | - // inputs[1] is the base (optional). |
76 | | - |
77 | | - if(inputs.Length == 1) |
78 | | - return Math.Log(inputs[0]); |
79 | | - else if(inputs.Length == 2) |
80 | | - return Math.Log(inputs[0], inputs[1]); |
81 | | - else |
82 | | - return 0; // Error. |
83 | | -}); |
| 66 | +// Add the function and its implementation. |
| 67 | +parser.LocalFunctions.Add("timesTwo", inputs => inputs[0] * 2); |
84 | 68 |
|
85 | 69 | // Parsing |
86 | | -Assert.IsTrue(parser.Parse("log(100)") == 2); |
87 | | -Assert.IsTrue(parser.Parse("log(8, 2)") == 3); |
| 70 | +Assert.AreEqual(8, parser.Parse("timesTwo(4)")); |
88 | 71 | ``` |
89 | 72 |
|
90 | | -### Custom Variables |
| 73 | +### Variables Through Parsing |
91 | 74 | ```csharp |
92 | 75 | // Create a parser. |
93 | 76 | MathParser parser = new MathParser(); |
94 | 77 |
|
95 | | -// Add the variable. |
96 | | -parser.LocalVariables.Add("a", 25); |
| 78 | +// Define the variable |
| 79 | +parser.ProgrammaticallyParse("let a = 25"); |
97 | 80 |
|
98 | 81 | // Parsing |
99 | | -Assert.IsTrue(parser.Parse("a+5") == 30); |
| 82 | +Assert.AreEqual(30, parser.Parse("a + 5")); |
100 | 83 | ``` |
101 | 84 |
|
102 | | -### Variables Through Parsing |
| 85 | +### Multi-Argument Functions |
103 | 86 | ```csharp |
104 | 87 | // Create a parser. |
105 | 88 | MathParser parser = new MathParser(); |
106 | 89 |
|
107 | | -// Add the variable |
108 | | -parser.ProgrammaticallyParse("let a = 25"); |
| 90 | +// Add the function and its implementation. |
| 91 | +parser.LocalFunctions.Add("clamp", delegate (double[] inputs) |
| 92 | +{ |
| 93 | + // The value. |
| 94 | + var value = inputs[0]; |
| 95 | + |
| 96 | + // The maximum value. |
| 97 | + var min = inputs[1]; |
| 98 | + |
| 99 | + // The minimum value. |
| 100 | + var max = inputs[2]; |
| 101 | + |
| 102 | + if (value > max) |
| 103 | + return max; |
| 104 | + |
| 105 | + if (value < min) |
| 106 | + return min; |
| 107 | + |
| 108 | + return value; |
| 109 | +}); |
109 | 110 |
|
110 | 111 | // Parsing |
111 | | -Assert.IsTrue(parser.Parse("a+5") == 30); |
| 112 | +Assert.AreEqual(3, parser.Parse("clamp(3,-1,5)")); |
| 113 | +Assert.AreEqual(-1, parser.Parse("clamp(-5,-1,5)")); |
| 114 | +Assert.AreEqual(5, parser.Parse("clamp(8,-1,5)")); |
112 | 115 | ``` |
0 commit comments