Refactor code flow statements by extracting function call and exit handling methods

This commit is contained in:
Stone_Red
2026-03-05 22:45:03 +01:00
parent 13ef994d8b
commit 276ae421e8
2 changed files with 39 additions and 56 deletions
@@ -92,19 +92,7 @@ internal class CodeFlowStatements : StatementRuntimeInformation
[Statement("call", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.DarkYellow, Priority = Priority.VeryLow)] [Statement("call", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.DarkYellow, Priority = Priority.VeryLow)]
public void Call(string args) public void Call(string args)
{ {
string key = NormalizeBlockName(args); CallFunction(NormalizeBlockName(args));
RuntimeInfo.FunctionCallStack.Push(new FunctionScope(RuntimeInfo.LineNumber, new Stack<string>(RuntimeInfo.InParametersStack)));
RuntimeInfo.InParametersStack.Clear();
if (RuntimeInfo.Functions.TryGetValue(key, out int value))
{
RuntimeInfo.LineNumber = value;
}
else
{
RuntimeInfo.SearchFunction = key;
}
} }
[Statement("if", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.DarkYellow, Priority = Priority.VeryLow, Separator = " call ")] [Statement("if", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.DarkYellow, Priority = Priority.VeryLow, Separator = " call ")]
@@ -118,8 +106,6 @@ internal class CodeFlowStatements : StatementRuntimeInformation
} }
string condition = parts[0].Trim(); string condition = parts[0].Trim();
string key = NormalizeBlockName(parts[1]);
bool? result = Evaluator.EvaluateCondition(condition); bool? result = Evaluator.EvaluateCondition(condition);
if (result is null) if (result is null)
@@ -128,21 +114,9 @@ internal class CodeFlowStatements : StatementRuntimeInformation
return; return;
} }
if (result == false) if (result == true)
{ {
return; CallFunction(NormalizeBlockName(parts[1]));
}
RuntimeInfo.FunctionCallStack.Push(new FunctionScope(RuntimeInfo.LineNumber, new Stack<string>(RuntimeInfo.InParametersStack)));
RuntimeInfo.InParametersStack.Clear();
if (RuntimeInfo.Functions.TryGetValue(key, out int value))
{
RuntimeInfo.LineNumber = value;
}
else
{
RuntimeInfo.SearchFunction = key;
} }
} }
@@ -248,37 +222,13 @@ internal class CodeFlowStatements : StatementRuntimeInformation
[Statement("exit", SearchMode.Exact, SpaceAround.None, ConsoleColor.Red, ExecuteInSearchMode = true)] [Statement("exit", SearchMode.Exact, SpaceAround.None, ConsoleColor.Red, ExecuteInSearchMode = true)]
public void End(string _) public void End(string _)
{ {
if (RuntimeInfo.IsSearching) HandleExit(ExitMessages.PlannedTermination, false);
{
RuntimeInfo.IsInFunction = false;
if (RuntimeInfo.IsLocalSearch)
{
RuntimeInfo.Exit(ExitMessages.LabelNotFound(RuntimeInfo.SearchLabel), true);
}
return;
}
RuntimeInfo.IsInFunction = false;
RuntimeInfo.Exit(ExitMessages.PlannedTermination, false);
} }
[Statement("abort_all", SearchMode.Exact, SpaceAround.None, ConsoleColor.Red, ExecuteInSearchMode = true)] [Statement("abort_all", SearchMode.Exact, SpaceAround.None, ConsoleColor.Red, ExecuteInSearchMode = true)]
public void Terminate(string _) public void Terminate(string _)
{ {
if (RuntimeInfo.IsSearching) HandleExit(ExitMessages.PlannedTerminationCancelingTasks, true);
{
RuntimeInfo.IsInFunction = false;
if (RuntimeInfo.IsLocalSearch)
{
RuntimeInfo.Exit(ExitMessages.LabelNotFound(RuntimeInfo.SearchLabel), true);
}
return;
}
RuntimeInfo.IsInFunction = false;
RuntimeInfo.Exit(ExitMessages.PlannedTerminationCancelingTasks, true);
} }
[Statement("throw", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.Red)] [Statement("throw", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.Red)]
@@ -293,6 +243,38 @@ internal class CodeFlowStatements : StatementRuntimeInformation
RuntimeInfo.Exit(message, false); RuntimeInfo.Exit(message, false);
} }
private void CallFunction(string key)
{
RuntimeInfo.FunctionCallStack.Push(new FunctionScope(RuntimeInfo.LineNumber, new Stack<string>(RuntimeInfo.InParametersStack)));
RuntimeInfo.InParametersStack.Clear();
if (RuntimeInfo.Functions.TryGetValue(key, out int value))
{
RuntimeInfo.LineNumber = value;
}
else
{
RuntimeInfo.SearchFunction = key;
}
}
private void HandleExit(string exitMessage, bool isError)
{
if (RuntimeInfo.IsSearching)
{
RuntimeInfo.IsInFunction = false;
if (RuntimeInfo.IsLocalSearch)
{
RuntimeInfo.Exit(ExitMessages.LabelNotFound(RuntimeInfo.SearchLabel), true);
}
return;
}
RuntimeInfo.IsInFunction = false;
RuntimeInfo.Exit(exitMessage, isError);
}
private int FindBlockBoundary(int currentLine) private int FindBlockBoundary(int currentLine)
{ {
return RuntimeInfo.BlockBoundaries.TryGetValue(currentLine, out int cached) ? cached : -1; return RuntimeInfo.BlockBoundaries.TryGetValue(currentLine, out int cached) ? cached : -1;
+2 -1
View File
@@ -126,7 +126,8 @@ internal static partial class Evaluator
string[] parts = input.Split(op); string[] parts = input.Split(op);
//Weird fix // If the expression starts with the operator (e.g. "-3 + 5" split by '-' gives ["", "3 + 5"]),
// prepend the operator back onto the first real part so it isn't lost.
if (parts.Length >= 2 && string.IsNullOrWhiteSpace(parts[0])) if (parts.Length >= 2 && string.IsNullOrWhiteSpace(parts[0]))
{ {
parts[1] = $"{op}{parts[1]}"; parts[1] = $"{op}{parts[1]}";