Add empty cwl command

This commit is contained in:
Stone_Red
2022-11-17 14:07:29 +01:00
parent ef1f731adc
commit 2e023f1a4e
2 changed files with 256 additions and 252 deletions
+212 -213
View File
@@ -9,279 +9,278 @@ using YesNt.Interpreter.Attributes;
using YesNt.Interpreter.Enums; using YesNt.Interpreter.Enums;
using YesNt.Interpreter.Utilities; using YesNt.Interpreter.Utilities;
namespace YesNt.Interpreter.Runtime namespace YesNt.Interpreter.Runtime;
public class YesNtInterpreter
{ {
public class YesNtInterpreter private readonly RuntimeInformation runtimeInfo = new RuntimeInformation();
private Dictionary<StatementAttribute, Action<string>> statements = new();
private List<KeyValuePair<StaticStatementAttribute, Action>> staticStatements = new();
public ReadOnlyCollection<StatementInformation> StatementInformation
{ {
private readonly RuntimeInformation runtimeInfo = new RuntimeInformation(); get
private Dictionary<StatementAttribute, Action<string>> statements = new();
private List<KeyValuePair<StaticStatementAttribute, Action>> staticStatements = new();
public ReadOnlyCollection<StatementInformation> StatementInformation
{ {
get List<StatementInformation> informations = statements.Select(s =>
{ {
List<StatementInformation> informations = statements.Select(s => return new StatementInformation()
{ {
return new StatementInformation() Name = s.Key.Name,
{ SearchMode = s.Key.SearchMode,
Name = s.Key.Name, SpaceAround = s.Key.SpaceAround,
SearchMode = s.Key.SearchMode, Color = s.Key.Color,
SpaceAround = s.Key.SpaceAround, IgnoreSyntaxHighlighting = s.Key.IgnoreSyntaxHighlighting,
Color = s.Key.Color, Seperator = s.Key.Seperator
IgnoreSyntaxHighlighting = s.Key.IgnoreSyntaxHighlighting, };
Seperator = s.Key.Seperator }).ToList();
};
}).ToList();
return new ReadOnlyCollection<StatementInformation>(informations); return new ReadOnlyCollection<StatementInformation>(informations);
}
} }
}
public event Action<DebugEventArgs> OnLineExecuted; public event Action<DebugEventArgs> OnLineExecuted;
public event Action<string> OnDebugOutput; public event Action<string> OnDebugOutput;
public void Stop() public void Stop()
{
runtimeInfo.Exit("Terminated by external process", true);
}
public void Initialize()
{
Assembly assembly = Assembly.GetExecutingAssembly();
Type[] types = assembly.GetTypes();
IEnumerable<Type> statementRuntimeInfos = types.Where(t => t.IsSubclassOf(typeof(StatementRuntimeInformation)));
statements.Clear();
foreach (Type type in statementRuntimeInfos)
{ {
runtimeInfo.Exit("Terminated by external process", true); object statementInfo = Activator.CreateInstance(type);
}
public void Initialize() MethodInfo[] methodInfos = statementInfo.GetType().GetMethods();
{
Assembly assembly = Assembly.GetExecutingAssembly();
Type[] types = assembly.GetTypes();
IEnumerable<Type> statementRuntimeInfos = types.Where(t => t.IsSubclassOf(typeof(StatementRuntimeInformation))); StatementRuntimeInformation statementRuntimeInfo = statementInfo as StatementRuntimeInformation;
statementRuntimeInfo.RuntimeInfo = runtimeInfo;
statements.Clear(); foreach (MethodInfo methodInfo in methodInfos)
foreach (Type type in statementRuntimeInfos)
{ {
object statementInfo = Activator.CreateInstance(type); StatementAttribute statementAttribute = methodInfo.GetCustomAttribute<StatementAttribute>();
if (statementAttribute is not null)
MethodInfo[] methodInfos = statementInfo.GetType().GetMethods();
StatementRuntimeInformation statementRuntimeInfo = statementInfo as StatementRuntimeInformation;
statementRuntimeInfo.RuntimeInfo = runtimeInfo;
foreach (MethodInfo methodInfo in methodInfos)
{ {
StatementAttribute statementAttribute = methodInfo.GetCustomAttribute<StatementAttribute>(); Action<string> method = methodInfo.CreateDelegate(typeof(Action<string>), statementInfo) as Action<string>;
if (statementAttribute is not null) statements.Add(statementAttribute, method);
{ }
Action<string> method = methodInfo.CreateDelegate(typeof(Action<string>), statementInfo) as Action<string>;
statements.Add(statementAttribute, method);
}
StaticStatementAttribute staticStatementAttribute = methodInfo.GetCustomAttribute<StaticStatementAttribute>(); StaticStatementAttribute staticStatementAttribute = methodInfo.GetCustomAttribute<StaticStatementAttribute>();
if (staticStatementAttribute is not null) if (staticStatementAttribute is not null)
{ {
Action method = methodInfo.CreateDelegate(typeof(Action), statementInfo) as Action; Action method = methodInfo.CreateDelegate(typeof(Action), statementInfo) as Action;
staticStatements.Add(new(staticStatementAttribute, method)); staticStatements.Add(new(staticStatementAttribute, method));
}
} }
} }
statements = statements.OrderBy(s => s.Key.Priority).ToDictionary(x => x.Key, x => x.Value);
staticStatements = staticStatements.OrderBy(s => s.Key.Priority).ToList();
runtimeInfo.OnDebugOutput += (s) => OnDebugOutput?.Invoke(s);
runtimeInfo.OnLineExecuted += (DebugEventArgs e) => OnLineExecuted?.Invoke(e);
} }
public void Execute(string path, bool isDebugMode = false) statements = statements.OrderBy(s => s.Key.Priority).ToDictionary(x => x.Key, x => x.Value);
staticStatements = staticStatements.OrderBy(s => s.Key.Priority).ToList();
runtimeInfo.OnDebugOutput += (s) => OnDebugOutput?.Invoke(s);
runtimeInfo.OnLineExecuted += (DebugEventArgs e) => OnLineExecuted?.Invoke(e);
}
public void Execute(string path, bool isDebugMode = false)
{
runtimeInfo.Reset();
runtimeInfo.IsDebugMode = isDebugMode;
if (LoadFile(path))
{ {
runtimeInfo.Reset();
runtimeInfo.IsDebugMode = isDebugMode;
if (LoadFile(path))
{
Execute();
}
}
public void Execute(List<string> lines, bool isDebugMode = false)
{
runtimeInfo.Reset();
runtimeInfo.IsDebugMode = isDebugMode;
for (int i = 0; i < lines.Count; i++)
{
runtimeInfo.Lines.Add(new Line(lines[i], Path.GetFileName("#Memory#"), i));
}
Execute(); Execute();
} }
}
internal void Execute(List<Line> lines, Dictionary<string, string> gloablVariables, int startLine, RuntimeInformation parentRuntimeInformation) public void Execute(List<string> lines, bool isDebugMode = false)
{
runtimeInfo.Reset();
runtimeInfo.IsDebugMode = isDebugMode;
for (int i = 0; i < lines.Count; i++)
{ {
runtimeInfo.Reset(); runtimeInfo.Lines.Add(new Line(lines[i], Path.GetFileName("#Memory#"), i));
runtimeInfo.IsDebugMode = parentRuntimeInformation.IsDebugMode;
runtimeInfo.Lines = lines;
runtimeInfo.LineNumber = startLine;
runtimeInfo.ParentRuntimeInformation = parentRuntimeInformation;
runtimeInfo.GloablVariables = gloablVariables;
if (parentRuntimeInformation.StopAllTasks)
{
runtimeInfo.Exit($"Parent task was terminated!", parentRuntimeInformation.StopAllTasks);
return;
}
Execute();
} }
private void Execute() Execute();
}
internal void Execute(List<Line> lines, Dictionary<string, string> gloablVariables, int startLine, RuntimeInformation parentRuntimeInformation)
{
runtimeInfo.Reset();
runtimeInfo.IsDebugMode = parentRuntimeInformation.IsDebugMode;
runtimeInfo.Lines = lines;
runtimeInfo.LineNumber = startLine;
runtimeInfo.ParentRuntimeInformation = parentRuntimeInformation;
runtimeInfo.GloablVariables = gloablVariables;
if (parentRuntimeInformation.StopAllTasks)
{ {
for (; runtimeInfo.LineNumber < runtimeInfo.Lines.Count; runtimeInfo.LineNumber++) runtimeInfo.Exit($"Parent task was terminated!", parentRuntimeInformation.StopAllTasks);
return;
}
Execute();
}
private void Execute()
{
for (; runtimeInfo.LineNumber < runtimeInfo.Lines.Count; runtimeInfo.LineNumber++)
{
if (runtimeInfo.Stop)
{ {
break;
}
runtimeInfo.CurrentLine = runtimeInfo.Lines[runtimeInfo.LineNumber].Content.TrimEnd().Replace("\r", string.Empty);
if (string.IsNullOrWhiteSpace(runtimeInfo.CurrentLine) || runtimeInfo.CurrentLine.StartsWith('#'))
{
continue;
}
DebugEventArgs debugEventArgs = new DebugEventArgs()
{
LineNumber = runtimeInfo.LineNumber + 1,
OriginalLine = runtimeInfo.CurrentLine.FromSafeString(),
IsTask = runtimeInfo.IsTask,
TaskId = runtimeInfo.TaskId
};
foreach (KeyValuePair<StaticStatementAttribute, Action> staticStatement in staticStatements)
{
StaticStatementAttribute staticStatementAttribute = staticStatement.Key;
if (!staticStatementAttribute.ExecuteInSearchMode && runtimeInfo.IsSearching)
{
continue;
}
staticStatement.Value.Invoke();
}
bool statementFound = false;
bool notSearchingLabel = !runtimeInfo.IsSearching;
foreach (KeyValuePair<StatementAttribute, Action<string>> statement in statements)
{
StatementAttribute statementAttribute = statement.Key;
if (!statementAttribute.ExecuteInSearchMode && runtimeInfo.IsSearching)
{
statementFound = true;
continue;
}
if (runtimeInfo.Stop) if (runtimeInfo.Stop)
{ {
break; break;
} }
runtimeInfo.CurrentLine = runtimeInfo.Lines[runtimeInfo.LineNumber].Content.TrimEnd().Replace("\r", string.Empty); string name = statementAttribute.SpaceAround switch
if (string.IsNullOrWhiteSpace(runtimeInfo.CurrentLine) || runtimeInfo.CurrentLine.StartsWith('#'))
{ {
continue; SpaceAround.StartEnd => $" {statementAttribute.Name.Trim()} ",
} SpaceAround.Start => $" {statementAttribute.Name.Trim()}",
SpaceAround.End => $"{statementAttribute.Name.Trim()} ",
DebugEventArgs debugEventArgs = new DebugEventArgs() _ => statementAttribute.Name
{
LineNumber = runtimeInfo.LineNumber + 1,
OriginalLine = runtimeInfo.CurrentLine.FromSafeString(),
IsTask = runtimeInfo.IsTask,
TaskId = runtimeInfo.TaskId
}; };
foreach (KeyValuePair<StaticStatementAttribute, Action> staticStatement in staticStatements) if (statementAttribute.Seperator is null || runtimeInfo.CurrentLine.Contains(statementAttribute.Seperator))
{ {
StaticStatementAttribute staticStatementAttribute = staticStatement.Key; if (statementAttribute.SearchMode == SearchMode.StartOfLine && runtimeInfo.CurrentLine.StartsWith(name))
if (!staticStatementAttribute.ExecuteInSearchMode && runtimeInfo.IsSearching)
{
continue;
}
staticStatement.Value.Invoke();
}
bool statementFound = false;
bool notSearchingLabel = !runtimeInfo.IsSearching;
foreach (KeyValuePair<StatementAttribute, Action<string>> statement in statements)
{
StatementAttribute statementAttribute = statement.Key;
if (!statementAttribute.ExecuteInSearchMode && runtimeInfo.IsSearching)
{ {
string copyLine = statementAttribute.KeepStatementInArgs ? runtimeInfo.CurrentLine : runtimeInfo.CurrentLine.Remove(0, name.Length);
statement.Value.Invoke(copyLine);
statementFound = true; statementFound = true;
continue;
} }
else if (statementAttribute.SearchMode == SearchMode.Contains && $" {runtimeInfo.CurrentLine} ".Contains(name))
if (runtimeInfo.Stop)
{ {
break; bool leadingWhitespace = runtimeInfo.CurrentLine.StartsWith(' ');
runtimeInfo.CurrentLine = $" {runtimeInfo.CurrentLine} ";
string copyLine = statementAttribute.KeepStatementInArgs ? runtimeInfo.CurrentLine : runtimeInfo.CurrentLine.Replace(name, string.Empty);
statement.Value.Invoke(copyLine);
statementFound = true;
runtimeInfo.CurrentLine = !leadingWhitespace ? runtimeInfo.CurrentLine.Trim() : runtimeInfo.CurrentLine.TrimEnd();
} }
else if (statementAttribute.SearchMode == SearchMode.EndOfLine && runtimeInfo.CurrentLine.EndsWith(name))
string name = statementAttribute.SpaceAround switch
{ {
SpaceAround.StartEnd => $" {statementAttribute.Name.Trim()} ", string copyLine = statementAttribute.KeepStatementInArgs ? runtimeInfo.CurrentLine : runtimeInfo.CurrentLine.Remove(runtimeInfo.CurrentLine.Length - name.Length);
SpaceAround.Start => $" {statementAttribute.Name.Trim()}", statement.Value.Invoke(copyLine);
SpaceAround.End => $"{statementAttribute.Name.Trim()} ", statementFound = true;
_ => statementAttribute.Name }
}; else if (statementAttribute.SearchMode == SearchMode.Exact && runtimeInfo.CurrentLine.Equals(name))
if (statementAttribute.Seperator is null || runtimeInfo.CurrentLine.Contains(statementAttribute.Seperator))
{ {
if (statementAttribute.SearchMode == SearchMode.StartOfLine && runtimeInfo.CurrentLine.StartsWith(name)) statement.Value.Invoke(runtimeInfo.CurrentLine);
{ statementFound = true;
string copyLine = statementAttribute.KeepStatementInArgs ? runtimeInfo.CurrentLine : runtimeInfo.CurrentLine.Remove(0, name.Length);
statement.Value.Invoke(copyLine);
statementFound = true;
}
else if (statementAttribute.SearchMode == SearchMode.Contains && $" {runtimeInfo.CurrentLine} ".Contains(name))
{
bool leadingWhitespace = runtimeInfo.CurrentLine.StartsWith(' ');
runtimeInfo.CurrentLine = $" {runtimeInfo.CurrentLine} ";
string copyLine = statementAttribute.KeepStatementInArgs ? runtimeInfo.CurrentLine : runtimeInfo.CurrentLine.Replace(name, string.Empty);
statement.Value.Invoke(copyLine);
statementFound = true;
runtimeInfo.CurrentLine = !leadingWhitespace ? runtimeInfo.CurrentLine.Trim() : runtimeInfo.CurrentLine.TrimEnd();
}
else if (statementAttribute.SearchMode == SearchMode.EndOfLine && runtimeInfo.CurrentLine.EndsWith(name))
{
string copyLine = statementAttribute.KeepStatementInArgs ? runtimeInfo.CurrentLine : runtimeInfo.CurrentLine.Remove(runtimeInfo.CurrentLine.Length - name.Length);
statement.Value.Invoke(copyLine);
statementFound = true;
}
else if (statementAttribute.SearchMode == SearchMode.Exact && runtimeInfo.CurrentLine.Equals(name))
{
statement.Value.Invoke(runtimeInfo.CurrentLine);
statementFound = true;
}
} }
}
if (!statementFound)
{
runtimeInfo.Exit("Invalid statement", true);
}
if (runtimeInfo.IsDebugMode && notSearchingLabel)
{
debugEventArgs.CurrentLine = runtimeInfo.CurrentLine.FromSafeString();
runtimeInfo.LineExecuted(debugEventArgs);
} }
} }
if (!runtimeInfo.Stop) if (!statementFound)
{ {
if (!string.IsNullOrWhiteSpace(runtimeInfo.SearchLabel)) runtimeInfo.Exit("Invalid statement", true);
{ }
runtimeInfo.Exit($"Label \"{runtimeInfo.SearchLabel}\" not found", true); if (runtimeInfo.IsDebugMode && notSearchingLabel)
} {
else if (!string.IsNullOrWhiteSpace(runtimeInfo.SearchFunction)) debugEventArgs.CurrentLine = runtimeInfo.CurrentLine.FromSafeString();
{ runtimeInfo.LineExecuted(debugEventArgs);
runtimeInfo.Exit($"Function \"{runtimeInfo.SearchFunction}\" not found", true);
}
else
{
runtimeInfo.Exit("End of file", false);
}
if (runtimeInfo.IsDebugMode)
{
runtimeInfo.LineExecuted(null);
}
} }
} }
private bool LoadFile(string path) if (!runtimeInfo.Stop)
{ {
path = Path.GetFullPath(path); if (!string.IsNullOrWhiteSpace(runtimeInfo.SearchLabel))
if (!File.Exists(path))
{ {
Console.WriteLine($"File \"{path}\" not found!"); runtimeInfo.Exit($"Label \"{runtimeInfo.SearchLabel}\" not found", true);
return false; }
else if (!string.IsNullOrWhiteSpace(runtimeInfo.SearchFunction))
{
runtimeInfo.Exit($"Function \"{runtimeInfo.SearchFunction}\" not found", true);
}
else
{
runtimeInfo.Exit("End of file", false);
} }
string[] lines = File.ReadAllLines(path); if (runtimeInfo.IsDebugMode)
if (lines.Length <= 0)
{ {
Console.WriteLine($"File \"{path}\" is empty!"); runtimeInfo.LineExecuted(null);
return false;
} }
runtimeInfo.WorkingDirectory = Path.GetDirectoryName(path);
for (int i = 0; i < lines.Length; i++)
{
runtimeInfo.Lines.Add(new Line(lines[i], Path.GetFileName(path), i));
}
return true;
} }
} }
private bool LoadFile(string path)
{
path = Path.GetFullPath(path);
if (!File.Exists(path))
{
Console.WriteLine($"File \"{path}\" not found!");
return false;
}
string[] lines = File.ReadAllLines(path);
if (lines.Length <= 0)
{
Console.WriteLine($"File \"{path}\" is empty!");
return false;
}
runtimeInfo.WorkingDirectory = Path.GetDirectoryName(path);
for (int i = 0; i < lines.Length; i++)
{
runtimeInfo.Lines.Add(new Line(lines[i], Path.GetFileName(path), i));
}
return true;
}
} }
@@ -6,56 +6,61 @@ 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 ConsoleStatements : StatementRuntimeInformation
{ {
internal class ConsoleStatements : StatementRuntimeInformation [Statement("cwl", SearchMode.Exact, SpaceAround.None, ConsoleColor.DarkGreen, Priority = Priority.VeryLow)]
public void WriteLineEmpty(string _)
{ {
[Statement("cwl", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.DarkGreen, Priority = Priority.VeryLow)] RuntimeInfo.WriteLine(string.Empty);
public void WriteLine(string args) }
{
RuntimeInfo.WriteLine(args);
}
[Statement("cw", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.DarkGreen, Priority = Priority.VeryLow)] [Statement("cwl", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.DarkGreen, Priority = Priority.VeryLow)]
public void Write(string args) public void WriteLine(string args)
{ {
RuntimeInfo.Write(args); RuntimeInfo.WriteLine(args);
} }
[Statement("%crl", SearchMode.Contains, SpaceAround.End, ConsoleColor.DarkGreen, KeepStatementInArgs = true, Priority = Priority.Highest)] [Statement("cw", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.DarkGreen, Priority = Priority.VeryLow)]
public void ReadLine(string args) public void Write(string args)
{
RuntimeInfo.Write(args);
}
[Statement("%crl", SearchMode.Contains, SpaceAround.End, ConsoleColor.DarkGreen, KeepStatementInArgs = true, Priority = Priority.Highest)]
public void ReadLine(string args)
{
args += " ";
while (args.Contains("%crl "))
{ {
args += " "; string input = Console.ReadLine();
while (args.Contains("%crl ")) if (input is null)
{ {
string input = Console.ReadLine(); RuntimeInfo.Exit("Terminated by external process", true);
if (input is null) return;
{
RuntimeInfo.Exit("Terminated by external process", true);
return;
}
args = args.ReplaceFirstOccurrence("%crl ", input.ToSafeString() + " ");
} }
RuntimeInfo.CurrentLine = args.TrimEnd(); args = args.ReplaceFirstOccurrence("%crl ", input.ToSafeString() + " ");
} }
RuntimeInfo.CurrentLine = args.TrimEnd();
}
[Statement("%cr", SearchMode.Contains, SpaceAround.End, ConsoleColor.DarkGreen, KeepStatementInArgs = true, Priority = Priority.Highest)] [Statement("%cr", SearchMode.Contains, SpaceAround.End, ConsoleColor.DarkGreen, KeepStatementInArgs = true, Priority = Priority.Highest)]
public void ReadKey(string args) public void ReadKey(string args)
{
args += " ";
while (args.Contains("%cr "))
{ {
args += " "; string input = ConsoleExtentions.ReadKey(RuntimeInfo).ToString();
while (args.Contains("%cr ")) args = args.ReplaceFirstOccurrence("%cr ", input.ToSafeString() + " ");
{
string input = ConsoleExtentions.ReadKey(RuntimeInfo).ToString();
args = args.ReplaceFirstOccurrence("%cr ", input.ToSafeString() + " ");
}
RuntimeInfo.CurrentLine = args.TrimEnd();
} }
RuntimeInfo.CurrentLine = args.TrimEnd();
}
[Statement("cls", SearchMode.Exact, SpaceAround.None, ConsoleColor.Magenta)] [Statement("cls", SearchMode.Exact, SpaceAround.None, ConsoleColor.Magenta)]
[SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "Won't work if static")] [SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "Won't work if static")]
public void Clear(string _) public void Clear(string _)
{ {
Console.Clear(); Console.Clear();
}
} }
} }