From 6a4fe4f871e2775f4f3da9a99e76f9a2a17c0d2c Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Sun, 27 Mar 2022 20:25:44 +0200 Subject: [PATCH] Change logic of some changes - `%rnd` now returns a random integer between `32767` and `int.MaxValue` - Fix syntax hyglighing problems when using comments - Remove the `mod` statement --- YesNt.CodeEditor/SyntaxHighlighter.cs | 167 +++++++++--------- .../PredifinedVariableStatements.cs | 2 +- .../Statements/ProcessingStatements.cs | 38 +--- 3 files changed, 87 insertions(+), 120 deletions(-) diff --git a/YesNt.CodeEditor/SyntaxHighlighter.cs b/YesNt.CodeEditor/SyntaxHighlighter.cs index 887a6b4..7c284a1 100644 --- a/YesNt.CodeEditor/SyntaxHighlighter.cs +++ b/YesNt.CodeEditor/SyntaxHighlighter.cs @@ -20,93 +20,96 @@ namespace YesNt.CodeEditor public void Write(string input) { input = input.Replace("\0", string.Empty); - foreach (StatementInformation statement in statementInformation) - { - if (statement.IgnoreSyntaxHighlighting) - { - continue; - } - - string name = statement.SpaceAround switch - { - SpaceAround.StartEnd => $" {statement.Name.Trim()} ", - SpaceAround.Start => $" {statement.Name.Trim()}", - SpaceAround.End => $"{statement.Name.Trim()} ", - _ => statement.Name - }; - input = input.TrimEnd(); - if (statement.SearchMode == SearchMode.StartOfLine && input.StartsWith(name)) - { - if (statement.Seperator is not null && input.Contains(statement.Seperator)) - { - input = AddColorInformation(input, statement.Seperator, statement.Color, SearchMode.StartOfLine); - } - else if (statement.Seperator is not null) - { - continue; - } - input = AddColorInformation(input, input[..name.Length], statement.Color, statement.SearchMode); - } - if (statement.SearchMode == SearchMode.Contains && $" {input} ".Contains(name)) - { - if (statement.Seperator is not null && input.Contains(statement.Seperator)) - { - input = AddColorInformation(input, statement.Seperator, statement.Color, SearchMode.StartOfLine); - } - else if (statement.Seperator is not null) - { - continue; - } - input = AddColorInformation($"{input} ", $"{input} ".Substring($"{input} ".IndexOf(name), name.Length), statement.Color, statement.SearchMode); - } - if (statement.SearchMode == SearchMode.EndOfLine && input.EndsWith(name)) - { - if (statement.Seperator is not null && input.Contains(statement.Seperator)) - { - input = AddColorInformation(input, statement.Seperator, statement.Color, SearchMode.StartOfLine); - } - else if (statement.Seperator is not null) - { - continue; - } - input = AddColorInformation(input, input[^name.Length..], statement.Color, statement.SearchMode); - } - if (statement.SearchMode == SearchMode.Exact && input.Equals(name)) - { - if (statement.Seperator is not null && input.Contains(statement.Seperator)) - { - input = AddColorInformation(input, statement.Seperator, statement.Color, SearchMode.StartOfLine); - } - else if (statement.Seperator is not null) - { - continue; - } - input = AddColorInformation(input, input, statement.Color, statement.SearchMode); - } - } - - MatchCollection matches = Regex.Matches(input, @">[a-zA-Z0-9]+"); - for (int i = 0; i < matches.Count; i++) - { - input = AddColorInformation(input, matches[i].Value, ConsoleColor.Cyan, SearchMode.Contains); - } - - matches = Regex.Matches(input, @"^<[a-zA-Z0-9]+"); - for (int i = 0; i < matches.Count; i++) - { - 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); - } if (input.StartsWith('#')) { input = AddColorInformation(input, input, ConsoleColor.Gray, SearchMode.Exact); } + else + { + foreach (StatementInformation statement in statementInformation) + { + if (statement.IgnoreSyntaxHighlighting) + { + continue; + } + + string name = statement.SpaceAround switch + { + SpaceAround.StartEnd => $" {statement.Name.Trim()} ", + SpaceAround.Start => $" {statement.Name.Trim()}", + SpaceAround.End => $"{statement.Name.Trim()} ", + _ => statement.Name + }; + input = input.TrimEnd(); + if (statement.SearchMode == SearchMode.StartOfLine && input.StartsWith(name)) + { + if (statement.Seperator is not null && input.Contains(statement.Seperator)) + { + input = AddColorInformation(input, statement.Seperator, statement.Color, SearchMode.StartOfLine); + } + else if (statement.Seperator is not null) + { + continue; + } + input = AddColorInformation(input, input[..name.Length], statement.Color, statement.SearchMode); + } + if (statement.SearchMode == SearchMode.Contains && $" {input} ".Contains(name)) + { + if (statement.Seperator is not null && input.Contains(statement.Seperator)) + { + input = AddColorInformation(input, statement.Seperator, statement.Color, SearchMode.StartOfLine); + } + else if (statement.Seperator is not null) + { + continue; + } + input = AddColorInformation($"{input} ", $"{input} ".Substring($"{input} ".IndexOf(name), name.Length), statement.Color, statement.SearchMode); + } + if (statement.SearchMode == SearchMode.EndOfLine && input.EndsWith(name)) + { + if (statement.Seperator is not null && input.Contains(statement.Seperator)) + { + input = AddColorInformation(input, statement.Seperator, statement.Color, SearchMode.StartOfLine); + } + else if (statement.Seperator is not null) + { + continue; + } + input = AddColorInformation(input, input[^name.Length..], statement.Color, statement.SearchMode); + } + if (statement.SearchMode == SearchMode.Exact && input.Equals(name)) + { + if (statement.Seperator is not null && input.Contains(statement.Seperator)) + { + input = AddColorInformation(input, statement.Seperator, statement.Color, SearchMode.StartOfLine); + } + else if (statement.Seperator is not null) + { + continue; + } + input = AddColorInformation(input, input, statement.Color, statement.SearchMode); + } + } + + MatchCollection matches = Regex.Matches(input, @">[a-zA-Z0-9]+"); + for (int i = 0; i < matches.Count; i++) + { + input = AddColorInformation(input, matches[i].Value, ConsoleColor.Cyan, SearchMode.Contains); + } + + matches = Regex.Matches(input, @"^<[a-zA-Z0-9]+"); + for (int i = 0; i < matches.Count; i++) + { + 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")) { diff --git a/YesNt.Interpreter/Statements/PredifinedVariableStatements.cs b/YesNt.Interpreter/Statements/PredifinedVariableStatements.cs index 92a78a0..7c4e50c 100644 --- a/YesNt.Interpreter/Statements/PredifinedVariableStatements.cs +++ b/YesNt.Interpreter/Statements/PredifinedVariableStatements.cs @@ -39,7 +39,7 @@ namespace YesNt.Interpreter.Statements while (args.Contains("%rnd ")) { - args = args.ReplaceFirstOccurrence("%rnd ", $"{random.Next() % 2} "); + args = args.ReplaceFirstOccurrence("%rnd ", $"{random.Next(32767, int.MaxValue)} "); } RuntimeInfo.CurrentLine = args.TrimEnd(); diff --git a/YesNt.Interpreter/Statements/ProcessingStatements.cs b/YesNt.Interpreter/Statements/ProcessingStatements.cs index 835cd2a..3d11961 100644 --- a/YesNt.Interpreter/Statements/ProcessingStatements.cs +++ b/YesNt.Interpreter/Statements/ProcessingStatements.cs @@ -85,7 +85,7 @@ namespace YesNt.Interpreter.Statements for (int i = 0; i < lines.Length; i++) { - RuntimeInfo.Lines.Add(new Line(lines[i], Path.GetFileName(path), i)); + RuntimeInfo.Lines.Insert(RuntimeInfo.LineNumber + i, new Line(lines[i], Path.GetFileName(path), i)); } RuntimeInfo.LineNumber--; } @@ -99,41 +99,5 @@ namespace YesNt.Interpreter.Statements RuntimeInfo.Exit($"Could not find file \"{path}\"", true); } } - - [Statement("mod", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.DarkYellow, Priority = Priority.VeryLow, Seperator = "|")] - public void GetModulo(string args) - { - string[] parts = args.Split('|'); - if (parts.Length != 2) - { - RuntimeInfo.Exit("Invalid syntax", true); - return; - } - - string[] nums = parts[1].Split(','); - if (nums.Length != 2) - { - RuntimeInfo.Exit("Invalid arguments", true); - return; - } - - string variableName = parts[0].Trim(); - - if (ulong.TryParse(nums[0], out ulong num1) && ulong.TryParse(nums[1], out ulong num2)) - { - if (RuntimeInfo.Variables.ContainsKey(variableName)) - { - RuntimeInfo.Variables[variableName] = (num1 % num2).ToString(); - } - else - { - RuntimeInfo.Variables.Add(variableName, (num1 % num2).ToString()); - } - } - else - { - RuntimeInfo.Exit("Invalid arguments", true); - } - } } } \ No newline at end of file