mirror of
https://github.com/Stone-Red-Code/YesNt-Interpreter.git
synced 2026-09-04 00:56:31 +02:00
- Add functions
- Code improvements
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Threading;
|
||||
|
||||
namespace YesNt.Interpreter.Utilities
|
||||
{
|
||||
internal static class ConsoleExtentions
|
||||
{
|
||||
public static char ReadKey(RuntimeInformation runtimeInformation)
|
||||
{
|
||||
while (!runtimeInformation.Stop)
|
||||
{
|
||||
if (Console.KeyAvailable)
|
||||
{
|
||||
return Console.ReadKey().KeyChar;
|
||||
}
|
||||
Thread.Sleep(10);
|
||||
}
|
||||
return ' ';
|
||||
}
|
||||
|
||||
public static void Sleep(int millisecondsTimeout, RuntimeInformation runtimeInformation)
|
||||
{
|
||||
Stopwatch stopwatch = new Stopwatch();
|
||||
stopwatch.Start();
|
||||
while (!runtimeInformation.Stop)
|
||||
{
|
||||
if (stopwatch.ElapsedMilliseconds > millisecondsTimeout)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Thread.Sleep(10);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace YesNt.Interpreter.Utilities
|
||||
{
|
||||
internal static class Evaluator
|
||||
{
|
||||
public static bool? EvaluateCondition(string input)
|
||||
{
|
||||
string[] parts = input.Split("==");
|
||||
if (parts.Length == 2)
|
||||
{
|
||||
string part1 = parts[0].FromSaveString().Trim();
|
||||
string part2 = parts[1].FromSaveString().Trim();
|
||||
return part1 == part2;
|
||||
}
|
||||
|
||||
parts = input.Split("!=");
|
||||
if (parts.Length == 2)
|
||||
{
|
||||
string part1 = parts[0].FromSaveString().Trim();
|
||||
string part2 = parts[1].FromSaveString().Trim();
|
||||
return part1 != part2;
|
||||
}
|
||||
|
||||
parts = input.Split(">");
|
||||
if (parts.Length == 2)
|
||||
{
|
||||
bool succ1 = parts[0].ToStandardizedNumber(out double part1);
|
||||
bool succ2 = parts[1].ToStandardizedNumber(out double part2);
|
||||
if (!succ1 || !succ2)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return part1 > part2;
|
||||
}
|
||||
|
||||
parts = input.Split("<");
|
||||
if (parts.Length == 2)
|
||||
{
|
||||
bool succ1 = parts[0].ToStandardizedNumber(out double part1);
|
||||
bool succ2 = parts[1].ToStandardizedNumber(out double part2);
|
||||
if (!succ1 || !succ2)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return part1 < part2;
|
||||
}
|
||||
|
||||
parts = input.Split(">=");
|
||||
if (parts.Length == 2)
|
||||
{
|
||||
bool succ1 = parts[0].ToStandardizedNumber(out double part1);
|
||||
bool succ2 = parts[1].ToStandardizedNumber(out double part2);
|
||||
if (!succ1 || !succ2)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return part1 >= part2;
|
||||
}
|
||||
|
||||
parts = input.Split("<=");
|
||||
if (parts.Length == 2)
|
||||
{
|
||||
bool succ1 = parts[0].ToStandardizedNumber(out double part1);
|
||||
bool succ2 = parts[1].ToStandardizedNumber(out double part2);
|
||||
if (!succ1 || !succ2)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return part1 <= part2;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static string Calculate(string input, char op = '+')
|
||||
{
|
||||
if (input is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
MatchCollection matches = Regex.Matches(input, @"\(([^()]+)\)");
|
||||
while (matches.Count > 0)
|
||||
{
|
||||
for (int i = 0; i < matches.Count; i++)
|
||||
{
|
||||
string calc = matches[i].Value.Substring(1, matches[i].Length - 2);
|
||||
string ret = Calculate(calc);
|
||||
input = input.Replace(matches[i].Value, ret);
|
||||
}
|
||||
matches = Regex.Matches(input, @"\(([^()]+)\)");
|
||||
}
|
||||
|
||||
string[] parts = input.Split(op);
|
||||
|
||||
double number = double.NaN;
|
||||
|
||||
foreach (string p in parts)
|
||||
{
|
||||
string part = p;
|
||||
|
||||
switch (op)
|
||||
{
|
||||
case '+':
|
||||
part = Calculate(part, '-');
|
||||
break;
|
||||
|
||||
case '-':
|
||||
part = Calculate(part, '*');
|
||||
break;
|
||||
|
||||
case '*':
|
||||
part = Calculate(part, '/');
|
||||
break;
|
||||
}
|
||||
|
||||
if (part is null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (part.ToStandardizedNumber(out double num))
|
||||
{
|
||||
if (double.IsNaN(number))
|
||||
{
|
||||
number = num;
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (op)
|
||||
{
|
||||
case '+':
|
||||
number += num;
|
||||
break;
|
||||
|
||||
case '-':
|
||||
number -= num;
|
||||
break;
|
||||
|
||||
case '*':
|
||||
number *= num;
|
||||
break;
|
||||
|
||||
case '/':
|
||||
number /= num;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return number.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
|
||||
namespace YesNt.Interpreter.Utilities
|
||||
{
|
||||
public static class StringExtentions
|
||||
{
|
||||
public static string ToSaveString(this string input)
|
||||
{
|
||||
StringBuilder output = new StringBuilder();
|
||||
foreach (char c in input)
|
||||
{
|
||||
output.Append($"\r{c}\r");
|
||||
}
|
||||
return output.ToString();
|
||||
}
|
||||
|
||||
public static string FromSaveString(this string input)
|
||||
{
|
||||
return input.Replace("\r", "");
|
||||
}
|
||||
|
||||
public static bool ToStandardizedNumber(this string input, out double result)
|
||||
{
|
||||
return double.TryParse(input.FromSaveString().Replace(',', '.'), NumberStyles.Any, CultureInfo.InvariantCulture, out result);
|
||||
}
|
||||
|
||||
public static string ReplaceFirstOccurrence(this string input, string oldValue, string newValue)
|
||||
{
|
||||
int place = input.IndexOf(oldValue);
|
||||
return input.Remove(place, oldValue.Length).Insert(place, newValue);
|
||||
}
|
||||
|
||||
public static string ReplaceLastOccurrence(this string input, string oldValue, string newValue)
|
||||
{
|
||||
int place = input.LastIndexOf(oldValue);
|
||||
return input.Remove(place, Math.Min(oldValue.Length, input.Length - place)).Insert(place, newValue);
|
||||
}
|
||||
|
||||
public static int WhiteSpaceAtEnd(this string input)
|
||||
{
|
||||
int count = 0;
|
||||
int index = input.Length - 1;
|
||||
while (index >= 0 && char.IsWhiteSpace(input[index--]))
|
||||
{
|
||||
count++;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user