Code structure improvements

This commit is contained in:
Stone_Red
2022-03-10 12:43:22 +01:00
parent c02104db5a
commit 83f99d4dd5
8 changed files with 26 additions and 379 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
namespace YesNt.CodeEditor
{
internal class Program
internal static class Program
{
private static void Main(string[] args)
{
+1 -1
View File
@@ -2,7 +2,7 @@
namespace YesNt.Interpreter
{
internal class Program
internal static class Program
{
private static void Main(string[] args)
{
+1 -1
View File
@@ -6,7 +6,7 @@ namespace YesNt.Interpreter.Runtime
{
public int CallerLine { get; }
public Dictionary<string, string> Variables { get; } = new();
public Stack<string> Arguemtns { get; } = new();
public Stack<string> Arguemtns { get; }
public Stack<string> Results { get; } = new();
public FunctionScope(int callerLine, Stack<string> arguemtns)
@@ -91,7 +91,7 @@ namespace YesNt.Interpreter
{
if (IsTask)
{
parentRuntimeInformation.WriteLine(output.FromSaveString(), forceWrite);
parentRuntimeInformation!.WriteLine(output.FromSaveString(), forceWrite);
}
else
{
@@ -115,7 +115,7 @@ namespace YesNt.Interpreter
{
if (IsTask)
{
parentRuntimeInformation.Write(output.FromSaveString(), forceWrite);
parentRuntimeInformation!.Write(output.FromSaveString(), forceWrite);
}
else
{
@@ -133,10 +133,10 @@ namespace YesNt.Interpreter
if (!Stop)
{
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);
Stop = true;
}
if (stopAllTasks == true && StopAllTasks == false)
if (stopAllTasks && !StopAllTasks)
{
StopAllTasks = true;
OnExit?.Invoke(message, StopAllTasks);
@@ -176,7 +176,9 @@ namespace YesNt.Interpreter
InternalIsInFunction = false;
LineNumber = 0;
taskId = internalTaskId + 1;
#pragma warning disable S2696 // Instance members should not write to "static" fields
internalTaskId++;
#pragma warning restore S2696 // Instance members should not write to "static" fields
}
}
}
@@ -142,7 +142,7 @@ namespace YesNt.Interpreter
foreach (KeyValuePair<StaticStatementAttribute, Action> staticStatement in staticStatements)
{
StaticStatementAttribute staticStatementAttribute = staticStatement.Key;
if (staticStatementAttribute.ExecuteInSearchLabelMode == false && runtimeInfo.IsSearching)
if (!staticStatementAttribute.ExecuteInSearchLabelMode && runtimeInfo.IsSearching)
{
continue;
}
@@ -157,7 +157,7 @@ namespace YesNt.Interpreter
{
StatementAttribute statementAttribute = statement.Key;
if (statementAttribute.ExecuteInSearchMode == false && runtimeInfo.IsSearching)
if (!statementAttribute.ExecuteInSearchMode && runtimeInfo.IsSearching)
{
statementFound = true;
continue;
@@ -227,7 +227,7 @@ namespace YesNt.Interpreter
}
}
if (runtimeInfo.Stop == false)
if (!runtimeInfo.Stop)
{
if (!string.IsNullOrWhiteSpace(runtimeInfo.SearchLabel))
{
@@ -41,12 +41,14 @@ namespace YesNt.Interpreter.Statements
bool? result = Evaluator.EvaluateCondition(condition);
if (result != true)
if (result is null)
{
RuntimeInfo.Exit("Invalid operation", true);
return;
}
if (result == false)
{
if (result is null)
{
RuntimeInfo.Exit("Invalid operation", true);
}
return;
}
@@ -112,12 +114,14 @@ namespace YesNt.Interpreter.Statements
bool? result = Evaluator.EvaluateCondition(condition);
if (result != true)
if (result is null)
{
RuntimeInfo.Exit("Invalid operation", true);
return;
}
if (result == false)
{
if (result is null)
{
RuntimeInfo.Exit("Invalid operation", true);
}
return;
}
@@ -32,7 +32,6 @@ namespace YesNt.Interpreter.Statements
else
{
RuntimeInfo.Exit("Invalid syntax", true);
return;
}
}
@@ -60,7 +59,6 @@ namespace YesNt.Interpreter.Statements
else
{
RuntimeInfo.Exit("Invalid syntax", true);
return;
}
}
-357
View File
@@ -1,357 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
namespace YesNt.Interpreter
{
public class YesNtInterpreter_OLD
{
private readonly Dictionary<string, string> variables = new Dictionary<string, string>();
private readonly Dictionary<string, int> labels = new Dictionary<string, int>();
private List<string> lines = new List<string>();
private Stack<int> lastLabels = new();
public void Execute(string path)
{
lines.Clear();
variables.Clear();
labels.Clear();
lastLabels.Clear();
LoadFile(path);
string searchLabel = string.Empty;
for (int lineNum = 0; lineNum < lines.Count; lineNum++)
{
string line = lines[lineNum].Trim().Replace("\r", "");
if (string.IsNullOrWhiteSpace(line))
{
continue;
}
if (searchLabel == string.Empty)
{
if (line.Contains("%crl"))
{
line = line.Replace("%crl", ToSaveString(Console.ReadLine()));
}
if (line.Contains("%cr"))
{
line = line.Replace("%cr", ToSaveString(Console.ReadKey().KeyChar.ToString()));
}
foreach (KeyValuePair<string, string> variable in variables)
{
line = line.Replace($">{variable.Key}", variable.Value);
}
if (line.Contains(" "))
{
int index = line.IndexOf(' ');
if (line.Contains(" = ") && line.IndexOf('=') > index)
{
index = line.IndexOf('=') + 1;
}
string cmd = line.Substring(0, index);
cmd += Calculate(line.Substring(index));
line = cmd;
}
}
if (line.StartsWith("%lbl "))
{
string key = line.Substring(5).Trim();
if (labels.ContainsKey(key))
{
labels[key] = lineNum;
}
else
{
labels.Add(key, lineNum);
}
if (searchLabel != string.Empty && searchLabel == key)
{
searchLabel = string.Empty;
}
continue;
}
if (searchLabel != string.Empty)
{
continue;
}
if (line.StartsWith("%imp "))
{
string pat = line.Substring(5).Trim();
lines.RemoveAt(lineNum);
LoadFile(pat, lineNum);
lineNum--;
}
else if (line.StartsWith("%jmp "))
{
string key = FromSaveString(line.Substring(5).Trim());
lastLabels.Push(lineNum);
if (labels.ContainsKey(key))
{
lineNum = labels[key];
}
else
{
searchLabel = key;
}
}
else if (line.StartsWith("%jif "))
{
if (line.Contains("|"))
{
string dat = line.Substring(5).Trim();
string key = dat.Substring(0, dat.IndexOf('|')).Trim();
string condition = dat.Substring(dat.IndexOf('|')).Replace("|", "").Trim();
if (EvaluateCondition(condition))
{
lastLabels.Push(lineNum);
if (labels.ContainsKey(key))
{
lineNum = labels[key];
}
else
{
searchLabel = key;
}
}
}
}
else if (line.Equals("%ret"))
{
lineNum = lastLabels.Pop();
}
else if (line.Equals("%end"))
{
break;
}
else if (line.StartsWith("%cwl "))
{
Console.WriteLine(FromSaveString(line.Substring(5)));
}
else if (line.StartsWith("%cw "))
{
Console.Write(FromSaveString(line.Substring(4)));
}
else if (line.StartsWith("<"))
{
string[] parts = line.Split(" = ");
if (parts.Length == 2)
{
string key = parts[0].Replace("<", "");
if (variables.ContainsKey(key))
{
variables[key] = parts[1];
}
else
{
variables.Add(key, parts[1]);
}
}
}
}
}
private void LoadFile(string path, int index = 0)
{
if (!File.Exists(path))
{
Console.WriteLine($"File \"{path}\" not found!");
return;
}
string input = File.ReadAllText(path);
string[] newLines = input.Split("\n");
foreach (string line in newLines)
{
lines.Insert(index, line);
index++;
}
}
private string ToSaveString(string input)
{
string output = "";
foreach (char c in input)
{
output += $"\r{c}\r";
}
return output;
}
private string FromSaveString(string input)
{
return input.Replace("\r", "");
}
private bool EvaluateCondition(string input)
{
string[] parts = input.Split(" == ");
if (parts.Length == 2)
{
string part1 = FromSaveString(parts[0]);
string part2 = FromSaveString(parts[1]);
return part1 == part2;
}
parts = input.Split(" != ");
if (parts.Length == 2)
{
string part1 = FromSaveString(parts[0]);
string part2 = FromSaveString(parts[1]);
return part1 != part2;
}
parts = input.Split(" > ");
if (parts.Length == 2)
{
bool succ1 = double.TryParse(FromSaveString(parts[0]), out double part1);
bool succ2 = double.TryParse(FromSaveString(parts[1]), out double part2);
if (!succ1 || !succ2)
{
return false;
}
return part1 > part2;
}
parts = input.Split(" < ");
if (parts.Length == 2)
{
bool succ1 = double.TryParse(FromSaveString(parts[0]), out double part1);
bool succ2 = double.TryParse(FromSaveString(parts[1]), out double part2);
if (!succ1 || !succ2)
{
return false;
}
return part1 < part2;
}
parts = input.Split(" >= ");
if (parts.Length == 2)
{
bool succ1 = double.TryParse(FromSaveString(parts[0]), out double part1);
bool succ2 = double.TryParse(FromSaveString(parts[1]), out double part2);
if (!succ1 || !succ2)
{
return false;
}
return part1 >= part2;
}
parts = input.Split(" <= ");
if (parts.Length == 2)
{
bool succ1 = double.TryParse(FromSaveString(parts[0]), out double part1);
bool succ2 = double.TryParse(FromSaveString(parts[1]), out double part2);
if (!succ1 || !succ2)
{
return false;
}
return part1 <= part2;
}
return false;
}
private string Calculate(string input, char op = '+')
{
string[] parts = input.Split($" {op} ");
string result = "";
double number = double.NaN;
int spacesToAdd = 0;
foreach (string p in parts)
{
string part = p;
switch (op)
{
case '+':
part = Calculate(part, '-');
break;
case '-':
part = Calculate(part, '*');
break;
case '*':
part = Calculate(part, '/');
break;
}
if (double.TryParse(FromSaveString(part), out double num))
{
if (double.IsNaN(number))
{
spacesToAdd = CountSpaces(part);
number = num;
}
else
{
switch (op)
{
case '+':
number += num;
break;
case '-':
number -= num;
break;
case '*':
number *= num;
break;
case '/':
number /= num;
break;
}
}
}
else
{
if (!double.IsNaN(number))
{
result += new string(' ', spacesToAdd) + number;
}
result += part;
number = double.NaN;
}
}
if (!double.IsNaN(number))
{
result += new string(' ', spacesToAdd) + number;
}
return result;
}
private int CountSpaces(string input)
{
int count = 0;
while (input[count] == ' ')
{
count++;
}
return count;
}
}
}