diff --git a/YesNt.Interpreter/Statements/CodeFlowStatements.cs b/YesNt.Interpreter/Statements/CodeFlowStatements.cs index b3623c1..bbdf9e7 100644 --- a/YesNt.Interpreter/Statements/CodeFlowStatements.cs +++ b/YesNt.Interpreter/Statements/CodeFlowStatements.cs @@ -92,19 +92,7 @@ internal class CodeFlowStatements : StatementRuntimeInformation [Statement("call", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.DarkYellow, Priority = Priority.VeryLow)] public void Call(string args) { - string key = NormalizeBlockName(args); - - RuntimeInfo.FunctionCallStack.Push(new FunctionScope(RuntimeInfo.LineNumber, new Stack(RuntimeInfo.InParametersStack))); - RuntimeInfo.InParametersStack.Clear(); - - if (RuntimeInfo.Functions.TryGetValue(key, out int value)) - { - RuntimeInfo.LineNumber = value; - } - else - { - RuntimeInfo.SearchFunction = key; - } + CallFunction(NormalizeBlockName(args)); } [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 key = NormalizeBlockName(parts[1]); - bool? result = Evaluator.EvaluateCondition(condition); if (result is null) @@ -128,21 +114,9 @@ internal class CodeFlowStatements : StatementRuntimeInformation return; } - if (result == false) + if (result == true) { - return; - } - - RuntimeInfo.FunctionCallStack.Push(new FunctionScope(RuntimeInfo.LineNumber, new Stack(RuntimeInfo.InParametersStack))); - RuntimeInfo.InParametersStack.Clear(); - - if (RuntimeInfo.Functions.TryGetValue(key, out int value)) - { - RuntimeInfo.LineNumber = value; - } - else - { - RuntimeInfo.SearchFunction = key; + CallFunction(NormalizeBlockName(parts[1])); } } @@ -248,37 +222,13 @@ internal class CodeFlowStatements : StatementRuntimeInformation [Statement("exit", SearchMode.Exact, SpaceAround.None, ConsoleColor.Red, ExecuteInSearchMode = true)] public void End(string _) { - if (RuntimeInfo.IsSearching) - { - RuntimeInfo.IsInFunction = false; - if (RuntimeInfo.IsLocalSearch) - { - RuntimeInfo.Exit(ExitMessages.LabelNotFound(RuntimeInfo.SearchLabel), true); - } - - return; - } - - RuntimeInfo.IsInFunction = false; - RuntimeInfo.Exit(ExitMessages.PlannedTermination, false); + HandleExit(ExitMessages.PlannedTermination, false); } [Statement("abort_all", SearchMode.Exact, SpaceAround.None, ConsoleColor.Red, ExecuteInSearchMode = true)] public void Terminate(string _) { - if (RuntimeInfo.IsSearching) - { - RuntimeInfo.IsInFunction = false; - if (RuntimeInfo.IsLocalSearch) - { - RuntimeInfo.Exit(ExitMessages.LabelNotFound(RuntimeInfo.SearchLabel), true); - } - - return; - } - - RuntimeInfo.IsInFunction = false; - RuntimeInfo.Exit(ExitMessages.PlannedTerminationCancelingTasks, true); + HandleExit(ExitMessages.PlannedTerminationCancelingTasks, true); } [Statement("throw", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.Red)] @@ -293,6 +243,38 @@ internal class CodeFlowStatements : StatementRuntimeInformation RuntimeInfo.Exit(message, false); } + private void CallFunction(string key) + { + RuntimeInfo.FunctionCallStack.Push(new FunctionScope(RuntimeInfo.LineNumber, new Stack(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) { return RuntimeInfo.BlockBoundaries.TryGetValue(currentLine, out int cached) ? cached : -1; diff --git a/YesNt.Interpreter/Utilities/Evaluator.cs b/YesNt.Interpreter/Utilities/Evaluator.cs index a3baba1..b8ddb3d 100644 --- a/YesNt.Interpreter/Utilities/Evaluator.cs +++ b/YesNt.Interpreter/Utilities/Evaluator.cs @@ -126,7 +126,8 @@ internal static partial class Evaluator 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])) { parts[1] = $"{op}{parts[1]}";