Performance improvements and code cleanup

This commit is contained in:
Stone_Red
2026-03-05 22:39:05 +01:00
parent 4bcaf30d9e
commit 13ef994d8b
15 changed files with 488 additions and 404 deletions
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using YesNt.Interpreter.Attributes;
@@ -80,14 +80,7 @@ internal class CodeFlowStatements : StatementRuntimeInformation
return;
}
if (RuntimeInfo.Labels.ContainsKey(key))
{
RuntimeInfo.Labels[key] = RuntimeInfo.LineNumber;
}
else
{
RuntimeInfo.Labels.Add(key, RuntimeInfo.LineNumber);
}
RuntimeInfo.Labels[key] = RuntimeInfo.LineNumber;
if (!string.IsNullOrWhiteSpace(RuntimeInfo.SearchLabel) && RuntimeInfo.SearchLabel == key)
{
@@ -153,7 +146,7 @@ internal class CodeFlowStatements : StatementRuntimeInformation
}
}
[Statement("if", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.Green, Priority = Priority.VeryLow, Separator = ":")]
[Statement("if", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.Green, Priority = Priority.VeryLow, Separator = ":", BlockPair = "end_if")]
public void IfBlock(string args)
{
args = args.Trim();
@@ -177,7 +170,7 @@ internal class CodeFlowStatements : StatementRuntimeInformation
return;
}
(int targetLine, _) = FindElseOrEndIf(RuntimeInfo.LineNumber);
int targetLine = FindBlockBoundary(RuntimeInfo.LineNumber);
if (targetLine < 0)
{
RuntimeInfo.Exit(ExitMessages.NoMatchingEndIf, true);
@@ -187,10 +180,10 @@ internal class CodeFlowStatements : StatementRuntimeInformation
RuntimeInfo.LineNumber = targetLine;
}
[Statement("else:", SearchMode.Exact, SpaceAround.None, ConsoleColor.Green)]
[Statement("else:", SearchMode.Exact, SpaceAround.None, ConsoleColor.Green, IsBlockIntermediate = true, BlockPair = "end_if")]
public void Else(string _)
{
int targetLine = FindEndIf(RuntimeInfo.LineNumber);
int targetLine = FindBlockBoundary(RuntimeInfo.LineNumber);
if (targetLine < 0)
{
RuntimeInfo.Exit(ExitMessages.NoMatchingEndIf, true);
@@ -200,12 +193,12 @@ internal class CodeFlowStatements : StatementRuntimeInformation
RuntimeInfo.LineNumber = targetLine;
}
[Statement("end_if", SearchMode.Exact, SpaceAround.None, ConsoleColor.Green)]
[Statement("end_if", SearchMode.Exact, SpaceAround.None, ConsoleColor.Green, IsBlockEnd = true)]
public void EndIf(string _)
{
}
[Statement("while", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.Green, Priority = Priority.VeryLow, Separator = ":")]
[Statement("while", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.Green, Priority = Priority.VeryLow, Separator = ":", BlockPair = "end_while")]
public void While(string args)
{
args = args.Trim();
@@ -229,7 +222,7 @@ internal class CodeFlowStatements : StatementRuntimeInformation
return;
}
int endWhileLine = FindEndWhile(RuntimeInfo.LineNumber);
int endWhileLine = FindBlockBoundary(RuntimeInfo.LineNumber);
if (endWhileLine < 0)
{
RuntimeInfo.Exit(ExitMessages.NoMatchingEndWhile, true);
@@ -239,10 +232,10 @@ internal class CodeFlowStatements : StatementRuntimeInformation
RuntimeInfo.LineNumber = endWhileLine;
}
[Statement("end_while", SearchMode.Exact, SpaceAround.None, ConsoleColor.Green)]
[Statement("end_while", SearchMode.Exact, SpaceAround.None, ConsoleColor.Green, IsBlockEnd = true)]
public void EndWhile(string _)
{
int whileLine = FindWhile(RuntimeInfo.LineNumber);
int whileLine = FindBlockBoundary(RuntimeInfo.LineNumber);
if (whileLine < 0)
{
RuntimeInfo.Exit(ExitMessages.NoMatchingWhile, true);
@@ -265,11 +258,8 @@ internal class CodeFlowStatements : StatementRuntimeInformation
return;
}
else
{
RuntimeInfo.IsInFunction = false;
}
RuntimeInfo.IsInFunction = false;
RuntimeInfo.Exit(ExitMessages.PlannedTermination, false);
}
@@ -286,11 +276,8 @@ internal class CodeFlowStatements : StatementRuntimeInformation
return;
}
else
{
RuntimeInfo.IsInFunction = false;
}
RuntimeInfo.IsInFunction = false;
RuntimeInfo.Exit(ExitMessages.PlannedTerminationCancelingTasks, true);
}
@@ -306,136 +293,8 @@ internal class CodeFlowStatements : StatementRuntimeInformation
RuntimeInfo.Exit(message, false);
}
private static string NormalizeBlockName(string value)
private int FindBlockBoundary(int currentLine)
{
return value.Trim().TrimEnd(':').Trim();
}
private (int TargetLine, bool IsElse) FindElseOrEndIf(int currentLine)
{
int depth = 0;
for (int i = currentLine + 1; i < RuntimeInfo.Lines.Count; i++)
{
string line = RuntimeInfo.Lines[i].Content.Trim().Replace("\r", string.Empty);
if (IsIfStart(line))
{
depth++;
continue;
}
if (line == "end_if")
{
if (depth == 0)
{
return (i, false);
}
depth--;
continue;
}
if (line == "else:" && depth == 0)
{
return (i, true);
}
}
return (-1, false);
}
private int FindEndIf(int currentLine)
{
int depth = 0;
for (int i = currentLine + 1; i < RuntimeInfo.Lines.Count; i++)
{
string line = RuntimeInfo.Lines[i].Content.Trim().Replace("\r", string.Empty);
if (IsIfStart(line))
{
depth++;
continue;
}
if (line == "end_if")
{
if (depth == 0)
{
return i;
}
depth--;
}
}
return -1;
}
private static bool IsIfStart(string line)
{
return line.StartsWith("if ", StringComparison.Ordinal) && line.EndsWith(':');
}
private int FindEndWhile(int currentLine)
{
int depth = 0;
for (int i = currentLine + 1; i < RuntimeInfo.Lines.Count; i++)
{
string line = RuntimeInfo.Lines[i].Content.Trim().Replace("\r", string.Empty);
if (IsWhileStart(line))
{
depth++;
continue;
}
if (line == "end_while")
{
if (depth == 0)
{
return i;
}
depth--;
}
}
return -1;
}
private int FindWhile(int currentLine)
{
int depth = 0;
for (int i = currentLine - 1; i >= 0; i--)
{
string line = RuntimeInfo.Lines[i].Content.Trim().Replace("\r", string.Empty);
if (line == "end_while")
{
depth++;
continue;
}
if (IsWhileStart(line))
{
if (depth == 0)
{
return i;
}
depth--;
}
}
return -1;
}
private static bool IsWhileStart(string line)
{
return line.StartsWith("while ", StringComparison.Ordinal) && line.EndsWith(':');
return RuntimeInfo.BlockBoundaries.TryGetValue(currentLine, out int cached) ? cached : -1;
}
}
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using YesNt.Interpreter.Attributes;
@@ -33,14 +33,7 @@ internal class FunctionStatements : StatementRuntimeInformation
return;
}
if (RuntimeInfo.Functions.ContainsKey(key))
{
RuntimeInfo.Functions[key] = RuntimeInfo.LineNumber;
}
else
{
RuntimeInfo.Functions.Add(key, RuntimeInfo.LineNumber);
}
RuntimeInfo.Functions[key] = RuntimeInfo.LineNumber;
if (!string.IsNullOrWhiteSpace(RuntimeInfo.SearchFunction) && RuntimeInfo.SearchFunction == key)
{
@@ -59,18 +52,7 @@ internal class FunctionStatements : StatementRuntimeInformation
[Statement("%out", SearchMode.Contains, SpaceAround.None, ConsoleColor.Yellow, KeepStatementInArgs = true, Priority = Priority.Highest)]
public void GetOutParameter(string args)
{
while (args.Contains("%out"))
{
if (RuntimeInfo.OutParametersStack.Count == 0)
{
RuntimeInfo.Exit(ExitMessages.NoOutArgumentInStack, true);
return;
}
args = args.ReplaceFirstOccurrence("%out", RuntimeInfo.OutParametersStack.Pop());
}
RuntimeInfo.CurrentLine = args.TrimEnd();
RuntimeInfo.CurrentLine = TemplateProcessor.ProcessStackParameters(args, "%out", RuntimeInfo.OutParametersStack, RuntimeInfo, ExitMessages.NoOutArgumentInStack);
}
[Statement("%has_out", SearchMode.Contains, SpaceAround.None, ConsoleColor.Yellow, KeepStatementInArgs = true, Priority = Priority.Highest)]
@@ -122,18 +104,7 @@ internal class FunctionStatements : StatementRuntimeInformation
return;
}
while (args.Contains("%in"))
{
if (RuntimeInfo.FunctionCallStack.Peek().Arguments.Count == 0)
{
RuntimeInfo.Exit(ExitMessages.NoInArgumentInStack, true);
return;
}
args = args.ReplaceFirstOccurrence("%in", RuntimeInfo.FunctionCallStack.Peek().Arguments.Pop());
}
RuntimeInfo.CurrentLine = args.TrimEnd();
RuntimeInfo.CurrentLine = TemplateProcessor.ProcessStackParameters(args, "%in", RuntimeInfo.FunctionCallStack.Peek().Arguments, RuntimeInfo, ExitMessages.NoInArgumentInStack);
}
[Statement("%has_in", SearchMode.Contains, SpaceAround.None, ConsoleColor.Yellow, KeepStatementInArgs = true, Priority = Priority.Highest)]
@@ -181,10 +152,8 @@ internal class FunctionStatements : StatementRuntimeInformation
}
return;
}
else
{
RuntimeInfo.IsInFunction = false;
}
RuntimeInfo.IsInFunction = false;
if (RuntimeInfo.FunctionCallStack.Count > 0)
{
@@ -204,9 +173,4 @@ internal class FunctionStatements : StatementRuntimeInformation
{
RuntimeInfo.FunctionCallStack.Clear();
}
private static string NormalizeBlockName(string value)
{
return value.Trim().TrimEnd(':').Trim();
}
}
@@ -1,4 +1,5 @@
using System;
using System;
using System.Runtime.InteropServices;
using YesNt.Interpreter.Attributes;
using YesNt.Interpreter.Enums;
@@ -14,51 +15,36 @@ internal class PredefinedVariableStatements : StatementRuntimeInformation
[Statement("%time", SearchMode.Contains, SpaceAround.None, ConsoleColor.Blue, KeepStatementInArgs = true, Priority = Priority.Highest)]
public void GetUnixTimestamp(string args)
{
args = args.Replace("%time", DateTimeOffset.Now.ToUnixTimeSeconds().ToString());
RuntimeInfo.CurrentLine = args.TrimEnd();
RuntimeInfo.CurrentLine = TemplateProcessor.ProcessSimplePlaceholders(args, "%time", DateTimeOffset.Now.ToUnixTimeSeconds().ToString()).TrimEnd();
}
[Statement("%os", SearchMode.Contains, SpaceAround.None, ConsoleColor.Blue, KeepStatementInArgs = true, Priority = Priority.Highest)]
public void GetOperatingSystem(string args)
{
args = args.Replace("%os", Environment.OSVersion.Platform.ToString());
RuntimeInfo.CurrentLine = args.TrimEnd();
RuntimeInfo.CurrentLine = TemplateProcessor.ProcessSimplePlaceholders(args, "%os", Environment.OSVersion.Platform.ToString()).TrimEnd();
}
[Statement("%cpu", SearchMode.Contains, SpaceAround.None, ConsoleColor.Blue, KeepStatementInArgs = true, Priority = Priority.Highest)]
public void GetProcessorArchitecture(string args)
{
args = args.Replace("%cpu", System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture.ToString());
RuntimeInfo.CurrentLine = args.TrimEnd();
RuntimeInfo.CurrentLine = TemplateProcessor.ProcessSimplePlaceholders(args, "%cpu", System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture.ToString()).TrimEnd();
}
[Statement("%is64", SearchMode.Contains, SpaceAround.None, ConsoleColor.Blue, KeepStatementInArgs = true, Priority = Priority.Highest)]
public void GetIsOperatingSystem64Bit(string args)
{
args = args.Replace("%is64", $"{Environment.Is64BitOperatingSystem}");
RuntimeInfo.CurrentLine = args.TrimEnd();
RuntimeInfo.CurrentLine = TemplateProcessor.ProcessSimplePlaceholders(args, "%is64", Environment.Is64BitOperatingSystem.ToString()).TrimEnd();
}
[Statement("%pi", SearchMode.Contains, SpaceAround.None, ConsoleColor.Blue, KeepStatementInArgs = true, Priority = Priority.Highest)]
public void GetPi(string args)
{
args = args.Replace("%pi", Math.PI.ToString());
RuntimeInfo.CurrentLine = args.TrimEnd();
RuntimeInfo.CurrentLine = TemplateProcessor.ProcessSimplePlaceholders(args, "%pi", Math.PI.ToString()).TrimEnd();
}
[Statement("%rand", SearchMode.Contains, SpaceAround.None, ConsoleColor.Blue, KeepStatementInArgs = true, Priority = Priority.Highest)]
public void GetRandom(string args)
{
while (args.Contains("%rand"))
{
args = args.ReplaceFirstOccurrence("%rand", random.Next(32767, int.MaxValue).ToString());
}
RuntimeInfo.CurrentLine = args.TrimEnd();
RuntimeInfo.CurrentLine = TemplateProcessor.ProcessDynamicPlaceholders(args, "%rand", () => random.Next(32767, int.MaxValue).ToString()).TrimEnd();
}
}
@@ -16,20 +16,7 @@ internal partial class ProcessingStatements : StatementRuntimeInformation
[Statement("calc", SearchMode.EndOfLine, SpaceAround.Start, ConsoleColor.DarkYellow, Priority = Priority.High)]
public void Calculate(string args)
{
MatchCollection matches = CalculationRegex().Matches(args.FromSafeString());
for (int i = 0; i < matches.Count; i++)
{
string res = Evaluator.Calculate(matches[i].Value);
if (res is null)
{
RuntimeInfo.Exit(ExitMessages.InvalidOperation, true);
return;
}
args = args.FromSafeString().Replace(matches[i].Value, res);
}
RuntimeInfo.CurrentLine = args;
RuntimeInfo.CurrentLine = TemplateProcessor.ProcessCalculations(args.FromSafeString(), RuntimeInfo, CalculationRegex());
}
[Statement("eval", SearchMode.EndOfLine, SpaceAround.Start, ConsoleColor.DarkYellow, Priority = Priority.VeryHigh)]
@@ -100,6 +87,7 @@ internal partial class ProcessingStatements : StatementRuntimeInformation
{
RuntimeInfo.Lines.Insert(RuntimeInfo.LineNumber + i, new Line(lines[i], Path.GetFileName(path), i));
}
RuntimeInfo.PreScanLinesAction?.Invoke();
RuntimeInfo.LineNumber--;
}
catch
@@ -1,8 +1,10 @@
using System.Text.RegularExpressions;
using System;
using System.Collections.Generic;
using YesNt.Interpreter.Attributes;
using YesNt.Interpreter.Enums;
using YesNt.Interpreter.Runtime;
using YesNt.Interpreter.Utilities;
namespace YesNt.Interpreter.Statements;
@@ -11,32 +13,16 @@ internal partial class VariableStatements : StatementRuntimeInformation
[Statement("var", SearchMode.StartOfLine, SpaceAround.End, System.ConsoleColor.DarkBlue, Priority = Priority.VeryLow, Separator = "=")]
public void DefineVariable(string args)
{
string[] parts = args.Split('=');
if (parts.Length == 2)
{
string key = parts[0].Trim();
if (key.Contains(' '))
{
RuntimeInfo.Exit(ExitMessages.InvalidSyntax, true);
}
if (RuntimeInfo.Variables.ContainsKey(key))
{
RuntimeInfo.Variables[key] = parts[1].Trim();
}
else
{
RuntimeInfo.Variables.Add(key, parts[1].Trim());
}
}
else
{
RuntimeInfo.Exit(ExitMessages.InvalidSyntax, true);
}
DefineVariableIn(RuntimeInfo.Variables, args);
}
[Statement("global", SearchMode.StartOfLine, SpaceAround.End, System.ConsoleColor.DarkBlue, Priority = Priority.VeryLow, Separator = "=")]
public void DefineGlobalVariable(string args)
{
DefineVariableIn(RuntimeInfo.GlobalVariables, args);
}
private void DefineVariableIn(Dictionary<string, string> dict, string args)
{
string[] parts = args.Split('=');
if (parts.Length == 2)
@@ -47,14 +33,7 @@ internal partial class VariableStatements : StatementRuntimeInformation
RuntimeInfo.Exit(ExitMessages.InvalidSyntax, true);
}
if (RuntimeInfo.GlobalVariables.ContainsKey(key))
{
RuntimeInfo.GlobalVariables[key] = parts[1].Trim();
}
else
{
RuntimeInfo.GlobalVariables.Add(key, parts[1].Trim());
}
dict[key] = parts[1].Trim();
}
else
{
@@ -84,38 +63,6 @@ internal partial class VariableStatements : StatementRuntimeInformation
[Statement("${", SearchMode.Contains, SpaceAround.None, Priority = Priority.Highest, Separator = "}")]
public void ReadVariable(string _)
{
if (!RuntimeInfo.CurrentLine.Contains("${"))
{
return;
}
MatchCollection matches = VariableStatementRegex().Matches(RuntimeInfo.CurrentLine);
if (matches.Count <= 0)
{
return;
}
for (int i = 0; i < matches.Count; i++)
{
string varName = matches[i].Groups[1].Value;
if (RuntimeInfo.Variables.TryGetValue(varName, out string value))
{
RuntimeInfo.CurrentLine = RuntimeInfo.CurrentLine.Replace(matches[i].Value, value);
}
else if (RuntimeInfo.GlobalVariables.TryGetValue(varName, out value))
{
RuntimeInfo.CurrentLine = RuntimeInfo.CurrentLine.Replace(matches[i].Value, value);
}
else if (!RuntimeInfo.IsSearching)
{
RuntimeInfo.Exit(ExitMessages.VariableNotFound(varName), true);
return;
}
}
RuntimeInfo.CurrentLine = TemplateProcessor.ProcessVariables(RuntimeInfo.CurrentLine, RuntimeInfo);
}
[GeneratedRegex("\\$\\{([a-zA-Z0-9]+)\\}")]
private static partial Regex VariableStatementRegex();
}