mirror of
https://github.com/Stone-Red-Code/YesNt-Interpreter.git
synced 2026-09-04 09:06:41 +02:00
Reformatting and reimperilment escape codes
This commit is contained in:
+18
-20
@@ -4,14 +4,15 @@ using System.IO;
|
||||
|
||||
using YesNt.Interpreter.Runtime;
|
||||
|
||||
namespace YesNt.CodeEditor
|
||||
namespace YesNt.CodeEditor;
|
||||
|
||||
internal class TextEditor
|
||||
{
|
||||
internal class TextEditor
|
||||
{
|
||||
private readonly InputHandler inputHandler;
|
||||
private readonly SyntaxHighlighter syntaxHighlighter;
|
||||
private readonly List<string> debugOutput = new();
|
||||
|
||||
private readonly Point oldSize = new Point(0, 0);
|
||||
public YesNtInterpreter YesNtInterpreter { get; } = new();
|
||||
public int LineOffset { get; set; } = 0;
|
||||
public List<string> Lines { get; } = new();
|
||||
@@ -177,6 +178,14 @@ namespace YesNt.CodeEditor
|
||||
}
|
||||
}
|
||||
|
||||
public int GetSpacing()
|
||||
{
|
||||
int padding = (Console.WindowHeight + LineOffset - 3).ToString().Length;
|
||||
padding = Math.Max(padding, Lines.Count.ToString().Length);
|
||||
padding += 1;
|
||||
return padding;
|
||||
}
|
||||
|
||||
private void YesNtInterpreter_OnDebugOutput(string output)
|
||||
{
|
||||
debugOutput.Add(output);
|
||||
@@ -225,16 +234,6 @@ namespace YesNt.CodeEditor
|
||||
}
|
||||
}
|
||||
|
||||
public int GetSpacing()
|
||||
{
|
||||
int padding = (Console.WindowHeight + LineOffset - 3).ToString().Length;
|
||||
padding = Math.Max(padding, Lines.Count.ToString().Length);
|
||||
padding += 1;
|
||||
return padding;
|
||||
}
|
||||
|
||||
private readonly Point oldSize = new Point(0, 0);
|
||||
|
||||
private bool SizeChanged()
|
||||
{
|
||||
if (oldSize.X != Console.WindowWidth || oldSize.Y != Console.WindowHeight)
|
||||
@@ -245,10 +244,10 @@ namespace YesNt.CodeEditor
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal class Point
|
||||
{
|
||||
internal class Point
|
||||
{
|
||||
public int X { get; set; }
|
||||
public int Y { get; set; }
|
||||
|
||||
@@ -257,12 +256,11 @@ namespace YesNt.CodeEditor
|
||||
X = x;
|
||||
Y = y;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal enum Mode
|
||||
{
|
||||
internal enum Mode
|
||||
{
|
||||
Edit,
|
||||
Command,
|
||||
Debug
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
|
||||
namespace YesNt.CodeEditor
|
||||
namespace YesNt.CodeEditor;
|
||||
|
||||
internal class InputHandler
|
||||
{
|
||||
internal class InputHandler
|
||||
{
|
||||
private readonly TextEditor textEditor;
|
||||
|
||||
public InputHandler(TextEditor textEditor)
|
||||
@@ -126,8 +126,13 @@ namespace YesNt.CodeEditor
|
||||
|
||||
textEditor.Lines[textEditor.CursorPosition.Y] = lineBuilder.ToString();
|
||||
|
||||
if (keyInfo.Key == ConsoleKey.Backspace)
|
||||
if (keyInfo.Key is ConsoleKey.Backspace or ConsoleKey.Delete)
|
||||
{
|
||||
if (keyInfo.Key == ConsoleKey.Delete)
|
||||
{
|
||||
textEditor.CursorPosition.X++;
|
||||
}
|
||||
|
||||
if (textEditor.CursorPosition.X > 0)
|
||||
{
|
||||
if (textEditor.Lines[textEditor.CursorPosition.Y][textEditor.CursorPosition.X - 1] == ' ' && textEditor.CursorPosition.X > textEditor.Lines[textEditor.CursorPosition.Y].TrimEnd().Length)
|
||||
@@ -309,5 +314,4 @@ namespace YesNt.CodeEditor
|
||||
Console.SetCursorPosition(0, Console.WindowHeight - 1);
|
||||
Console.Write(input + new string(' ', Console.WindowWidth - input.Length - 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,4 @@
|
||||
namespace YesNt.CodeEditor
|
||||
{
|
||||
internal static class Program
|
||||
{
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
TextEditor textEditor = args.Length > 0 ? new TextEditor(args[0]) : new TextEditor();
|
||||
textEditor.Run();
|
||||
}
|
||||
}
|
||||
}
|
||||
using YesNt.CodeEditor;
|
||||
|
||||
TextEditor textEditor = args.Length > 0 ? new TextEditor(args[0]) : new TextEditor();
|
||||
textEditor.Run();
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
using YesNt.Interpreter.Enums;
|
||||
@@ -11,10 +12,12 @@ namespace YesNt.CodeEditor;
|
||||
internal partial class SyntaxHighlighter
|
||||
{
|
||||
private readonly ReadOnlyCollection<StatementInformation> statementInformation;
|
||||
private readonly string[] replacementValues;
|
||||
|
||||
public SyntaxHighlighter(ReadOnlyCollection<StatementInformation> statementInformation)
|
||||
{
|
||||
this.statementInformation = statementInformation;
|
||||
replacementValues = StringExtentions.ReplacementRules.Values.ToArray();
|
||||
}
|
||||
|
||||
public static string Base64Encode(string plainText)
|
||||
@@ -39,10 +42,9 @@ internal partial class SyntaxHighlighter
|
||||
}
|
||||
else
|
||||
{
|
||||
MatchCollection matches = EscapeSequenceRegex().Matches(input);
|
||||
for (int i = 0; i < matches.Count; i++)
|
||||
for (int i = 0; i < replacementValues.Length; i++)
|
||||
{
|
||||
input = AddColorInformation(input, matches[i].Value, ConsoleColor.DarkYellow, SearchMode.Contains);
|
||||
input = AddColorInformation(input, replacementValues[i], ConsoleColor.Blue, SearchMode.Contains);
|
||||
}
|
||||
|
||||
foreach (StatementInformation statement in statementInformation)
|
||||
@@ -110,7 +112,7 @@ internal partial class SyntaxHighlighter
|
||||
}
|
||||
}
|
||||
|
||||
matches = VariableRegex().Matches(input);
|
||||
MatchCollection matches = VariableRegex().Matches(input);
|
||||
for (int i = 0; i < matches.Count; i++)
|
||||
{
|
||||
input = AddColorInformation(input, matches[i].Value, ConsoleColor.Cyan, SearchMode.Contains);
|
||||
@@ -171,9 +173,6 @@ internal partial class SyntaxHighlighter
|
||||
return reult;
|
||||
}
|
||||
|
||||
[GeneratedRegex("!!.")]
|
||||
private static partial Regex EscapeSequenceRegex();
|
||||
|
||||
[GeneratedRegex("^<[a-zA-Z0-9]+")]
|
||||
private static partial Regex VariableDeclarationRegex();
|
||||
|
||||
|
||||
@@ -38,7 +38,6 @@ public class CodeFlowTests
|
||||
[TestMethod]
|
||||
public void CalculationsTest()
|
||||
{
|
||||
Assert.Inconclusive();
|
||||
YesNtAssert.IsLineEqual("10 * 10 !calc", 20.ToString());
|
||||
YesNtAssert.IsLineEqual("10 * 10 !calc", 100.ToString());
|
||||
}
|
||||
}
|
||||
@@ -60,4 +60,26 @@ internal static class YesNtAssert
|
||||
|
||||
Assert.AreEqual(expected, debugEventArgs.CurrentLine);
|
||||
}
|
||||
|
||||
public static void IsLineNotEqual(string line, string expected, int timeout = 1000)
|
||||
{
|
||||
AutoResetEvent onDone = new AutoResetEvent(false);
|
||||
List<string> lines = new List<string>()
|
||||
{
|
||||
line
|
||||
};
|
||||
|
||||
DebugEventArgs debugEventArgs = new DebugEventArgs();
|
||||
yesNtInterpreter.OnLineExecuted += (er) =>
|
||||
{
|
||||
debugEventArgs = er ?? debugEventArgs;
|
||||
_ = onDone.Set();
|
||||
};
|
||||
|
||||
yesNtInterpreter.Execute(lines, true);
|
||||
|
||||
_ = onDone.WaitOne(TimeSpan.FromMilliseconds(timeout));
|
||||
|
||||
Assert.AreNotEqual(expected, debugEventArgs.CurrentLine);
|
||||
}
|
||||
}
|
||||
@@ -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,9 +60,15 @@ internal partial class ProcessingStatements : StatementRuntimeInformation
|
||||
[Statement("slp", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.Magenta)]
|
||||
public void Sleep(string args)
|
||||
{
|
||||
_ = int.TryParse(args, out int millisecondsTimeout);
|
||||
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)]
|
||||
public void Length(string args)
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user