mirror of
https://github.com/Stone-Red-Code/YesNt-Interpreter.git
synced 2026-09-04 09:06:41 +02:00
Add more statements and bug fixes
- Add `Execute(List<string>...` method to interpreter - Add escape sequence (`!!`) - Fix `Evaluator.Calulate` method not correctly handling `-` and `+` prefixes
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
{
|
||||
internal enum Priority
|
||||
{
|
||||
PreProcessing,
|
||||
Highest,
|
||||
VeryHigh,
|
||||
High,
|
||||
|
||||
@@ -6,6 +6,7 @@ namespace YesNt.Interpreter.Runtime
|
||||
{
|
||||
public int CallerLine { get; }
|
||||
public Dictionary<string, string> Variables { get; } = new();
|
||||
public Dictionary<string, int> Labels { get; } = new();
|
||||
public Stack<string> Arguemtns { get; }
|
||||
public Stack<string> Results { get; } = new();
|
||||
|
||||
|
||||
@@ -99,6 +99,19 @@ namespace YesNt.Interpreter
|
||||
Execute();
|
||||
}
|
||||
|
||||
public void Execute(List<string> lines, bool isDebugMode = false)
|
||||
{
|
||||
runtimeInfo.Reset();
|
||||
runtimeInfo.IsDebugMode = isDebugMode;
|
||||
|
||||
for (int i = 0; i < lines.Count; i++)
|
||||
{
|
||||
runtimeInfo.Lines.Add(new Line(lines[i], Path.GetFileName("#Memory#"), i));
|
||||
}
|
||||
|
||||
Execute();
|
||||
}
|
||||
|
||||
internal void Execute(List<Line> lines, Dictionary<string, string> gloablVariables, int startLine, RuntimeInformation parentRuntimeInformation)
|
||||
{
|
||||
runtimeInfo.Reset();
|
||||
@@ -142,7 +155,7 @@ namespace YesNt.Interpreter
|
||||
foreach (KeyValuePair<StaticStatementAttribute, Action> staticStatement in staticStatements)
|
||||
{
|
||||
StaticStatementAttribute staticStatementAttribute = staticStatement.Key;
|
||||
if (!staticStatementAttribute.ExecuteInSearchLabelMode && runtimeInfo.IsSearching)
|
||||
if (!staticStatementAttribute.ExecuteInSearchMode && runtimeInfo.IsSearching)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
using YesNt.Interpreter.Attributes;
|
||||
using YesNt.Interpreter.Enums;
|
||||
@@ -50,7 +51,8 @@ namespace YesNt.Interpreter.Statements
|
||||
}
|
||||
|
||||
[Statement("cls", SearchMode.Exact, SpaceAround.None, ConsoleColor.Magenta)]
|
||||
public void Clear(string args)
|
||||
[SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "Won't work if static")]
|
||||
public void Clear(string _)
|
||||
{
|
||||
Console.Clear();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
using YesNt.Interpreter.Attributes;
|
||||
using YesNt.Interpreter.Enums;
|
||||
@@ -62,11 +61,11 @@ namespace YesNt.Interpreter.Statements
|
||||
}
|
||||
}
|
||||
|
||||
[Statement("%iso", SearchMode.Contains, SpaceAround.End, ConsoleColor.Yellow, KeepStatementInArgs = true, Priority = Priority.Highest)]
|
||||
[Statement("%iso", SearchMode.Contains, SpaceAround.None, ConsoleColor.Yellow, KeepStatementInArgs = true, Priority = Priority.Highest)]
|
||||
public void CheckIfOutParameterAvalible(string args)
|
||||
{
|
||||
args += " ";
|
||||
args = args.Replace("%iso ", $"{RuntimeInfo.OutParametersStack.Count > 0} ");
|
||||
args = args.Replace("%iso", (RuntimeInfo.OutParametersStack.Count > 0).ToString());
|
||||
|
||||
RuntimeInfo.CurrentLine = args.TrimEnd();
|
||||
}
|
||||
@@ -89,7 +88,7 @@ namespace YesNt.Interpreter.Statements
|
||||
RuntimeInfo.InParametersStack.Push(argumanet.Trim());
|
||||
}
|
||||
|
||||
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.CurrentLine = string.Empty;
|
||||
|
||||
@@ -138,7 +137,7 @@ namespace YesNt.Interpreter.Statements
|
||||
}
|
||||
|
||||
args += " ";
|
||||
args = args.Replace("%isi ", $"{RuntimeInfo.FunctionCallStack.Peek().Arguemtns.Count > 0} ");
|
||||
args = args.Replace("%isi", (RuntimeInfo.FunctionCallStack.Peek().Arguemtns.Count > 0).ToString());
|
||||
|
||||
RuntimeInfo.CurrentLine = args.TrimEnd();
|
||||
}
|
||||
@@ -167,6 +166,11 @@ namespace YesNt.Interpreter.Statements
|
||||
if (RuntimeInfo.IsSearching)
|
||||
{
|
||||
RuntimeInfo.IsInFunction = false;
|
||||
|
||||
if (RuntimeInfo.IsLocalSearch)
|
||||
{
|
||||
RuntimeInfo.Exit($"Label \"{RuntimeInfo.SearchLabel}\" not found", true);
|
||||
}
|
||||
return;
|
||||
}
|
||||
else
|
||||
@@ -178,7 +182,7 @@ namespace YesNt.Interpreter.Statements
|
||||
{
|
||||
FunctionScope functionScope = RuntimeInfo.FunctionCallStack.Pop();
|
||||
|
||||
RuntimeInfo.OutParametersStack = functionScope.Results;
|
||||
RuntimeInfo.OutParametersStack = new Stack<string>(functionScope.Results);
|
||||
RuntimeInfo.LineNumber = functionScope.CallerLine;
|
||||
}
|
||||
else
|
||||
|
||||
@@ -10,36 +10,31 @@ namespace YesNt.Interpreter.Statements
|
||||
{
|
||||
private readonly Random random = new Random();
|
||||
|
||||
[Statement("%tim", SearchMode.Contains, SpaceAround.End, ConsoleColor.Blue, KeepStatementInArgs = true, Priority = Priority.Highest)]
|
||||
[Statement("%tim", SearchMode.Contains, SpaceAround.None, ConsoleColor.Blue, KeepStatementInArgs = true, Priority = Priority.Highest)]
|
||||
public void GetUnixTimestamp(string args)
|
||||
{
|
||||
args += " ";
|
||||
|
||||
while (args.Contains("%tim "))
|
||||
while (args.Contains("%tim"))
|
||||
{
|
||||
args = args.ReplaceFirstOccurrence("%tim ", $"{DateTimeOffset.Now.ToUnixTimeSeconds()}");
|
||||
args = args.ReplaceFirstOccurrence("%tim", $"{DateTimeOffset.Now.ToUnixTimeSeconds()}");
|
||||
}
|
||||
|
||||
RuntimeInfo.CurrentLine = args.TrimEnd();
|
||||
}
|
||||
|
||||
[Statement("%pi", SearchMode.Contains, SpaceAround.End, ConsoleColor.Blue, KeepStatementInArgs = true, Priority = Priority.Highest)]
|
||||
[Statement("%pi", SearchMode.Contains, SpaceAround.None, ConsoleColor.Blue, KeepStatementInArgs = true, Priority = Priority.Highest)]
|
||||
public void GetPi(string args)
|
||||
{
|
||||
args += " ";
|
||||
args = args.Replace("%pi ", $"{Math.PI} ");
|
||||
args = args.Replace("%pi", $"{Math.PI}");
|
||||
|
||||
RuntimeInfo.CurrentLine = args.TrimEnd();
|
||||
}
|
||||
|
||||
[Statement("%rnd", SearchMode.Contains, SpaceAround.End, ConsoleColor.Blue, KeepStatementInArgs = true, Priority = Priority.Highest)]
|
||||
[Statement("%rnd", SearchMode.Contains, SpaceAround.None, ConsoleColor.Blue, KeepStatementInArgs = true, Priority = Priority.Highest)]
|
||||
public void GetRandom(string args)
|
||||
{
|
||||
args += " ";
|
||||
|
||||
while (args.Contains("%rnd "))
|
||||
while (args.Contains("%rnd"))
|
||||
{
|
||||
args = args.ReplaceFirstOccurrence("%rnd ", $"{random.Next(32767, int.MaxValue)} ");
|
||||
args = args.ReplaceFirstOccurrence("%rnd", $"{random.Next(32767, int.MaxValue)}");
|
||||
}
|
||||
|
||||
RuntimeInfo.CurrentLine = args.TrimEnd();
|
||||
|
||||
@@ -13,11 +13,9 @@ namespace YesNt.Interpreter.Statements
|
||||
{
|
||||
internal class ProcessingStatements : StatementRuntimeInformation
|
||||
{
|
||||
private static readonly Regex calculationRegex = new Regex(@"((\)?)+(\(?)+[0-9]+(((\s?)+(\)?)(\s?)+\+(\s?)+(\(?)+(\s?)+|(\s?)+(\)?)(\s?)+\-(\s?)+(\(?)+(\s?)+|(\s?)+(\)?)(\s?)+\*(\s?)+(\(?)+(\s?)+|(\s?)+(\)?)(\s?)+\%(\s?)+(\(?)+(\s?)+|(\s?)+(\)?)(\s?)+\^(\s?)+(\(?)+(\s?)+|(\s?)+(\)?)(\s?)+\/(\s?)+(\(?)+(\s?)+)|[,.])(?=[0-9])+)+[0-9]+(\)?)+");
|
||||
private static readonly Regex calculationRegex = new Regex(@"[0-9*+().,^%/-]+[0-9*+ ().,^%/-]+[0-9*+().,^%/-]+");
|
||||
|
||||
//new Regex(@"((\)?)+(\(?)+[0-9]+(((\s?)+(\)?)(\s?)+\+(\s?)+(\(?)+(\s?)+|(\s?)+(\)?)(\s?)+\-(\s?)+(\(?)+(\s?)+|(\s?)+(\)?)(\s?)+\*(\s?)+(\(?)+(\s?)+|(\s?)+(\)?)(\s?)+\/(\s?)+(\(?)+(\s?)+)|[,.])(?=[0-9])+)+[0-9]+(\)?)+");
|
||||
|
||||
[Statement("!calc", SearchMode.EndOfLine, SpaceAround.Start, ConsoleColor.DarkYellow, Priority = Priority.High, ExecuteInSearchMode = true)]
|
||||
[Statement("!calc", SearchMode.EndOfLine, SpaceAround.Start, ConsoleColor.DarkYellow, Priority = Priority.High)]
|
||||
public void Calculate(string args)
|
||||
{
|
||||
MatchCollection matches = calculationRegex.Matches(args.FromSaveString());
|
||||
@@ -30,7 +28,7 @@ namespace YesNt.Interpreter.Statements
|
||||
RuntimeInfo.Exit("Invalid operation", true);
|
||||
return;
|
||||
}
|
||||
args = args.FromSaveString().Replace(matches[0].Value, res);
|
||||
args = args.FromSaveString().Replace(matches[i].Value, res);
|
||||
}
|
||||
|
||||
RuntimeInfo.CurrentLine = args;
|
||||
@@ -42,6 +40,23 @@ namespace YesNt.Interpreter.Statements
|
||||
RuntimeInfo.CurrentLine = args.FromSaveString();
|
||||
}
|
||||
|
||||
[Statement("!!", SearchMode.Contains, SpaceAround.None, ConsoleColor.DarkYellow, Priority = Priority.PreProcessing, KeepStatementInArgs = true)]
|
||||
public void DontEvaluate(string args)
|
||||
{
|
||||
int index;
|
||||
while ((index = args.IndexOf("!!")) != -1)
|
||||
{
|
||||
args = args.Remove(index, 2);
|
||||
if (index < args.Length)
|
||||
{
|
||||
char charToEscape = args[index];
|
||||
args = args.Remove(index, 1);
|
||||
args = args.Insert(index, charToEscape.ToString().ToSaveString());
|
||||
}
|
||||
}
|
||||
RuntimeInfo.CurrentLine = args;
|
||||
}
|
||||
|
||||
[Statement("!task", SearchMode.EndOfLine, SpaceAround.Start, ConsoleColor.DarkYellow, Priority = Priority.VeryHigh)]
|
||||
public void RunTask(string line)
|
||||
{
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace YesNt.Interpreter.Utilities
|
||||
@@ -87,7 +88,18 @@ namespace YesNt.Interpreter.Utilities
|
||||
return null;
|
||||
}
|
||||
|
||||
public static string Calculate(string input, char op = '+')
|
||||
public static string Calculate(string input)
|
||||
{
|
||||
input = Regex.Replace(input, @"(\+ +\+)+", "+");
|
||||
input = Regex.Replace(input, @"(\- +\-)+", "+");
|
||||
input = Regex.Replace(input, @"(\- +\+)+", "-");
|
||||
input = Regex.Replace(input, @"(\+ +\-)+", "-");
|
||||
|
||||
string yes = Calculate(input, '+');
|
||||
return yes;
|
||||
}
|
||||
|
||||
private static string Calculate(string input, char op)
|
||||
{
|
||||
if (input is null)
|
||||
{
|
||||
@@ -110,6 +122,13 @@ namespace YesNt.Interpreter.Utilities
|
||||
|
||||
string[] parts = input.Split(op);
|
||||
|
||||
//Weird fix
|
||||
if (parts.Length >= 2 && string.IsNullOrWhiteSpace(parts[0]))
|
||||
{
|
||||
parts[1] = $"{op}{parts[1]}";
|
||||
parts = parts.Skip(1).ToArray();
|
||||
}
|
||||
|
||||
double number = double.NaN;
|
||||
|
||||
foreach (string p in parts)
|
||||
@@ -173,7 +192,7 @@ namespace YesNt.Interpreter.Utilities
|
||||
}
|
||||
}
|
||||
|
||||
return number.ToString();
|
||||
return number.ToString(System.Globalization.CultureInfo.InvariantCulture);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user