From 3eeb220aa8b60607a1b71e07d000edb457c44bdb Mon Sep 17 00:00:00 2001 From: kdanzer-tgm <44087538+kdanzer-tgm@users.noreply.github.com> Date: Sun, 6 Mar 2022 23:04:02 +0100 Subject: [PATCH 1/3] Updated Lib and Added --- Console.cs | 82 ++++++++++++++++++---------- FileSystem.cs | 54 ++++++++++--------- Parser.cs | 130 +++++++++++++++++++++++++++++++++++++++++++++ Random.cs | 80 ++++++++++++++++++++++++++++ Sqript.Lib.csproj | 26 --------- Sqript.Libs.csproj | 24 +++++++++ 6 files changed, 315 insertions(+), 81 deletions(-) create mode 100644 Parser.cs create mode 100644 Random.cs delete mode 100644 Sqript.Lib.csproj create mode 100644 Sqript.Libs.csproj diff --git a/Console.cs b/Console.cs index 44f7e17..e22b6af 100644 --- a/Console.cs +++ b/Console.cs @@ -1,34 +1,58 @@ 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 Value 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 Value write(Dictionary parameters) { + Console.ForegroundColor = _consoleColor; + Console.Write(parameters["value"].getValue()); + return null; + } + + public Value writeln(Dictionary parameters) { + Console.ForegroundColor = _consoleColor; + string text = parameters["value"].getValue().ToString(); + Console.Write(Regex.Unescape(text) + Environment.NewLine); + return null; + } + + public Value readKey() { + var key = Console.ReadKey(); + return new Value((int) key.KeyChar, Sqript.ValueType.Integer); + } + + public Value read() { + var line = Console.ReadLine(); + return new Value(line, Sqript.ValueType.String); + } + + public override void load() { + define(new Call(write, new string[] { "value" }, Sqript.ValueType.Null)); + define(new Call(writeln, new string[] { "value" }, 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)); + } + } } diff --git a/FileSystem.cs b/FileSystem.cs index 1c0f057..d26547a 100644 --- a/FileSystem.cs +++ b/FileSystem.cs @@ -3,35 +3,37 @@ using System.Collections.Generic; using System.IO; -namespace Qrakhen.SqriptLib -{ - public class FileInterface : Interface - { - public FileInterface() : base("file") { +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 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 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 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)); - } - } + 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")); + } + } } diff --git a/Parser.cs b/Parser.cs new file mode 100644 index 0000000..d3a1eca --- /dev/null +++ b/Parser.cs @@ -0,0 +1,130 @@ +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; +using Qrakhen.Sqript; + +namespace Qrakhen.SqriptLib { + public class ParserInterface : Interface { + + private ConsoleColor _consoleColor = ConsoleColor.White; + + 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 Value toNumber(Dictionary parameters) { + if(!parameters.ContainsKey("value")) { + throw new ArgumentException("The needed parameter 'min' is missing!"); + } + decimal number = toDecimal(parameters, "value"); + return new Value( + number, + Sqript.ValueType.Number + ); + } + + public Value toInt(Dictionary parameters) { + if(!parameters.ContainsKey("value")) { + throw new ArgumentException("The needed parameter 'min' is missing!"); + } + int number = toInt(parameters, "value"); + return new Value( + number, + Sqript.ValueType.Integer + ); + } + + public Value toDecimal(Dictionary parameters) { + if(!parameters.ContainsKey("value")) { + throw new ArgumentException("The needed parameter 'min' is missing!"); + } + double number = toDouble(parameters, "value"); + return new Value( + number, + Sqript.ValueType.Decimal + ); + } + + public Value toBool(Dictionary parameters) { + if(!parameters.ContainsKey("value")) { + throw new ArgumentException("The needed parameter 'min' is missing!"); + } + bool number = toBool(parameters, "value"); + return new Value( + 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..2348843 --- /dev/null +++ b/Random.cs @@ -0,0 +1,80 @@ +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 Value 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 Value 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 Value( + random.Next(min, max + 1), + Sqript.ValueType.Integer + ); + } + + public Value rangeD() { + return new Value( + random.NextDouble(), + Sqript.ValueType.Decimal + ); + } + + 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)); + } + + 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); + } + } + } +} 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 + + + + + + + + + + + From a6c595db0717068dfcc98ae639c00c40c08e912b Mon Sep 17 00:00:00 2001 From: kdanzer-tgm <44087538+kdanzer-tgm@users.noreply.github.com> Date: Tue, 8 Mar 2022 01:23:38 +0100 Subject: [PATCH 2/3] Refactoring --- Console.cs | 41 ++++++++++---------- FileSystem.cs | 39 +++++++++---------- Parser.cs | 105 ++++++++++++++++++++++++-------------------------- Random.cs | 69 +++++++++++++++++---------------- 4 files changed, 124 insertions(+), 130 deletions(-) diff --git a/Console.cs b/Console.cs index e22b6af..3edeb1c 100644 --- a/Console.cs +++ b/Console.cs @@ -4,19 +4,18 @@ using Qrakhen.Sqript; namespace Qrakhen.SqriptLib { + public class ConsoleInterface : Interface { private ConsoleColor _consoleColor = ConsoleColor.White; - public ConsoleInterface() : base("console") { - - } + public ConsoleInterface() : base("console") { } - public Value setColor(Dictionary parameters) { - object value = parameters["color"].getValue(); - if(value is string colorString) { + 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) { + } 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)}!"); @@ -24,35 +23,35 @@ public Value setColor(Dictionary parameters) { return null; } - public Value write(Dictionary parameters) { + public QValue write(Dictionary parameters) { Console.ForegroundColor = _consoleColor; - Console.Write(parameters["value"].getValue()); + Console.Write(parameters["value"].GetValue()); return null; } - public Value writeln(Dictionary parameters) { + public QValue writeln(Dictionary parameters) { Console.ForegroundColor = _consoleColor; - string text = parameters["value"].getValue().ToString(); + string text = parameters["value"].GetValue().ToString(); Console.Write(Regex.Unescape(text) + Environment.NewLine); return null; } - public Value readKey() { + public QValue readKey() { var key = Console.ReadKey(); - return new Value((int) key.KeyChar, Sqript.ValueType.Integer); + return new QValue((int) key.KeyChar, Sqript.ValueType.Integer); } - public Value read() { + public QValue read() { var line = Console.ReadLine(); - return new Value(line, Sqript.ValueType.String); + return new QValue(line, Sqript.ValueType.String); } - public override void load() { - define(new Call(write, new string[] { "value" }, Sqript.ValueType.Null)); - define(new Call(writeln, new string[] { "value" }, 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 override void Load() { + Define(new Call(write, new string[] { "value" }, Sqript.ValueType.Null)); + Define(new Call(writeln, new string[] { "value" }, 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)); } } } diff --git a/FileSystem.cs b/FileSystem.cs index d26547a..2803f81 100644 --- a/FileSystem.cs +++ b/FileSystem.cs @@ -4,36 +4,33 @@ using System.IO; namespace Qrakhen.SqriptLib { + public class FileInterface : Interface { - public FileInterface() : base("file") { - } + public FileInterface() : base("file") { } - public Value exists(Dictionary parameters) { - return new Value(File.Exists(parameters["file"].str()), Qrakhen.Sqript.ValueType.Boolean); + public QValue Exists(Dictionary parameters) { + return new QValue(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 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 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 QValue write(Dictionary parameters) { + string content = parameters["content"].GetValue() == null ? "" : parameters["content"].Str(); + File.WriteAllText(parameters["file"].Str(), content); + return QValue.True; } - 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 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")); } } } diff --git a/Parser.cs b/Parser.cs index d3a1eca..7d89833 100644 --- a/Parser.cs +++ b/Parser.cs @@ -4,60 +4,57 @@ using Qrakhen.Sqript; namespace Qrakhen.SqriptLib { - public class ParserInterface : Interface { - - private ConsoleColor _consoleColor = ConsoleColor.White; - public ParserInterface() : base("parser") { + 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 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 Value toNumber(Dictionary parameters) { + 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 Value( + return new QValue( number, Sqript.ValueType.Number ); } - public Value toInt(Dictionary parameters) { + 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 Value( + return new QValue( number, Sqript.ValueType.Integer ); } - public Value toDecimal(Dictionary parameters) { + 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 Value( + return new QValue( number, Sqript.ValueType.Decimal ); } - public Value toBool(Dictionary parameters) { + 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 Value( + return new QValue( number, Sqript.ValueType.Boolean ); @@ -65,63 +62,63 @@ public Value toBool(Dictionary parameters) { #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); + 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()); + 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); + 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); + 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()); + 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); + 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); + 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()); + 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); + 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); + 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()); + 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); + throw new ArgumentException("The parameter '" + name + "' should have they type 'Boolean' but it is: " + parameters[name].Type); } } diff --git a/Random.cs b/Random.cs index 2348843..0cc4db5 100644 --- a/Random.cs +++ b/Random.cs @@ -3,77 +3,78 @@ using Qrakhen.Sqript; namespace Qrakhen.SqriptLib { + public class RandomInterface : Interface { - private Random random; + private Random _random; public RandomInterface() : base("random") { - random = new Random(); + _random = new Random(); } - public Value set_seed(Dictionary parameters) { + 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")); + _random = new Random(ToInt(parameters, "seed")); return null; } - public Value range(Dictionary parameters) { + 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 Value( - random.Next(min, max + 1), + int min = ToInt(parameters, "min"); + int max = ToInt(parameters, "max"); + return new QValue( + _random.Next(min, max + 1), Sqript.ValueType.Integer ); } - public Value rangeD() { - return new Value( - random.NextDouble(), + public QValue rangeD() { + return new QValue( + _random.NextDouble(), Sqript.ValueType.Decimal ); } - 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 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)); } - 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); + 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()); + 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); + 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); + 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()); + 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); + throw new ArgumentException("The parameter '" + name + "' should have they type 'Decimal' but it is: " + parameters[name].Type); } } } From 604c9e8ad388c9683246b0d0d65aa30fe7a070ac Mon Sep 17 00:00:00 2001 From: kdanzer-tgm <44087538+kdanzer-tgm@users.noreply.github.com> Date: Tue, 8 Mar 2022 12:20:40 +0100 Subject: [PATCH 3/3] Reffactoring and Feature Connsole write and writeln now have a overload with color --- Console.cs | 59 +++++++++++++++++++++++++++++++++++++++------------ FileSystem.cs | 13 ++++++------ Parser.cs | 1 + Random.cs | 15 ++++++++----- 4 files changed, 64 insertions(+), 24 deletions(-) diff --git a/Console.cs b/Console.cs index 3edeb1c..b464ad9 100644 --- a/Console.cs +++ b/Console.cs @@ -9,8 +9,18 @@ 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) { @@ -24,15 +34,46 @@ public QValue setColor(Dictionary parameters) { } public QValue write(Dictionary parameters) { - Console.ForegroundColor = _consoleColor; - Console.Write(parameters["value"].GetValue()); + 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) { - Console.ForegroundColor = _consoleColor; - string text = parameters["value"].GetValue().ToString(); - Console.Write(Regex.Unescape(text) + Environment.NewLine); + 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; } @@ -45,13 +86,5 @@ public QValue read() { var line = Console.ReadLine(); return new QValue(line, Sqript.ValueType.String); } - - public override void Load() { - Define(new Call(write, new string[] { "value" }, Sqript.ValueType.Null)); - Define(new Call(writeln, new string[] { "value" }, 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)); - } } } diff --git a/FileSystem.cs b/FileSystem.cs index 2803f81..ff1e80c 100644 --- a/FileSystem.cs +++ b/FileSystem.cs @@ -9,6 +9,13 @@ 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); } @@ -26,11 +33,5 @@ public QValue write(Dictionary parameters) { File.WriteAllText(parameters["file"].Str(), content); return QValue.True; } - - 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")); - } } } diff --git a/Parser.cs b/Parser.cs index 7d89833..6a2520f 100644 --- a/Parser.cs +++ b/Parser.cs @@ -16,6 +16,7 @@ public override void Load() { 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!"); diff --git a/Random.cs b/Random.cs index 0cc4db5..7d7846f 100644 --- a/Random.cs +++ b/Random.cs @@ -8,10 +8,18 @@ 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!"); @@ -42,11 +50,7 @@ public QValue rangeD() { ); } - 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)); - } + #region Helper private int ToInt(Dictionary parameters, string name) { if(parameters[name].Type != Sqript.ValueType.Integer @@ -77,5 +81,6 @@ private decimal ToDecimal(Dictionary parameters, string name) { throw new ArgumentException("The parameter '" + name + "' should have they type 'Decimal' but it is: " + parameters[name].Type); } } + #endregion } }