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.word_separator =
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 SyntaxHighlighter syntaxHighlighter;
private readonly List<string> debugOutput = new();
private readonly List<string> debugOutput = [];
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();
public List<string> Lines { get; } = [];
public Point CursorPosition { get; } = new(0, 0);
public Mode EditMode { get; set; } = Mode.Command;
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 Y { get; set; }
public Point(int x, int y)
{
X = x;
Y = y;
}
public int X { get; set; } = x;
public int Y { get; set; } = y;
}
internal enum Mode
+2 -7
View File
@@ -3,14 +3,9 @@ using System.Text;
namespace YesNt.CodeEditor;
internal class InputHandler
internal class InputHandler(TextEditor textEditor)
{
private readonly TextEditor textEditor;
public InputHandler(TextEditor textEditor)
{
this.textEditor = textEditor;
}
private readonly TextEditor textEditor = textEditor;
public bool HandleInput()
{
+17 -23
View File
@@ -9,16 +9,10 @@ using YesNt.Interpreter.Utilities;
namespace YesNt.CodeEditor;
internal partial class SyntaxHighlighter
internal partial class SyntaxHighlighter(ReadOnlyCollection<StatementInformation> statementInformation)
{
private readonly ReadOnlyCollection<StatementInformation> statementInformation;
private readonly string[] replacementValues;
public SyntaxHighlighter(ReadOnlyCollection<StatementInformation> statementInformation)
{
this.statementInformation = statementInformation;
replacementValues = StringExtentions.ReplacementRules.Values.ToArray();
}
private readonly ReadOnlyCollection<StatementInformation> statementInformation = statementInformation;
private readonly string[] replacementValues = StringExtensions.ReplacementRules.Values.ToArray();
public static string Base64Encode(string plainText)
{
@@ -64,11 +58,11 @@ internal partial class SyntaxHighlighter
input = input.TrimEnd(' ');
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;
}
@@ -76,11 +70,11 @@ internal partial class SyntaxHighlighter
}
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;
}
@@ -88,11 +82,11 @@ internal partial class SyntaxHighlighter
}
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;
}
@@ -100,11 +94,11 @@ internal partial class SyntaxHighlighter
}
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;
}
@@ -164,13 +158,13 @@ internal partial class SyntaxHighlighter
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.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))
};
return reult;
return result;
}
[GeneratedRegex("^<[a-zA-Z0-9]+")]
+1 -1
View File
@@ -2,7 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Platforms>AnyCPU;x64</Platforms>
</PropertyGroup>
+6 -6
View File
@@ -10,28 +10,28 @@ public class CodeFlowTests
[TestMethod]
public void FunctionTest()
{
List<string> lines = new List<string>()
{
List<string> lines =
[
"cal yes",
"fnc yes",
"!<result = 1",
"ret",
">result"
};
];
YesNtAssert.IsLastLineEqual(lines, "1");
}
[TestMethod]
public void LabelsTest()
{
List<string> lines = new List<string>()
{
List<string> lines =
[
"<result = 1",
"jmp yes",
"<result = 0",
"lbl yes",
">result"
};
];
YesNtAssert.IsLastLineEqual(lines, "1");
}
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<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)
{
AutoResetEvent onDone = new AutoResetEvent(false);
List<string> lines = new List<string>()
{
List<string> lines =
[
line
};
];
DebugEventArgs debugEventArgs = new DebugEventArgs();
yesNtInterpreter.OnLineExecuted += (er) =>
@@ -64,10 +64,10 @@ internal static class YesNtAssert
public static void IsLineNotEqual(string line, string expected, int timeout = 1000)
{
AutoResetEvent onDone = new AutoResetEvent(false);
List<string> lines = new List<string>()
{
List<string> lines =
[
line
};
];
DebugEventArgs debugEventArgs = new DebugEventArgs();
yesNtInterpreter.OnLineExecuted += (er) =>
@@ -2,11 +2,11 @@
using YesNt.Interpreter.Enums;
namespace YesNt.Interpreter.Attributes
namespace YesNt.Interpreter.Attributes;
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
internal class StatementAttribute : Attribute
{
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
internal class StatementAttribute : Attribute
{
public string Name { get; }
public SearchMode SearchMode { get; }
public SpaceAround SpaceAround { get; }
@@ -15,7 +15,7 @@ namespace YesNt.Interpreter.Attributes
public bool ExecuteInSearchMode { get; set; }
public bool KeepStatementInArgs { get; set; }
public bool IgnoreSyntaxHighlighting { get; }
public string Seperator { get; set; }
public string Separator { get; set; }
internal StatementAttribute(string name, SearchMode searchMode, SpaceAround spaceAround, ConsoleColor color)
{
@@ -32,5 +32,4 @@ namespace YesNt.Interpreter.Attributes
SpaceAround = spaceAround;
IgnoreSyntaxHighlighting = true;
}
}
}
@@ -2,12 +2,11 @@
using YesNt.Interpreter.Enums;
namespace YesNt.Interpreter.Attributes
namespace YesNt.Interpreter.Attributes;
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
internal class StaticStatementAttribute : Attribute
{
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
internal class StaticStatementAttribute : Attribute
{
public bool ExecuteInSearchMode { get; set; }
public Priority Priority { get; set; } = Priority.Normal;
}
}
+3 -4
View File
@@ -1,7 +1,7 @@
namespace YesNt.Interpreter.Enums
namespace YesNt.Interpreter.Enums;
internal enum Priority
{
internal enum Priority
{
PreProcessing,
Highest,
VeryHigh,
@@ -9,5 +9,4 @@
Normal,
Low,
VeryLow
}
}
+3 -4
View File
@@ -1,10 +1,9 @@
namespace YesNt.Interpreter.Enums
namespace YesNt.Interpreter.Enums;
public enum SearchMode
{
public enum SearchMode
{
StartOfLine,
EndOfLine,
Contains,
Exact
}
}
+3 -4
View File
@@ -1,10 +1,9 @@
namespace YesNt.Interpreter.Enums
namespace YesNt.Interpreter.Enums;
public enum SpaceAround
{
public enum SpaceAround
{
StartEnd,
Start,
End,
None
}
}
+4 -13
View File
@@ -2,22 +2,13 @@
using YesNt.Interpreter.Runtime;
namespace YesNt.Interpreter
if (args.Length == 1)
{
internal static class Program
{
private static void Main(string[] args)
{
if (args.Length == 1)
{
YesNtInterpreter interpreter = new YesNtInterpreter();
interpreter.Initialize();
interpreter.Execute(args[0]);
}
else
{
}
else
{
Console.WriteLine("No path specified!");
}
}
}
}
+3 -4
View File
@@ -1,13 +1,12 @@
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 string CurrentLine { get; internal set; }
public string OriginalLine { get; internal set; }
public int TaskId { get; internal set; }
public bool IsTask { get; internal set; }
}
}
+5 -11
View File
@@ -2,17 +2,11 @@
namespace YesNt.Interpreter.Runtime;
internal class FunctionScope
internal class FunctionScope(int callerLine, Stack<string> arguments)
{
public int CallerLine { get; }
public Dictionary<string, string> Variables { get; } = new();
public Dictionary<string, int> Labels { get; } = new();
public Stack<string> Arguemtns { get; }
public int CallerLine { get; } = callerLine;
public Dictionary<string, string> Variables { get; } = [];
public Dictionary<string, int> Labels { get; } = [];
public Stack<string> Arguments { get; } = arguments;
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
{
internal class Line
{
public Line(string content, string fileName, int lineNumber)
{
Content = content;
FileName = fileName;
LineNumber = lineNumber;
}
namespace YesNt.Interpreter.Runtime;
public string Content { get; set; }
public string FileName { get; set; }
public int LineNumber { get; set; }
}
internal class Line(string content, string fileName, int lineNumber)
{
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 static int internalTaskId = 0;
private readonly Dictionary<string, string> topVariables = new();
private readonly Dictionary<string, int> topLabels = new();
private readonly Dictionary<string, string> topVariables = [];
private readonly Dictionary<string, int> topLabels = [];
private RuntimeInformation parentRuntimeInformation;
private int taskId = 0;
public Dictionary<string, string> GloablVariables { get; set; } = new();
public Dictionary<string, int> Functions { get; } = new();
public Dictionary<string, string> GlobalVariables { get; set; } = [];
public Dictionary<string, int> Functions { get; } = [];
public Stack<FunctionScope> FunctionCallStack { get; } = new();
public Stack<string> InParametersStack { get; } = 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 SearchLabel { get; set; } = string.Empty;
public string SearchFunction { get; set; } = string.Empty;
@@ -149,7 +149,7 @@ internal sealed class RuntimeInformation
{
topVariables.Clear();
Lines.Clear();
GloablVariables.Clear();
GlobalVariables.Clear();
Labels.Clear();
Functions.Clear();
FunctionCallStack.Clear();
@@ -2,15 +2,14 @@
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 SearchMode SearchMode { get; internal set; }
public SpaceAround SpaceAround { get; internal set; }
public ConsoleColor Color { 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; }
}
}
@@ -18,14 +18,14 @@ public class YesNtInterpreter
public event Action<string> OnDebugOutput;
private readonly RuntimeInformation runtimeInfo = new RuntimeInformation();
private Dictionary<StatementAttribute, Action<string>> statements = new();
private List<KeyValuePair<StaticStatementAttribute, Action>> staticStatements = new();
private Dictionary<StatementAttribute, Action<string>> statements = [];
private List<KeyValuePair<StaticStatementAttribute, Action>> staticStatements = [];
public ReadOnlyCollection<StatementInformation> StatementInformation
{
get
{
List<StatementInformation> informations = statements.Select(s =>
List<StatementInformation> information = statements.Select(s =>
{
return new StatementInformation()
{
@@ -34,11 +34,11 @@ public class YesNtInterpreter
SpaceAround = s.Key.SpaceAround,
Color = s.Key.Color,
IgnoreSyntaxHighlighting = s.Key.IgnoreSyntaxHighlighting,
Seperator = s.Key.Seperator
Separator = s.Key.Separator
};
}).ToList();
return new ReadOnlyCollection<StatementInformation>(informations);
return new ReadOnlyCollection<StatementInformation>(information);
}
}
@@ -118,14 +118,14 @@ public class YesNtInterpreter
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.IsDebugMode = parentRuntimeInformation.IsDebugMode;
runtimeInfo.Lines = lines;
runtimeInfo.LineNumber = startLine;
runtimeInfo.ParentRuntimeInformation = parentRuntimeInformation;
runtimeInfo.GloablVariables = gloablVariables;
runtimeInfo.GlobalVariables = globalVariables;
if (parentRuntimeInformation.StopAllTasks)
{
runtimeInfo.Exit($"Parent task was terminated!", parentRuntimeInformation.StopAllTasks);
@@ -195,7 +195,7 @@ public class YesNtInterpreter
_ => 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))
{
@@ -7,18 +7,18 @@ using YesNt.Interpreter.Enums;
using YesNt.Interpreter.Runtime;
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)]
public void Jump(string args)
{
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
{
@@ -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)
{
string[] parts = args.Split('|');
@@ -53,9 +53,9 @@ namespace YesNt.Interpreter.Statements
return;
}
if (RuntimeInfo.Labels.ContainsKey(key))
if (RuntimeInfo.Labels.TryGetValue(key, out int value))
{
RuntimeInfo.LineNumber = RuntimeInfo.Labels[key];
RuntimeInfo.LineNumber = value;
}
else
{
@@ -92,9 +92,9 @@ namespace YesNt.Interpreter.Statements
RuntimeInfo.FunctionCallStack.Push(new FunctionScope(RuntimeInfo.LineNumber, new Stack<string>(RuntimeInfo.InParametersStack.Reverse())));
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
{
@@ -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)
{
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.InParametersStack.Clear();
if (RuntimeInfo.Functions.ContainsKey(key))
if (RuntimeInfo.Functions.TryGetValue(key, out int value))
{
RuntimeInfo.LineNumber = RuntimeInfo.Functions[key];
RuntimeInfo.LineNumber = value;
}
else
{
@@ -194,5 +194,4 @@ namespace YesNt.Interpreter.Statements
{
RuntimeInfo.Exit(message, false);
}
}
}
@@ -5,10 +5,10 @@ using YesNt.Interpreter.Attributes;
using YesNt.Interpreter.Enums;
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)]
public void FindFunction(string args)
{
@@ -62,7 +62,7 @@ namespace YesNt.Interpreter.Statements
}
[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.Replace("%iso", (RuntimeInfo.OutParametersStack.Count > 0).ToString());
@@ -70,7 +70,7 @@ namespace YesNt.Interpreter.Statements
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)
{
string[] parts = args.Split('|');
@@ -81,20 +81,20 @@ namespace YesNt.Interpreter.Statements
}
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.InParametersStack.Clear();
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
{
@@ -111,7 +111,7 @@ namespace YesNt.Interpreter.Statements
return;
}
if (RuntimeInfo.FunctionCallStack.Peek().Arguemtns.Count == 0)
if (RuntimeInfo.FunctionCallStack.Peek().Arguments.Count == 0)
{
RuntimeInfo.Exit("No in argument in stack", true);
return;
@@ -119,16 +119,16 @@ namespace YesNt.Interpreter.Statements
if (RuntimeInfo.Variables.ContainsKey(args))
{
RuntimeInfo.Variables[args] = RuntimeInfo.FunctionCallStack.Peek().Arguemtns.Pop();
RuntimeInfo.Variables[args] = RuntimeInfo.FunctionCallStack.Peek().Arguments.Pop();
}
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)]
public void CheckIfInParameterAvalible(string args)
public void CheckIfInParameterAvailable(string args)
{
if (!RuntimeInfo.IsInFunction)
{
@@ -137,7 +137,7 @@ namespace YesNt.Interpreter.Statements
}
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();
}
@@ -196,5 +196,4 @@ namespace YesNt.Interpreter.Statements
{
RuntimeInfo.FunctionCallStack.Clear();
}
}
}
@@ -5,10 +5,10 @@ using YesNt.Interpreter.Enums;
using YesNt.Interpreter.Runtime;
using YesNt.Interpreter.Utilities;
namespace YesNt.Interpreter.Statements
namespace YesNt.Interpreter.Statements;
internal class PredefinedVariableStatements : StatementRuntimeInformation
{
internal class PredifinedVariableStatements : StatementRuntimeInformation
{
private readonly Random random = new Random();
[Statement("%time", SearchMode.Contains, SpaceAround.None, ConsoleColor.Blue, KeepStatementInArgs = true, Priority = Priority.Highest)]
@@ -61,5 +61,4 @@ namespace YesNt.Interpreter.Statements
RuntimeInfo.CurrentLine = args.TrimEnd();
}
}
}
@@ -41,17 +41,17 @@ internal partial class ProcessingStatements : StatementRuntimeInformation
[Statement("!task", SearchMode.EndOfLine, SpaceAround.Start, ConsoleColor.DarkYellow, Priority = Priority.VeryHigh)]
public void RunTask(string line)
{
int lineNumer = RuntimeInfo.LineNumber;
int lineNumber = RuntimeInfo.LineNumber;
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(() =>
{
YesNtInterpreter interpreter = new YesNtInterpreter();
interpreter.Initialize();
interpreter.Execute(lines, RuntimeInfo.GloablVariables, lineNumer, RuntimeInfo);
interpreter.Execute(lines, RuntimeInfo.GlobalVariables, lineNumber, RuntimeInfo);
});
RuntimeInfo.CurrentLine = string.Empty;
@@ -9,21 +9,21 @@ using YesNt.Interpreter.Enums;
using YesNt.Interpreter.Runtime;
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)
{
string[] parts = input.FromSafeString().Split('|');
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
@@ -109,5 +109,4 @@ namespace YesNt.Interpreter.Statements
RuntimeInfo.OutParametersStack.Push(e.Data);
Console.Write(e.Data);
}
}
}
@@ -47,13 +47,13 @@ internal partial class VariableStatements : StatementRuntimeInformation
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
{
RuntimeInfo.GloablVariables.Add(key, parts[1].Trim());
RuntimeInfo.GlobalVariables.Add(key, parts[1].Trim());
}
}
else
@@ -71,9 +71,9 @@ internal partial class VariableStatements : StatementRuntimeInformation
{
_ = 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
{
@@ -103,7 +103,7 @@ internal partial class VariableStatements : StatementRuntimeInformation
{
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);
}
+78 -79
View File
@@ -5,21 +5,21 @@ using System.IO;
using System.Text;
using System.Threading;
namespace YesNt.Interpreter.Utilities
namespace YesNt.Interpreter.Utilities;
public delegate void DataReceivedEventHandler(object sender, DataReceivedEventArgs e);
internal delegate void UserCallBack(string data);
public class FixedProcess : Process
{
internal delegate void UserCallBack(string data);
public delegate void DataReceivedEventHandler(object sender, DataReceivedEventArgs e);
public class FixedProcess : Process
{
internal AsyncStreamReader output;
internal AsyncStreamReader error;
public new event DataReceivedEventHandler OutputDataReceived;
public new event DataReceivedEventHandler ErrorDataReceived;
internal AsyncStreamReader output;
internal AsyncStreamReader error;
public new void BeginOutputReadLine()
{
Stream baseStream = StandardOutput.BaseStream;
@@ -42,11 +42,11 @@ namespace YesNt.Interpreter.Utilities
DataReceivedEventArgs dataReceivedEventArgs = new DataReceivedEventArgs(data);
if (SynchronizingObject != null && SynchronizingObject.InvokeRequired)
{
_ = SynchronizingObject.Invoke(outputDataReceived, new object[]
{
_ = SynchronizingObject.Invoke(outputDataReceived,
[
this,
dataReceivedEventArgs
});
]);
return;
}
outputDataReceived(this, dataReceivedEventArgs);
@@ -61,21 +61,37 @@ namespace YesNt.Interpreter.Utilities
DataReceivedEventArgs dataReceivedEventArgs = new DataReceivedEventArgs(data);
if (SynchronizingObject != null && SynchronizingObject.InvokeRequired)
{
_ = SynchronizingObject.Invoke(errorDataReceived, new object[]
{
_ = SynchronizingObject.Invoke(errorDataReceived,
[
this,
dataReceivedEventArgs
});
]);
return;
}
errorDataReceived(this, dataReceivedEventArgs);
}
}
}
}
internal class AsyncStreamReader : IDisposable
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 const int DefaultBufferSize = 1024;
private readonly Queue messageQueue;
private Stream stream;
private Encoding encoding;
private Decoder decoder;
@@ -84,7 +100,6 @@ namespace YesNt.Interpreter.Utilities
private UserCallBack userCallBack;
private bool cancelOperation;
private ManualResetEvent eofEvent;
private readonly Queue messageQueue;
private StringBuilder sb;
private bool bLastCarriageReturn;
public virtual Encoding CurrentEncoding => encoding;
@@ -100,25 +115,6 @@ namespace YesNt.Interpreter.Utilities
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()
{
Dispose(true);
@@ -130,6 +126,36 @@ namespace YesNt.Interpreter.Utilities
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)
{
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;
}
if (sb == null)
{
sb = new StringBuilder(1024);
_ = stream.BeginRead(byteBuffer, 0, byteBuffer.Length, new AsyncCallback(ReadBuffer), null);
return;
}
FlushMessageQueue();
}
internal void CancelOperation()
{
cancelOperation = true;
eofEvent = new ManualResetEvent(false);
sb = null;
bLastCarriageReturn = false;
}
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;
public static class StringExtentions
public static class StringExtensions
{
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")]
static StringExtentions()
static StringExtensions()
{
reverseReplacementRules = ReplacementRules.ToDictionary(x => x.Value, x => x.Key);
}
+1 -1
View File
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>YesNt.Interpreter</RootNamespace>
<ApplicationIcon />
<OutputType>Exe</OutputType>