From 20f7f16dd95835c923c704b3c6caf0afc1f643c8 Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Fri, 25 Mar 2022 23:49:13 +0100 Subject: [PATCH] - Add mod (`%`) and pow (`^`) operator - Fix task creation bug --- .../Statements/ProcessingStatements.cs | 10 ++++- YesNt.Interpreter/Utilities/Evaluator.cs | 41 +++++++++---------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/YesNt.Interpreter/Statements/ProcessingStatements.cs b/YesNt.Interpreter/Statements/ProcessingStatements.cs index a1cdb7e..835cd2a 100644 --- a/YesNt.Interpreter/Statements/ProcessingStatements.cs +++ b/YesNt.Interpreter/Statements/ProcessingStatements.cs @@ -13,10 +13,14 @@ namespace YesNt.Interpreter.Statements { internal class ProcessingStatements : StatementRuntimeInformation { + private static readonly Regex calculationRegex = new Regex(@"((\)?)+(\(?)+[0-9]+(((\s?)+(\)?)(\s?)+\+(\s?)+(\(?)+(\s?)+|(\s?)+(\)?)(\s?)+\-(\s?)+(\(?)+(\s?)+|(\s?)+(\)?)(\s?)+\*(\s?)+(\(?)+(\s?)+|(\s?)+(\)?)(\s?)+\%(\s?)+(\(?)+(\s?)+|(\s?)+(\)?)(\s?)+\^(\s?)+(\(?)+(\s?)+|(\s?)+(\)?)(\s?)+\/(\s?)+(\(?)+(\s?)+)|[,.])(?=[0-9])+)+[0-9]+(\)?)+"); + + //new Regex(@"((\)?)+(\(?)+[0-9]+(((\s?)+(\)?)(\s?)+\+(\s?)+(\(?)+(\s?)+|(\s?)+(\)?)(\s?)+\-(\s?)+(\(?)+(\s?)+|(\s?)+(\)?)(\s?)+\*(\s?)+(\(?)+(\s?)+|(\s?)+(\)?)(\s?)+\/(\s?)+(\(?)+(\s?)+)|[,.])(?=[0-9])+)+[0-9]+(\)?)+"); + [Statement("!calc", SearchMode.EndOfLine, SpaceAround.Start, ConsoleColor.DarkYellow, Priority = Priority.High, ExecuteInSearchMode = true)] public void Calculate(string args) { - MatchCollection matches = Regex.Matches(args.FromSaveString(), @"((\)?)+(\(?)+[0-9]+(((\s?)+(\)?)(\s?)+\+(\s?)+(\(?)+(\s?)+|(\s?)+(\)?)(\s?)+\-(\s?)+(\(?)+(\s?)+|(\s?)+(\)?)(\s?)+\*(\s?)+(\(?)+(\s?)+|(\s?)+(\)?)(\s?)+\/(\s?)+(\(?)+(\s?)+)|[,.])(?=[0-9])+)+[0-9]+(\)?)+"); + MatchCollection matches = calculationRegex.Matches(args.FromSaveString()); for (int i = 0; i < matches.Count; i++) { @@ -44,7 +48,9 @@ namespace YesNt.Interpreter.Statements int lineNumer = RuntimeInfo.LineNumber; List lines = RuntimeInfo.Lines.GetRange(0, RuntimeInfo.Lines.Count); - lines[lineNumer].Content = line; + Line oldLine = lines[lineNumer]; + + lines[lineNumer] = new Line(line, oldLine.FileName, oldLine.LineNumber); _ = Task.Run(() => { YesNtInterpreter interpreter = new YesNtInterpreter(); diff --git a/YesNt.Interpreter/Utilities/Evaluator.cs b/YesNt.Interpreter/Utilities/Evaluator.cs index 8e27d07..688bed5 100644 --- a/YesNt.Interpreter/Utilities/Evaluator.cs +++ b/YesNt.Interpreter/Utilities/Evaluator.cs @@ -1,4 +1,5 @@ -using System.Text.RegularExpressions; +using System; +using System.Text.RegularExpressions; namespace YesNt.Interpreter.Utilities { @@ -115,20 +116,15 @@ namespace YesNt.Interpreter.Utilities { string part = p; - switch (op) + part = op switch { - case '+': - part = Calculate(part, '-'); - break; - - case '-': - part = Calculate(part, '*'); - break; - - case '*': - part = Calculate(part, '/'); - break; - } + '+' => Calculate(part, '-'), + '-' => Calculate(part, '*'), + '*' => Calculate(part, '/'), + '/' => Calculate(part, '%'), + '%' => Calculate(part, '^'), + _ => part + }; if (part is null) { @@ -160,6 +156,14 @@ namespace YesNt.Interpreter.Utilities case '/': number /= num; break; + + case '%': + number %= num; + break; + + case '^': + number = Math.Pow(number, num); + break; } } } @@ -169,14 +173,7 @@ namespace YesNt.Interpreter.Utilities } } - if (number % 1 == 0) - { - return number.ToString(); - } - else - { - return number.ToString("F99").Trim('0'); - } + return number.ToString(); } } } \ No newline at end of file