diff --git a/Console.cs b/Console.cs index 44f7e17..b464ad9 100644 --- a/Console.cs +++ b/Console.cs @@ -1,34 +1,90 @@ using System; using System.Collections.Generic; +using System.Text.RegularExpressions; using Qrakhen.Sqript; -namespace Qrakhen.SqriptLib -{ - public class ConsoleInterface : Interface - { - public ConsoleInterface() : base("console") { - - } - - public Value write(Dictionary parameters) { - Console.Write(parameters["value"].getValue()); - return null; - } - - public Value read(Dictionary parameters) { - var key = Console.ReadKey(); - return new Value((int) key.KeyChar, Sqript.ValueType.INTEGER); - } - - public Value readLine(Dictionary parameters) { - var line = Console.ReadLine(); - return new Value(line, Sqript.ValueType.STRING); - } - - public override void load() { - define(new Call("write", new string[] { "value" }, write, Sqript.ValueType.NULL)); - define(new Call("read", new string[] { }, read, Sqript.ValueType.INTEGER)); - define(new Call("readLine", new string[] { }, readLine, Sqript.ValueType.STRING)); - } - } +namespace Qrakhen.SqriptLib { + + public class ConsoleInterface : Interface { + + private ConsoleColor _consoleColor = ConsoleColor.White; + + + public ConsoleInterface() : base("console") { } + + public override void Load() { + Define(new Call(write, new string[] { "value", "color" }, Sqript.ValueType.Null)); + Define(new Call(writeln, new string[] { "value", "color" }, Sqript.ValueType.Null)); + Define(new Call(readKey, Sqript.ValueType.Integer)); + Define(new Call(read, Sqript.ValueType.String)); + Define(new Call(setColor, new string[] { "color" }, Sqript.ValueType.Null)); + } + + + public QValue setColor(Dictionary parameters) { + object value = parameters["color"].GetValue(); + if (value is string colorString) { + _consoleColor = (ConsoleColor) Enum.Parse(typeof(ConsoleColor), colorString, true); + } else if (value is int colorInt) { + _consoleColor = (ConsoleColor) colorInt; + } else { + throw new ArgumentException($"The parameter 'color' needs to be an {typeof(int)} or {typeof(string)}!"); + } + return null; + } + + public QValue write(Dictionary parameters) { + if(parameters.ContainsKey("color") && parameters["color"] != null) { + ConsoleColor oldColor = Console.ForegroundColor; + object value = parameters["color"].GetValue(); + if (value is string colorString) { + Console.ForegroundColor = (ConsoleColor) Enum.Parse(typeof(ConsoleColor), colorString, true); + } else if (value is int colorInt) { + Console.ForegroundColor = (ConsoleColor) colorInt; + } else { + throw new ArgumentException($"The parameter 'color' needs to be an {typeof(int)} or {typeof(string)}!"); + } + string text = parameters["value"].GetValue().ToString(); + Console.Write(Regex.Unescape(text)); + Console.ForegroundColor = oldColor; + } else { + Console.ForegroundColor = _consoleColor; + string text = parameters["value"].GetValue().ToString(); + Console.Write(Regex.Unescape(text)); + } + return null; + } + + public QValue writeln(Dictionary parameters) { + if (parameters.ContainsKey("color") && parameters["color"] != null) { + ConsoleColor oldColor = Console.ForegroundColor; + object value = parameters["color"].GetValue(); + if (value is string colorString) { + Console.ForegroundColor = (ConsoleColor) Enum.Parse(typeof(ConsoleColor), colorString, true); + } else if (value is int colorInt) { + Console.ForegroundColor = (ConsoleColor) colorInt; + } else { + throw new ArgumentException($"The parameter 'color' needs to be an {typeof(int)} or {typeof(string)}!"); + } + string text = parameters["value"].GetValue().ToString(); + Console.Write(Regex.Unescape(text) + Environment.NewLine); + Console.ForegroundColor = oldColor; + } else { + Console.ForegroundColor = _consoleColor; + string text = parameters["value"].GetValue().ToString(); + Console.Write(Regex.Unescape(text) + Environment.NewLine); + } + return null; + } + + public QValue readKey() { + var key = Console.ReadKey(); + return new QValue((int) key.KeyChar, Sqript.ValueType.Integer); + } + + public QValue read() { + var line = Console.ReadLine(); + return new QValue(line, Sqript.ValueType.String); + } + } } diff --git a/FileSystem.cs b/FileSystem.cs index 1c0f057..ff1e80c 100644 --- a/FileSystem.cs +++ b/FileSystem.cs @@ -3,35 +3,35 @@ using System.Collections.Generic; using System.IO; -namespace Qrakhen.SqriptLib -{ - public class FileInterface : Interface - { - public FileInterface() : base("file") { - - } - - public Value exists(Dictionary parameters) { - return new Value(File.Exists(parameters["file"].str()), Qrakhen.Sqript.ValueType.BOOLEAN); - } - - public Value read(Dictionary parameters) { - if (!File.Exists(parameters["file"].str())) throw new Qrakhen.Sqript.Exception("could not find file '" + parameters["file"] + "'"); - else return new Value(File.ReadAllText(parameters["file"].str()), Qrakhen.Sqript.ValueType.STRING); - } - - public Value write(Dictionary parameters) { - string content; - if (parameters["content"].getValue() == null) content = ""; - else content = parameters["content"].str(); - File.WriteAllText(parameters["file"].str(), content); - return Value.TRUE; - } - - public override void load() { - define(new Call("read", new string[] { "file" }, read, Sqript.ValueType.STRING)); - define(new Call("write", new string[] { "file", "content" }, write, Sqript.ValueType.BOOLEAN)); - define(new Call("exists", new string[] { "file" }, exists, Sqript.ValueType.BOOLEAN)); - } - } +namespace Qrakhen.SqriptLib { + + public class FileInterface : Interface { + + public FileInterface() : base("file") { } + + public override void Load() { + Define(new Call(read, new string[] { "file" }, Sqript.ValueType.String, "read")); + Define(new Call(write, new string[] { "file", "content" }, Sqript.ValueType.Boolean, "write")); + Define(new Call(Exists, new string[] { "file" }, Sqript.ValueType.Boolean, "exists")); + } + + + public QValue Exists(Dictionary parameters) { + return new QValue(File.Exists(parameters["file"].Str()), Qrakhen.Sqript.ValueType.Boolean); + } + + public QValue read(Dictionary parameters) { + if (!File.Exists(parameters["file"].Str())) { + throw new Sqript.Exception("could not find file '" + parameters["file"] + "'"); + } else { + return new QValue(File.ReadAllText(parameters["file"].Str()), Qrakhen.Sqript.ValueType.String); + } + } + + public QValue write(Dictionary parameters) { + string content = parameters["content"].GetValue() == null ? "" : parameters["content"].Str(); + File.WriteAllText(parameters["file"].Str(), content); + return QValue.True; + } + } } diff --git a/Parser.cs b/Parser.cs new file mode 100644 index 0000000..6a2520f --- /dev/null +++ b/Parser.cs @@ -0,0 +1,128 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; +using Qrakhen.Sqript; + +namespace Qrakhen.SqriptLib { + + public class ParserInterface : Interface { + + public ParserInterface() : base("parser") { } + + public override void Load() { + Define(new Call(toNumber, new string[] { "value" }, Sqript.ValueType.Number)); + Define(new Call(toInt, new string[] { "value" }, Sqript.ValueType.Number)); + Define(new Call(toDecimal, new string[] { "value" }, Sqript.ValueType.Number)); + Define(new Call(toBool, new string[] { "value" }, Sqript.ValueType.Number)); + } + + + public QValue toNumber(Dictionary parameters) { + if(!parameters.ContainsKey("value")) { + throw new ArgumentException("The needed parameter 'min' is missing!"); + } + decimal number = toDecimal(parameters, "value"); + return new QValue( + number, + Sqript.ValueType.Number + ); + } + + public QValue toInt(Dictionary parameters) { + if(!parameters.ContainsKey("value")) { + throw new ArgumentException("The needed parameter 'min' is missing!"); + } + int number = toInt(parameters, "value"); + return new QValue( + number, + Sqript.ValueType.Integer + ); + } + + public QValue toDecimal(Dictionary parameters) { + if(!parameters.ContainsKey("value")) { + throw new ArgumentException("The needed parameter 'min' is missing!"); + } + double number = toDouble(parameters, "value"); + return new QValue( + number, + Sqript.ValueType.Decimal + ); + } + + public QValue toBool(Dictionary parameters) { + if(!parameters.ContainsKey("value")) { + throw new ArgumentException("The needed parameter 'min' is missing!"); + } + bool number = toBool(parameters, "value"); + return new QValue( + number, + Sqript.ValueType.Boolean + ); + } + + #region Helper + + private decimal toDecimal(Dictionary parameters, string name) { + if(parameters[name].Type != Sqript.ValueType.Integer + && parameters[name].Type != Sqript.ValueType.Decimal + && parameters[name].Type != Sqript.ValueType.Number + && parameters[name].Type != Sqript.ValueType.Any + && parameters[name].Type != Sqript.ValueType.String) { + throw new ArgumentException("The parameter '" + name + "' should have they type 'Decimal' but it is: " + parameters[name].Type); + } + try { + return decimal.Parse(parameters[name].Value.ToString()); + } catch(FormatException) { + throw new ArgumentException("The parameter '" + name + "' should have they type 'Decimal' but it is: " + parameters[name].Type); + } + } + + private int toInt(Dictionary parameters, string name) { + if(parameters[name].Type != Sqript.ValueType.Integer + && parameters[name].Type != Sqript.ValueType.Decimal + && parameters[name].Type != Sqript.ValueType.Number + && parameters[name].Type != Sqript.ValueType.Any + && parameters[name].Type != Sqript.ValueType.String) { + throw new ArgumentException("The parameter '" + name + "' should have they type 'Integer' but it is: " + parameters[name].Type); + } + try { + return int.Parse(parameters[name].Value.ToString()); + } catch(FormatException) { + throw new ArgumentException("The parameter '" + name + "' should have they type 'Integer' but it is: " + parameters[name].Type); + } + } + + private double toDouble(Dictionary parameters, string name) { + if(parameters[name].Type != Sqript.ValueType.Integer + && parameters[name].Type != Sqript.ValueType.Decimal + && parameters[name].Type != Sqript.ValueType.Number + && parameters[name].Type != Sqript.ValueType.Any + && parameters[name].Type != Sqript.ValueType.String) { + throw new ArgumentException("The parameter '" + name + "' should have they type 'Double' but it is: " + parameters[name].Type); + } + try { + return double.Parse(parameters[name].Value.ToString()); + } catch(FormatException) { + throw new ArgumentException("The parameter '" + name + "' should have they type 'Double' but it is: " + parameters[name].Type); + } + } + + private bool toBool(Dictionary parameters, string name) { + if(parameters[name].Type != Sqript.ValueType.Integer + && parameters[name].Type != Sqript.ValueType.Decimal + && parameters[name].Type != Sqript.ValueType.Number + && parameters[name].Type != Sqript.ValueType.Any + && parameters[name].Type != Sqript.ValueType.String) { + throw new ArgumentException("The parameter '" + name + "' should have they type 'Boolean' but it is: " + parameters[name].Type); + } + try { + return bool.Parse(parameters[name].Value.ToString()); + } catch(FormatException) { + throw new ArgumentException("The parameter '" + name + "' should have they type 'Boolean' but it is: " + parameters[name].Type); + } + } + + #endregion + } +} diff --git a/Random.cs b/Random.cs new file mode 100644 index 0000000..7d7846f --- /dev/null +++ b/Random.cs @@ -0,0 +1,86 @@ +using System; +using System.Collections.Generic; +using Qrakhen.Sqript; + +namespace Qrakhen.SqriptLib { + + public class RandomInterface : Interface { + + private Random _random; + + + public RandomInterface() : base("random") { + _random = new Random(); + } + + public override void Load() { + Define(new Call(set_seed, new string[] { "seed" }, Sqript.ValueType.Null)); + Define(new Call(range, new string[] { "min", "max" }, Sqript.ValueType.Integer)); + Define(new Call(rangeD, Sqript.ValueType.Decimal)); + } + + + public QValue set_seed(Dictionary parameters) { + if(!parameters.ContainsKey("seed")) { + throw new ArgumentException("The needed parameter 'seed' is missing!"); + } + _random = new Random(ToInt(parameters, "seed")); + return null; + } + + public QValue range(Dictionary parameters) { + if(!parameters.ContainsKey("min")) { + throw new ArgumentException("The needed parameter 'min' is missing!"); + } + if(!parameters.ContainsKey("max")) { + throw new ArgumentException("The needed parameter 'max' is missing!"); + } + int min = ToInt(parameters, "min"); + int max = ToInt(parameters, "max"); + return new QValue( + _random.Next(min, max + 1), + Sqript.ValueType.Integer + ); + } + + public QValue rangeD() { + return new QValue( + _random.NextDouble(), + Sqript.ValueType.Decimal + ); + } + + #region Helper + + private int ToInt(Dictionary parameters, string name) { + if(parameters[name].Type != Sqript.ValueType.Integer + && parameters[name].Type != Sqript.ValueType.Decimal + && parameters[name].Type != Sqript.ValueType.Number + && parameters[name].Type != Sqript.ValueType.Any + && parameters[name].Type != Sqript.ValueType.String){ + throw new ArgumentException("The parameter '" + name + "' should have they type 'Integer' but it is: " + parameters[name].Type); + } + try { + return int.Parse(parameters[name].Value.ToString()); + } catch(FormatException) { + throw new ArgumentException("The parameter '" + name + "' should have they type 'Integer' but it is: " + parameters[name].Type); + } + } + + private decimal ToDecimal(Dictionary parameters, string name) { + if(parameters[name].Type != Sqript.ValueType.Integer + && parameters[name].Type != Sqript.ValueType.Decimal + && parameters[name].Type != Sqript.ValueType.Number + && parameters[name].Type != Sqript.ValueType.Any + && parameters[name].Type != Sqript.ValueType.String) { + throw new ArgumentException("The parameter '" + name + "' should have they type 'Decimal' but it is: " + parameters[name].Type); + } + try { + return decimal.Parse(parameters[name].Value.ToString()); + } catch(FormatException) { + throw new ArgumentException("The parameter '" + name + "' should have they type 'Decimal' but it is: " + parameters[name].Type); + } + } + #endregion + } +} diff --git a/Sqript.Lib.csproj b/Sqript.Lib.csproj deleted file mode 100644 index 89afac6..0000000 --- a/Sqript.Lib.csproj +++ /dev/null @@ -1,26 +0,0 @@ - - - - netstandard1.6 - Qrakhen.Sqript.Lib - Qrakhen.SqriptLib - Qrakhen - Qrakhen - Sqript - QrakhenQrakhenQrakhenQrakhenQrakhen - 0.1.0 - - - - ..\Sqript\bin\Debug\netcoreapp1.1\lib\ - Off - - - - - ..\Sqript\bin\Debug\netcoreapp1.1\Qrakhen.Sqript.dll - false - - - - \ No newline at end of file diff --git a/Sqript.Libs.csproj b/Sqript.Libs.csproj new file mode 100644 index 0000000..e4de205 --- /dev/null +++ b/Sqript.Libs.csproj @@ -0,0 +1,24 @@ + + + + netcoreapp3.1 + Qrakhen + This project was created by Qrakhen and Updated by Vision + false + false + true + + + + ..\bin\Sqript.Libs + + + + + + + + + + +