Improve Regex performance and change .NET version to .NET 7

This commit is contained in:
Stone_Red
2022-11-17 12:37:32 +01:00
parent e539813314
commit 0f7f63ae9a
8 changed files with 485 additions and 457 deletions
@@ -9,112 +9,112 @@ using YesNt.Interpreter.Enums;
using YesNt.Interpreter.Runtime;
using YesNt.Interpreter.Utilities;
namespace YesNt.Interpreter.Statements
namespace YesNt.Interpreter.Statements;
internal partial class ProcessingStatements : StatementRuntimeInformation
{
internal class ProcessingStatements : StatementRuntimeInformation
[Statement("!calc", SearchMode.EndOfLine, SpaceAround.Start, ConsoleColor.DarkYellow, Priority = Priority.High)]
public void Calculate(string args)
{
private static readonly Regex calculationRegex = new Regex(@"[0-9*+().,^%/-]+[0-9*+ ().,^%/-]+[0-9*+().,^%/-]+");
MatchCollection matches = CalculationRegex().Matches(args.FromSaveString());
[Statement("!calc", SearchMode.EndOfLine, SpaceAround.Start, ConsoleColor.DarkYellow, Priority = Priority.High)]
public void Calculate(string args)
for (int i = 0; i < matches.Count; i++)
{
MatchCollection matches = calculationRegex.Matches(args.FromSaveString());
for (int i = 0; i < matches.Count; i++)
string res = Evaluator.Calculate(matches[i].Value);
if (res is null)
{
string res = Evaluator.Calculate(matches[i].Value);
if (res is null)
RuntimeInfo.Exit("Invalid operation", true);
return;
}
args = args.FromSaveString().Replace(matches[i].Value, res);
}
RuntimeInfo.CurrentLine = args;
}
[Statement("!eval", SearchMode.EndOfLine, SpaceAround.Start, ConsoleColor.DarkYellow, Priority = Priority.VeryHigh)]
public void Evaluate(string args)
{
RuntimeInfo.CurrentLine = args.FromSaveString();
}
[Statement("!!", SearchMode.Contains, SpaceAround.None, ConsoleColor.DarkYellow, Priority = Priority.PreProcessing, KeepStatementInArgs = true)]
public void DontEvaluate(string args)
{
int index;
while ((index = args.IndexOf("!!")) != -1)
{
args = args.Remove(index, 2);
if (index < args.Length)
{
char charToEscape = args[index];
args = args.Remove(index, 1);
args = args.Insert(index, charToEscape.ToString().ToSaveString());
}
}
RuntimeInfo.CurrentLine = args;
}
[Statement("!task", SearchMode.EndOfLine, SpaceAround.Start, ConsoleColor.DarkYellow, Priority = Priority.VeryHigh)]
public void RunTask(string line)
{
int lineNumer = RuntimeInfo.LineNumber;
List<Line> lines = RuntimeInfo.Lines.GetRange(0, RuntimeInfo.Lines.Count);
Line oldLine = lines[lineNumer];
lines[lineNumer] = new Line(line, oldLine.FileName, oldLine.LineNumber);
_ = Task.Run(() =>
{
YesNtInterpreter interpreter = new YesNtInterpreter();
interpreter.Initialize();
interpreter.Execute(lines, RuntimeInfo.GloablVariables, lineNumer, RuntimeInfo);
});
RuntimeInfo.CurrentLine = string.Empty;
}
[Statement("slp", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.Magenta)]
public void Sleep(string args)
{
_ = int.TryParse(args, out int millisecondsTimeout);
ConsoleExtentions.Sleep(millisecondsTimeout, RuntimeInfo);
}
[Statement("imp", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.Magenta)]
public void Import(string path)
{
path = Path.Combine(RuntimeInfo.WorkingDirectory, path);
if (string.IsNullOrEmpty(Path.GetExtension(path)))
{
path = Path.ChangeExtension(path, "ynt");
}
if (File.Exists(path))
{
try
{
RuntimeInfo.Lines.RemoveAt(RuntimeInfo.LineNumber);
string[] lines = File.ReadAllLines(path);
for (int i = 0; i < lines.Length; i++)
{
RuntimeInfo.Exit("Invalid operation", true);
return;
RuntimeInfo.Lines.Insert(RuntimeInfo.LineNumber + i, new Line(lines[i], Path.GetFileName(path), i));
}
args = args.FromSaveString().Replace(matches[i].Value, res);
RuntimeInfo.LineNumber--;
}
RuntimeInfo.CurrentLine = args;
}
[Statement("!eval", SearchMode.EndOfLine, SpaceAround.Start, ConsoleColor.DarkYellow, Priority = Priority.VeryHigh)]
public void Evaluate(string args)
{
RuntimeInfo.CurrentLine = args.FromSaveString();
}
[Statement("!!", SearchMode.Contains, SpaceAround.None, ConsoleColor.DarkYellow, Priority = Priority.PreProcessing, KeepStatementInArgs = true)]
public void DontEvaluate(string args)
{
int index;
while ((index = args.IndexOf("!!")) != -1)
catch
{
args = args.Remove(index, 2);
if (index < args.Length)
{
char charToEscape = args[index];
args = args.Remove(index, 1);
args = args.Insert(index, charToEscape.ToString().ToSaveString());
}
RuntimeInfo.Exit($"Could not load file \"{path}\"", true);
}
RuntimeInfo.CurrentLine = args;
}
[Statement("!task", SearchMode.EndOfLine, SpaceAround.Start, ConsoleColor.DarkYellow, Priority = Priority.VeryHigh)]
public void RunTask(string line)
else
{
int lineNumer = RuntimeInfo.LineNumber;
List<Line> lines = RuntimeInfo.Lines.GetRange(0, RuntimeInfo.Lines.Count);
Line oldLine = lines[lineNumer];
lines[lineNumer] = new Line(line, oldLine.FileName, oldLine.LineNumber);
_ = Task.Run(() =>
{
YesNtInterpreter interpreter = new YesNtInterpreter();
interpreter.Initialize();
interpreter.Execute(lines, RuntimeInfo.GloablVariables, lineNumer, RuntimeInfo);
});
RuntimeInfo.CurrentLine = string.Empty;
}
[Statement("slp", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.Magenta)]
public void Sleep(string args)
{
_ = int.TryParse(args, out int millisecondsTimeout);
ConsoleExtentions.Sleep(millisecondsTimeout, RuntimeInfo);
}
[Statement("imp", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.Magenta)]
public void Import(string path)
{
path = Path.Combine(RuntimeInfo.WorkingDirectory, path);
if (string.IsNullOrEmpty(Path.GetExtension(path)))
{
path = Path.ChangeExtension(path, "ynt");
}
if (File.Exists(path))
{
try
{
RuntimeInfo.Lines.RemoveAt(RuntimeInfo.LineNumber);
string[] lines = File.ReadAllLines(path);
for (int i = 0; i < lines.Length; i++)
{
RuntimeInfo.Lines.Insert(RuntimeInfo.LineNumber + i, new Line(lines[i], Path.GetFileName(path), i));
}
RuntimeInfo.LineNumber--;
}
catch
{
RuntimeInfo.Exit($"Could not load file \"{path}\"", true);
}
}
else
{
RuntimeInfo.Exit($"Could not find file \"{path}\"", true);
}
RuntimeInfo.Exit($"Could not find file \"{path}\"", true);
}
}
[GeneratedRegex("[0-9*+().,^%/-]+[0-9*+ ().,^%/-]+[0-9*+().,^%/-]+")]
private static partial Regex CalculationRegex();
}
@@ -5,119 +5,119 @@ using YesNt.Interpreter.Attributes;
using YesNt.Interpreter.Enums;
using YesNt.Interpreter.Runtime;
namespace YesNt.Interpreter.Statements
namespace YesNt.Interpreter.Statements;
internal partial class VariableStatements : StatementRuntimeInformation
{
internal class VariableStatements : StatementRuntimeInformation
[Statement("<", SearchMode.StartOfLine, SpaceAround.None, Priority = Priority.VeryLow)]
public void DefineVariable(string args)
{
private static readonly Regex variableStatementRegex = new Regex(@">[a-zA-Z0-9]+");
[Statement("<", SearchMode.StartOfLine, SpaceAround.None, Priority = Priority.VeryLow)]
public void DefineVariable(string args)
string[] parts = args.Split('=');
if (parts.Length == 2)
{
string[] parts = args.Split('=');
if (parts.Length == 2)
string key = parts[0].Trim();
if (key.Contains(' '))
{
string key = parts[0].Trim();
if (key.Contains(' '))
{
RuntimeInfo.Exit("Invalid Syntax", true);
}
if (RuntimeInfo.Variables.ContainsKey(key))
{
RuntimeInfo.Variables[key] = parts[1].Trim();
}
else
{
RuntimeInfo.Variables.Add(key, parts[1].Trim());
}
RuntimeInfo.Exit("Invalid Syntax", true);
}
else
{
RuntimeInfo.Exit("Invalid syntax", true);
}
}
[Statement("!<", SearchMode.StartOfLine, SpaceAround.None, Priority = Priority.VeryLow)]
public void DefineGlobalVariable(string args)
{
string[] parts = args.Split('=');
if (parts.Length == 2)
{
string key = parts[0].Trim();
if (key.Contains(' '))
{
RuntimeInfo.Exit("Invalid Syntax", true);
}
if (RuntimeInfo.GloablVariables.ContainsKey(key))
{
RuntimeInfo.GloablVariables[key] = parts[1].Trim();
}
else
{
RuntimeInfo.GloablVariables.Add(key, parts[1].Trim());
}
}
else
{
RuntimeInfo.Exit("Invalid syntax", true);
}
}
[Statement("del", SearchMode.StartOfLine, SpaceAround.End, System.ConsoleColor.Red, Priority = Priority.VeryLow)]
public void DeleteVariable(string args)
{
string key = args.Trim();
if (RuntimeInfo.Variables.ContainsKey(key))
{
_ = RuntimeInfo.Variables.Remove(key);
}
else if (RuntimeInfo.GloablVariables.ContainsKey(key))
{
_ = RuntimeInfo.GloablVariables.Remove(key);
RuntimeInfo.Variables[key] = parts[1].Trim();
}
else
{
RuntimeInfo.Exit($"Variable \"{key}\" not found", true);
RuntimeInfo.Variables.Add(key, parts[1].Trim());
}
}
[Statement(">", SearchMode.Contains, SpaceAround.None, Priority = Priority.Highest)]
public void ReadVariable(string _)
else
{
if (!RuntimeInfo.CurrentLine.Contains('>'))
RuntimeInfo.Exit("Invalid syntax", true);
}
}
[Statement("!<", SearchMode.StartOfLine, SpaceAround.None, Priority = Priority.VeryLow)]
public void DefineGlobalVariable(string args)
{
string[] parts = args.Split('=');
if (parts.Length == 2)
{
string key = parts[0].Trim();
if (key.Contains(' '))
{
RuntimeInfo.Exit("Invalid Syntax", true);
}
if (RuntimeInfo.GloablVariables.ContainsKey(key))
{
RuntimeInfo.GloablVariables[key] = parts[1].Trim();
}
else
{
RuntimeInfo.GloablVariables.Add(key, parts[1].Trim());
}
}
else
{
RuntimeInfo.Exit("Invalid syntax", true);
}
}
[Statement("del", SearchMode.StartOfLine, SpaceAround.End, System.ConsoleColor.Red, Priority = Priority.VeryLow)]
public void DeleteVariable(string args)
{
string key = args.Trim();
if (RuntimeInfo.Variables.ContainsKey(key))
{
_ = RuntimeInfo.Variables.Remove(key);
}
else if (RuntimeInfo.GloablVariables.ContainsKey(key))
{
_ = RuntimeInfo.GloablVariables.Remove(key);
}
else
{
RuntimeInfo.Exit($"Variable \"{key}\" not found", true);
}
}
[Statement(">", SearchMode.Contains, SpaceAround.None, Priority = Priority.Highest)]
public void ReadVariable(string _)
{
if (!RuntimeInfo.CurrentLine.Contains('>'))
{
return;
}
foreach (KeyValuePair<string, string> variable in RuntimeInfo.Variables)
{
RuntimeInfo.CurrentLine = RuntimeInfo.CurrentLine.Replace($">{variable.Key}", variable.Value);
}
foreach (KeyValuePair<string, string> variable in RuntimeInfo.GloablVariables)
{
RuntimeInfo.CurrentLine = RuntimeInfo.CurrentLine.Replace($">{variable.Key}", variable.Value);
}
if (RuntimeInfo.IsSearching)
{
return;
}
MatchCollection matches = VariableStatementRegex().Matches(RuntimeInfo.CurrentLine);
for (int i = 0; i < matches.Count; i++)
{
string varName = matches[i].Value.Replace(">", string.Empty);
if (!RuntimeInfo.Variables.ContainsKey(varName) && !RuntimeInfo.GloablVariables.ContainsKey(varName))
{
RuntimeInfo.Exit($"Variable \"{varName}\" not found", true);
return;
}
foreach (KeyValuePair<string, string> variable in RuntimeInfo.Variables)
{
RuntimeInfo.CurrentLine = RuntimeInfo.CurrentLine.Replace($">{variable.Key}", variable.Value);
}
foreach (KeyValuePair<string, string> variable in RuntimeInfo.GloablVariables)
{
RuntimeInfo.CurrentLine = RuntimeInfo.CurrentLine.Replace($">{variable.Key}", variable.Value);
}
if (RuntimeInfo.IsSearching)
{
return;
}
MatchCollection matches = variableStatementRegex.Matches(RuntimeInfo.CurrentLine);
for (int i = 0; i < matches.Count; i++)
{
string varName = matches[i].Value.Replace(">", string.Empty);
if (!RuntimeInfo.Variables.ContainsKey(varName) && !RuntimeInfo.GloablVariables.ContainsKey(varName))
{
RuntimeInfo.Exit($"Variable \"{varName}\" not found", true);
return;
}
}
}
}
[GeneratedRegex(">[a-zA-Z0-9]+")]
private static partial Regex VariableStatementRegex();
}