Change code flow and variable statements

- Labels (`lbl`) in functions are now local
- Fix code smell problems
This commit is contained in:
Stone_Red
2022-03-31 12:56:35 +02:00
parent 6a4fe4f871
commit 29bbcebf74
4 changed files with 47 additions and 10 deletions
@@ -7,7 +7,7 @@ namespace YesNt.Interpreter.Attributes
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
internal class StaticStatementAttribute : Attribute internal class StaticStatementAttribute : Attribute
{ {
public bool ExecuteInSearchLabelMode { get; set; } public bool ExecuteInSearchMode { get; set; }
public Priority Priority { get; set; } = Priority.Normal; public Priority Priority { get; set; } = Priority.Normal;
} }
} }
@@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using YesNt.Interpreter.Runtime; using YesNt.Interpreter.Runtime;
using YesNt.Interpreter.Utilities; using YesNt.Interpreter.Utilities;
@@ -11,10 +12,11 @@ namespace YesNt.Interpreter
private RuntimeInformation parentRuntimeInformation; private RuntimeInformation parentRuntimeInformation;
private static int internalTaskId = 0; private static int internalTaskId = 0;
private int taskId = 0; private int taskId = 0;
private readonly Dictionary<string, string> topVariables = new(); private readonly Dictionary<string, string> topVariables = new();
private readonly Dictionary<string, int> topLabels = new();
public Dictionary<string, string> GloablVariables { get; set; } = new(); public Dictionary<string, string> GloablVariables { get; set; } = new();
public Dictionary<string, int> Labels { get; } = new();
public Dictionary<string, int> Functions { get; } = new(); public Dictionary<string, int> Functions { get; } = new();
public Stack<FunctionScope> FunctionCallStack { get; } = new(); public Stack<FunctionScope> FunctionCallStack { get; } = new();
public Stack<string> InParametersStack { get; } = new(); public Stack<string> InParametersStack { get; } = new();
@@ -30,8 +32,7 @@ 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 int TaskId => IsTask ? taskId : 0;
public bool InternalIsInFunction { get; set; }
public bool InternalIsInFunction { get; private set; } = false;
public bool IsInFunction public bool IsInFunction
{ {
@@ -54,6 +55,21 @@ namespace YesNt.Interpreter
} }
} }
public Dictionary<string, int> Labels
{
get
{
if (FunctionCallStack.Count == 0)
{
return topLabels;
}
else
{
return FunctionCallStack.Peek().Labels;
}
}
}
public RuntimeInformation ParentRuntimeInformation public RuntimeInformation ParentRuntimeInformation
{ {
get => parentRuntimeInformation; get => parentRuntimeInformation;
@@ -68,6 +84,7 @@ namespace YesNt.Interpreter
} }
public bool IsSearching => !string.IsNullOrWhiteSpace(SearchLabel + SearchFunction) || IsInFunction && FunctionCallStack.Count == 0; public bool IsSearching => !string.IsNullOrWhiteSpace(SearchLabel + SearchFunction) || IsInFunction && FunctionCallStack.Count == 0;
public bool IsLocalSearch { get; set; }
private event Action<string, bool> OnExit; private event Action<string, bool> OnExit;
@@ -75,9 +92,9 @@ namespace YesNt.Interpreter
public event Action<DebugEventArgs> OnLineExecuted; public event Action<DebugEventArgs> OnLineExecuted;
private void ParentRuntimeInformation_OnExit(string exitMessage, bool stopChildTasks) private void ParentRuntimeInformation_OnExit(string exitMessage, bool stopAllTasks)
{ {
Exit($"Terminated by parent task", stopChildTasks); Exit($"Terminated by parent task", stopAllTasks);
} }
public void WriteLine(string output, bool forceWrite = false) public void WriteLine(string output, bool forceWrite = false)
@@ -134,6 +151,13 @@ namespace YesNt.Interpreter
{ {
Line line = Lines[Math.Min(LineNumber, Lines.Count - 1)]; 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); 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);
while (FunctionCallStack.Count > 0)
{
int stackLineNumber = FunctionCallStack.Pop().CallerLine;
Line stackLine = (ParentRuntimeInformation?.Lines ?? Lines).ElementAt(stackLineNumber);
WriteLine($" at line {stackLine.LineNumber + 1} in the file \"{stackLine.FileName}\"", true);
}
Stop = true; Stop = true;
} }
if (stopAllTasks && !StopAllTasks) if (stopAllTasks && !StopAllTasks)
@@ -173,7 +197,7 @@ namespace YesNt.Interpreter
StopAllTasks = false; StopAllTasks = false;
IsDebugMode = false; IsDebugMode = false;
IsInFunction = false; IsInFunction = false;
InternalIsInFunction = false; IsLocalSearch = false;
LineNumber = 0; LineNumber = 0;
taskId = internalTaskId + 1; taskId = internalTaskId + 1;
#pragma warning disable S2696 // Instance members should not write to "static" fields #pragma warning disable S2696 // Instance members should not write to "static" fields
@@ -23,6 +23,7 @@ namespace YesNt.Interpreter.Statements
else else
{ {
RuntimeInfo.SearchLabel = key; RuntimeInfo.SearchLabel = key;
RuntimeInfo.IsLocalSearch = RuntimeInfo.IsInFunction;
} }
} }
@@ -59,6 +60,7 @@ namespace YesNt.Interpreter.Statements
else else
{ {
RuntimeInfo.SearchLabel = key; RuntimeInfo.SearchLabel = key;
RuntimeInfo.IsLocalSearch = RuntimeInfo.IsInFunction;
} }
} }
@@ -78,6 +80,7 @@ namespace YesNt.Interpreter.Statements
if (!string.IsNullOrWhiteSpace(RuntimeInfo.SearchLabel) && RuntimeInfo.SearchLabel == key) if (!string.IsNullOrWhiteSpace(RuntimeInfo.SearchLabel) && RuntimeInfo.SearchLabel == key)
{ {
RuntimeInfo.SearchLabel = string.Empty; RuntimeInfo.SearchLabel = string.Empty;
RuntimeInfo.IsLocalSearch = false;
} }
} }
@@ -125,7 +128,7 @@ namespace YesNt.Interpreter.Statements
return; return;
} }
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)));
RuntimeInfo.InParametersStack.Clear(); RuntimeInfo.InParametersStack.Clear();
if (RuntimeInfo.Functions.ContainsKey(key)) if (RuntimeInfo.Functions.ContainsKey(key))
@@ -144,6 +147,11 @@ namespace YesNt.Interpreter.Statements
if (RuntimeInfo.IsSearching) if (RuntimeInfo.IsSearching)
{ {
RuntimeInfo.IsInFunction = false; RuntimeInfo.IsInFunction = false;
if (RuntimeInfo.IsLocalSearch)
{
RuntimeInfo.Exit($"Label \"{RuntimeInfo.SearchLabel}\" not found", true);
}
return; return;
} }
else else
@@ -160,6 +168,11 @@ namespace YesNt.Interpreter.Statements
if (RuntimeInfo.IsSearching) if (RuntimeInfo.IsSearching)
{ {
RuntimeInfo.IsInFunction = false; RuntimeInfo.IsInFunction = false;
if (RuntimeInfo.IsLocalSearch)
{
RuntimeInfo.Exit($"Label \"{RuntimeInfo.SearchLabel}\" not found", true);
}
return; return;
} }
else else
@@ -81,8 +81,8 @@ namespace YesNt.Interpreter.Statements
} }
} }
[StaticStatement(ExecuteInSearchLabelMode = true)] [Statement(">", SearchMode.Contains, SpaceAround.None, Priority = Priority.Highest)]
public void ReadVariable() public void ReadVariable(string _)
{ {
if (!RuntimeInfo.CurrentLine.Contains('>')) if (!RuntimeInfo.CurrentLine.Contains('>'))
{ {