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
@@ -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;
}
}