Eval parser
This commit is contained in:
parent
5a01162859
commit
7185a308fb
88
NadekoBot/Modules/Searches/Commands/EvalCommand.cs
Normal file
88
NadekoBot/Modules/Searches/Commands/EvalCommand.cs
Normal file
@ -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<CommandEventArgs, Task> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -28,6 +28,7 @@ namespace NadekoBot.Modules.Searches
|
|||||||
commands.Add(new ConverterCommand(this));
|
commands.Add(new ConverterCommand(this));
|
||||||
commands.Add(new RedditCommand(this));
|
commands.Add(new RedditCommand(this));
|
||||||
commands.Add(new WowJokeCommand(this));
|
commands.Add(new WowJokeCommand(this));
|
||||||
|
commands.Add(new EvalCommand(this));
|
||||||
rng = new Random();
|
rng = new Random();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,6 +89,10 @@
|
|||||||
<HintPath>..\packages\Manatee.Trello.WebApi.1.0.1\lib\net45\Manatee.Trello.WebApi.dll</HintPath>
|
<HintPath>..\packages\Manatee.Trello.WebApi.1.0.1\lib\net45\Manatee.Trello.WebApi.dll</HintPath>
|
||||||
<Private>True</Private>
|
<Private>True</Private>
|
||||||
</Reference>
|
</Reference>
|
||||||
|
<Reference Include="MathParser, Version=1.0.10.1, Culture=neutral, processorArchitecture=MSIL">
|
||||||
|
<SpecificVersion>False</SpecificVersion>
|
||||||
|
<HintPath>lib\MathParser.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
||||||
<HintPath>..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
|
<HintPath>..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||||
<Private>True</Private>
|
<Private>True</Private>
|
||||||
@ -128,6 +132,7 @@
|
|||||||
<Compile Include="Modules\CustomReactions\CustomReactions.cs" />
|
<Compile Include="Modules\CustomReactions\CustomReactions.cs" />
|
||||||
<Compile Include="Modules\Programming\Commands\HaskellRepl.cs" />
|
<Compile Include="Modules\Programming\Commands\HaskellRepl.cs" />
|
||||||
<Compile Include="Modules\Programming\ProgrammingModule.cs" />
|
<Compile Include="Modules\Programming\ProgrammingModule.cs" />
|
||||||
|
<Compile Include="Modules\Searches\Commands\EvalCommand.cs" />
|
||||||
<Compile Include="Modules\Searches\Commands\IMDB\ImdbMovie.cs" />
|
<Compile Include="Modules\Searches\Commands\IMDB\ImdbMovie.cs" />
|
||||||
<Compile Include="Modules\Searches\Commands\IMDB\ImdbScraper.cs" />
|
<Compile Include="Modules\Searches\Commands\IMDB\ImdbScraper.cs" />
|
||||||
<Compile Include="Classes\IncidentsHandler.cs" />
|
<Compile Include="Classes\IncidentsHandler.cs" />
|
||||||
@ -475,6 +480,7 @@
|
|||||||
</ProjectReference>
|
</ProjectReference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Content Include="lib\MathParser.dll" />
|
||||||
<Content Include="lib\ScaredFingers.UnitsConversion.dll" />
|
<Content Include="lib\ScaredFingers.UnitsConversion.dll" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
|
Loading…
Reference in New Issue
Block a user