From 7185a308fbea7c8d099e06b7ee7e95c05d8a5fa1 Mon Sep 17 00:00:00 2001 From: appelemac Date: Sun, 29 May 2016 19:45:52 +0200 Subject: [PATCH 01/12] Eval parser --- .../Modules/Searches/Commands/EvalCommand.cs | 88 +++++++++++++++++++ NadekoBot/Modules/Searches/SearchesModule.cs | 1 + NadekoBot/NadekoBot.csproj | 8 +- 3 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 NadekoBot/Modules/Searches/Commands/EvalCommand.cs diff --git a/NadekoBot/Modules/Searches/Commands/EvalCommand.cs b/NadekoBot/Modules/Searches/Commands/EvalCommand.cs new file mode 100644 index 00000000..006039b2 --- /dev/null +++ b/NadekoBot/Modules/Searches/Commands/EvalCommand.cs @@ -0,0 +1,88 @@ +using NadekoBot.Classes; +using System; +using Mathos.Parser; +using System.Threading.Tasks; +using Discord.Commands; +using System.Text.RegularExpressions; + +namespace NadekoBot.Modules.Searches.Commands +{ + class EvalCommand : DiscordCommand + { + public EvalCommand(DiscordModule module) : base(module) + { + } + + internal override void Init(CommandGroupBuilder cgb) + { + cgb.CreateCommand(Module.Prefix + "evaluate") + .Alias(Module.Prefix + "eval") + .Description("Evaluate a mathematical expression") + .Parameter("expression", ParameterType.Unparsed) + .Do(EvalFunc()); + } + + + private CustomParser parser = new CustomParser(); + private Func EvalFunc() => async e => + { + string expression = e.GetArg("expression")?.Trim(); + if (string.IsNullOrWhiteSpace(expression)) + { + await e.Channel.SendMessage("Must give expression"); + return; + } + string answer = evaluate(expression); + if (answer == null) + { + await e.Channel.SendMessage($"Expression {expression} failed to evaluate"); + return; + } + await e.Channel.SendMessage($"`result: {answer}`"); + }; + + private string evaluate(string expression) + { + //check for factorial + expression = Regex.Replace(expression, @"\d+!", x => x.Value + "0"); + try + { + string result = parser.Parse(expression).ToString(); + return result; + } + catch (OverflowException e) + { + return $"Overflow error on {expression}"; + } + catch (FormatException e) + { + return $"\"{expression}\" was not formatted correctly"; + } + } + + + + class CustomParser : MathParser + { + public CustomParser() : base() + { + OperatorList.Add("!"); + OperatorList.Add("_"); + OperatorAction.Add("!", (x, y) => factorial(x)); + OperatorAction.Add("_", (x, y) => 10.130M); + } + + static decimal factorial(decimal x) + { + decimal y = x-1; + while (y >0) + { + x = x * y--; + } + return x; + } + } + + + } +} diff --git a/NadekoBot/Modules/Searches/SearchesModule.cs b/NadekoBot/Modules/Searches/SearchesModule.cs index 22c22955..5b73097b 100644 --- a/NadekoBot/Modules/Searches/SearchesModule.cs +++ b/NadekoBot/Modules/Searches/SearchesModule.cs @@ -28,6 +28,7 @@ namespace NadekoBot.Modules.Searches commands.Add(new ConverterCommand(this)); commands.Add(new RedditCommand(this)); commands.Add(new WowJokeCommand(this)); + commands.Add(new EvalCommand(this)); rng = new Random(); } diff --git a/NadekoBot/NadekoBot.csproj b/NadekoBot/NadekoBot.csproj index f1b8b205..b443f0ad 100644 --- a/NadekoBot/NadekoBot.csproj +++ b/NadekoBot/NadekoBot.csproj @@ -89,6 +89,10 @@ ..\packages\Manatee.Trello.WebApi.1.0.1\lib\net45\Manatee.Trello.WebApi.dll True + + False + lib\MathParser.dll + ..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll True @@ -128,6 +132,7 @@ + @@ -475,6 +480,7 @@ + @@ -485,4 +491,4 @@ --> - + \ No newline at end of file From 78c27ad217e8e3d6aa61317b3cc0ca2ffd083ed4 Mon Sep 17 00:00:00 2001 From: appelemac Date: Sun, 29 May 2016 19:53:58 +0200 Subject: [PATCH 02/12] here it is --- NadekoBot/packages.config | 1 + 1 file changed, 1 insertion(+) diff --git a/NadekoBot/packages.config b/NadekoBot/packages.config index 055f7b1d..9a7951bf 100644 --- a/NadekoBot/packages.config +++ b/NadekoBot/packages.config @@ -5,6 +5,7 @@ + From 54114a5cd4af96665b73e885d48b60221e1cac64 Mon Sep 17 00:00:00 2001 From: appelemac Date: Sun, 29 May 2016 19:57:38 +0200 Subject: [PATCH 03/12] reference not necessary since it's a nuget package --- NadekoBot/NadekoBot.csproj | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/NadekoBot/NadekoBot.csproj b/NadekoBot/NadekoBot.csproj index b443f0ad..ea45f598 100644 --- a/NadekoBot/NadekoBot.csproj +++ b/NadekoBot/NadekoBot.csproj @@ -89,10 +89,6 @@ ..\packages\Manatee.Trello.WebApi.1.0.1\lib\net45\Manatee.Trello.WebApi.dll True - - False - lib\MathParser.dll - ..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll True @@ -491,4 +487,4 @@ --> - \ No newline at end of file + From a1f2123fe43f4288cd837fe74dbed756e07a87e4 Mon Sep 17 00:00:00 2001 From: appelemac Date: Sun, 29 May 2016 19:58:06 +0200 Subject: [PATCH 04/12] reference not necessary since it's a nuget package --- NadekoBot/NadekoBot.csproj | 1 - 1 file changed, 1 deletion(-) diff --git a/NadekoBot/NadekoBot.csproj b/NadekoBot/NadekoBot.csproj index ea45f598..ab5a5f5d 100644 --- a/NadekoBot/NadekoBot.csproj +++ b/NadekoBot/NadekoBot.csproj @@ -476,7 +476,6 @@ - From 544022c8e5d1608acf5aeca20f8fba9a6dea9c90 Mon Sep 17 00:00:00 2001 From: appelemac Date: Sun, 29 May 2016 20:07:50 +0200 Subject: [PATCH 05/12] he --- Tests/app.config | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 Tests/app.config diff --git a/Tests/app.config b/Tests/app.config new file mode 100644 index 00000000..de5386a4 --- /dev/null +++ b/Tests/app.config @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file From 338cb6d24b477f29e965829d046e9e79958ae4ee Mon Sep 17 00:00:00 2001 From: appelemac Date: Sun, 29 May 2016 20:12:02 +0200 Subject: [PATCH 06/12] oh --- NadekoBot/NadekoBot.csproj | 6 +++++- Tests/Tests.csproj | 3 +++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/NadekoBot/NadekoBot.csproj b/NadekoBot/NadekoBot.csproj index ab5a5f5d..df03d19e 100644 --- a/NadekoBot/NadekoBot.csproj +++ b/NadekoBot/NadekoBot.csproj @@ -89,6 +89,10 @@ ..\packages\Manatee.Trello.WebApi.1.0.1\lib\net45\Manatee.Trello.WebApi.dll True + + ..\packages\MathosParser.1.0.10.1\lib\MathParser.dll + True + ..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll True @@ -486,4 +490,4 @@ --> - + \ No newline at end of file diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 0ddc6cc5..d358584a 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -60,6 +60,9 @@ NadekoBot + + + From fa0918806ebe757ee5a82aa887c14a2d17d39f05 Mon Sep 17 00:00:00 2001 From: appelemac Date: Mon, 30 May 2016 20:03:33 +0200 Subject: [PATCH 07/12] Kwoth's changes --- .../Modules/Searches/Commands/EvalCommand.cs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/NadekoBot/Modules/Searches/Commands/EvalCommand.cs b/NadekoBot/Modules/Searches/Commands/EvalCommand.cs index 006039b2..4d9f6213 100644 --- a/NadekoBot/Modules/Searches/Commands/EvalCommand.cs +++ b/NadekoBot/Modules/Searches/Commands/EvalCommand.cs @@ -15,9 +15,9 @@ namespace NadekoBot.Modules.Searches.Commands internal override void Init(CommandGroupBuilder cgb) { - cgb.CreateCommand(Module.Prefix + "evaluate") - .Alias(Module.Prefix + "eval") - .Description("Evaluate a mathematical expression") + cgb.CreateCommand(Module.Prefix + "calculate") + .Alias(Module.Prefix + "calc") + .Description("Evaluate a mathematical expression.\n**Usage**: ~calc 1+1") .Parameter("expression", ParameterType.Unparsed) .Do(EvalFunc()); } @@ -32,7 +32,7 @@ namespace NadekoBot.Modules.Searches.Commands await e.Channel.SendMessage("Must give expression"); return; } - string answer = evaluate(expression); + string answer = Evaluate(expression); if (answer == null) { await e.Channel.SendMessage($"Expression {expression} failed to evaluate"); @@ -41,7 +41,7 @@ namespace NadekoBot.Modules.Searches.Commands await e.Channel.SendMessage($"`result: {answer}`"); }; - private string evaluate(string expression) + private string Evaluate(string expression) { //check for factorial expression = Regex.Replace(expression, @"\d+!", x => x.Value + "0"); @@ -67,12 +67,10 @@ namespace NadekoBot.Modules.Searches.Commands public CustomParser() : base() { OperatorList.Add("!"); - OperatorList.Add("_"); - OperatorAction.Add("!", (x, y) => factorial(x)); - OperatorAction.Add("_", (x, y) => 10.130M); + OperatorAction.Add("!", (x, y) => Factorial(x)); } - static decimal factorial(decimal x) + static decimal Factorial(decimal x) { decimal y = x-1; while (y >0) From acbae6fbe36f9d4aab8f007ddae1db8f02ced930 Mon Sep 17 00:00:00 2001 From: appelemac Date: Mon, 30 May 2016 20:10:36 +0200 Subject: [PATCH 08/12] error message --- NadekoBot/Modules/Searches/Commands/EvalCommand.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/NadekoBot/Modules/Searches/Commands/EvalCommand.cs b/NadekoBot/Modules/Searches/Commands/EvalCommand.cs index 4d9f6213..6ff59345 100644 --- a/NadekoBot/Modules/Searches/Commands/EvalCommand.cs +++ b/NadekoBot/Modules/Searches/Commands/EvalCommand.cs @@ -29,7 +29,6 @@ namespace NadekoBot.Modules.Searches.Commands string expression = e.GetArg("expression")?.Trim(); if (string.IsNullOrWhiteSpace(expression)) { - await e.Channel.SendMessage("Must give expression"); return; } string answer = Evaluate(expression); From 319e4ef2a9d38ccbe3ad7717901dd88bd33c4d59 Mon Sep 17 00:00:00 2001 From: appelemac Date: Mon, 30 May 2016 20:55:38 +0200 Subject: [PATCH 09/12] d --- Tests/Tests.csproj | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index d358584a..2a99410a 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -60,9 +60,6 @@ NadekoBot - - - @@ -90,4 +87,4 @@ --> - \ No newline at end of file + From fd485fe2621f4805c9ace6574d7e3d5f89577751 Mon Sep 17 00:00:00 2001 From: appelemac Date: Mon, 30 May 2016 20:56:00 +0200 Subject: [PATCH 10/12] delete --- Tests/app.config | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/Tests/app.config b/Tests/app.config index de5386a4..8b137891 100644 --- a/Tests/app.config +++ b/Tests/app.config @@ -1,11 +1 @@ - - - - - - - - - - - \ No newline at end of file + From 92145e42ee0d92ae9f15b1547934e6847bb98c99 Mon Sep 17 00:00:00 2001 From: appelemac Date: Mon, 30 May 2016 20:56:12 +0200 Subject: [PATCH 11/12] Update app.config From bf177c1a79bf9ac48bbaf5a34c7b8e7a8cd23186 Mon Sep 17 00:00:00 2001 From: appelemac Date: Mon, 30 May 2016 20:56:47 +0200 Subject: [PATCH 12/12] Delete app.config --- Tests/app.config | 1 - 1 file changed, 1 deletion(-) delete mode 100644 Tests/app.config diff --git a/Tests/app.config b/Tests/app.config deleted file mode 100644 index 8b137891..00000000 --- a/Tests/app.config +++ /dev/null @@ -1 +0,0 @@ -