- Add predefined variables

- Add `mod` function
- Improve function calling syntax
This commit is contained in:
Stone_Red
2022-01-13 13:39:02 +01:00
parent ea5bbda2c7
commit 4bef308ff2
5 changed files with 155 additions and 32 deletions
@@ -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);
}
}
}
}