Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 85 additions & 29 deletions Console.cs
Original file line number Diff line number Diff line change
@@ -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<string, Value> parameters) {
Console.Write(parameters["value"].getValue());
return null;
}

public Value read(Dictionary<string, Value> parameters) {
var key = Console.ReadKey();
return new Value((int) key.KeyChar, Sqript.ValueType.INTEGER);
}

public Value readLine(Dictionary<string, Value> 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<string, QValue> 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<string, QValue> 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<string, QValue> 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);
}
}
}
62 changes: 31 additions & 31 deletions FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Value> parameters) {
return new Value(File.Exists(parameters["file"].str()), Qrakhen.Sqript.ValueType.BOOLEAN);
}

public Value read(Dictionary<string, Value> 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<string, Value> 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<string, QValue> parameters) {
return new QValue(File.Exists(parameters["file"].Str()), Qrakhen.Sqript.ValueType.Boolean);
}

public QValue read(Dictionary<string, QValue> 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<string, QValue> parameters) {
string content = parameters["content"].GetValue() == null ? "" : parameters["content"].Str();
File.WriteAllText(parameters["file"].Str(), content);
return QValue.True;
}
}
}
128 changes: 128 additions & 0 deletions Parser.cs
Original file line number Diff line number Diff line change
@@ -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<string, QValue> 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<string, QValue> 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<string, QValue> 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<string, QValue> 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<string, QValue> 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<string, QValue> 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<string, QValue> 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<string, QValue> 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
}
}
Loading