diff --git a/.github/workflows/build-branch-TermRewriting.yml b/.github/workflows/build-branch-TermRewriting.yml new file mode 100644 index 0000000..7f0deec --- /dev/null +++ b/.github/workflows/build-branch-TermRewriting.yml @@ -0,0 +1,22 @@ +name: branch TermRewriting + +on: + push: + branches: + - feature/TermRewriting + +jobs: + build: + + runs-on: windows-latest + + steps: + - uses: actions/checkout@v1 + - name: Setup .NET Core + uses: actions/setup-dotnet@v1 + with: + dotnet-version: 2.2.108 + - name: Run Tests + run: dotnet test + - name: Build with dotnet + run: dotnet build .\ThinkSharp.FormulaParser\ThinkSharp.FormulaParser.csproj --configuration Release diff --git a/.github/workflows/build-dev.yml b/.github/workflows/build-dev.yml new file mode 100644 index 0000000..51d6721 --- /dev/null +++ b/.github/workflows/build-dev.yml @@ -0,0 +1,23 @@ +name: .NET Core + +on: + push: + branches: + - develop + +jobs: + build: + + runs-on: windows-latest + + steps: + - uses: actions/checkout@v1 + - name: Setup .NET Core + uses: actions/setup-dotnet@v1 + with: + dotnet-version: 2.2.108 + - name: Run Tests + run: dotnet test + - name: Build with dotnet + run: dotnet build .\ThinkSharp.FormulaParser\ThinkSharp.FormulaParser.csproj --configuration Release + diff --git a/README.md b/README.md index 19afe4d..e6925f8 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ var variables = new Dictionary { ["x"] = 2.0 }; var result2 = parser.Evaluate("1+x", variables).Value; // result2 = 3.0 // Usage of functions -var result3 = parser.Evaluate("1+min(3,4)").Value; // result2 = 4.0 +var result3 = parser.Evaluate("1+min(3,4)").Value; // result3 = 4.0 // Handle errors var parsingResult = parser.Evaluate("2*?"); @@ -93,17 +93,18 @@ var parser = FormulaParser { // functions.RemoveAll() for removing all default functions // functions.Remove("sum") for removing function by name - // define function with 2 to n number of parameters (typeof(nums) = double[]) - functions.Add("product", nums => nums.Aggregate((p1, p2) => p1 * p2)); - // define functions with specified number of parameters (1-5 parameters are supported) + // define functions with certain number of parameters (1-5 parameters are supported) functions.Add("celsiusToFarenheit", celsius => celsius * 1.8 + 32); functions.Add("fahrenheitToCelsius", fahrenheit => (fahrenheit - 32) * 5 / 9); functions.Add("p1_plus_p2_plus_p3", (p1, p2, p3) => p1 + p2 + p3); + + // define function with 2 to n number of parameters (typeof(nums) = double[]) + functions.Add("product", nums => nums.Aggregate((p1, p2) => p1 * p2)); }).Build(); var poolTemperatureInCelsius = parser.Evaluate("celsiusToFarenheit(fahrenheitToCelsius(30))").Value; // poolTemperatureInCelsius = 30 -var result2 = parser.Evaluate("product(2, 2, 2, 2, 2, 2, 2)").Value; // result2 = 2^6 = 128 +var result2 = parser.Evaluate("product(2, 2, 2, 2, 2, 2)").Value; // result2 = 2^6 = 128 var result3 = parser.Evaluate("p1_plus_p2_plus_p3(1, 2, 3)").Value; // result3 = 6 string error1 = parser.Evaluate("celsiusToFarenheit(1, 2)").Error; // column 0: There is no function 'celsiusToFarenheit' that takes 2 argument(s). diff --git a/ThinkSharp.FormulaParser.Test.Core/FormularParser.NumberFormats.Test.cs b/ThinkSharp.FormulaParser.Test.Core/FormularParser.NumberFormats.Test.cs index 93d1ec2..fb620ff 100644 --- a/ThinkSharp.FormulaParser.Test.Core/FormularParser.NumberFormats.Test.cs +++ b/ThinkSharp.FormulaParser.Test.Core/FormularParser.NumberFormats.Test.cs @@ -1,8 +1,10 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using System; using System.Collections.Generic; +using System.Globalization; using System.Linq; using System.Text; +using System.Threading; using System.Threading.Tasks; using ThinkSharp.FormulaParsing.Ast.Nodes; using ThinkSharp.FormulaParsing.Ast.Visitors; @@ -50,7 +52,19 @@ public void TestDecimalNotation() Assert.AreEqual(202.0, parser.Evaluate("0d101 + 101").Value); } - [TestMethod] + [TestMethod] + public void TestDifferentCultures() + { + var parser = FormulaParser.Create(); + + Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE"); + Assert.AreEqual(0.1, parser.Evaluate("0.1").Value); + + Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); + Assert.AreEqual(0.1, parser.Evaluate("0.1").Value); + } + + [TestMethod] [Ignore] public void TestScientificNotation() { diff --git a/ThinkSharp.FormulaParser.Test.Core/ThinkSharp.FormulaParser.Test.csproj b/ThinkSharp.FormulaParser.Test.Core/ThinkSharp.FormulaParser.Test.csproj index 232823b..4648127 100644 --- a/ThinkSharp.FormulaParser.Test.Core/ThinkSharp.FormulaParser.Test.csproj +++ b/ThinkSharp.FormulaParser.Test.Core/ThinkSharp.FormulaParser.Test.csproj @@ -1,9 +1,10 @@  - netcoreapp2.0 + net7.0 false + true ThinkSharp.FormulaParser.Test.Core @@ -11,9 +12,9 @@ - - - + + + diff --git a/ThinkSharp.FormulaParser/ANTLR/AntlrToAstNodesGrammerTreeVisitor.cs b/ThinkSharp.FormulaParser/ANTLR/AntlrToAstNodesGrammerTreeVisitor.cs index 6f0a097..381423f 100644 --- a/ThinkSharp.FormulaParser/ANTLR/AntlrToAstNodesGrammerTreeVisitor.cs +++ b/ThinkSharp.FormulaParser/ANTLR/AntlrToAstNodesGrammerTreeVisitor.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Globalization; using Antlr4.Runtime.Misc; using ThinkSharp.FormulaParsing.Ast.Nodes; @@ -131,7 +132,7 @@ public override Node VisitAtom([NotNull] FormulaGrammerParser.AtomContext contex // //var pow = new PowerNode(new IntegerNode("10", 10), exponentNumber); // //return new BinaryOperatorNode(BinaryOperator.BySymbol("*"), baseNumber, exponentNumber); - // return new DecimalNode(scientificNumber, double.Parse(scientificNumber)); + // return new DecimalNode(scientificNumber, double.Parse(scientificNumber.Parse)); //} public override Node VisitPrefixedIntNumber([NotNull] FormulaGrammerParser.PrefixedIntNumberContext context) @@ -154,7 +155,7 @@ public override Node VisitPrefixedBinNumber([NotNull] FormulaGrammerParser.Prefi public override Node VisitPrefixedDecNumber([NotNull] FormulaGrammerParser.PrefixedDecNumberContext context) { var token = context.PREFIX_DEC_NUMBER().Symbol.Text; - return new DecimalNode(double.Parse(token.Substring(2))); + return new DecimalNode(double.Parse(token.Substring(2), CultureInfo.InvariantCulture)); } public override Node VisitPrefixedHexNumber([NotNull] FormulaGrammerParser.PrefixedHexNumberContext context) @@ -182,13 +183,13 @@ public override Node VisitPrefixedOctNumber([NotNull] FormulaGrammerParser.Prefi public override Node VisitDecimalNumber([NotNull] FormulaGrammerParser.DecimalNumberContext context) { var token = context.DECIMAL_NUMBER().Symbol.Text; - return new DecimalNode(double.Parse(token)); + return new DecimalNode(double.Parse(token, CultureInfo.InvariantCulture)); } public override Node VisitIntgerNumber([NotNull] FormulaGrammerParser.IntgerNumberContext context) { var token = context.INTEGER_NUMBER().Symbol.Text; - return new IntegerNode(NumberFormat.Dec, long.Parse(token)); + return new IntegerNode(NumberFormat.Dec, long.Parse(token, CultureInfo.InvariantCulture)); } public override Node VisitVariable([NotNull] FormulaGrammerParser.VariableContext context) diff --git a/ThinkSharp.FormulaParser/ThinkSharp.FormulaParser.csproj b/ThinkSharp.FormulaParser/ThinkSharp.FormulaParser.csproj index 3b48427..b556f14 100644 --- a/ThinkSharp.FormulaParser/ThinkSharp.FormulaParser.csproj +++ b/ThinkSharp.FormulaParser/ThinkSharp.FormulaParser.csproj @@ -8,16 +8,17 @@ Jan-Niklas Schäfer ThinkSharp A formula parser with fluent API that allows parsing and evaluation of mathematically formulas. Features: functions, constants, variables, scientific numbers, focus on customization and flexibility. - © 2020 Jan-Niklas Schäfer - 0.9.0.0 - 0.9.0.0 + © 2020-2024 Jan-Niklas Schäfer + 0.10.0.0 + 0.10.0.0 LICENSE.txt Removed term rewriting functionality (has been moved to its own project) - + 0.10.0 Bugfix: decimal parsing should not depend on OS Culture https://github.com/JanDotNet/ThinkSharp.FormulaParser true - 0.9.0 + 0.10.0 + README.md @@ -32,6 +33,10 @@ True + + True + \ +