├── global.json
├── src
├── NCalcAsync
│ ├── NumberConversionTypePreference.cs
│ ├── EvaluateFunctionAsyncHandler.cs
│ ├── EvaluateParameterAsyncHandler.cs
│ ├── EvaluationException.cs
│ ├── Domain
│ │ ├── Parameter.cs
│ │ ├── Function.cs
│ │ ├── LogicalExpressionVisitor.cs
│ │ ├── UnaryExpression.cs
│ │ ├── TernaryExpression.cs
│ │ ├── BinaryExpression.cs
│ │ ├── Value.cs
│ │ ├── LogicalExpression.cs
│ │ └── EvaluationVisitor.cs
│ ├── ParameterArgs.cs
│ ├── FunctionArgs.cs
│ ├── ErrorListeners.cs
│ ├── EvaluationOption.cs
│ ├── NCalcAsync.csproj
│ ├── NCalcListener.cs
│ ├── Expression.cs
│ ├── NCalcBaseListener.cs
│ └── NCalcLexer.cs
└── Grammar
│ └── NCalc.g
├── .config
└── dotnet-tools.json
├── Directory.Build.props
├── .editorconfig
├── README.md
├── version.json
├── test
└── NCalcAsync.Tests
│ ├── run-code-coverage.ps1
│ ├── NCalcAsync.Tests.csproj
│ ├── coverlet.runsettings
│ └── Fixtures.cs
├── .github
└── workflows
│ ├── ci.yml
│ └── publish-nuget.yml
├── LICENSE
├── CHANGELOG.md
├── CONTRIBUTING.md
├── NCalcAsync.sln
└── .gitignore
/global.json:
--------------------------------------------------------------------------------
1 | {
2 | "sdk": {
3 | "version": "7.0.200",
4 | "rollForward": "latestFeature"
5 | }
6 | }
7 |
--------------------------------------------------------------------------------
/src/NCalcAsync/NumberConversionTypePreference.cs:
--------------------------------------------------------------------------------
1 | namespace NCalcAsync
2 | {
3 | public enum NumberConversionTypePreference
4 | {
5 | Decimal = 0,
6 | Double = 1
7 | }
8 | }
9 |
--------------------------------------------------------------------------------
/src/NCalcAsync/EvaluateFunctionAsyncHandler.cs:
--------------------------------------------------------------------------------
1 | using System.Threading.Tasks;
2 |
3 | namespace NCalcAsync
4 | {
5 | public delegate Task EvaluateFunctionAsyncHandler(string name, FunctionArgs args);
6 | }
7 |
--------------------------------------------------------------------------------
/src/NCalcAsync/EvaluateParameterAsyncHandler.cs:
--------------------------------------------------------------------------------
1 | using System.Threading.Tasks;
2 |
3 | namespace NCalcAsync
4 | {
5 | public delegate Task EvaluateParameterAsyncHandler(string name, ParameterArgs args);
6 | }
7 |
--------------------------------------------------------------------------------
/.config/dotnet-tools.json:
--------------------------------------------------------------------------------
1 | {
2 | "version": 1,
3 | "isRoot": true,
4 | "tools": {
5 | "dotnet-outdated-tool": {
6 | "version": "4.5.1",
7 | "commands": [
8 | "dotnet-outdated"
9 | ]
10 | }
11 | }
12 | }
--------------------------------------------------------------------------------
/Directory.Build.props:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | 3.0.28
6 | all
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.editorconfig:
--------------------------------------------------------------------------------
1 | [*.cs]
2 |
3 | # CS4014: Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
4 | dotnet_diagnostic.CS4014.severity = error
5 |
6 | # CS3021: Type or member does not need a CLSCompliant attribute because the assembly does not have a CLSCompliant attribute
7 | dotnet_diagnostic.CS3021.severity = silent
8 |
--------------------------------------------------------------------------------
/src/NCalcAsync/EvaluationException.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace NCalcAsync
4 | {
5 | public class EvaluationException : ApplicationException
6 | {
7 | public EvaluationException(string message)
8 | : base(message)
9 | {
10 | }
11 |
12 | public EvaluationException(string message, Exception innerException)
13 | : base(message, innerException)
14 | {
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/NCalcAsync/Domain/Parameter.cs:
--------------------------------------------------------------------------------
1 | using System.Threading.Tasks;
2 |
3 | namespace NCalcAsync.Domain
4 | {
5 | public class Identifier : LogicalExpression
6 | {
7 | public Identifier(string name)
8 | {
9 | Name = name;
10 | }
11 |
12 | public string Name { get; set; }
13 |
14 |
15 | public override async Task AcceptAsync(LogicalExpressionVisitor visitor)
16 | {
17 | await visitor.VisitAsync(this);
18 | }
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/NCalcAsync/ParameterArgs.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace NCalcAsync
4 | {
5 | public class ParameterArgs : EventArgs
6 | {
7 | private object _result;
8 | public object Result
9 | {
10 | get { return _result; }
11 | set
12 | {
13 | _result = value;
14 | HasResult = true;
15 | }
16 | }
17 |
18 | public bool HasResult { get; set; }
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # NCalcAsync
2 |
3 | NCalcAsync is a fully async port of the [NCalc](https://github.com/ncalc/ncalc) mathematical expressions evaluator in .NET. NCalc can parse any expression and evaluate the result, including static or dynamic parameters and custom functions. Originally created by [@petli](https://github.com/petli).
4 |
5 | > [!CAUTION]
6 | > ⚠️This repository is discontinued and merged at NCalc main repo, please go to [NCalc main repository](https://github.com/ncalc/ncalc) to receive support.⚠️
7 |
--------------------------------------------------------------------------------
/version.json:
--------------------------------------------------------------------------------
1 | {
2 | "$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/main/src/NerdBank.GitVersioning/version.schema.json",
3 | "version": "4.1-alpha",
4 | "publicReleaseRefSpec": [
5 | "^refs/heads/master$",
6 | "^refs/heads/release/v\\d+\\.\\d+$",
7 | "^refs/tags/v\\d+\\.\\d+\\.\\d+(-[a-zA-Z0-9]+)?$"
8 | ],
9 | "cloudBuild": {
10 | "buildNumber": {
11 | "enabled": true
12 | }
13 | },
14 | "release": {
15 | "branchName": "release/v{version}"
16 | }
17 | }
--------------------------------------------------------------------------------
/test/NCalcAsync.Tests/run-code-coverage.ps1:
--------------------------------------------------------------------------------
1 | $config = 'Release'
2 |
3 | if (Test-Path coverage) { Remove-Item coverage -Recurse }
4 | if (Test-Path coverage.zip) { Remove-Item coverage.zip }
5 |
6 | dotnet tool install dotnet-reportgenerator-globaltool --tool-path bin
7 |
8 | $coverage_file = 'coverage.opencover.xml'
9 |
10 | dotnet test -c $config --settings "$PWD/coverlet.runsettings" "$PWD/NCalcAsync.Tests.csproj"
11 |
12 | $report_args = @('-verbosity:Info', '-reporttypes:Html')
13 | ./bin/reportgenerator @report_args "-verbosity:Info" "-reporttypes:Html" "-reports:$PWD/coverage/*/$coverage_file" "-targetdir:$PWD/coverage/"
14 |
--------------------------------------------------------------------------------
/.github/workflows/ci.yml:
--------------------------------------------------------------------------------
1 | name: Continuous Integration
2 |
3 | on:
4 | push:
5 | branches:
6 | - master
7 | - release/*
8 | pull_request:
9 | branches:
10 | - master
11 | - release/*
12 |
13 | jobs:
14 | build:
15 |
16 | runs-on: ubuntu-latest
17 |
18 | steps:
19 | - uses: actions/checkout@v3
20 | with:
21 | fetch-depth: 0
22 | - name: Setup .NET
23 | uses: actions/setup-dotnet@v3
24 | - name: Restore dependencies
25 | run: dotnet restore
26 | - name: Build
27 | run: dotnet build --no-restore
28 | - name: Test
29 | run: dotnet test --no-build --verbosity normal
30 |
--------------------------------------------------------------------------------
/src/NCalcAsync/Domain/Function.cs:
--------------------------------------------------------------------------------
1 | using System.Threading.Tasks;
2 |
3 | namespace NCalcAsync.Domain
4 | {
5 | public class Function : LogicalExpression
6 | {
7 | public Function(Identifier identifier, LogicalExpression[] expressions)
8 | {
9 | Identifier = identifier;
10 | Expressions = expressions;
11 | }
12 |
13 | public Identifier Identifier { get; set; }
14 |
15 | public LogicalExpression[] Expressions { get; set; }
16 |
17 | public override async Task AcceptAsync(LogicalExpressionVisitor visitor)
18 | {
19 | await visitor.VisitAsync(this);
20 | }
21 | }
22 | }
--------------------------------------------------------------------------------
/src/NCalcAsync/Domain/LogicalExpressionVisitor.cs:
--------------------------------------------------------------------------------
1 | using System.Threading.Tasks;
2 |
3 | namespace NCalcAsync.Domain
4 | {
5 | public abstract class LogicalExpressionVisitor
6 | {
7 | public abstract Task VisitAsync(LogicalExpression expression);
8 | public abstract Task VisitAsync(TernaryExpression expression);
9 | public abstract Task VisitAsync(BinaryExpression expression);
10 | public abstract Task VisitAsync(UnaryExpression expression);
11 | public abstract Task VisitAsync(ValueExpression expression);
12 | public abstract Task VisitAsync(Function function);
13 | public abstract Task VisitAsync(Identifier function);
14 | }
15 | }
--------------------------------------------------------------------------------
/.github/workflows/publish-nuget.yml:
--------------------------------------------------------------------------------
1 | name: Publish to Nuget
2 |
3 | on:
4 | release:
5 | types: [published]
6 |
7 | jobs:
8 | build:
9 |
10 | runs-on: ubuntu-latest
11 |
12 | steps:
13 | - uses: actions/checkout@v3
14 | with:
15 | fetch-depth: 0
16 | - name: Setup .NET
17 | uses: actions/setup-dotnet@v3
18 | - name: Restore dependencies
19 | run: dotnet restore
20 | - name: Build
21 | run: dotnet build -c Release --no-restore
22 | - name: Test
23 | run: dotnet test -c Release --no-build --verbosity normal
24 | - name: Pack
25 | run: dotnet pack -c Release --no-build
26 | - name: Publish
27 | run: dotnet nuget push src/NCalcAsync/bin/Release/NCalcAsync*.nupkg -k "${{ secrets.NUGET_API_TOKEN }}" -s https://api.nuget.org/v3/index.json
28 |
--------------------------------------------------------------------------------
/src/NCalcAsync/Domain/UnaryExpression.cs:
--------------------------------------------------------------------------------
1 | using System.Threading.Tasks;
2 |
3 | namespace NCalcAsync.Domain
4 | {
5 | public class UnaryExpression : LogicalExpression
6 | {
7 | public UnaryExpression(UnaryExpressionType type, LogicalExpression expression)
8 | {
9 | Type = type;
10 | Expression = expression;
11 | }
12 |
13 | public LogicalExpression Expression { get; set; }
14 |
15 | public UnaryExpressionType Type { get; set; }
16 |
17 | public override async Task AcceptAsync(LogicalExpressionVisitor visitor)
18 | {
19 | await visitor.VisitAsync(this);
20 | }
21 | }
22 |
23 | public enum UnaryExpressionType
24 | {
25 | Not,
26 | Negate,
27 | BitwiseNot,
28 | Positive
29 | }
30 | }
--------------------------------------------------------------------------------
/src/NCalcAsync/Domain/TernaryExpression.cs:
--------------------------------------------------------------------------------
1 | using System.Threading.Tasks;
2 |
3 | namespace NCalcAsync.Domain
4 | {
5 | public class TernaryExpression : LogicalExpression
6 | {
7 | public TernaryExpression(LogicalExpression leftExpression, LogicalExpression middleExpression, LogicalExpression rightExpression)
8 | {
9 | this.LeftExpression = leftExpression;
10 | this.MiddleExpression = middleExpression;
11 | this.RightExpression = rightExpression;
12 | }
13 |
14 | public LogicalExpression LeftExpression { get; set; }
15 |
16 | public LogicalExpression MiddleExpression { get; set; }
17 |
18 | public LogicalExpression RightExpression { get; set; }
19 |
20 | public override async Task AcceptAsync(LogicalExpressionVisitor visitor)
21 | {
22 | await visitor.VisitAsync(this);
23 | }
24 | }
25 | }
--------------------------------------------------------------------------------
/test/NCalcAsync.Tests/NCalcAsync.Tests.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | net7.0
5 |
6 | false
7 |
8 |
9 |
10 |
11 | all
12 | runtime; build; native; contentfiles; analyzers; buildtransitive
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/src/NCalcAsync/FunctionArgs.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading.Tasks;
3 |
4 | namespace NCalcAsync
5 | {
6 | public class FunctionArgs : EventArgs
7 | {
8 | private object _result;
9 |
10 | public object Result
11 | {
12 | get { return _result; }
13 | set
14 | {
15 | _result = value;
16 | HasResult = true;
17 | }
18 | }
19 |
20 | public bool HasResult { get; set; }
21 |
22 | private Expression[] _parameters = new Expression[0];
23 |
24 | public Expression[] Parameters
25 | {
26 | get { return _parameters; }
27 | set { _parameters = value; }
28 | }
29 |
30 | public async Task