From 83f99d4dd5c8847b4d7530c4bb84d340ea8df9ca Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Thu, 10 Mar 2022 12:43:22 +0100 Subject: [PATCH] Code structure improvements --- YesNt.CodeEditor/Program.cs | 2 +- YesNt.Interpreter/Program.cs | 2 +- YesNt.Interpreter/Runtime/FunctionScope.cs | 2 +- .../Runtime/RuntimeInformation.cs | 10 +- YesNt.Interpreter/Runtime/YesNtInterpreter.cs | 6 +- .../Statements/CodeFlowStatements.cs | 24 +- .../Statements/VariableStatements.cs | 2 - YesNt.Interpreter/YesNtInterpreter_OLD.cs | 357 ------------------ 8 files changed, 26 insertions(+), 379 deletions(-) delete mode 100644 YesNt.Interpreter/YesNtInterpreter_OLD.cs diff --git a/YesNt.CodeEditor/Program.cs b/YesNt.CodeEditor/Program.cs index a631dbe..d95b4a7 100644 --- a/YesNt.CodeEditor/Program.cs +++ b/YesNt.CodeEditor/Program.cs @@ -1,6 +1,6 @@ namespace YesNt.CodeEditor { - internal class Program + internal static class Program { private static void Main(string[] args) { diff --git a/YesNt.Interpreter/Program.cs b/YesNt.Interpreter/Program.cs index 29ec627..f0e0ddc 100644 --- a/YesNt.Interpreter/Program.cs +++ b/YesNt.Interpreter/Program.cs @@ -2,7 +2,7 @@ namespace YesNt.Interpreter { - internal class Program + internal static class Program { private static void Main(string[] args) { diff --git a/YesNt.Interpreter/Runtime/FunctionScope.cs b/YesNt.Interpreter/Runtime/FunctionScope.cs index 65865ff..9a72675 100644 --- a/YesNt.Interpreter/Runtime/FunctionScope.cs +++ b/YesNt.Interpreter/Runtime/FunctionScope.cs @@ -6,7 +6,7 @@ namespace YesNt.Interpreter.Runtime { public int CallerLine { get; } public Dictionary Variables { get; } = new(); - public Stack Arguemtns { get; } = new(); + public Stack Arguemtns { get; } public Stack Results { get; } = new(); public FunctionScope(int callerLine, Stack arguemtns) diff --git a/YesNt.Interpreter/Runtime/RuntimeInformation.cs b/YesNt.Interpreter/Runtime/RuntimeInformation.cs index 699fadc..a8aff17 100644 --- a/YesNt.Interpreter/Runtime/RuntimeInformation.cs +++ b/YesNt.Interpreter/Runtime/RuntimeInformation.cs @@ -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 } } } \ No newline at end of file diff --git a/YesNt.Interpreter/Runtime/YesNtInterpreter.cs b/YesNt.Interpreter/Runtime/YesNtInterpreter.cs index 31e9c9f..6654446 100644 --- a/YesNt.Interpreter/Runtime/YesNtInterpreter.cs +++ b/YesNt.Interpreter/Runtime/YesNtInterpreter.cs @@ -142,7 +142,7 @@ namespace YesNt.Interpreter foreach (KeyValuePair 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)) { diff --git a/YesNt.Interpreter/Statements/CodeFlowStatements.cs b/YesNt.Interpreter/Statements/CodeFlowStatements.cs index 78d4d64..2bd38ae 100644 --- a/YesNt.Interpreter/Statements/CodeFlowStatements.cs +++ b/YesNt.Interpreter/Statements/CodeFlowStatements.cs @@ -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; } diff --git a/YesNt.Interpreter/Statements/VariableStatements.cs b/YesNt.Interpreter/Statements/VariableStatements.cs index 2fb3f76..5d3232b 100644 --- a/YesNt.Interpreter/Statements/VariableStatements.cs +++ b/YesNt.Interpreter/Statements/VariableStatements.cs @@ -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; } } diff --git a/YesNt.Interpreter/YesNtInterpreter_OLD.cs b/YesNt.Interpreter/YesNtInterpreter_OLD.cs deleted file mode 100644 index 5ed19fb..0000000 --- a/YesNt.Interpreter/YesNtInterpreter_OLD.cs +++ /dev/null @@ -1,357 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; - -namespace YesNt.Interpreter -{ - public class YesNtInterpreter_OLD - { - private readonly Dictionary variables = new Dictionary(); - private readonly Dictionary labels = new Dictionary(); - private List lines = new List(); - private Stack 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 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; - } - } -} \ No newline at end of file