mirror of
https://github.com/Stone-Red-Code/YesNt-Interpreter.git
synced 2026-09-09 16:06:08 +02:00
- Add global variables
- Add task id's - Fixed editor bugs - Fix input handler bugs
This commit is contained in:
@@ -7,5 +7,7 @@ namespace YesNt.Interpreter.Runtime
|
|||||||
public int LineNumber { get; internal set; }
|
public int LineNumber { get; internal set; }
|
||||||
public string CurrentLine { get; internal set; }
|
public string CurrentLine { get; internal set; }
|
||||||
public string OriginalLine { get; internal set; }
|
public string OriginalLine { get; internal set; }
|
||||||
|
public int TaskId { get; internal set; }
|
||||||
|
public bool IsTask { get; internal set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -9,8 +9,11 @@ namespace YesNt.Interpreter
|
|||||||
internal class RuntimeInformation
|
internal class RuntimeInformation
|
||||||
{
|
{
|
||||||
private RuntimeInformation parentRuntimeInformation;
|
private RuntimeInformation parentRuntimeInformation;
|
||||||
|
private static int internalTaskId = 0;
|
||||||
|
private int taskId = 0;
|
||||||
|
|
||||||
public Dictionary<string, string> Variables { get; } = new();
|
public Dictionary<string, string> Variables { get; } = new();
|
||||||
|
public Dictionary<string, string> GloablVariables { get; set; } = new();
|
||||||
public Dictionary<string, int> Labels { get; } = new();
|
public Dictionary<string, int> Labels { get; } = new();
|
||||||
public List<string> Lines { get; set; } = new();
|
public List<string> Lines { get; set; } = new();
|
||||||
public string CurrentLine { get; set; } = string.Empty;
|
public string CurrentLine { get; set; } = string.Empty;
|
||||||
@@ -23,6 +26,8 @@ namespace YesNt.Interpreter
|
|||||||
public string CurrentFilePath { get; set; } = string.Empty;
|
public string CurrentFilePath { get; set; } = string.Empty;
|
||||||
public bool IsTask => ParentRuntimeInformation is not null;
|
public bool IsTask => ParentRuntimeInformation is not null;
|
||||||
|
|
||||||
|
public int TaskId => IsTask ? taskId : 0;
|
||||||
|
|
||||||
public RuntimeInformation ParentRuntimeInformation
|
public RuntimeInformation ParentRuntimeInformation
|
||||||
{
|
{
|
||||||
get => parentRuntimeInformation;
|
get => parentRuntimeInformation;
|
||||||
@@ -126,6 +131,7 @@ namespace YesNt.Interpreter
|
|||||||
{
|
{
|
||||||
Lines.Clear();
|
Lines.Clear();
|
||||||
Variables.Clear();
|
Variables.Clear();
|
||||||
|
GloablVariables.Clear();
|
||||||
Labels.Clear();
|
Labels.Clear();
|
||||||
LabelStack.Clear();
|
LabelStack.Clear();
|
||||||
ParentRuntimeInformation = null;
|
ParentRuntimeInformation = null;
|
||||||
@@ -136,6 +142,8 @@ namespace YesNt.Interpreter
|
|||||||
StopAllTasks = false;
|
StopAllTasks = false;
|
||||||
IsDebugMode = false;
|
IsDebugMode = false;
|
||||||
LineNumber = 0;
|
LineNumber = 0;
|
||||||
|
taskId = internalTaskId + 1;
|
||||||
|
internalTaskId++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -96,13 +96,14 @@ namespace YesNt.Interpreter
|
|||||||
Execute();
|
Execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void Execute(List<string> lines, int startLine, RuntimeInformation parentRuntimeInformation)
|
internal void Execute(List<string> lines, Dictionary<string, string> gloablVariables, 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;
|
||||||
if (parentRuntimeInformation.StopAllTasks)
|
if (parentRuntimeInformation.StopAllTasks)
|
||||||
{
|
{
|
||||||
runtimeInfo.Exit($"Parent task was terminated!", parentRuntimeInformation.StopAllTasks);
|
runtimeInfo.Exit($"Parent task was terminated!", parentRuntimeInformation.StopAllTasks);
|
||||||
@@ -125,7 +126,9 @@ namespace YesNt.Interpreter
|
|||||||
DebugEventArgs debugEventArgs = new DebugEventArgs()
|
DebugEventArgs debugEventArgs = new DebugEventArgs()
|
||||||
{
|
{
|
||||||
LineNumber = runtimeInfo.LineNumber + 1,
|
LineNumber = runtimeInfo.LineNumber + 1,
|
||||||
OriginalLine = runtimeInfo.CurrentLine.FromSaveString()
|
OriginalLine = runtimeInfo.CurrentLine.FromSaveString(),
|
||||||
|
IsTask = runtimeInfo.IsTask,
|
||||||
|
TaskId = runtimeInfo.TaskId
|
||||||
};
|
};
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(runtimeInfo.CurrentLine))
|
if (string.IsNullOrWhiteSpace(runtimeInfo.CurrentLine))
|
||||||
@@ -217,6 +220,10 @@ namespace YesNt.Interpreter
|
|||||||
{
|
{
|
||||||
runtimeInfo.Exit($"Label \"{runtimeInfo.SearchLabel}\" not found", true);
|
runtimeInfo.Exit($"Label \"{runtimeInfo.SearchLabel}\" not found", true);
|
||||||
}
|
}
|
||||||
|
if (runtimeInfo.IsDebugMode)
|
||||||
|
{
|
||||||
|
runtimeInfo.LineExecuted(null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ namespace YesNt.Interpreter.Statements
|
|||||||
{
|
{
|
||||||
YesNtInterpreter interpreter = new YesNtInterpreter();
|
YesNtInterpreter interpreter = new YesNtInterpreter();
|
||||||
interpreter.Initialize();
|
interpreter.Initialize();
|
||||||
interpreter.Execute(lines, lineNumer, RuntimeInfo);
|
interpreter.Execute(lines, RuntimeInfo.GloablVariables, lineNumer, RuntimeInfo);
|
||||||
});
|
});
|
||||||
|
|
||||||
RuntimeInfo.CurrentLine = string.Empty;
|
RuntimeInfo.CurrentLine = string.Empty;
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ namespace YesNt.Interpreter.Statements
|
|||||||
string[] parts = args.Split('=');
|
string[] parts = args.Split('=');
|
||||||
if (parts.Length == 2)
|
if (parts.Length == 2)
|
||||||
{
|
{
|
||||||
string key = parts[0].Replace("<", "").Trim();
|
string key = parts[0].Replace("<", string.Empty).Trim();
|
||||||
if (key.Contains(' '))
|
if (key.Contains(' '))
|
||||||
{
|
{
|
||||||
RuntimeInfo.Exit("Invalid Syntax", true);
|
RuntimeInfo.Exit("Invalid Syntax", true);
|
||||||
@@ -35,6 +35,34 @@ namespace YesNt.Interpreter.Statements
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Statement("!<", SearchMode.StartOfLine, SpaceAround.None, Priority = Priority.VeryLow, IgnoreSyntaxHighlighting = true)]
|
||||||
|
public void DefineGlobalVariable(string args)
|
||||||
|
{
|
||||||
|
string[] parts = args.Split('=');
|
||||||
|
if (parts.Length == 2)
|
||||||
|
{
|
||||||
|
string key = parts[0].Replace("!<", string.Empty).Trim();
|
||||||
|
if (key.Contains(' '))
|
||||||
|
{
|
||||||
|
RuntimeInfo.Exit("Invalid Syntax", true);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (RuntimeInfo.GloablVariables.ContainsKey(key))
|
||||||
|
{
|
||||||
|
RuntimeInfo.GloablVariables[key] = parts[1].Trim();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
RuntimeInfo.GloablVariables.Add(key, parts[1].Trim());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
RuntimeInfo.Exit("Invalid syntax", true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[StaticStatement(ExecuteInSearchLabelMode = true)]
|
[StaticStatement(ExecuteInSearchLabelMode = true)]
|
||||||
public void ReadVariable()
|
public void ReadVariable()
|
||||||
{
|
{
|
||||||
@@ -47,6 +75,11 @@ namespace YesNt.Interpreter.Statements
|
|||||||
{
|
{
|
||||||
RuntimeInfo.CurrentLine = RuntimeInfo.CurrentLine.Replace($">{variable.Key}", variable.Value);
|
RuntimeInfo.CurrentLine = RuntimeInfo.CurrentLine.Replace($">{variable.Key}", variable.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
foreach (KeyValuePair<string, string> variable in RuntimeInfo.GloablVariables)
|
||||||
|
{
|
||||||
|
RuntimeInfo.CurrentLine = RuntimeInfo.CurrentLine.Replace($">{variable.Key}", variable.Value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+22
-11
@@ -13,7 +13,6 @@ namespace YesNt.CodeEditor
|
|||||||
private readonly List<string> debugOutput = new();
|
private readonly List<string> debugOutput = new();
|
||||||
|
|
||||||
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; } = new();
|
||||||
public Point CursorPosition { get; } = new(0, 0);
|
public Point CursorPosition { get; } = new(0, 0);
|
||||||
@@ -47,12 +46,15 @@ namespace YesNt.CodeEditor
|
|||||||
{
|
{
|
||||||
Display(false);
|
Display(false);
|
||||||
} while (inputHandler.HandleInput());
|
} while (inputHandler.HandleInput());
|
||||||
|
|
||||||
Console.Clear();
|
Console.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Display(bool drawAll)
|
public void Display(bool drawAll)
|
||||||
{
|
{
|
||||||
Console.CursorVisible = false;
|
Console.CursorVisible = false;
|
||||||
|
Console.ForegroundColor = ConsoleColor.Gray;
|
||||||
|
Console.BackgroundColor = ConsoleColor.Black;
|
||||||
|
|
||||||
Console.SetCursorPosition(0, 0);
|
Console.SetCursorPosition(0, 0);
|
||||||
|
|
||||||
@@ -138,6 +140,10 @@ namespace YesNt.CodeEditor
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (string.IsNullOrEmpty(Path.GetExtension(path)))
|
||||||
|
{
|
||||||
|
path = Path.ChangeExtension(path, "ynt");
|
||||||
|
}
|
||||||
CurrentPath = path;
|
CurrentPath = path;
|
||||||
}
|
}
|
||||||
else if (!string.IsNullOrWhiteSpace(CurrentPath))
|
else if (!string.IsNullOrWhiteSpace(CurrentPath))
|
||||||
@@ -151,7 +157,7 @@ namespace YesNt.CodeEditor
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
inputHandler.WriteStatus("File path is empty!");
|
inputHandler.WriteStatus("File path is empty! (Save the file before you can use this command)");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,17 +193,22 @@ namespace YesNt.CodeEditor
|
|||||||
{
|
{
|
||||||
lock (Console.Out)
|
lock (Console.Out)
|
||||||
{
|
{
|
||||||
Console.ForegroundColor = ConsoleColor.Magenta;
|
if (e is not null)
|
||||||
if (e.OriginalLine == e.CurrentLine)
|
|
||||||
{
|
{
|
||||||
Console.WriteLine($"{Environment.NewLine}[{e.LineNumber}] [{e.CurrentLine}] ==>");
|
Console.ForegroundColor = ConsoleColor.Magenta;
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Console.WriteLine($"{Environment.NewLine}[{e.LineNumber}] [{e.OriginalLine}] => [{e.CurrentLine}] ==>");
|
|
||||||
}
|
|
||||||
Console.ResetColor();
|
|
||||||
|
|
||||||
|
string taskId = e.IsTask ? $"[Task: {e.TaskId}] " : string.Empty;
|
||||||
|
if (e.OriginalLine == e.CurrentLine)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"{Environment.NewLine}{taskId}[{e.LineNumber}] [{e.CurrentLine}] ==>");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.WriteLine($"{Environment.NewLine}{taskId}[{e.LineNumber}] [{e.OriginalLine}] => [{e.CurrentLine}] ==>");
|
||||||
|
}
|
||||||
|
Console.ForegroundColor = ConsoleColor.Gray;
|
||||||
|
|
||||||
|
}
|
||||||
string[] outputs = debugOutput.ToArray();
|
string[] outputs = debugOutput.ToArray();
|
||||||
debugOutput.Clear();
|
debugOutput.Clear();
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,10 @@ namespace YesNt.CodeEditor
|
|||||||
|
|
||||||
public bool HandleInput()
|
public bool HandleInput()
|
||||||
{
|
{
|
||||||
|
while (Console.KeyAvailable)
|
||||||
|
{
|
||||||
|
Console.ReadKey(true);
|
||||||
|
}
|
||||||
if (textEditor.EditMode == Mode.Edit)
|
if (textEditor.EditMode == Mode.Edit)
|
||||||
{
|
{
|
||||||
ConsoleKeyInfo keyInfo = Console.ReadKey(true);
|
ConsoleKeyInfo keyInfo = Console.ReadKey(true);
|
||||||
@@ -23,7 +27,7 @@ namespace YesNt.CodeEditor
|
|||||||
{
|
{
|
||||||
case ConsoleKey.C:
|
case ConsoleKey.C:
|
||||||
textEditor.EditMode = Mode.Command;
|
textEditor.EditMode = Mode.Command;
|
||||||
break;
|
return true;
|
||||||
|
|
||||||
case ConsoleKey.B:
|
case ConsoleKey.B:
|
||||||
int position = textEditor.Lines.Count;
|
int position = textEditor.Lines.Count;
|
||||||
@@ -31,18 +35,18 @@ namespace YesNt.CodeEditor
|
|||||||
textEditor.LineOffset = Math.Max(position - Console.WindowHeight + 3, 0);
|
textEditor.LineOffset = Math.Max(position - Console.WindowHeight + 3, 0);
|
||||||
|
|
||||||
textEditor.Display(true);
|
textEditor.Display(true);
|
||||||
break;
|
return true;
|
||||||
|
|
||||||
case ConsoleKey.T:
|
case ConsoleKey.T:
|
||||||
textEditor.CursorPosition.Y = 0;
|
textEditor.CursorPosition.Y = 0;
|
||||||
textEditor.LineOffset = 0;
|
textEditor.LineOffset = 0;
|
||||||
|
|
||||||
textEditor.Display(true);
|
textEditor.Display(true);
|
||||||
break;
|
return true;
|
||||||
|
|
||||||
case ConsoleKey.S:
|
case ConsoleKey.S:
|
||||||
textEditor.CursorPosition.X = 0;
|
textEditor.CursorPosition.X = 0;
|
||||||
break;
|
return true;
|
||||||
|
|
||||||
case ConsoleKey.E:
|
case ConsoleKey.E:
|
||||||
if (textEditor.Lines.Count > textEditor.CursorPosition.Y)
|
if (textEditor.Lines.Count > textEditor.CursorPosition.Y)
|
||||||
@@ -53,10 +57,10 @@ namespace YesNt.CodeEditor
|
|||||||
{
|
{
|
||||||
textEditor.CursorPosition.X = 0;
|
textEditor.CursorPosition.X = 0;
|
||||||
}
|
}
|
||||||
break;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (keyInfo.Key == ConsoleKey.DownArrow)
|
if (keyInfo.Key == ConsoleKey.DownArrow)
|
||||||
{
|
{
|
||||||
textEditor.CursorPosition.Y++;
|
textEditor.CursorPosition.Y++;
|
||||||
if (textEditor.CursorPosition.Y - textEditor.LineOffset >= Console.WindowHeight - 3)
|
if (textEditor.CursorPosition.Y - textEditor.LineOffset >= Console.WindowHeight - 3)
|
||||||
@@ -228,6 +232,7 @@ namespace YesNt.CodeEditor
|
|||||||
{
|
{
|
||||||
textEditor.EditMode = Mode.Debug;
|
textEditor.EditMode = Mode.Debug;
|
||||||
Console.Clear();
|
Console.Clear();
|
||||||
|
Console.CursorVisible = true;
|
||||||
textEditor.yesNtInterpreter.Execute(textEditor.CurrentPath);
|
textEditor.yesNtInterpreter.Execute(textEditor.CurrentPath);
|
||||||
while (Console.KeyAvailable)
|
while (Console.KeyAvailable)
|
||||||
{
|
{
|
||||||
@@ -244,6 +249,7 @@ namespace YesNt.CodeEditor
|
|||||||
{
|
{
|
||||||
textEditor.EditMode = Mode.Debug;
|
textEditor.EditMode = Mode.Debug;
|
||||||
Console.Clear();
|
Console.Clear();
|
||||||
|
Console.CursorVisible = true;
|
||||||
textEditor.yesNtInterpreter.Execute(textEditor.CurrentPath, true);
|
textEditor.yesNtInterpreter.Execute(textEditor.CurrentPath, true);
|
||||||
while (Console.KeyAvailable)
|
while (Console.KeyAvailable)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -65,6 +65,12 @@ namespace YesNt.CodeEditor
|
|||||||
input = AddColorInformation(input, matches[i].Value, ConsoleColor.DarkCyan, SearchMode.StartOfLine);
|
input = AddColorInformation(input, matches[i].Value, ConsoleColor.DarkCyan, SearchMode.StartOfLine);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
matches = Regex.Matches(input, @"^!<[a-zA-Z0-9]+");
|
||||||
|
for (int i = 0; i < matches.Count; i++)
|
||||||
|
{
|
||||||
|
input = AddColorInformation(input, matches[i].Value, ConsoleColor.Blue, SearchMode.StartOfLine);
|
||||||
|
}
|
||||||
|
|
||||||
foreach (string part in input.Split("\0"))
|
foreach (string part in input.Split("\0"))
|
||||||
{
|
{
|
||||||
ConsoleColor consoleColor;
|
ConsoleColor consoleColor;
|
||||||
@@ -88,7 +94,7 @@ namespace YesNt.CodeEditor
|
|||||||
}
|
}
|
||||||
Console.ForegroundColor = consoleColor;
|
Console.ForegroundColor = consoleColor;
|
||||||
Console.Write(messagePart, consoleColor);
|
Console.Write(messagePart, consoleColor);
|
||||||
Console.ResetColor();
|
Console.ForegroundColor = ConsoleColor.Gray;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user