mirror of
https://github.com/Stone-Red-Code/YesNt-Interpreter.git
synced 2026-09-04 09:06:41 +02:00
- Fix code editor resize problems
- Fix crash when using `get` our `out` multiple times with the same variable name - Show the name of the file in which the error occured
This commit is contained in:
@@ -244,7 +244,7 @@ namespace YesNt.CodeEditor
|
||||
|
||||
private bool SizeChanged()
|
||||
{
|
||||
if (oldSize.X != Console.WindowWidth && oldSize.Y != Console.WindowHeight)
|
||||
if (oldSize.X != Console.WindowWidth || oldSize.Y != Console.WindowHeight)
|
||||
{
|
||||
oldSize.X = Console.WindowWidth;
|
||||
oldSize.Y = Console.WindowHeight;
|
||||
|
||||
@@ -105,8 +105,8 @@ namespace YesNt.CodeEditor
|
||||
|
||||
string line = textEditor.Lines[textEditor.CursorPosition.Y + 1];
|
||||
|
||||
textEditor.Lines[textEditor.CursorPosition.Y] = line.Substring(0, Math.Min(textEditor.CursorPosition.X, line.Length));
|
||||
textEditor.Lines[textEditor.CursorPosition.Y + 1] = line.Substring(Math.Min(textEditor.CursorPosition.X, line.Length));
|
||||
textEditor.Lines[textEditor.CursorPosition.Y] = line[..Math.Min(textEditor.CursorPosition.X, line.Length)];
|
||||
textEditor.Lines[textEditor.CursorPosition.Y + 1] = line[Math.Min(textEditor.CursorPosition.X, line.Length)..];
|
||||
|
||||
textEditor.CursorPosition.X = 0;
|
||||
textEditor.CursorPosition.Y++;
|
||||
@@ -167,7 +167,7 @@ namespace YesNt.CodeEditor
|
||||
}
|
||||
}
|
||||
|
||||
while (textEditor.Lines.Count > 0 && string.IsNullOrWhiteSpace(textEditor.Lines[textEditor.Lines.Count - 1]))
|
||||
while (textEditor.Lines.Count > 0 && string.IsNullOrWhiteSpace(textEditor.Lines[^1]))
|
||||
{
|
||||
textEditor.Lines.RemoveAt(textEditor.Lines.Count - 1);
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ namespace YesNt.CodeEditor
|
||||
{
|
||||
internal class SyntaxHighlighter
|
||||
{
|
||||
private ReadOnlyCollection<StatementInformation> statementInformation;
|
||||
private readonly ReadOnlyCollection<StatementInformation> statementInformation;
|
||||
|
||||
public SyntaxHighlighter(ReadOnlyCollection<StatementInformation> statementInformation)
|
||||
{
|
||||
@@ -45,7 +45,7 @@ namespace YesNt.CodeEditor
|
||||
{
|
||||
continue;
|
||||
}
|
||||
input = AddColorInformation(input, input.Substring(0, name.Length), statement.Color, statement.SearchMode);
|
||||
input = AddColorInformation(input, input[..name.Length], statement.Color, statement.SearchMode);
|
||||
}
|
||||
if (statement.SearchMode == SearchMode.Contains && $" {input} ".Contains(name))
|
||||
{
|
||||
@@ -69,7 +69,7 @@ namespace YesNt.CodeEditor
|
||||
{
|
||||
continue;
|
||||
}
|
||||
input = AddColorInformation(input, input.Substring(input.Length - name.Length), statement.Color, statement.SearchMode);
|
||||
input = AddColorInformation(input, input[^name.Length..], statement.Color, statement.SearchMode);
|
||||
}
|
||||
if (statement.SearchMode == SearchMode.Exact && input.Equals(name))
|
||||
{
|
||||
@@ -103,6 +103,11 @@ namespace YesNt.CodeEditor
|
||||
input = AddColorInformation(input, matches[i].Value, ConsoleColor.Blue, SearchMode.StartOfLine);
|
||||
}
|
||||
|
||||
if (input.StartsWith('#'))
|
||||
{
|
||||
input = AddColorInformation(input, input, ConsoleColor.Gray, SearchMode.Exact);
|
||||
}
|
||||
|
||||
foreach (string part in input.Split("\0"))
|
||||
{
|
||||
ConsoleColor consoleColor;
|
||||
@@ -120,12 +125,12 @@ namespace YesNt.CodeEditor
|
||||
consoleColor = ConsoleColor.White;
|
||||
}
|
||||
|
||||
if (consoleColor == Console.BackgroundColor || consoleColor == ConsoleColor.DarkGray)
|
||||
if (consoleColor == Console.BackgroundColor)
|
||||
{
|
||||
consoleColor = ConsoleColor.White;
|
||||
}
|
||||
Console.ForegroundColor = consoleColor;
|
||||
Console.Write(messagePart, consoleColor);
|
||||
Console.Write(messagePart);
|
||||
Console.ForegroundColor = ConsoleColor.Gray;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
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; }
|
||||
public string FileName { get; set; }
|
||||
public int LineNumber { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -19,7 +19,7 @@ namespace YesNt.Interpreter
|
||||
public Stack<FunctionScope> FunctionCallStack { get; } = new();
|
||||
public Stack<string> InParametersStack { get; } = new();
|
||||
public Stack<string> OutParametersStack { get; set; } = new();
|
||||
public List<string> Lines { get; set; } = new();
|
||||
public List<Line> Lines { get; set; } = new();
|
||||
public string CurrentLine { get; set; } = string.Empty;
|
||||
public string SearchLabel { get; set; } = string.Empty;
|
||||
public string SearchFunction { get; set; } = string.Empty;
|
||||
@@ -132,7 +132,8 @@ namespace YesNt.Interpreter
|
||||
{
|
||||
if (!Stop)
|
||||
{
|
||||
WriteLine($"{Environment.NewLine}[{(IsTask ? $"Task: {TaskId}" : "The process")} was terminated at line {LineNumber + 1} with the message: {message}]", true);
|
||||
Line line = Lines[Math.Min(LineNumber, Lines.Count - 1)];
|
||||
WriteLine($"{Environment.NewLine}[{(IsTask ? $"Task: {TaskId}" : "The process")} was terminated at line {line.LineNumber + 1} in the file \"{line.FileName}\" with the message: {message}]", true);
|
||||
Stop = true;
|
||||
}
|
||||
if (stopAllTasks == true && StopAllTasks == false)
|
||||
|
||||
@@ -99,7 +99,7 @@ namespace YesNt.Interpreter
|
||||
Execute();
|
||||
}
|
||||
|
||||
internal void Execute(List<string> lines, Dictionary<string, string> gloablVariables, int startLine, RuntimeInformation parentRuntimeInformation)
|
||||
internal void Execute(List<Line> lines, Dictionary<string, string> gloablVariables, int startLine, RuntimeInformation parentRuntimeInformation)
|
||||
{
|
||||
runtimeInfo.Reset();
|
||||
runtimeInfo.IsDebugMode = parentRuntimeInformation.IsDebugMode;
|
||||
@@ -124,7 +124,7 @@ namespace YesNt.Interpreter
|
||||
break;
|
||||
}
|
||||
|
||||
runtimeInfo.CurrentLine = runtimeInfo.Lines[runtimeInfo.LineNumber].TrimEnd().Replace("\r", string.Empty);
|
||||
runtimeInfo.CurrentLine = runtimeInfo.Lines[runtimeInfo.LineNumber].Content.TrimEnd().Replace("\r", string.Empty);
|
||||
|
||||
DebugEventArgs debugEventArgs = new DebugEventArgs()
|
||||
{
|
||||
@@ -134,7 +134,7 @@ namespace YesNt.Interpreter
|
||||
TaskId = runtimeInfo.TaskId
|
||||
};
|
||||
|
||||
if (string.IsNullOrWhiteSpace(runtimeInfo.CurrentLine))
|
||||
if (string.IsNullOrWhiteSpace(runtimeInfo.CurrentLine) || runtimeInfo.CurrentLine.StartsWith('#'))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
@@ -254,8 +254,12 @@ namespace YesNt.Interpreter
|
||||
return;
|
||||
}
|
||||
|
||||
IEnumerable<string> lines = File.ReadAllLines(path);
|
||||
runtimeInfo.Lines.AddRange(lines);
|
||||
string[] lines = File.ReadAllLines(path);
|
||||
|
||||
for (int i = 0; i < lines.Length; i++)
|
||||
{
|
||||
runtimeInfo.Lines.Add(new Line(lines[i], Path.GetFileName(path), i));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -42,41 +42,55 @@ namespace YesNt.Interpreter.Statements
|
||||
}
|
||||
|
||||
[Statement("out", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.Yellow)]
|
||||
public void GetOutParameter(string name)
|
||||
public void GetOutParameter(string args)
|
||||
{
|
||||
if (RuntimeInfo.OutParametersStack.Count == 0)
|
||||
{
|
||||
RuntimeInfo.Exit("No out parameter in stack", true);
|
||||
RuntimeInfo.Exit("No out argument in stack", true);
|
||||
return;
|
||||
}
|
||||
|
||||
RuntimeInfo.Variables.Add(name, RuntimeInfo.OutParametersStack.Pop());
|
||||
if (RuntimeInfo.Variables.ContainsKey(args))
|
||||
{
|
||||
RuntimeInfo.Variables[args] = RuntimeInfo.OutParametersStack.Pop();
|
||||
}
|
||||
else
|
||||
{
|
||||
RuntimeInfo.Variables.Add(args, RuntimeInfo.OutParametersStack.Pop());
|
||||
}
|
||||
}
|
||||
|
||||
[Statement("get", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.Yellow)]
|
||||
public void GetInParameter(string args)
|
||||
{
|
||||
if (RuntimeInfo.FunctionCallStack.Count == 0)
|
||||
if (!RuntimeInfo.IsInFunction)
|
||||
{
|
||||
RuntimeInfo.Exit("No function in stack", true);
|
||||
RuntimeInfo.Exit("Statement not allowed outside of function", true);
|
||||
return;
|
||||
}
|
||||
|
||||
if (RuntimeInfo.FunctionCallStack.Peek().Arguemtns.Count == 0)
|
||||
{
|
||||
RuntimeInfo.Exit("No in parameter in stack", true);
|
||||
RuntimeInfo.Exit("No in argument in stack", true);
|
||||
return;
|
||||
}
|
||||
|
||||
RuntimeInfo.Variables.TryAdd(args, RuntimeInfo.FunctionCallStack.Peek().Arguemtns.Pop());
|
||||
if (RuntimeInfo.Variables.ContainsKey(args))
|
||||
{
|
||||
RuntimeInfo.Variables[args] = RuntimeInfo.FunctionCallStack.Peek().Arguemtns.Pop();
|
||||
}
|
||||
else
|
||||
{
|
||||
RuntimeInfo.Variables.Add(args, RuntimeInfo.FunctionCallStack.Peek().Arguemtns.Pop());
|
||||
}
|
||||
}
|
||||
|
||||
[Statement("put", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.Yellow)]
|
||||
public void AddOutParameter(string args)
|
||||
{
|
||||
if (RuntimeInfo.FunctionCallStack.Count == 0)
|
||||
if (!RuntimeInfo.IsInFunction)
|
||||
{
|
||||
RuntimeInfo.Exit("No function in stack", true);
|
||||
RuntimeInfo.Exit("Statement not allowed outside of function", true);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -88,7 +102,7 @@ namespace YesNt.Interpreter.Statements
|
||||
{
|
||||
if (!RuntimeInfo.IsInFunction)
|
||||
{
|
||||
RuntimeInfo.Exit("Not in function", true);
|
||||
RuntimeInfo.Exit("Statement not allowed outside of function", true);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ using System.Threading.Tasks;
|
||||
|
||||
using YesNt.Interpreter.Attributes;
|
||||
using YesNt.Interpreter.Enums;
|
||||
using YesNt.Interpreter.Runtime;
|
||||
using YesNt.Interpreter.Utilities;
|
||||
|
||||
namespace YesNt.Interpreter.Statements
|
||||
@@ -41,8 +42,9 @@ namespace YesNt.Interpreter.Statements
|
||||
public void RunTask(string line)
|
||||
{
|
||||
int lineNumer = RuntimeInfo.LineNumber;
|
||||
List<string> lines = RuntimeInfo.Lines.GetRange(0, RuntimeInfo.Lines.Count);
|
||||
lines[lineNumer] = line;
|
||||
List<Line> lines = RuntimeInfo.Lines.GetRange(0, RuntimeInfo.Lines.Count);
|
||||
|
||||
lines[lineNumer].Content = line;
|
||||
_ = Task.Run(() =>
|
||||
{
|
||||
YesNtInterpreter interpreter = new YesNtInterpreter();
|
||||
@@ -73,7 +75,12 @@ namespace YesNt.Interpreter.Statements
|
||||
try
|
||||
{
|
||||
RuntimeInfo.Lines.RemoveAt(0);
|
||||
RuntimeInfo.Lines.InsertRange(RuntimeInfo.LineNumber, File.ReadAllLines(path));
|
||||
string[] lines = File.ReadAllLines(path);
|
||||
|
||||
for (int i = 0; i < lines.Length; i++)
|
||||
{
|
||||
RuntimeInfo.Lines.Add(new Line(lines[i], Path.GetFileName(path), i));
|
||||
}
|
||||
RuntimeInfo.LineNumber--;
|
||||
}
|
||||
catch
|
||||
|
||||
Reference in New Issue
Block a user