forked from ncalc/ncalc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master'
- Loading branch information
Showing
51 changed files
with
802 additions
and
675 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Architecture | ||
|
||
The entire process of evaluating an expression can be demonstrated at this flowchart: | ||
|
||
```mermaid | ||
flowchart TB | ||
A["1+1"] -->|Parsing| B("new BinaryExpression(new ValueExpression(1), new ValueExpression(1), BinaryExpressionType.Plus)") | ||
B --> |Evaluation|2 | ||
``` | ||
|
||
## Parsing | ||
|
||
Parsing is the process of analyzing the input expression and converting it into a structured format that can be easily | ||
evaluated. We use [Parlot](https://github.com/sebastienros/parlot) to handle parsing, but you can use any parser you | ||
want if you implement the interface <xref:NCalc.Factories.ILogicalExpressionFactory>. | ||
For our example, "1+1", the parsing step converts the string into an abstract syntax tree (AST). | ||
This tree is made up of different types of expressions, such as binary expressions, value expressions our even | ||
functions. | ||
Our AST is represented by <xref:NCalc.Domain.LogicalExpression> class. | ||
|
||
## Evaluation | ||
|
||
Evaluation refers to the process of determining the value of an expression. We use the visitor pattern at evaluation. | ||
This pattern allows you to add new operations to existing object structures without modifying those structures. | ||
With the method <xref:NCalc.Domain.LogicalExpression.Accept``1(NCalc.Visitors.ILogicalExpressionVisitor{``0})> is possible to accept any kind of visitor that | ||
implements <xref:NCalc.Visitors.ILogicalExpressionVisitor`1>. Example implementations | ||
include <xref:NCalc.Visitors.EvaluationVisitor> that returns a <xref:System.Object> | ||
and <xref:NCalc.Visitors.SerializationVisitor> that converts the AST into a <xref:System.String>. | ||
|
||
If you are creating your custom implementation, beware it should be stateless to be easier to debug and read. This is | ||
enforced by the [PureAttribute](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.contracts.pureattribute0) and generic return at | ||
the <xref:NCalc.Domain.LogicalExpression.Accept``1(NCalc.Visitors.ILogicalExpressionVisitor{``0})> method. | ||
|
||
## <xref:NCalc.Expression> Class | ||
|
||
This is the main class of NCalc. It abstracts the process of parsing and evaluating the string. | ||
The method <xref:NCalc.Expression.Evaluate> returns the actual value of its <xref:System.String> representation. | ||
|
||
Example: | ||
|
||
```c# | ||
var expression = new Expression("2 * 3"); | ||
var result = expression.Evaluate(); | ||
|
||
Console.WriteLine(result); | ||
``` | ||
|
||
This example above first creates an instance of <xref:NCalc.Expression> using a valued constructor. This constructor | ||
takes a <xref:System.String> as parameter. | ||
Then the method <xref:NCalc.Expression.Evaluate> is called to parse the <xref:System.String> and returns the actual | ||
value represented by the <xref:System.String>. | ||
|
||
To create expressions you can combine several [Operators](operators.md) and [Values](values.md). | ||
|
||
## Learn More | ||
For additional information on the technique we used to create this library please read [this | ||
article](http://www.codeproject.com/KB/recipes/sota_expression_evaluator.aspx). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
namespace NCalc; | ||
|
||
public delegate ValueTask<object?> AsyncExpressionFunction(AsyncExpression[] arguments, AsyncExpressionContext context); | ||
public delegate ValueTask<object?> AsyncExpressionFunction(AsyncExpressionFunctionData data); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
namespace NCalc; | ||
|
||
public class AsyncExpressionFunctionData(Guid id, AsyncExpression[] arguments, AsyncExpressionContext context) : IEnumerable<AsyncExpression> | ||
{ | ||
public Guid Id { get; } = id; | ||
private AsyncExpression[] Arguments { get; } = arguments; | ||
public AsyncExpressionContext Context { get; } = context; | ||
|
||
public AsyncExpression this[int index] | ||
{ | ||
get => Arguments[index]; | ||
set => Arguments[index] = value; | ||
} | ||
|
||
public IEnumerator<AsyncExpression> GetEnumerator() | ||
{ | ||
return ((IEnumerable<AsyncExpression>)Arguments).GetEnumerator(); | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return Arguments.GetEnumerator(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
namespace NCalc; | ||
|
||
public delegate ValueTask<object?> AsyncExpressionParameter(AsyncExpressionContext context); | ||
public delegate ValueTask<object?> AsyncExpressionParameter(AsyncExpressionParameterData data); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace NCalc; | ||
|
||
public class AsyncExpressionParameterData(Guid id, AsyncExpressionContext context) | ||
{ | ||
public Guid Id { get; } = id; | ||
public AsyncExpressionContext Context { get; } = context; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.