From 7e357884f53b31076526c68bc964215268fc9491 Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Wed, 7 Aug 2024 21:19:45 +0200 Subject: [PATCH] `get` and `out` statements can now be placed anywhere and function arguments are now always in correct order --- .../Statements/CodeFlowStatements.cs | 3 +- .../Statements/FunctionStatements.cs | 49 +++++++++---------- 2 files changed, 23 insertions(+), 29 deletions(-) diff --git a/YesNt.Interpreter/Statements/CodeFlowStatements.cs b/YesNt.Interpreter/Statements/CodeFlowStatements.cs index d1ccdd6..7ae96a9 100644 --- a/YesNt.Interpreter/Statements/CodeFlowStatements.cs +++ b/YesNt.Interpreter/Statements/CodeFlowStatements.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; using YesNt.Interpreter.Attributes; using YesNt.Interpreter.Enums; @@ -89,7 +88,7 @@ internal class CodeFlowStatements : StatementRuntimeInformation { string key = args.Trim(); - RuntimeInfo.FunctionCallStack.Push(new FunctionScope(RuntimeInfo.LineNumber, new Stack(RuntimeInfo.InParametersStack.Reverse()))); + RuntimeInfo.FunctionCallStack.Push(new FunctionScope(RuntimeInfo.LineNumber, new Stack(RuntimeInfo.InParametersStack))); RuntimeInfo.InParametersStack.Clear(); if (RuntimeInfo.Functions.TryGetValue(key, out int value)) diff --git a/YesNt.Interpreter/Statements/FunctionStatements.cs b/YesNt.Interpreter/Statements/FunctionStatements.cs index de923c2..4fef510 100644 --- a/YesNt.Interpreter/Statements/FunctionStatements.cs +++ b/YesNt.Interpreter/Statements/FunctionStatements.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using YesNt.Interpreter.Attributes; using YesNt.Interpreter.Enums; using YesNt.Interpreter.Runtime; +using YesNt.Interpreter.Utilities; namespace YesNt.Interpreter.Statements; @@ -42,29 +43,26 @@ internal class FunctionStatements : StatementRuntimeInformation RuntimeInfo.InParametersStack.Push(args); } - [Statement("out", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.Yellow)] + [Statement("%out", SearchMode.Contains, SpaceAround.None, ConsoleColor.Yellow, KeepStatementInArgs = true, Priority = Priority.Highest)] public void GetOutParameter(string args) { - if (RuntimeInfo.OutParametersStack.Count == 0) + while (args.Contains("%out")) { - RuntimeInfo.Exit("No out argument in stack", true); - return; + if (RuntimeInfo.OutParametersStack.Count == 0) + { + RuntimeInfo.Exit("No out argument in stack", true); + return; + } + + args = args.ReplaceFirstOccurrence("%out", RuntimeInfo.OutParametersStack.Pop()); } - if (RuntimeInfo.Variables.ContainsKey(args)) - { - RuntimeInfo.Variables[args] = RuntimeInfo.OutParametersStack.Pop(); - } - else - { - RuntimeInfo.Variables.Add(args, RuntimeInfo.OutParametersStack.Pop()); - } + RuntimeInfo.CurrentLine = args.TrimEnd(); } [Statement("%iso", SearchMode.Contains, SpaceAround.None, ConsoleColor.Yellow, KeepStatementInArgs = true, Priority = Priority.Highest)] public void CheckIfOutParameterAvailable(string args) { - args += " "; args = args.Replace("%iso", (RuntimeInfo.OutParametersStack.Count > 0).ToString()); RuntimeInfo.CurrentLine = args.TrimEnd(); @@ -102,7 +100,7 @@ internal class FunctionStatements : StatementRuntimeInformation } } - [Statement("get", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.Yellow)] + [Statement("%get", SearchMode.Contains, SpaceAround.None, ConsoleColor.Yellow, KeepStatementInArgs = true, Priority = Priority.Highest)] public void GetInParameter(string args) { if (!RuntimeInfo.IsInFunction) @@ -111,23 +109,21 @@ internal class FunctionStatements : StatementRuntimeInformation return; } - if (RuntimeInfo.FunctionCallStack.Peek().Arguments.Count == 0) + while (args.Contains("%get")) { - RuntimeInfo.Exit("No in argument in stack", true); - return; + if (RuntimeInfo.FunctionCallStack.Peek().Arguments.Count == 0) + { + RuntimeInfo.Exit("No in argument in stack", true); + return; + } + + args = args.ReplaceFirstOccurrence("%get", RuntimeInfo.FunctionCallStack.Peek().Arguments.Pop()); } - if (RuntimeInfo.Variables.ContainsKey(args)) - { - RuntimeInfo.Variables[args] = RuntimeInfo.FunctionCallStack.Peek().Arguments.Pop(); - } - else - { - RuntimeInfo.Variables.Add(args, RuntimeInfo.FunctionCallStack.Peek().Arguments.Pop()); - } + RuntimeInfo.CurrentLine = args.TrimEnd(); } - [Statement("%isi", SearchMode.Contains, SpaceAround.End, ConsoleColor.Yellow, KeepStatementInArgs = true, Priority = Priority.Highest)] + [Statement("%isi", SearchMode.Contains, SpaceAround.None, ConsoleColor.Yellow, KeepStatementInArgs = true, Priority = Priority.Highest)] public void CheckIfInParameterAvailable(string args) { if (!RuntimeInfo.IsInFunction) @@ -136,7 +132,6 @@ internal class FunctionStatements : StatementRuntimeInformation return; } - args += " "; args = args.Replace("%isi", (RuntimeInfo.FunctionCallStack.Peek().Arguments.Count > 0).ToString()); RuntimeInfo.CurrentLine = args.TrimEnd();