Reformatting and reimperilment escape codes

This commit is contained in:
Stone_Red
2023-09-27 17:17:28 +02:00
parent 79df5c9b97
commit ce3715c1b5
9 changed files with 591 additions and 543 deletions
@@ -38,23 +38,6 @@ internal partial class ProcessingStatements : StatementRuntimeInformation
RuntimeInfo.CurrentLine = args.FromSafeString();
}
[Statement("!!", SearchMode.Contains, SpaceAround.None, ConsoleColor.DarkYellow, Priority = Priority.PreProcessing, KeepStatementInArgs = true)]
public void DontEvaluate(string args)
{
int index;
while ((index = args.IndexOf("!!")) != -1)
{
args = args.Remove(index, 2);
if (index < args.Length)
{
char charToEscape = args[index];
args = args.Remove(index, 1);
args = args.Insert(index, charToEscape.ToString().ToSafeString());
}
}
RuntimeInfo.CurrentLine = args;
}
[Statement("!task", SearchMode.EndOfLine, SpaceAround.Start, ConsoleColor.DarkYellow, Priority = Priority.VeryHigh)]
public void RunTask(string line)
{
@@ -77,8 +60,14 @@ internal partial class ProcessingStatements : StatementRuntimeInformation
[Statement("slp", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.Magenta)]
public void Sleep(string args)
{
_ = int.TryParse(args, out int millisecondsTimeout);
ConsoleExtentions.Sleep(millisecondsTimeout, RuntimeInfo);
if (int.TryParse(args, out int millisecondsTimeout))
{
ConsoleExtentions.Sleep(millisecondsTimeout, RuntimeInfo);
}
else
{
RuntimeInfo.Exit($"\"{args}\" is not a valid time-out value", true);
}
}
[Statement("len", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.Magenta)]
@@ -91,6 +91,11 @@ internal partial class VariableStatements : StatementRuntimeInformation
MatchCollection matches = VariableStatementRegex().Matches(RuntimeInfo.CurrentLine);
if (matches.Count <= 0)
{
RuntimeInfo.Exit("Invalid syntax", true);
}
for (int i = 0; i < matches.Count; i++)
{
string varName = matches[i].Value.Replace(">", string.Empty);
@@ -1,11 +1,34 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Text;
namespace YesNt.Interpreter.Utilities;
public static class StringExtentions
{
private static readonly Dictionary<string, string> reverseReplacementRules;
public static Dictionary<string, string> ReplacementRules { get; } = new()
{
{"~", "~til" },
{" ", "~spc" },
{"%", "~per" },
{"<", "~let" },
{">", "~grt" },
{",", "~com" },
{"!", "~exm" },
{"|", "~pip" }
};
[SuppressMessage("Minor Code Smell", "S3963:\"static\" fields should be initialized inline", Justification = "Doesn't work because it throws a TypeInitializationException")]
static StringExtentions()
{
reverseReplacementRules = ReplacementRules.ToDictionary(x => x.Value, x => x.Key);
}
public static string ToSafeString(this string input)
{
StringBuilder output = new StringBuilder();
@@ -13,16 +36,32 @@ public static class StringExtentions
{
_ = output.Append($"\v{c}\v");
}
return output
.ToString()
.Replace(' ', '~');
return ReplaceOnce(output.ToString(), ReplacementRules);
}
public static string FromSafeString(this string input)
{
return input
.Replace("\v", "")
.Replace('~', ' ');
return ReplaceOnce(input.Replace("\v", ""), reverseReplacementRules);
}
public static string ReplaceOnce(string input, Dictionary<string, string> replacementRules)
{
IEnumerable<KeyValuePair<string, string>> matches = replacementRules.Where(rule => input.Contains(rule.Key));
if (!matches.Any())
{
return input;
}
KeyValuePair<string, string> match = matches.First();
int startIndex = input.IndexOf(match.Key);
int endIndex = startIndex + match.Key.Length;
string before = ReplaceOnce(input[..startIndex], replacementRules);
string replaced = match.Value;
string after = ReplaceOnce(input[endIndex..], replacementRules);
return before + replaced + after;
}
public static bool ToStandardizedNumber(this string input, out double result)