mirror of
https://github.com/Stone-Red-Code/YesNt-Interpreter.git
synced 2026-09-04 00:56:31 +02:00
- Add predefined variables
- Add `mod` function - Improve function calling syntax
This commit is contained in:
@@ -81,7 +81,7 @@ namespace YesNt.CodeEditor
|
||||
if (CursorPosition.Y == i || drawAll)
|
||||
{
|
||||
Console.Write(lineCountString);
|
||||
string printLine = $"{Lines[i].Substring(0, Math.Min(Lines[i].Length, Console.WindowWidth))}".TrimEnd();
|
||||
string printLine = $"{Lines[i][..Math.Min(Lines[i].Length, Console.WindowWidth)]}".TrimEnd();
|
||||
syntaxHighlighter.Write(printLine);
|
||||
Console.Write(new string(' ', Math.Max(Console.WindowWidth - lineCountString.Length - printLine.Length, 0)));
|
||||
}
|
||||
@@ -166,7 +166,7 @@ namespace YesNt.CodeEditor
|
||||
path = Path.ChangeExtension(path, "ynt");
|
||||
}
|
||||
|
||||
while (Lines.Count > 0 && string.IsNullOrWhiteSpace(Lines[Lines.Count - 1]))
|
||||
while (Lines.Count > 0 && string.IsNullOrWhiteSpace(Lines[^1]))
|
||||
{
|
||||
Lines.RemoveAt(Lines.Count - 1);
|
||||
}
|
||||
|
||||
@@ -176,40 +176,43 @@ namespace YesNt.Interpreter
|
||||
_ => statementAttribute.Name
|
||||
};
|
||||
|
||||
if (statementAttribute.SearchMode == SearchMode.StartOfLine && runtimeInfo.CurrentLine.StartsWith(name))
|
||||
if (statementAttribute.Seperator is null || runtimeInfo.CurrentLine.Contains(statementAttribute.Seperator))
|
||||
{
|
||||
string copyLine = statementAttribute.KeepStatementInArgs ? runtimeInfo.CurrentLine : runtimeInfo.CurrentLine.Remove(0, name.Length);
|
||||
statement.Value.Invoke(copyLine);
|
||||
statementFound = true;
|
||||
}
|
||||
else if (statementAttribute.SearchMode == SearchMode.Contains && $" {runtimeInfo.CurrentLine} ".Contains(name))
|
||||
{
|
||||
bool leadingWhitespace = runtimeInfo.CurrentLine.StartsWith(' ');
|
||||
|
||||
runtimeInfo.CurrentLine = $" {runtimeInfo.CurrentLine} ";
|
||||
string copyLine = statementAttribute.KeepStatementInArgs ? runtimeInfo.CurrentLine : runtimeInfo.CurrentLine.Replace(name, string.Empty);
|
||||
statement.Value.Invoke(copyLine);
|
||||
statementFound = true;
|
||||
|
||||
if (!leadingWhitespace)
|
||||
if (statementAttribute.SearchMode == SearchMode.StartOfLine && runtimeInfo.CurrentLine.StartsWith(name))
|
||||
{
|
||||
runtimeInfo.CurrentLine = runtimeInfo.CurrentLine.Trim();
|
||||
string copyLine = statementAttribute.KeepStatementInArgs ? runtimeInfo.CurrentLine : runtimeInfo.CurrentLine.Remove(0, name.Length);
|
||||
statement.Value.Invoke(copyLine);
|
||||
statementFound = true;
|
||||
}
|
||||
else
|
||||
else if (statementAttribute.SearchMode == SearchMode.Contains && $" {runtimeInfo.CurrentLine} ".Contains(name))
|
||||
{
|
||||
runtimeInfo.CurrentLine = runtimeInfo.CurrentLine.TrimEnd();
|
||||
bool leadingWhitespace = runtimeInfo.CurrentLine.StartsWith(' ');
|
||||
|
||||
runtimeInfo.CurrentLine = $" {runtimeInfo.CurrentLine} ";
|
||||
string copyLine = statementAttribute.KeepStatementInArgs ? runtimeInfo.CurrentLine : runtimeInfo.CurrentLine.Replace(name, string.Empty);
|
||||
statement.Value.Invoke(copyLine);
|
||||
statementFound = true;
|
||||
|
||||
if (!leadingWhitespace)
|
||||
{
|
||||
runtimeInfo.CurrentLine = runtimeInfo.CurrentLine.Trim();
|
||||
}
|
||||
else
|
||||
{
|
||||
runtimeInfo.CurrentLine = runtimeInfo.CurrentLine.TrimEnd();
|
||||
}
|
||||
}
|
||||
else if (statementAttribute.SearchMode == SearchMode.EndOfLine && runtimeInfo.CurrentLine.EndsWith(name))
|
||||
{
|
||||
string copyLine = statementAttribute.KeepStatementInArgs ? runtimeInfo.CurrentLine : runtimeInfo.CurrentLine.Remove(runtimeInfo.CurrentLine.Length - name.Length);
|
||||
statement.Value.Invoke(copyLine);
|
||||
statementFound = true;
|
||||
}
|
||||
else if (statementAttribute.SearchMode == SearchMode.Exact && runtimeInfo.CurrentLine.Equals(name))
|
||||
{
|
||||
statement.Value.Invoke(runtimeInfo.CurrentLine);
|
||||
statementFound = true;
|
||||
}
|
||||
}
|
||||
else if (statementAttribute.SearchMode == SearchMode.EndOfLine && runtimeInfo.CurrentLine.EndsWith(name))
|
||||
{
|
||||
string copyLine = statementAttribute.KeepStatementInArgs ? runtimeInfo.CurrentLine : runtimeInfo.CurrentLine.Remove(runtimeInfo.CurrentLine.Length - name.Length);
|
||||
statement.Value.Invoke(copyLine);
|
||||
statementFound = true;
|
||||
}
|
||||
else if (statementAttribute.SearchMode == SearchMode.Exact && runtimeInfo.CurrentLine.Equals(name))
|
||||
{
|
||||
statement.Value.Invoke(runtimeInfo.CurrentLine);
|
||||
statementFound = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -232,7 +235,7 @@ namespace YesNt.Interpreter
|
||||
}
|
||||
else if (!string.IsNullOrWhiteSpace(runtimeInfo.SearchFunction))
|
||||
{
|
||||
runtimeInfo.Exit($"Function \"{runtimeInfo.SearchLabel}\" not found", true);
|
||||
runtimeInfo.Exit($"Function \"{runtimeInfo.SearchFunction}\" not found", true);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
using YesNt.Interpreter.Attributes;
|
||||
using YesNt.Interpreter.Enums;
|
||||
@@ -60,6 +62,38 @@ namespace YesNt.Interpreter.Statements
|
||||
}
|
||||
}
|
||||
|
||||
[Statement("cal", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.DarkYellow, Priority = Priority.Low, Seperator = "|")]
|
||||
public void Call(string args)
|
||||
{
|
||||
string[] parts = args.Split('|');
|
||||
if (parts.Length != 2)
|
||||
{
|
||||
RuntimeInfo.Exit("Invalid syntax", true);
|
||||
return;
|
||||
}
|
||||
|
||||
string key = parts[0].Trim();
|
||||
string[] functionArgumets = parts[1].Split(',');
|
||||
|
||||
foreach (string argumanet in functionArgumets)
|
||||
{
|
||||
RuntimeInfo.InParametersStack.Push(argumanet.Trim());
|
||||
}
|
||||
|
||||
RuntimeInfo.FunctionCallStack.Push(new FunctionScope(RuntimeInfo.LineNumber, new Stack<string>(RuntimeInfo.InParametersStack.Reverse())));
|
||||
RuntimeInfo.InParametersStack.Clear();
|
||||
RuntimeInfo.CurrentLine = string.Empty;
|
||||
|
||||
if (RuntimeInfo.Functions.ContainsKey(key))
|
||||
{
|
||||
RuntimeInfo.LineNumber = RuntimeInfo.Functions[key];
|
||||
}
|
||||
else
|
||||
{
|
||||
RuntimeInfo.SearchFunction = key;
|
||||
}
|
||||
}
|
||||
|
||||
[Statement("get", SearchMode.StartOfLine, SpaceAround.End, ConsoleColor.Yellow)]
|
||||
public void GetInParameter(string args)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
using System;
|
||||
|
||||
using YesNt.Interpreter.Attributes;
|
||||
using YesNt.Interpreter.Enums;
|
||||
using YesNt.Interpreter.Utilities;
|
||||
|
||||
namespace YesNt.Interpreter.Statements
|
||||
{
|
||||
internal class PredifinedVariableStatements : StatementRuntimeInformation
|
||||
{
|
||||
private readonly Random random = new Random();
|
||||
|
||||
[Statement("%tim", SearchMode.Contains, SpaceAround.End, ConsoleColor.Blue, KeepStatementInArgs = true, Priority = Priority.Highest)]
|
||||
public void GetUnixTimestamp(string args)
|
||||
{
|
||||
args += " ";
|
||||
|
||||
while (args.Contains("%tim "))
|
||||
{
|
||||
args = args.ReplaceFirstOccurrence("%tim ", $"{DateTimeOffset.Now.ToUnixTimeSeconds()}");
|
||||
}
|
||||
|
||||
RuntimeInfo.CurrentLine = args.TrimEnd();
|
||||
}
|
||||
|
||||
[Statement("%pi", SearchMode.Contains, SpaceAround.End, ConsoleColor.Blue, KeepStatementInArgs = true, Priority = Priority.Highest)]
|
||||
public void GetPi(string args)
|
||||
{
|
||||
args += " ";
|
||||
|
||||
|
||||
args = args.Replace("%pi ", $"{Math.PI} ");
|
||||
|
||||
RuntimeInfo.CurrentLine = args.TrimEnd();
|
||||
}
|
||||
|
||||
[Statement("%rnd", SearchMode.Contains, SpaceAround.End, ConsoleColor.Blue, KeepStatementInArgs = true, Priority = Priority.Highest)]
|
||||
public void GetRandom(string args)
|
||||
{
|
||||
args += " ";
|
||||
|
||||
while (args.Contains("%rnd "))
|
||||
{
|
||||
args = args.ReplaceFirstOccurrence("%rnd ", $"{random.Next() % 2} ");
|
||||
}
|
||||
|
||||
RuntimeInfo.CurrentLine = args.TrimEnd();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -93,5 +93,41 @@ 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user