Centralize exit messages in ExitMessages class

This commit is contained in:
Stone_Red
2026-03-04 20:32:25 +01:00
parent 3411bb88de
commit 10d5766873
12 changed files with 99 additions and 63 deletions
@@ -88,7 +88,7 @@ public class VariableStatementsTests
"var a b = 1"
];
YesNtAssert.ContainsTerminationMessage(lines, "Invalid Syntax");
YesNtAssert.ContainsTerminationMessage(lines, "Invalid syntax");
}
[TestMethod]
+36
View File
@@ -0,0 +1,36 @@
namespace YesNt.Interpreter.Runtime;
internal static class ExitMessages
{
internal const string InvalidSyntax = "Invalid syntax";
internal const string InvalidSyntaxColonRequired = "Invalid syntax. Statement must end with ':'";
internal const string InvalidOperation = "Invalid operation";
internal const string InvalidStatement = "Invalid statement";
internal const string InvalidStringLiteral = "Invalid string literal";
internal const string EndOfFile = "End of file";
internal const string TerminatedByExternalProcess = "Terminated by external process";
internal const string TerminatedByChildTask = "Terminated by child task";
internal const string TerminatedByParentTask = "Terminated by parent task";
internal const string PlannedTermination = "Planned termination by code";
internal const string PlannedTerminationCancelingTasks = "Planned termination by code. Canceling all tasks";
internal const string NoMatchingEndIf = "No matching end_if found";
internal const string NoMatchingEndWhile = "No matching end_while found";
internal const string NoMatchingWhile = "No matching while found";
internal const string NestedFunctionsNotAllowed = "Nested functions are not allowed";
internal const string NoOutArgumentInStack = "No out argument in stack";
internal const string StatementNotAllowedOutsideFunction = "Statement not allowed outside of function";
internal const string NoInArgumentInStack = "No in argument in stack";
internal const string NoFunctionInStack = "No function in stack";
internal static string LabelNotFound(string label) => $"Label \"{label}\" not found";
internal static string FunctionNotFound(string function) => $"Function \"{function}\" not found";
internal static string VariableNotFound(string variable) => $"Variable \"{variable}\" not found";
internal static string ListNotFound(string list) => $"List \"{list}\" not found";
internal static string InvalidIndex(string rawIndex) => $"\"{rawIndex}\" is not a valid index";
internal static string IndexOutOfRange(int index) => $"Index {index} out of range";
internal static string InvalidTimeoutValue(string value) => $"\"{value}\" is not a valid time-out value";
internal static string CouldNotLoadFile(string path) => $"Could not load file \"{path}\"";
internal static string CouldNotFindFile(string path) => $"Could not find file \"{path}\"";
internal static string CannotFindFile(string path) => $"Cannot find file \"{path}\".";
internal static string FailedToStart(string program, string message) => $"Failed to start \"{program}\". {message}";
}
@@ -131,7 +131,7 @@ internal sealed class RuntimeInformation
{
StopAllTasks = true;
OnExit?.Invoke(message, StopAllTasks);
parentRuntimeInformation?.Exit("Terminated by child task", true);
parentRuntimeInformation?.Exit(ExitMessages.TerminatedByChildTask, true);
}
}
@@ -177,6 +177,6 @@ internal sealed class RuntimeInformation
private void ParentRuntimeInformation_OnExit(string exitMessage, bool stopAllTasks)
{
Exit($"Terminated by parent task", stopAllTasks);
Exit(ExitMessages.TerminatedByParentTask, stopAllTasks);
}
}
@@ -70,7 +70,7 @@ public class YesNtInterpreter
public void Stop()
{
runtimeInfo.Exit("Terminated by external process", true);
runtimeInfo.Exit(ExitMessages.TerminatedByExternalProcess, true);
}
public void Execute(string path, bool isDebugMode = false)
@@ -106,7 +106,7 @@ public class YesNtInterpreter
runtimeInfo.GlobalVariables = globalVariables;
if (parentRuntimeInformation.StopAllTasks)
{
runtimeInfo.Exit($"Parent task was terminated!", parentRuntimeInformation.StopAllTasks);
runtimeInfo.Exit(ExitMessages.TerminatedByParentTask, parentRuntimeInformation.StopAllTasks);
return;
}
Execute();
@@ -203,7 +203,7 @@ public class YesNtInterpreter
if (!statementFound)
{
runtimeInfo.Exit("Invalid statement", true);
runtimeInfo.Exit(ExitMessages.InvalidStatement, true);
}
if (runtimeInfo.IsDebugMode && notSearchingLabel)
{
@@ -216,15 +216,15 @@ public class YesNtInterpreter
{
if (!string.IsNullOrWhiteSpace(runtimeInfo.SearchLabel))
{
runtimeInfo.Exit($"Label \"{runtimeInfo.SearchLabel}\" not found", true);
runtimeInfo.Exit(ExitMessages.LabelNotFound(runtimeInfo.SearchLabel), true);
}
else if (!string.IsNullOrWhiteSpace(runtimeInfo.SearchFunction))
{
runtimeInfo.Exit($"Function \"{runtimeInfo.SearchFunction}\" not found", true);
runtimeInfo.Exit(ExitMessages.FunctionNotFound(runtimeInfo.SearchFunction), true);
}
else
{
runtimeInfo.Exit("End of file", false);
runtimeInfo.Exit(ExitMessages.EndOfFile, false);
}
if (runtimeInfo.IsDebugMode)
@@ -32,7 +32,7 @@ internal class CodeFlowStatements : StatementRuntimeInformation
string[] parts = args.Split(" goto ", 2, StringSplitOptions.None);
if (parts.Length != 2)
{
RuntimeInfo.Exit("Invalid syntax", true);
RuntimeInfo.Exit(ExitMessages.InvalidSyntax, true);
return;
}
@@ -43,7 +43,7 @@ internal class CodeFlowStatements : StatementRuntimeInformation
if (result is null)
{
RuntimeInfo.Exit("Invalid operation", true);
RuntimeInfo.Exit(ExitMessages.InvalidOperation, true);
return;
}
@@ -69,14 +69,14 @@ internal class CodeFlowStatements : StatementRuntimeInformation
string labelDeclaration = args.Trim();
if (!labelDeclaration.EndsWith(':'))
{
RuntimeInfo.Exit("Invalid syntax. Statement must end with ':'", true);
RuntimeInfo.Exit(ExitMessages.InvalidSyntaxColonRequired, true);
return;
}
string key = NormalizeBlockName(labelDeclaration);
if (string.IsNullOrWhiteSpace(key))
{
RuntimeInfo.Exit("Invalid syntax", true);
RuntimeInfo.Exit(ExitMessages.InvalidSyntax, true);
return;
}
@@ -120,7 +120,7 @@ internal class CodeFlowStatements : StatementRuntimeInformation
string[] parts = args.Split(" call ", 2, StringSplitOptions.None);
if (parts.Length != 2)
{
RuntimeInfo.Exit("Invalid syntax", true);
RuntimeInfo.Exit(ExitMessages.InvalidSyntax, true);
return;
}
@@ -131,7 +131,7 @@ internal class CodeFlowStatements : StatementRuntimeInformation
if (result is null)
{
RuntimeInfo.Exit("Invalid operation", true);
RuntimeInfo.Exit(ExitMessages.InvalidOperation, true);
return;
}
@@ -159,7 +159,7 @@ internal class CodeFlowStatements : StatementRuntimeInformation
args = args.Trim();
if (!args.EndsWith(':'))
{
RuntimeInfo.Exit("Invalid syntax. Statement must end with ':'", true);
RuntimeInfo.Exit(ExitMessages.InvalidSyntaxColonRequired, true);
return;
}
@@ -168,7 +168,7 @@ internal class CodeFlowStatements : StatementRuntimeInformation
if (result is null)
{
RuntimeInfo.Exit("Invalid operation", true);
RuntimeInfo.Exit(ExitMessages.InvalidOperation, true);
return;
}
@@ -180,7 +180,7 @@ internal class CodeFlowStatements : StatementRuntimeInformation
(int targetLine, _) = FindElseOrEndIf(RuntimeInfo.LineNumber);
if (targetLine < 0)
{
RuntimeInfo.Exit("No matching end_if found", true);
RuntimeInfo.Exit(ExitMessages.NoMatchingEndIf, true);
return;
}
@@ -193,7 +193,7 @@ internal class CodeFlowStatements : StatementRuntimeInformation
int targetLine = FindEndIf(RuntimeInfo.LineNumber);
if (targetLine < 0)
{
RuntimeInfo.Exit("No matching end_if found", true);
RuntimeInfo.Exit(ExitMessages.NoMatchingEndIf, true);
return;
}
@@ -211,7 +211,7 @@ internal class CodeFlowStatements : StatementRuntimeInformation
args = args.Trim();
if (!args.EndsWith(':'))
{
RuntimeInfo.Exit("Invalid syntax. Statement must end with ':'", true);
RuntimeInfo.Exit(ExitMessages.InvalidSyntaxColonRequired, true);
return;
}
@@ -220,7 +220,7 @@ internal class CodeFlowStatements : StatementRuntimeInformation
if (result is null)
{
RuntimeInfo.Exit("Invalid operation", true);
RuntimeInfo.Exit(ExitMessages.InvalidOperation, true);
return;
}
@@ -232,7 +232,7 @@ internal class CodeFlowStatements : StatementRuntimeInformation
int endWhileLine = FindEndWhile(RuntimeInfo.LineNumber);
if (endWhileLine < 0)
{
RuntimeInfo.Exit("No matching end_while found", true);
RuntimeInfo.Exit(ExitMessages.NoMatchingEndWhile, true);
return;
}
@@ -245,7 +245,7 @@ internal class CodeFlowStatements : StatementRuntimeInformation
int whileLine = FindWhile(RuntimeInfo.LineNumber);
if (whileLine < 0)
{
RuntimeInfo.Exit("No matching while found", true);
RuntimeInfo.Exit(ExitMessages.NoMatchingWhile, true);
return;
}
@@ -260,7 +260,7 @@ internal class CodeFlowStatements : StatementRuntimeInformation
RuntimeInfo.IsInFunction = false;
if (RuntimeInfo.IsLocalSearch)
{
RuntimeInfo.Exit($"Label \"{RuntimeInfo.SearchLabel}\" not found", true);
RuntimeInfo.Exit(ExitMessages.LabelNotFound(RuntimeInfo.SearchLabel), true);
}
return;
@@ -270,7 +270,7 @@ internal class CodeFlowStatements : StatementRuntimeInformation
RuntimeInfo.IsInFunction = false;
}
RuntimeInfo.Exit("Planned termination by code", false);
RuntimeInfo.Exit(ExitMessages.PlannedTermination, false);
}
[Statement("abort_all", SearchMode.Exact, SpaceAround.None, ConsoleColor.Red, ExecuteInSearchMode = true)]
@@ -281,7 +281,7 @@ internal class CodeFlowStatements : StatementRuntimeInformation
RuntimeInfo.IsInFunction = false;
if (RuntimeInfo.IsLocalSearch)
{
RuntimeInfo.Exit($"Label \"{RuntimeInfo.SearchLabel}\" not found", true);
RuntimeInfo.Exit(ExitMessages.LabelNotFound(RuntimeInfo.SearchLabel), true);
}
return;
@@ -291,7 +291,7 @@ internal class CodeFlowStatements : StatementRuntimeInformation
RuntimeInfo.IsInFunction = false;
}
RuntimeInfo.Exit("Planned termination by code. Canceling all tasks", true);
RuntimeInfo.Exit(ExitMessages.PlannedTerminationCancelingTasks, true);
}
[Statement("throw", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.Red)]
@@ -37,7 +37,7 @@ internal class ConsoleStatements : StatementRuntimeInformation
string input = Console.ReadLine();
if (input is null)
{
RuntimeInfo.Exit("Terminated by external process", true);
RuntimeInfo.Exit(ExitMessages.TerminatedByExternalProcess, true);
return;
}
args = args.ReplaceFirstOccurrence("%read_line ", input.ToSafeString() + " ");
@@ -15,21 +15,21 @@ internal class FunctionStatements : StatementRuntimeInformation
{
if (RuntimeInfo.InternalIsInFunction)
{
RuntimeInfo.Exit("Nested functions are not allowed", true);
RuntimeInfo.Exit(ExitMessages.NestedFunctionsNotAllowed, true);
return;
}
string functionDeclaration = args.Trim();
if (!functionDeclaration.EndsWith(':'))
{
RuntimeInfo.Exit("Invalid syntax. Statement must end with ':'", true);
RuntimeInfo.Exit(ExitMessages.InvalidSyntaxColonRequired, true);
return;
}
string key = NormalizeBlockName(functionDeclaration);
if (string.IsNullOrWhiteSpace(key))
{
RuntimeInfo.Exit("Invalid syntax", true);
RuntimeInfo.Exit(ExitMessages.InvalidSyntax, true);
return;
}
@@ -63,7 +63,7 @@ internal class FunctionStatements : StatementRuntimeInformation
{
if (RuntimeInfo.OutParametersStack.Count == 0)
{
RuntimeInfo.Exit("No out argument in stack", true);
RuntimeInfo.Exit(ExitMessages.NoOutArgumentInStack, true);
return;
}
@@ -87,7 +87,7 @@ internal class FunctionStatements : StatementRuntimeInformation
string[] parts = args.Split(" with ", 2, StringSplitOptions.None);
if (parts.Length != 2)
{
RuntimeInfo.Exit("Invalid syntax", true);
RuntimeInfo.Exit(ExitMessages.InvalidSyntax, true);
return;
}
@@ -118,7 +118,7 @@ internal class FunctionStatements : StatementRuntimeInformation
{
if (!RuntimeInfo.IsInFunction)
{
RuntimeInfo.Exit("Statement not allowed outside of function", true);
RuntimeInfo.Exit(ExitMessages.StatementNotAllowedOutsideFunction, true);
return;
}
@@ -126,7 +126,7 @@ internal class FunctionStatements : StatementRuntimeInformation
{
if (RuntimeInfo.FunctionCallStack.Peek().Arguments.Count == 0)
{
RuntimeInfo.Exit("No in argument in stack", true);
RuntimeInfo.Exit(ExitMessages.NoInArgumentInStack, true);
return;
}
@@ -141,7 +141,7 @@ internal class FunctionStatements : StatementRuntimeInformation
{
if (!RuntimeInfo.IsInFunction)
{
RuntimeInfo.Exit("Statement not allowed outside of function", true);
RuntimeInfo.Exit(ExitMessages.StatementNotAllowedOutsideFunction, true);
return;
}
@@ -155,7 +155,7 @@ internal class FunctionStatements : StatementRuntimeInformation
{
if (!RuntimeInfo.IsInFunction)
{
RuntimeInfo.Exit("Statement not allowed outside of function", true);
RuntimeInfo.Exit(ExitMessages.StatementNotAllowedOutsideFunction, true);
return;
}
@@ -167,7 +167,7 @@ internal class FunctionStatements : StatementRuntimeInformation
{
if (!RuntimeInfo.IsInFunction)
{
RuntimeInfo.Exit("Statement not allowed outside of function", true);
RuntimeInfo.Exit(ExitMessages.StatementNotAllowedOutsideFunction, true);
return;
}
@@ -177,7 +177,7 @@ internal class FunctionStatements : StatementRuntimeInformation
if (RuntimeInfo.IsLocalSearch)
{
RuntimeInfo.Exit($"Label \"{RuntimeInfo.SearchLabel}\" not found", true);
RuntimeInfo.Exit(ExitMessages.LabelNotFound(RuntimeInfo.SearchLabel), true);
}
return;
}
@@ -195,7 +195,7 @@ internal class FunctionStatements : StatementRuntimeInformation
}
else
{
RuntimeInfo.Exit("No function in stack", true);
RuntimeInfo.Exit(ExitMessages.NoFunctionInStack, true);
}
}
@@ -42,7 +42,7 @@ internal class ListStatements : StatementRuntimeInformation
if (!RuntimeInfo.Lists.ContainsKey(name))
{
RuntimeInfo.Exit($"List \"{name}\" not found", true);
RuntimeInfo.Exit(ExitMessages.ListNotFound(name), true);
return;
}
@@ -100,7 +100,7 @@ internal class ListStatements : StatementRuntimeInformation
if (string.IsNullOrWhiteSpace(parts[1]))
{
RuntimeInfo.Exit("Invalid syntax", true);
RuntimeInfo.Exit(ExitMessages.InvalidSyntax, true);
return;
}
@@ -213,7 +213,7 @@ internal class ListStatements : StatementRuntimeInformation
string[] parts = input.Split(separator, 2, StringSplitOptions.None);
if (parts.Length != 2)
{
RuntimeInfo.Exit("Invalid syntax", true);
RuntimeInfo.Exit(ExitMessages.InvalidSyntax, true);
return [];
}
@@ -222,7 +222,7 @@ internal class ListStatements : StatementRuntimeInformation
if (string.IsNullOrWhiteSpace(parts[0]))
{
RuntimeInfo.Exit("Invalid syntax", true);
RuntimeInfo.Exit(ExitMessages.InvalidSyntax, true);
return [];
}
@@ -233,7 +233,7 @@ internal class ListStatements : StatementRuntimeInformation
{
if (!RuntimeInfo.Lists.TryGetValue(name, out list))
{
RuntimeInfo.Exit($"List \"{name}\" not found", true);
RuntimeInfo.Exit(ExitMessages.ListNotFound(name), true);
return false;
}
@@ -245,7 +245,7 @@ internal class ListStatements : StatementRuntimeInformation
string[] parts = input.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length != 2)
{
RuntimeInfo.Exit("Invalid syntax", true);
RuntimeInfo.Exit(ExitMessages.InvalidSyntax, true);
return [];
}
@@ -259,13 +259,13 @@ internal class ListStatements : StatementRuntimeInformation
bool success = int.TryParse(rawIndex.Trim(), out index);
if (!success)
{
RuntimeInfo.Exit($"\"{rawIndex}\" is not a valid index", true);
RuntimeInfo.Exit(ExitMessages.InvalidIndex(rawIndex), true);
return false;
}
if (index < 0 || index >= maxExclusive)
{
RuntimeInfo.Exit($"Index {index} out of range", true);
RuntimeInfo.Exit(ExitMessages.IndexOutOfRange(index), true);
return false;
}
@@ -23,7 +23,7 @@ internal partial class ProcessingStatements : StatementRuntimeInformation
string res = Evaluator.Calculate(matches[i].Value);
if (res is null)
{
RuntimeInfo.Exit("Invalid operation", true);
RuntimeInfo.Exit(ExitMessages.InvalidOperation, true);
return;
}
args = args.FromSafeString().Replace(matches[i].Value, res);
@@ -65,7 +65,7 @@ internal partial class ProcessingStatements : StatementRuntimeInformation
}
else
{
RuntimeInfo.Exit($"\"{args}\" is not a valid time-out value", true);
RuntimeInfo.Exit(ExitMessages.InvalidTimeoutValue(args), true);
}
}
@@ -104,12 +104,12 @@ internal partial class ProcessingStatements : StatementRuntimeInformation
}
catch
{
RuntimeInfo.Exit($"Could not load file \"{path}\"", true);
RuntimeInfo.Exit(ExitMessages.CouldNotLoadFile(path), true);
}
}
else
{
RuntimeInfo.Exit($"Could not find file \"{path}\"", true);
RuntimeInfo.Exit(ExitMessages.CouldNotFindFile(path), true);
}
}
@@ -53,7 +53,7 @@ internal class StringLiteralStatements : StatementRuntimeInformation
if (!closed)
{
RuntimeInfo.Exit("Invalid string literal", true);
RuntimeInfo.Exit(ExitMessages.InvalidStringLiteral, true);
return;
}
@@ -33,11 +33,11 @@ internal class SystemStatements : StatementRuntimeInformation
}
catch (FileNotFoundException)
{
RuntimeInfo.Exit($"Cannot find file \"{parts[0]}\".", false);
RuntimeInfo.Exit(ExitMessages.CannotFindFile(parts[0]), false);
}
catch (Win32Exception ex)
{
RuntimeInfo.Exit($"Failed to start \"{parts[0]}\". {ex.Message}", false);
RuntimeInfo.Exit(ExitMessages.FailedToStart(parts[0], ex.Message), false);
}
// HACK: Clear line to avoid execution from another "exec" statement.
@@ -53,11 +53,11 @@ internal class SystemStatements : StatementRuntimeInformation
}
catch (FileNotFoundException)
{
RuntimeInfo.Exit($"Cannot find file \"{input}\".", false);
RuntimeInfo.Exit(ExitMessages.CannotFindFile(input), false);
}
catch (Win32Exception ex)
{
RuntimeInfo.Exit($"Failed to start \"{input}\". {ex.Message}", false);
RuntimeInfo.Exit(ExitMessages.FailedToStart(input, ex.Message), false);
}
}
@@ -17,7 +17,7 @@ internal partial class VariableStatements : StatementRuntimeInformation
string key = parts[0].Trim();
if (key.Contains(' '))
{
RuntimeInfo.Exit("Invalid Syntax", true);
RuntimeInfo.Exit(ExitMessages.InvalidSyntax, true);
}
if (RuntimeInfo.Variables.ContainsKey(key))
@@ -31,7 +31,7 @@ internal partial class VariableStatements : StatementRuntimeInformation
}
else
{
RuntimeInfo.Exit("Invalid syntax", true);
RuntimeInfo.Exit(ExitMessages.InvalidSyntax, true);
}
}
@@ -44,7 +44,7 @@ internal partial class VariableStatements : StatementRuntimeInformation
string key = parts[0].Trim();
if (key.Contains(' '))
{
RuntimeInfo.Exit("Invalid Syntax", true);
RuntimeInfo.Exit(ExitMessages.InvalidSyntax, true);
}
if (RuntimeInfo.GlobalVariables.ContainsKey(key))
@@ -58,7 +58,7 @@ internal partial class VariableStatements : StatementRuntimeInformation
}
else
{
RuntimeInfo.Exit("Invalid syntax", true);
RuntimeInfo.Exit(ExitMessages.InvalidSyntax, true);
}
}
@@ -77,7 +77,7 @@ internal partial class VariableStatements : StatementRuntimeInformation
}
else
{
RuntimeInfo.Exit($"Variable \"{key}\" not found", true);
RuntimeInfo.Exit(ExitMessages.VariableNotFound(key), true);
}
}
@@ -109,7 +109,7 @@ internal partial class VariableStatements : StatementRuntimeInformation
}
else if (!RuntimeInfo.IsSearching)
{
RuntimeInfo.Exit($"Variable \"{varName}\" not found", true);
RuntimeInfo.Exit(ExitMessages.VariableNotFound(varName), true);
return;
}
}