mirror of
https://github.com/Stone-Red-Code/YesNt-Interpreter.git
synced 2026-09-04 09:06:41 +02:00
- Add mod (%) and pow (^) operator
- Fix task creation bug
This commit is contained in:
@@ -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<Line> 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();
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user