Upgrade to .NET 8 and code cleanup

This commit is contained in:
Stone_Red
2024-08-07 19:48:03 +02:00
parent 84afe70c96
commit 7078ed5637
33 changed files with 960 additions and 1008 deletions
+3
View File
@@ -114,3 +114,6 @@ dotnet_naming_style.pascal_case.required_prefix =
dotnet_naming_style.pascal_case.required_suffix = dotnet_naming_style.pascal_case.required_suffix =
dotnet_naming_style.pascal_case.word_separator = dotnet_naming_style.pascal_case.word_separator =
dotnet_naming_style.pascal_case.capitalization = pascal_case dotnet_naming_style.pascal_case.capitalization = pascal_case
# IDE0305: Simplify collection initialization
dotnet_diagnostic.IDE0305.severity = none
+5 -11
View File
@@ -11,12 +11,12 @@ internal class TextEditor
{ {
private readonly InputHandler inputHandler; private readonly InputHandler inputHandler;
private readonly SyntaxHighlighter syntaxHighlighter; private readonly SyntaxHighlighter syntaxHighlighter;
private readonly List<string> debugOutput = new(); private readonly List<string> debugOutput = [];
private readonly Point oldSize = new Point(0, 0); private readonly Point oldSize = new Point(0, 0);
public YesNtInterpreter YesNtInterpreter { get; } = new(); public YesNtInterpreter YesNtInterpreter { get; } = new();
public int LineOffset { get; set; } = 0; public int LineOffset { get; set; } = 0;
public List<string> Lines { get; } = new(); public List<string> Lines { get; } = [];
public Point CursorPosition { get; } = new(0, 0); public Point CursorPosition { get; } = new(0, 0);
public Mode EditMode { get; set; } = Mode.Command; public Mode EditMode { get; set; } = Mode.Command;
public string CurrentPath { get; set; } = string.Empty; public string CurrentPath { get; set; } = string.Empty;
@@ -263,16 +263,10 @@ internal class TextEditor
} }
} }
internal class Point internal class Point(int x, int y)
{ {
public int X { get; set; } public int X { get; set; } = x;
public int Y { get; set; } public int Y { get; set; } = y;
public Point(int x, int y)
{
X = x;
Y = y;
}
} }
internal enum Mode internal enum Mode
+2 -7
View File
@@ -3,14 +3,9 @@ using System.Text;
namespace YesNt.CodeEditor; namespace YesNt.CodeEditor;
internal class InputHandler internal class InputHandler(TextEditor textEditor)
{ {
private readonly TextEditor textEditor; private readonly TextEditor textEditor = textEditor;
public InputHandler(TextEditor textEditor)
{
this.textEditor = textEditor;
}
public bool HandleInput() public bool HandleInput()
{ {
+17 -23
View File
@@ -9,16 +9,10 @@ using YesNt.Interpreter.Utilities;
namespace YesNt.CodeEditor; namespace YesNt.CodeEditor;
internal partial class SyntaxHighlighter internal partial class SyntaxHighlighter(ReadOnlyCollection<StatementInformation> statementInformation)
{ {
private readonly ReadOnlyCollection<StatementInformation> statementInformation; private readonly ReadOnlyCollection<StatementInformation> statementInformation = statementInformation;
private readonly string[] replacementValues; private readonly string[] replacementValues = StringExtensions.ReplacementRules.Values.ToArray();
public SyntaxHighlighter(ReadOnlyCollection<StatementInformation> statementInformation)
{
this.statementInformation = statementInformation;
replacementValues = StringExtentions.ReplacementRules.Values.ToArray();
}
public static string Base64Encode(string plainText) public static string Base64Encode(string plainText)
{ {
@@ -64,11 +58,11 @@ internal partial class SyntaxHighlighter
input = input.TrimEnd(' '); input = input.TrimEnd(' ');
if (statement.SearchMode == SearchMode.StartOfLine && input.StartsWith(name)) if (statement.SearchMode == SearchMode.StartOfLine && input.StartsWith(name))
{ {
if (statement.Seperator is not null && input.Contains(statement.Seperator)) if (statement.Separator is not null && input.Contains(statement.Separator))
{ {
input = AddColorInformation(input, statement.Seperator, statement.Color, SearchMode.StartOfLine); input = AddColorInformation(input, statement.Separator, statement.Color, SearchMode.StartOfLine);
} }
else if (statement.Seperator is not null) else if (statement.Separator is not null)
{ {
continue; continue;
} }
@@ -76,11 +70,11 @@ internal partial class SyntaxHighlighter
} }
else if (statement.SearchMode == SearchMode.Contains && input.Contains(name)) else if (statement.SearchMode == SearchMode.Contains && input.Contains(name))
{ {
if (statement.Seperator is not null && input.Contains(statement.Seperator)) if (statement.Separator is not null && input.Contains(statement.Separator))
{ {
input = AddColorInformation(input, statement.Seperator, statement.Color, SearchMode.StartOfLine); input = AddColorInformation(input, statement.Separator, statement.Color, SearchMode.StartOfLine);
} }
else if (statement.Seperator is not null) else if (statement.Separator is not null)
{ {
continue; continue;
} }
@@ -88,11 +82,11 @@ internal partial class SyntaxHighlighter
} }
else if (statement.SearchMode == SearchMode.EndOfLine && input.EndsWith(name)) else if (statement.SearchMode == SearchMode.EndOfLine && input.EndsWith(name))
{ {
if (statement.Seperator is not null && input.Contains(statement.Seperator)) if (statement.Separator is not null && input.Contains(statement.Separator))
{ {
input = AddColorInformation(input, statement.Seperator, statement.Color, SearchMode.StartOfLine); input = AddColorInformation(input, statement.Separator, statement.Color, SearchMode.StartOfLine);
} }
else if (statement.Seperator is not null) else if (statement.Separator is not null)
{ {
continue; continue;
} }
@@ -100,11 +94,11 @@ internal partial class SyntaxHighlighter
} }
else if (statement.SearchMode == SearchMode.Exact && input.Equals(name)) else if (statement.SearchMode == SearchMode.Exact && input.Equals(name))
{ {
if (statement.Seperator is not null && input.Contains(statement.Seperator)) if (statement.Separator is not null && input.Contains(statement.Separator))
{ {
input = AddColorInformation(input, statement.Seperator, statement.Color, SearchMode.StartOfLine); input = AddColorInformation(input, statement.Separator, statement.Color, SearchMode.StartOfLine);
} }
else if (statement.Seperator is not null) else if (statement.Separator is not null)
{ {
continue; continue;
} }
@@ -164,13 +158,13 @@ internal partial class SyntaxHighlighter
string base64Value = Base64Encode(value.TrimEnd()); string base64Value = Base64Encode(value.TrimEnd());
string reult = searchMode switch string result = searchMode switch
{ {
SearchMode.StartOfLine => originalString.ReplaceFirstOccurrence(value, $"\0\r{(int)color}\r{base64Value}\0" + new string(' ', spacesAtEnd)), SearchMode.StartOfLine => originalString.ReplaceFirstOccurrence(value, $"\0\r{(int)color}\r{base64Value}\0" + new string(' ', spacesAtEnd)),
SearchMode.EndOfLine => originalString.ReplaceLastOccurrence(value, $"\0\r{(int)color}\r{base64Value}\0" + new string(' ', spacesAtEnd)), SearchMode.EndOfLine => originalString.ReplaceLastOccurrence(value, $"\0\r{(int)color}\r{base64Value}\0" + new string(' ', spacesAtEnd)),
_ => originalString.Replace(value, $"\0\r{(int)color}\r{base64Value}\0" + new string(' ', spacesAtEnd)) _ => originalString.Replace(value, $"\0\r{(int)color}\r{base64Value}\0" + new string(' ', spacesAtEnd))
}; };
return reult; return result;
} }
[GeneratedRegex("^<[a-zA-Z0-9]+")] [GeneratedRegex("^<[a-zA-Z0-9]+")]
+1 -1
View File
@@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<Platforms>AnyCPU;x64</Platforms> <Platforms>AnyCPU;x64</Platforms>
</PropertyGroup> </PropertyGroup>
+6 -6
View File
@@ -10,28 +10,28 @@ public class CodeFlowTests
[TestMethod] [TestMethod]
public void FunctionTest() public void FunctionTest()
{ {
List<string> lines = new List<string>() List<string> lines =
{ [
"cal yes", "cal yes",
"fnc yes", "fnc yes",
"!<result = 1", "!<result = 1",
"ret", "ret",
">result" ">result"
}; ];
YesNtAssert.IsLastLineEqual(lines, "1"); YesNtAssert.IsLastLineEqual(lines, "1");
} }
[TestMethod] [TestMethod]
public void LabelsTest() public void LabelsTest()
{ {
List<string> lines = new List<string>() List<string> lines =
{ [
"<result = 1", "<result = 1",
"jmp yes", "jmp yes",
"<result = 0", "<result = 0",
"lbl yes", "lbl yes",
">result" ">result"
}; ];
YesNtAssert.IsLastLineEqual(lines, "1"); YesNtAssert.IsLastLineEqual(lines, "1");
} }
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net7.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<IsPackable>false</IsPackable> <IsPackable>false</IsPackable>
+6 -6
View File
@@ -42,10 +42,10 @@ internal static class YesNtAssert
public static void IsLineEqual(string line, string expected, int timeout = 1000) public static void IsLineEqual(string line, string expected, int timeout = 1000)
{ {
AutoResetEvent onDone = new AutoResetEvent(false); AutoResetEvent onDone = new AutoResetEvent(false);
List<string> lines = new List<string>() List<string> lines =
{ [
line line
}; ];
DebugEventArgs debugEventArgs = new DebugEventArgs(); DebugEventArgs debugEventArgs = new DebugEventArgs();
yesNtInterpreter.OnLineExecuted += (er) => yesNtInterpreter.OnLineExecuted += (er) =>
@@ -64,10 +64,10 @@ internal static class YesNtAssert
public static void IsLineNotEqual(string line, string expected, int timeout = 1000) public static void IsLineNotEqual(string line, string expected, int timeout = 1000)
{ {
AutoResetEvent onDone = new AutoResetEvent(false); AutoResetEvent onDone = new AutoResetEvent(false);
List<string> lines = new List<string>() List<string> lines =
{ [
line line
}; ];
DebugEventArgs debugEventArgs = new DebugEventArgs(); DebugEventArgs debugEventArgs = new DebugEventArgs();
yesNtInterpreter.OnLineExecuted += (er) => yesNtInterpreter.OnLineExecuted += (er) =>
@@ -2,8 +2,8 @@
using YesNt.Interpreter.Enums; using YesNt.Interpreter.Enums;
namespace YesNt.Interpreter.Attributes namespace YesNt.Interpreter.Attributes;
{
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
internal class StatementAttribute : Attribute internal class StatementAttribute : Attribute
{ {
@@ -15,7 +15,7 @@ namespace YesNt.Interpreter.Attributes
public bool ExecuteInSearchMode { get; set; } public bool ExecuteInSearchMode { get; set; }
public bool KeepStatementInArgs { get; set; } public bool KeepStatementInArgs { get; set; }
public bool IgnoreSyntaxHighlighting { get; } public bool IgnoreSyntaxHighlighting { get; }
public string Seperator { get; set; } public string Separator { get; set; }
internal StatementAttribute(string name, SearchMode searchMode, SpaceAround spaceAround, ConsoleColor color) internal StatementAttribute(string name, SearchMode searchMode, SpaceAround spaceAround, ConsoleColor color)
{ {
@@ -33,4 +33,3 @@ namespace YesNt.Interpreter.Attributes
IgnoreSyntaxHighlighting = true; IgnoreSyntaxHighlighting = true;
} }
} }
}
@@ -2,12 +2,11 @@
using YesNt.Interpreter.Enums; using YesNt.Interpreter.Enums;
namespace YesNt.Interpreter.Attributes namespace YesNt.Interpreter.Attributes;
{
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
internal class StaticStatementAttribute : Attribute internal class StaticStatementAttribute : Attribute
{ {
public bool ExecuteInSearchMode { get; set; } public bool ExecuteInSearchMode { get; set; }
public Priority Priority { get; set; } = Priority.Normal; public Priority Priority { get; set; } = Priority.Normal;
} }
}
+2 -3
View File
@@ -1,5 +1,5 @@
namespace YesNt.Interpreter.Enums namespace YesNt.Interpreter.Enums;
{
internal enum Priority internal enum Priority
{ {
PreProcessing, PreProcessing,
@@ -10,4 +10,3 @@
Low, Low,
VeryLow VeryLow
} }
}
+2 -3
View File
@@ -1,5 +1,5 @@
namespace YesNt.Interpreter.Enums namespace YesNt.Interpreter.Enums;
{
public enum SearchMode public enum SearchMode
{ {
StartOfLine, StartOfLine,
@@ -7,4 +7,3 @@
Contains, Contains,
Exact Exact
} }
}
+2 -3
View File
@@ -1,5 +1,5 @@
namespace YesNt.Interpreter.Enums namespace YesNt.Interpreter.Enums;
{
public enum SpaceAround public enum SpaceAround
{ {
StartEnd, StartEnd,
@@ -7,4 +7,3 @@
End, End,
None None
} }
}
-9
View File
@@ -2,12 +2,6 @@
using YesNt.Interpreter.Runtime; using YesNt.Interpreter.Runtime;
namespace YesNt.Interpreter
{
internal static class Program
{
private static void Main(string[] args)
{
if (args.Length == 1) if (args.Length == 1)
{ {
YesNtInterpreter interpreter = new YesNtInterpreter(); YesNtInterpreter interpreter = new YesNtInterpreter();
@@ -18,6 +12,3 @@ namespace YesNt.Interpreter
{ {
Console.WriteLine("No path specified!"); Console.WriteLine("No path specified!");
} }
}
}
}
+2 -3
View File
@@ -1,7 +1,7 @@
using System; using System;
namespace YesNt.Interpreter.Runtime namespace YesNt.Interpreter.Runtime;
{
public class DebugEventArgs : EventArgs public class DebugEventArgs : EventArgs
{ {
public int LineNumber { get; internal set; } public int LineNumber { get; internal set; }
@@ -10,4 +10,3 @@ namespace YesNt.Interpreter.Runtime
public int TaskId { get; internal set; } public int TaskId { get; internal set; }
public bool IsTask { get; internal set; } public bool IsTask { get; internal set; }
} }
}
+5 -11
View File
@@ -2,17 +2,11 @@
namespace YesNt.Interpreter.Runtime; namespace YesNt.Interpreter.Runtime;
internal class FunctionScope internal class FunctionScope(int callerLine, Stack<string> arguments)
{ {
public int CallerLine { get; } public int CallerLine { get; } = callerLine;
public Dictionary<string, string> Variables { get; } = new(); public Dictionary<string, string> Variables { get; } = [];
public Dictionary<string, int> Labels { get; } = new(); public Dictionary<string, int> Labels { get; } = [];
public Stack<string> Arguemtns { get; } public Stack<string> Arguments { get; } = arguments;
public Stack<string> Results { get; } = new(); public Stack<string> Results { get; } = new();
public FunctionScope(int callerLine, Stack<string> arguemtns)
{
CallerLine = callerLine;
Arguemtns = arguemtns;
}
} }
+8 -14
View File
@@ -1,16 +1,10 @@
namespace YesNt.Interpreter.Runtime namespace YesNt.Interpreter.Runtime;
{
internal class Line
{
public Line(string content, string fileName, int lineNumber)
{
Content = content;
FileName = fileName;
LineNumber = lineNumber;
}
public string Content { get; set; } internal class Line(string content, string fileName, int lineNumber)
public string FileName { get; set; } {
public int LineNumber { get; set; } public string Content { get; set; } = content;
}
public string FileName { get; set; } = fileName;
public int LineNumber { get; set; } = lineNumber;
} }
@@ -14,16 +14,16 @@ internal sealed class RuntimeInformation
private event Action<string, bool> OnExit; private event Action<string, bool> OnExit;
private static int internalTaskId = 0; private static int internalTaskId = 0;
private readonly Dictionary<string, string> topVariables = new(); private readonly Dictionary<string, string> topVariables = [];
private readonly Dictionary<string, int> topLabels = new(); private readonly Dictionary<string, int> topLabels = [];
private RuntimeInformation parentRuntimeInformation; private RuntimeInformation parentRuntimeInformation;
private int taskId = 0; private int taskId = 0;
public Dictionary<string, string> GloablVariables { get; set; } = new(); public Dictionary<string, string> GlobalVariables { get; set; } = [];
public Dictionary<string, int> Functions { get; } = new(); public Dictionary<string, int> Functions { get; } = [];
public Stack<FunctionScope> FunctionCallStack { get; } = new(); public Stack<FunctionScope> FunctionCallStack { get; } = new();
public Stack<string> InParametersStack { get; } = new(); public Stack<string> InParametersStack { get; } = new();
public Stack<string> OutParametersStack { get; set; } = new(); public Stack<string> OutParametersStack { get; set; } = new();
public List<Line> Lines { get; set; } = new(); public List<Line> Lines { get; set; } = [];
public string CurrentLine { get; set; } = string.Empty; public string CurrentLine { get; set; } = string.Empty;
public string SearchLabel { get; set; } = string.Empty; public string SearchLabel { get; set; } = string.Empty;
public string SearchFunction { get; set; } = string.Empty; public string SearchFunction { get; set; } = string.Empty;
@@ -149,7 +149,7 @@ internal sealed class RuntimeInformation
{ {
topVariables.Clear(); topVariables.Clear();
Lines.Clear(); Lines.Clear();
GloablVariables.Clear(); GlobalVariables.Clear();
Labels.Clear(); Labels.Clear();
Functions.Clear(); Functions.Clear();
FunctionCallStack.Clear(); FunctionCallStack.Clear();
@@ -2,8 +2,8 @@
using YesNt.Interpreter.Enums; using YesNt.Interpreter.Enums;
namespace YesNt.Interpreter.Runtime namespace YesNt.Interpreter.Runtime;
{
public class StatementInformation public class StatementInformation
{ {
public string Name { get; internal set; } public string Name { get; internal set; }
@@ -11,6 +11,5 @@ namespace YesNt.Interpreter.Runtime
public SpaceAround SpaceAround { get; internal set; } public SpaceAround SpaceAround { get; internal set; }
public ConsoleColor Color { get; internal set; } public ConsoleColor Color { get; internal set; }
public bool IgnoreSyntaxHighlighting { get; internal set; } public bool IgnoreSyntaxHighlighting { get; internal set; }
public string Seperator { get; set; } public string Separator { get; set; }
}
} }
@@ -1,7 +1,6 @@
namespace YesNt.Interpreter.Runtime namespace YesNt.Interpreter.Runtime;
{
internal abstract class StatementRuntimeInformation internal abstract class StatementRuntimeInformation
{ {
public RuntimeInformation RuntimeInfo { get; set; } public RuntimeInformation RuntimeInfo { get; set; }
} }
}
@@ -18,14 +18,14 @@ public class YesNtInterpreter
public event Action<string> OnDebugOutput; public event Action<string> OnDebugOutput;
private readonly RuntimeInformation runtimeInfo = new RuntimeInformation(); private readonly RuntimeInformation runtimeInfo = new RuntimeInformation();
private Dictionary<StatementAttribute, Action<string>> statements = new(); private Dictionary<StatementAttribute, Action<string>> statements = [];
private List<KeyValuePair<StaticStatementAttribute, Action>> staticStatements = new(); private List<KeyValuePair<StaticStatementAttribute, Action>> staticStatements = [];
public ReadOnlyCollection<StatementInformation> StatementInformation public ReadOnlyCollection<StatementInformation> StatementInformation
{ {
get get
{ {
List<StatementInformation> informations = statements.Select(s => List<StatementInformation> information = statements.Select(s =>
{ {
return new StatementInformation() return new StatementInformation()
{ {
@@ -34,11 +34,11 @@ public class YesNtInterpreter
SpaceAround = s.Key.SpaceAround, SpaceAround = s.Key.SpaceAround,
Color = s.Key.Color, Color = s.Key.Color,
IgnoreSyntaxHighlighting = s.Key.IgnoreSyntaxHighlighting, IgnoreSyntaxHighlighting = s.Key.IgnoreSyntaxHighlighting,
Seperator = s.Key.Seperator Separator = s.Key.Separator
}; };
}).ToList(); }).ToList();
return new ReadOnlyCollection<StatementInformation>(informations); return new ReadOnlyCollection<StatementInformation>(information);
} }
} }
@@ -118,14 +118,14 @@ public class YesNtInterpreter
Execute(); Execute();
} }
internal void Execute(List<Line> lines, Dictionary<string, string> gloablVariables, int startLine, RuntimeInformation parentRuntimeInformation) internal void Execute(List<Line> lines, Dictionary<string, string> globalVariables, int startLine, RuntimeInformation parentRuntimeInformation)
{ {
runtimeInfo.Reset(); runtimeInfo.Reset();
runtimeInfo.IsDebugMode = parentRuntimeInformation.IsDebugMode; runtimeInfo.IsDebugMode = parentRuntimeInformation.IsDebugMode;
runtimeInfo.Lines = lines; runtimeInfo.Lines = lines;
runtimeInfo.LineNumber = startLine; runtimeInfo.LineNumber = startLine;
runtimeInfo.ParentRuntimeInformation = parentRuntimeInformation; runtimeInfo.ParentRuntimeInformation = parentRuntimeInformation;
runtimeInfo.GloablVariables = gloablVariables; runtimeInfo.GlobalVariables = globalVariables;
if (parentRuntimeInformation.StopAllTasks) if (parentRuntimeInformation.StopAllTasks)
{ {
runtimeInfo.Exit($"Parent task was terminated!", parentRuntimeInformation.StopAllTasks); runtimeInfo.Exit($"Parent task was terminated!", parentRuntimeInformation.StopAllTasks);
@@ -195,7 +195,7 @@ public class YesNtInterpreter
_ => statementAttribute.Name.Trim() _ => statementAttribute.Name.Trim()
}; };
if (statementAttribute.Seperator is null || runtimeInfo.CurrentLine.Contains(statementAttribute.Seperator)) if (statementAttribute.Separator is null || runtimeInfo.CurrentLine.Contains(statementAttribute.Separator))
{ {
if (statementAttribute.SearchMode == SearchMode.StartOfLine && runtimeInfo.CurrentLine.StartsWith(name)) if (statementAttribute.SearchMode == SearchMode.StartOfLine && runtimeInfo.CurrentLine.StartsWith(name))
{ {
@@ -7,8 +7,8 @@ using YesNt.Interpreter.Enums;
using YesNt.Interpreter.Runtime; using YesNt.Interpreter.Runtime;
using YesNt.Interpreter.Utilities; using YesNt.Interpreter.Utilities;
namespace YesNt.Interpreter.Statements namespace YesNt.Interpreter.Statements;
{
internal class CodeFlowStatements : StatementRuntimeInformation internal class CodeFlowStatements : StatementRuntimeInformation
{ {
[Statement("jmp", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.Green, Priority = Priority.VeryLow)] [Statement("jmp", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.Green, Priority = Priority.VeryLow)]
@@ -16,9 +16,9 @@ namespace YesNt.Interpreter.Statements
{ {
string key = args.Trim(); string key = args.Trim();
if (RuntimeInfo.Labels.ContainsKey(key)) if (RuntimeInfo.Labels.TryGetValue(key, out int value))
{ {
RuntimeInfo.LineNumber = RuntimeInfo.Labels[key]; RuntimeInfo.LineNumber = value;
} }
else else
{ {
@@ -27,7 +27,7 @@ namespace YesNt.Interpreter.Statements
} }
} }
[Statement("jif", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.Green, Priority = Priority.VeryLow, Seperator = "|")] [Statement("jif", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.Green, Priority = Priority.VeryLow, Separator = "|")]
public void JumpIf(string args) public void JumpIf(string args)
{ {
string[] parts = args.Split('|'); string[] parts = args.Split('|');
@@ -53,9 +53,9 @@ namespace YesNt.Interpreter.Statements
return; return;
} }
if (RuntimeInfo.Labels.ContainsKey(key)) if (RuntimeInfo.Labels.TryGetValue(key, out int value))
{ {
RuntimeInfo.LineNumber = RuntimeInfo.Labels[key]; RuntimeInfo.LineNumber = value;
} }
else else
{ {
@@ -92,9 +92,9 @@ namespace YesNt.Interpreter.Statements
RuntimeInfo.FunctionCallStack.Push(new FunctionScope(RuntimeInfo.LineNumber, new Stack<string>(RuntimeInfo.InParametersStack.Reverse()))); RuntimeInfo.FunctionCallStack.Push(new FunctionScope(RuntimeInfo.LineNumber, new Stack<string>(RuntimeInfo.InParametersStack.Reverse())));
RuntimeInfo.InParametersStack.Clear(); RuntimeInfo.InParametersStack.Clear();
if (RuntimeInfo.Functions.ContainsKey(key)) if (RuntimeInfo.Functions.TryGetValue(key, out int value))
{ {
RuntimeInfo.LineNumber = RuntimeInfo.Functions[key]; RuntimeInfo.LineNumber = value;
} }
else else
{ {
@@ -102,7 +102,7 @@ namespace YesNt.Interpreter.Statements
} }
} }
[Statement("cif", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.DarkYellow, Priority = Priority.VeryLow, Seperator = "|")] [Statement("cif", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.DarkYellow, Priority = Priority.VeryLow, Separator = "|")]
public void CallIf(string args) public void CallIf(string args)
{ {
string[] parts = args.Split('|'); string[] parts = args.Split('|');
@@ -131,9 +131,9 @@ namespace YesNt.Interpreter.Statements
RuntimeInfo.FunctionCallStack.Push(new FunctionScope(RuntimeInfo.LineNumber, new Stack<string>(RuntimeInfo.InParametersStack))); RuntimeInfo.FunctionCallStack.Push(new FunctionScope(RuntimeInfo.LineNumber, new Stack<string>(RuntimeInfo.InParametersStack)));
RuntimeInfo.InParametersStack.Clear(); RuntimeInfo.InParametersStack.Clear();
if (RuntimeInfo.Functions.ContainsKey(key)) if (RuntimeInfo.Functions.TryGetValue(key, out int value))
{ {
RuntimeInfo.LineNumber = RuntimeInfo.Functions[key]; RuntimeInfo.LineNumber = value;
} }
else else
{ {
@@ -195,4 +195,3 @@ namespace YesNt.Interpreter.Statements
RuntimeInfo.Exit(message, false); RuntimeInfo.Exit(message, false);
} }
} }
}
@@ -5,8 +5,8 @@ using YesNt.Interpreter.Attributes;
using YesNt.Interpreter.Enums; using YesNt.Interpreter.Enums;
using YesNt.Interpreter.Runtime; using YesNt.Interpreter.Runtime;
namespace YesNt.Interpreter.Statements namespace YesNt.Interpreter.Statements;
{
internal class FunctionStatements : StatementRuntimeInformation internal class FunctionStatements : StatementRuntimeInformation
{ {
[Statement("fnc", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.DarkYellow, ExecuteInSearchMode = true)] [Statement("fnc", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.DarkYellow, ExecuteInSearchMode = true)]
@@ -62,7 +62,7 @@ namespace YesNt.Interpreter.Statements
} }
[Statement("%iso", SearchMode.Contains, SpaceAround.None, ConsoleColor.Yellow, KeepStatementInArgs = true, Priority = Priority.Highest)] [Statement("%iso", SearchMode.Contains, SpaceAround.None, ConsoleColor.Yellow, KeepStatementInArgs = true, Priority = Priority.Highest)]
public void CheckIfOutParameterAvalible(string args) public void CheckIfOutParameterAvailable(string args)
{ {
args += " "; args += " ";
args = args.Replace("%iso", (RuntimeInfo.OutParametersStack.Count > 0).ToString()); args = args.Replace("%iso", (RuntimeInfo.OutParametersStack.Count > 0).ToString());
@@ -70,7 +70,7 @@ namespace YesNt.Interpreter.Statements
RuntimeInfo.CurrentLine = args.TrimEnd(); RuntimeInfo.CurrentLine = args.TrimEnd();
} }
[Statement("cal", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.DarkYellow, Priority = Priority.Low, Seperator = "|")] [Statement("cal", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.DarkYellow, Priority = Priority.Low, Separator = "|")]
public void Call(string args) public void Call(string args)
{ {
string[] parts = args.Split('|'); string[] parts = args.Split('|');
@@ -81,20 +81,20 @@ namespace YesNt.Interpreter.Statements
} }
string key = parts[0].Trim(); string key = parts[0].Trim();
string[] functionArgumets = parts[1].Split(','); string[] functionArguments = parts[1].Split(',');
foreach (string argumanet in functionArgumets) foreach (string argument in functionArguments)
{ {
RuntimeInfo.InParametersStack.Push(argumanet.Trim()); RuntimeInfo.InParametersStack.Push(argument.Trim());
} }
RuntimeInfo.FunctionCallStack.Push(new FunctionScope(RuntimeInfo.LineNumber, new Stack<string>(RuntimeInfo.InParametersStack))); RuntimeInfo.FunctionCallStack.Push(new FunctionScope(RuntimeInfo.LineNumber, new Stack<string>(RuntimeInfo.InParametersStack)));
RuntimeInfo.InParametersStack.Clear(); RuntimeInfo.InParametersStack.Clear();
RuntimeInfo.CurrentLine = string.Empty; RuntimeInfo.CurrentLine = string.Empty;
if (RuntimeInfo.Functions.ContainsKey(key)) if (RuntimeInfo.Functions.TryGetValue(key, out int value))
{ {
RuntimeInfo.LineNumber = RuntimeInfo.Functions[key]; RuntimeInfo.LineNumber = value;
} }
else else
{ {
@@ -111,7 +111,7 @@ namespace YesNt.Interpreter.Statements
return; return;
} }
if (RuntimeInfo.FunctionCallStack.Peek().Arguemtns.Count == 0) if (RuntimeInfo.FunctionCallStack.Peek().Arguments.Count == 0)
{ {
RuntimeInfo.Exit("No in argument in stack", true); RuntimeInfo.Exit("No in argument in stack", true);
return; return;
@@ -119,16 +119,16 @@ namespace YesNt.Interpreter.Statements
if (RuntimeInfo.Variables.ContainsKey(args)) if (RuntimeInfo.Variables.ContainsKey(args))
{ {
RuntimeInfo.Variables[args] = RuntimeInfo.FunctionCallStack.Peek().Arguemtns.Pop(); RuntimeInfo.Variables[args] = RuntimeInfo.FunctionCallStack.Peek().Arguments.Pop();
} }
else else
{ {
RuntimeInfo.Variables.Add(args, RuntimeInfo.FunctionCallStack.Peek().Arguemtns.Pop()); RuntimeInfo.Variables.Add(args, RuntimeInfo.FunctionCallStack.Peek().Arguments.Pop());
} }
} }
[Statement("%isi", SearchMode.Contains, SpaceAround.End, ConsoleColor.Yellow, KeepStatementInArgs = true, Priority = Priority.Highest)] [Statement("%isi", SearchMode.Contains, SpaceAround.End, ConsoleColor.Yellow, KeepStatementInArgs = true, Priority = Priority.Highest)]
public void CheckIfInParameterAvalible(string args) public void CheckIfInParameterAvailable(string args)
{ {
if (!RuntimeInfo.IsInFunction) if (!RuntimeInfo.IsInFunction)
{ {
@@ -137,7 +137,7 @@ namespace YesNt.Interpreter.Statements
} }
args += " "; args += " ";
args = args.Replace("%isi", (RuntimeInfo.FunctionCallStack.Peek().Arguemtns.Count > 0).ToString()); args = args.Replace("%isi", (RuntimeInfo.FunctionCallStack.Peek().Arguments.Count > 0).ToString());
RuntimeInfo.CurrentLine = args.TrimEnd(); RuntimeInfo.CurrentLine = args.TrimEnd();
} }
@@ -197,4 +197,3 @@ namespace YesNt.Interpreter.Statements
RuntimeInfo.FunctionCallStack.Clear(); RuntimeInfo.FunctionCallStack.Clear();
} }
} }
}
@@ -5,9 +5,9 @@ using YesNt.Interpreter.Enums;
using YesNt.Interpreter.Runtime; using YesNt.Interpreter.Runtime;
using YesNt.Interpreter.Utilities; using YesNt.Interpreter.Utilities;
namespace YesNt.Interpreter.Statements namespace YesNt.Interpreter.Statements;
{
internal class PredifinedVariableStatements : StatementRuntimeInformation internal class PredefinedVariableStatements : StatementRuntimeInformation
{ {
private readonly Random random = new Random(); private readonly Random random = new Random();
@@ -62,4 +62,3 @@ namespace YesNt.Interpreter.Statements
RuntimeInfo.CurrentLine = args.TrimEnd(); RuntimeInfo.CurrentLine = args.TrimEnd();
} }
} }
}
@@ -41,17 +41,17 @@ internal partial class ProcessingStatements : StatementRuntimeInformation
[Statement("!task", SearchMode.EndOfLine, SpaceAround.Start, ConsoleColor.DarkYellow, Priority = Priority.VeryHigh)] [Statement("!task", SearchMode.EndOfLine, SpaceAround.Start, ConsoleColor.DarkYellow, Priority = Priority.VeryHigh)]
public void RunTask(string line) public void RunTask(string line)
{ {
int lineNumer = RuntimeInfo.LineNumber; int lineNumber = RuntimeInfo.LineNumber;
List<Line> lines = RuntimeInfo.Lines.GetRange(0, RuntimeInfo.Lines.Count); List<Line> lines = RuntimeInfo.Lines.GetRange(0, RuntimeInfo.Lines.Count);
Line oldLine = lines[lineNumer]; Line oldLine = lines[lineNumber];
lines[lineNumer] = new Line(line, oldLine.FileName, oldLine.LineNumber); lines[lineNumber] = new Line(line, oldLine.FileName, oldLine.LineNumber);
_ = Task.Run(() => _ = Task.Run(() =>
{ {
YesNtInterpreter interpreter = new YesNtInterpreter(); YesNtInterpreter interpreter = new YesNtInterpreter();
interpreter.Initialize(); interpreter.Initialize();
interpreter.Execute(lines, RuntimeInfo.GloablVariables, lineNumer, RuntimeInfo); interpreter.Execute(lines, RuntimeInfo.GlobalVariables, lineNumber, RuntimeInfo);
}); });
RuntimeInfo.CurrentLine = string.Empty; RuntimeInfo.CurrentLine = string.Empty;
@@ -9,21 +9,21 @@ using YesNt.Interpreter.Enums;
using YesNt.Interpreter.Runtime; using YesNt.Interpreter.Runtime;
using YesNt.Interpreter.Utilities; using YesNt.Interpreter.Utilities;
namespace YesNt.Interpreter.Statements namespace YesNt.Interpreter.Statements;
{
internal class SystemStatements : StatementRuntimeInformation internal class SystemStatements : StatementRuntimeInformation
{ {
[Statement("exc", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.Magenta, Priority = Priority.Low, Seperator = "|")] [Statement("exc", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.Magenta, Priority = Priority.Low, Separator = "|")]
public void ExecuteProgramWithArgs(string input) public void ExecuteProgramWithArgs(string input)
{ {
string[] parts = input.FromSafeString().Split('|'); string[] parts = input.FromSafeString().Split('|');
parts[0] = parts[0].Trim(); parts[0] = parts[0].Trim();
string[] functionArgumets = parts[1].Split(','); string[] functionArguments = parts[1].Split(',');
foreach (string argumanet in functionArgumets) foreach (string argument in functionArguments)
{ {
RuntimeInfo.InParametersStack.Push(argumanet.Trim()); RuntimeInfo.InParametersStack.Push(argument.Trim());
} }
try try
@@ -110,4 +110,3 @@ namespace YesNt.Interpreter.Statements
Console.Write(e.Data); Console.Write(e.Data);
} }
} }
}
@@ -47,13 +47,13 @@ internal partial class VariableStatements : StatementRuntimeInformation
RuntimeInfo.Exit("Invalid Syntax", true); RuntimeInfo.Exit("Invalid Syntax", true);
} }
if (RuntimeInfo.GloablVariables.ContainsKey(key)) if (RuntimeInfo.GlobalVariables.ContainsKey(key))
{ {
RuntimeInfo.GloablVariables[key] = parts[1].Trim(); RuntimeInfo.GlobalVariables[key] = parts[1].Trim();
} }
else else
{ {
RuntimeInfo.GloablVariables.Add(key, parts[1].Trim()); RuntimeInfo.GlobalVariables.Add(key, parts[1].Trim());
} }
} }
else else
@@ -71,9 +71,9 @@ internal partial class VariableStatements : StatementRuntimeInformation
{ {
_ = RuntimeInfo.Variables.Remove(key); _ = RuntimeInfo.Variables.Remove(key);
} }
else if (RuntimeInfo.GloablVariables.ContainsKey(key)) else if (RuntimeInfo.GlobalVariables.ContainsKey(key))
{ {
_ = RuntimeInfo.GloablVariables.Remove(key); _ = RuntimeInfo.GlobalVariables.Remove(key);
} }
else else
{ {
@@ -103,7 +103,7 @@ internal partial class VariableStatements : StatementRuntimeInformation
{ {
RuntimeInfo.CurrentLine = RuntimeInfo.CurrentLine.Replace($">{varName}", value); RuntimeInfo.CurrentLine = RuntimeInfo.CurrentLine.Replace($">{varName}", value);
} }
else if (RuntimeInfo.GloablVariables.TryGetValue(varName, out value)) else if (RuntimeInfo.GlobalVariables.TryGetValue(varName, out value))
{ {
RuntimeInfo.CurrentLine = RuntimeInfo.CurrentLine.Replace($">{varName}", value); RuntimeInfo.CurrentLine = RuntimeInfo.CurrentLine.Replace($">{varName}", value);
} }
+72 -73
View File
@@ -5,21 +5,21 @@ using System.IO;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
namespace YesNt.Interpreter.Utilities namespace YesNt.Interpreter.Utilities;
{
internal delegate void UserCallBack(string data);
public delegate void DataReceivedEventHandler(object sender, DataReceivedEventArgs e); public delegate void DataReceivedEventHandler(object sender, DataReceivedEventArgs e);
internal delegate void UserCallBack(string data);
public class FixedProcess : Process public class FixedProcess : Process
{ {
internal AsyncStreamReader output;
internal AsyncStreamReader error;
public new event DataReceivedEventHandler OutputDataReceived; public new event DataReceivedEventHandler OutputDataReceived;
public new event DataReceivedEventHandler ErrorDataReceived; public new event DataReceivedEventHandler ErrorDataReceived;
internal AsyncStreamReader output;
internal AsyncStreamReader error;
public new void BeginOutputReadLine() public new void BeginOutputReadLine()
{ {
Stream baseStream = StandardOutput.BaseStream; Stream baseStream = StandardOutput.BaseStream;
@@ -42,11 +42,11 @@ namespace YesNt.Interpreter.Utilities
DataReceivedEventArgs dataReceivedEventArgs = new DataReceivedEventArgs(data); DataReceivedEventArgs dataReceivedEventArgs = new DataReceivedEventArgs(data);
if (SynchronizingObject != null && SynchronizingObject.InvokeRequired) if (SynchronizingObject != null && SynchronizingObject.InvokeRequired)
{ {
_ = SynchronizingObject.Invoke(outputDataReceived, new object[] _ = SynchronizingObject.Invoke(outputDataReceived,
{ [
this, this,
dataReceivedEventArgs dataReceivedEventArgs
}); ]);
return; return;
} }
outputDataReceived(this, dataReceivedEventArgs); outputDataReceived(this, dataReceivedEventArgs);
@@ -61,11 +61,11 @@ namespace YesNt.Interpreter.Utilities
DataReceivedEventArgs dataReceivedEventArgs = new DataReceivedEventArgs(data); DataReceivedEventArgs dataReceivedEventArgs = new DataReceivedEventArgs(data);
if (SynchronizingObject != null && SynchronizingObject.InvokeRequired) if (SynchronizingObject != null && SynchronizingObject.InvokeRequired)
{ {
_ = SynchronizingObject.Invoke(errorDataReceived, new object[] _ = SynchronizingObject.Invoke(errorDataReceived,
{ [
this, this,
dataReceivedEventArgs dataReceivedEventArgs
}); ]);
return; return;
} }
errorDataReceived(this, dataReceivedEventArgs); errorDataReceived(this, dataReceivedEventArgs);
@@ -73,9 +73,25 @@ namespace YesNt.Interpreter.Utilities
} }
} }
public class DataReceivedEventArgs : EventArgs
{
internal string _data;
/// <summary>Gets the line of characters that was written to a redirected <see cref="T:System.Diagnostics.Process" /> output stream.</summary>
/// <returns>The line that was written by an associated <see cref="T:System.Diagnostics.Process" /> to its redirected <see cref="P:System.Diagnostics.Process.StandardOutput" /> or <see cref="P:System.Diagnostics.Process.StandardError" /> stream.</returns>
/// <filterpriority>2</filterpriority>
public string Data => _data;
internal DataReceivedEventArgs(string data)
{
_data = data;
}
}
internal class AsyncStreamReader : IDisposable internal class AsyncStreamReader : IDisposable
{ {
internal const int DefaultBufferSize = 1024; internal const int DefaultBufferSize = 1024;
private readonly Queue messageQueue;
private Stream stream; private Stream stream;
private Encoding encoding; private Encoding encoding;
private Decoder decoder; private Decoder decoder;
@@ -84,7 +100,6 @@ namespace YesNt.Interpreter.Utilities
private UserCallBack userCallBack; private UserCallBack userCallBack;
private bool cancelOperation; private bool cancelOperation;
private ManualResetEvent eofEvent; private ManualResetEvent eofEvent;
private readonly Queue messageQueue;
private StringBuilder sb; private StringBuilder sb;
private bool bLastCarriageReturn; private bool bLastCarriageReturn;
public virtual Encoding CurrentEncoding => encoding; public virtual Encoding CurrentEncoding => encoding;
@@ -100,25 +115,6 @@ namespace YesNt.Interpreter.Utilities
messageQueue = new Queue(); messageQueue = new Queue();
} }
private void Init(Stream stream, UserCallBack callback, Encoding encoding, int bufferSize)
{
this.stream = stream;
this.encoding = encoding;
userCallBack = callback;
decoder = encoding.GetDecoder();
if (bufferSize < 128)
{
bufferSize = 128;
}
byteBuffer = new byte[bufferSize];
int _maxCharsPerBuffer = encoding.GetMaxCharCount(bufferSize);
charBuffer = new char[_maxCharsPerBuffer];
cancelOperation = false;
eofEvent = new ManualResetEvent(false);
sb = null;
bLastCarriageReturn = false;
}
public virtual void Close() public virtual void Close()
{ {
Dispose(true); Dispose(true);
@@ -130,6 +126,36 @@ namespace YesNt.Interpreter.Utilities
GC.SuppressFinalize(this); GC.SuppressFinalize(this);
} }
internal void BeginReadLine()
{
if (cancelOperation)
{
cancelOperation = false;
}
if (sb == null)
{
sb = new StringBuilder(1024);
_ = stream.BeginRead(byteBuffer, 0, byteBuffer.Length, new AsyncCallback(ReadBuffer), null);
return;
}
FlushMessageQueue();
}
internal void CancelOperation()
{
cancelOperation = true;
}
internal void WaitUtilEOF()
{
if (eofEvent != null)
{
_ = eofEvent.WaitOne();
eofEvent.Close();
eofEvent = null;
}
}
protected virtual void Dispose(bool disposing) protected virtual void Dispose(bool disposing)
{ {
if (disposing && stream != null) if (disposing && stream != null)
@@ -151,24 +177,23 @@ namespace YesNt.Interpreter.Utilities
} }
} }
internal void BeginReadLine() private void Init(Stream stream, UserCallBack callback, Encoding encoding, int bufferSize)
{ {
if (cancelOperation) this.stream = stream;
this.encoding = encoding;
userCallBack = callback;
decoder = encoding.GetDecoder();
if (bufferSize < 128)
{ {
bufferSize = 128;
}
byteBuffer = new byte[bufferSize];
int _maxCharsPerBuffer = encoding.GetMaxCharCount(bufferSize);
charBuffer = new char[_maxCharsPerBuffer];
cancelOperation = false; cancelOperation = false;
} eofEvent = new ManualResetEvent(false);
if (sb == null) sb = null;
{ bLastCarriageReturn = false;
sb = new StringBuilder(1024);
_ = stream.BeginRead(byteBuffer, 0, byteBuffer.Length, new AsyncCallback(ReadBuffer), null);
return;
}
FlushMessageQueue();
}
internal void CancelOperation()
{
cancelOperation = true;
} }
private void ReadBuffer(IAsyncResult ar) private void ReadBuffer(IAsyncResult ar)
@@ -287,30 +312,4 @@ namespace YesNt.Interpreter.Utilities
} }
} }
} }
internal void WaitUtilEOF()
{
if (eofEvent != null)
{
_ = eofEvent.WaitOne();
eofEvent.Close();
eofEvent = null;
}
}
}
public class DataReceivedEventArgs : EventArgs
{
internal string _data;
/// <summary>Gets the line of characters that was written to a redirected <see cref="T:System.Diagnostics.Process" /> output stream.</summary>
/// <returns>The line that was written by an associated <see cref="T:System.Diagnostics.Process" /> to its redirected <see cref="P:System.Diagnostics.Process.StandardOutput" /> or <see cref="P:System.Diagnostics.Process.StandardError" /> stream.</returns>
/// <filterpriority>2</filterpriority>
public string Data => _data;
internal DataReceivedEventArgs(string data)
{
_data = data;
}
}
} }
@@ -7,7 +7,7 @@ using System.Text;
namespace YesNt.Interpreter.Utilities; namespace YesNt.Interpreter.Utilities;
public static class StringExtentions public static class StringExtensions
{ {
private static readonly Dictionary<string, string> reverseReplacementRules; private static readonly Dictionary<string, string> reverseReplacementRules;
@@ -24,7 +24,7 @@ public static class StringExtentions
}; };
[SuppressMessage("Minor Code Smell", "S3963:\"static\" fields should be initialized inline", Justification = "Doesn't work because it throws a TypeInitializationException")] [SuppressMessage("Minor Code Smell", "S3963:\"static\" fields should be initialized inline", Justification = "Doesn't work because it throws a TypeInitializationException")]
static StringExtentions() static StringExtensions()
{ {
reverseReplacementRules = ReplacementRules.ToDictionary(x => x.Value, x => x.Key); reverseReplacementRules = ReplacementRules.ToDictionary(x => x.Value, x => x.Key);
} }
+1 -1
View File
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net7.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<RootNamespace>YesNt.Interpreter</RootNamespace> <RootNamespace>YesNt.Interpreter</RootNamespace>
<ApplicationIcon /> <ApplicationIcon />
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>