From d0316fe930c2f8bb3125a218ba71fedf06914a25 Mon Sep 17 00:00:00 2001 From: zdimension Date: Sat, 19 Sep 2015 20:48:44 +0200 Subject: [PATCH] Formatting again --- src/Hassium/Debug/Debug.cs | 7 +- src/Hassium/Functions/ConsoleFunctions.cs | 12 +- src/Hassium/Functions/Constructors.cs | 10 +- src/Hassium/Functions/ILibrary.cs | 3 +- src/Hassium/Functions/InternalFunction.cs | 11 +- src/Hassium/Functions/MiscFunctions.cs | 127 +- src/Hassium/Functions/SystemFunctions.cs | 22 +- .../Collections/HassiumStack.cs | 5 +- .../Conversion/HassiumConvert.cs | 19 +- .../HassiumObjects/Drawing/HassiumBitmap.cs | 21 +- .../HassiumObjects/Drawing/HassiumColor.cs | 1046 ++++++++--------- .../HassiumObjects/Drawing/HassiumImage.cs | 6 +- src/Hassium/HassiumObjects/HassiumClass.cs | 9 +- src/Hassium/HassiumObjects/HassiumModule.cs | 3 +- src/Hassium/HassiumObjects/HassiumObject.cs | 68 +- .../HassiumObjects/IO/HassiumBinaryReader.cs | 7 +- .../HassiumObjects/IO/HassiumBinaryWriter.cs | 5 +- .../HassiumObjects/IO/HassiumConsole.cs | 23 +- .../HassiumObjects/IO/HassiumDirectory.cs | 12 +- src/Hassium/HassiumObjects/IO/HassiumFile.cs | 16 +- .../HassiumObjects/IO/HassiumFileStream.cs | 7 +- src/Hassium/HassiumObjects/IO/HassiumIO.cs | 2 +- src/Hassium/HassiumObjects/IO/HassiumPath.cs | 5 +- .../HassiumObjects/IO/HassiumStream.cs | 7 +- .../HassiumObjects/IO/HassiumStreamReader.cs | 11 +- .../HassiumObjects/IO/HassiumStreamWriter.cs | 5 +- .../Interactive/HassiumDebug.cs | 25 +- .../HassiumObjects/Math/HassiumMath.cs | 15 +- .../Networking/HTTP/HassiumHttpListener.cs | 5 +- .../HTTP/HassiumHttpListenerContext.cs | 5 +- .../HTTP/HassiumHttpListenerRequest.cs | 5 +- .../HTTP/HassiumHttpListenerResponse.cs | 15 +- .../Networking/HassiumNetworkStream.cs | 6 +- .../Networking/HassiumSocket.cs | 5 +- .../Networking/HassiumTcpClient.cs | 5 +- .../Networking/HassiumWebClient.cs | 9 +- .../HassiumObjects/Random/HassiumRandom.cs | 7 +- .../Reflection/HassiumAssembly.cs | 5 +- .../Reflection/HassiumModule.cs | 8 +- .../HassiumObjects/Text/HassiumEncoding.cs | 7 +- .../Text/HassiumStringBuilder.cs | 5 +- .../HassiumObjects/Text/HassiumTextReader.cs | 9 +- .../HassiumObjects/Text/HassiumTextWriter.cs | 5 +- .../HassiumObjects/Types/HassiumArray.cs | 9 +- .../HassiumObjects/Types/HassiumBool.cs | 7 +- .../HassiumObjects/Types/HassiumByte.cs | 5 +- .../HassiumObjects/Types/HassiumChar.cs | 5 +- .../HassiumObjects/Types/HassiumDate.cs | 27 +- .../HassiumObjects/Types/HassiumDictionary.cs | 51 +- .../HassiumObjects/Types/HassiumDouble.cs | 20 +- .../HassiumObjects/Types/HassiumEvent.cs | 2 +- .../HassiumObjects/Types/HassiumInt.cs | 14 +- .../HassiumObjects/Types/HassiumString.cs | 9 +- src/Hassium/Interpreter/HassiumMethod.cs | 32 +- src/Hassium/Interpreter/HassiumProperty.cs | 2 +- src/Hassium/Interpreter/IVisitor.cs | 2 +- src/Hassium/Interpreter/Interpreter.cs | 313 +++-- src/Hassium/Interpreter/ParseException.cs | 2 +- src/Hassium/Interpreter/StackFrame.cs | 11 +- src/Hassium/Lexer/IFunction.cs | 3 +- src/Hassium/Lexer/Lexer.cs | 44 +- src/Hassium/Lexer/Token.cs | 3 +- src/Hassium/Parser/Ast/ArgListNode.cs | 5 +- src/Hassium/Parser/Ast/ArrayGetNode.cs | 15 +- .../Parser/Ast/ArrayInitializerNode.cs | 2 +- src/Hassium/Parser/Ast/BinaryOpNode.cs | 17 +- src/Hassium/Parser/Ast/BreakNode.cs | 2 +- src/Hassium/Parser/Ast/ClassNode.cs | 10 +- src/Hassium/Parser/Ast/CodeBlock.cs | 5 +- src/Hassium/Parser/Ast/ConditionalOpNode.cs | 22 +- src/Hassium/Parser/Ast/ContinueNode.cs | 2 +- src/Hassium/Parser/Ast/Expression.cs | 2 +- src/Hassium/Parser/Ast/ForEachNode.cs | 18 +- src/Hassium/Parser/Ast/ForNode.cs | 25 +- src/Hassium/Parser/Ast/FuncNode.cs | 10 +- src/Hassium/Parser/Ast/FunctionCallNode.cs | 16 +- src/Hassium/Parser/Ast/IdentifierNode.cs | 5 +- src/Hassium/Parser/Ast/IfNode.cs | 25 +- src/Hassium/Parser/Ast/InstanceNode.cs | 2 +- src/Hassium/Parser/Ast/LambdaFuncNode.cs | 9 +- src/Hassium/Parser/Ast/MemberAccessNode.cs | 14 +- src/Hassium/Parser/Ast/MentalNode.cs | 5 +- src/Hassium/Parser/Ast/NumberNode.cs | 7 +- src/Hassium/Parser/Ast/PropertyNode.cs | 2 +- src/Hassium/Parser/Ast/ReturnNode.cs | 11 +- src/Hassium/Parser/Ast/StatementNode.cs | 5 +- src/Hassium/Parser/Ast/StringNode.cs | 5 +- src/Hassium/Parser/Ast/SwitchNode.cs | 13 +- src/Hassium/Parser/Ast/ThreadNode.cs | 7 +- src/Hassium/Parser/Ast/TryNode.cs | 22 +- src/Hassium/Parser/Ast/UnaryOpNode.cs | 10 +- src/Hassium/Parser/Ast/UncheckedNode.cs | 7 +- src/Hassium/Parser/Ast/UseNode.cs | 8 +- src/Hassium/Parser/Ast/WhileNode.cs | 25 +- src/Hassium/Parser/AstNode.cs | 22 +- src/Hassium/Parser/Parser.cs | 78 +- src/Hassium/Program.cs | 13 +- src/Hassium/Properties/AssemblyInfo.cs | 5 +- src/Hassium/Semantics/LocalScope.cs | 3 +- src/Hassium/Semantics/SemanticAnalyser.cs | 177 ++- src/Hassium/Semantics/SymbolTable.cs | 3 +- 101 files changed, 1407 insertions(+), 1398 deletions(-) diff --git a/src/Hassium/Debug/Debug.cs b/src/Hassium/Debug/Debug.cs index 9903def..5001dd2 100644 --- a/src/Hassium/Debug/Debug.cs +++ b/src/Hassium/Debug/Debug.cs @@ -16,7 +16,9 @@ public static void PrintTokens(List tokens) var position = 0; foreach (Token token in tokens) { - Console.WriteLine(position + new string(' ', tokens.Count.ToString().Length + 2 - position.ToString().Length) + "Type: " + token.TokenClass + + Console.WriteLine(position + + new string(' ', tokens.Count.ToString().Length + 2 - position.ToString().Length) + + "Type: " + token.TokenClass + new string(' ', tokens.Max(x => x.TokenClass.ToString().Length) + 3 - token.TokenClass.ToString().Length) + "Value: " + token.Value); @@ -26,5 +28,4 @@ public static void PrintTokens(List tokens) Console.WriteLine(); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/Functions/ConsoleFunctions.cs b/src/Hassium/Functions/ConsoleFunctions.cs index 84fa367..923a918 100644 --- a/src/Hassium/Functions/ConsoleFunctions.cs +++ b/src/Hassium/Functions/ConsoleFunctions.cs @@ -18,6 +18,7 @@ public static HassiumObject Print(HassiumObject[] args) Console.Write(String.Join("", args.Select(x => x.ToString()))); return null; } + /// /// Prints the specified string to the console (see Print) followed by a newline. /// @@ -29,6 +30,7 @@ public static HassiumObject PrintLn(HassiumObject[] args) Console.WriteLine(String.Join("", args.Select(x => x.ToString()))); return null; } + /// /// Prints the specified array to the console followed by a newline. /// @@ -40,17 +42,20 @@ public static HassiumObject PrintArr(HassiumObject[] args) Console.WriteLine(HassiumConvert.toString(new[] {args[0]}).ToString()); return null; } + /// /// Prompt the user to type something and return the input. /// /// No parameters. /// Nothing. - [IntFunc("input", new []{0,1,2})] + [IntFunc("input", new[] {0, 1, 2})] public static HassiumObject Input(HassiumObject[] args) { - if (args.Length > 0 && args[0].HBool().Value) return Console.ReadKey(args.Length == 2 && args[1].HBool().Value).KeyChar; + if (args.Length > 0 && args[0].HBool().Value) + return Console.ReadKey(args.Length == 2 && args[1].HBool().Value).KeyChar; return Console.ReadLine(); } + /// /// Clears the console. /// @@ -62,6 +67,7 @@ public static HassiumObject Cls(HassiumObject[] args) Console.Clear(); return null; } + /// /// Waits for the user to press any key. /// @@ -74,4 +80,4 @@ public static HassiumObject Pause(HassiumObject[] args) return null; } } -} +} \ No newline at end of file diff --git a/src/Hassium/Functions/Constructors.cs b/src/Hassium/Functions/Constructors.cs index 707bc6a..4a315c0 100644 --- a/src/Hassium/Functions/Constructors.cs +++ b/src/Hassium/Functions/Constructors.cs @@ -39,13 +39,14 @@ public static HassiumObject Array(HassiumObject[] args) return new HassiumArray(new HassiumObject[args[0].HInt().Value]); } - [IntFunc("Random", true, new []{0, 1})] + [IntFunc("Random", true, new[] {0, 1})] public static HassiumObject Random(HassiumObject[] args) { - return args.Length > 0 ? new HassiumRandom(new Random(args[0].HInt().Value)) : new HassiumRandom(new Random()); + return args.Length > 0 + ? new HassiumRandom(new Random(args[0].HInt().Value)) + : new HassiumRandom(new Random()); } - [IntFunc("Event", true, -1)] public static HassiumObject Event(HassiumObject[] args) @@ -62,5 +63,4 @@ public static HassiumObject Event(HassiumObject[] args) return ret; } } -} - +} \ No newline at end of file diff --git a/src/Hassium/Functions/ILibrary.cs b/src/Hassium/Functions/ILibrary.cs index 89887a0..61a42ca 100644 --- a/src/Hassium/Functions/ILibrary.cs +++ b/src/Hassium/Functions/ILibrary.cs @@ -4,5 +4,4 @@ public interface ILibrary { //Dictionary GetFunctions(); } -} - +} \ No newline at end of file diff --git a/src/Hassium/Functions/InternalFunction.cs b/src/Hassium/Functions/InternalFunction.cs index 2a79e80..419d779 100644 --- a/src/Hassium/Functions/InternalFunction.cs +++ b/src/Hassium/Functions/InternalFunction.cs @@ -5,7 +5,7 @@ namespace Hassium.Functions { - public delegate HassiumObject HassiumFunctionDelegate (params HassiumObject[] arguments); + public delegate HassiumObject HassiumFunctionDelegate(params HassiumObject[] arguments); public class InternalFunction : HassiumObject { @@ -36,13 +36,15 @@ public InternalFunction(HassiumFunctionDelegate target, int[] args, bool prop = public override string ToString() { - return string.Format("[InternalFunction: {0}`{1}]", target.Method.Name, target.Method.GetParameters().Count()); + return string.Format("[InternalFunction: {0}`{1}]", target.Method.Name, + target.Method.GetParameters().Count()); } public override HassiumObject Invoke(params HassiumObject[] args) { if (!Arguments.Contains(args.Length) && Arguments[0] != -1) - throw new Exception("Function " + target.Method.Name + " has " + Arguments.Max() + " arguments, but is invoked with " + args.Length); + throw new Exception("Function " + target.Method.Name + " has " + Arguments.Max() + + " arguments, but is invoked with " + args.Length); return target(args); } } @@ -95,5 +97,4 @@ public IntFunc(string name, bool constr, int[] args) Arguments = args; } } -} - +} \ No newline at end of file diff --git a/src/Hassium/Functions/MiscFunctions.cs b/src/Hassium/Functions/MiscFunctions.cs index 37e8a39..370f13d 100644 --- a/src/Hassium/Functions/MiscFunctions.cs +++ b/src/Hassium/Functions/MiscFunctions.cs @@ -8,32 +8,35 @@ namespace Hassium.Functions { - public class MiscFunctions : ILibrary - { - [IntFunc("type", 1)] - public static HassiumObject Type(HassiumObject[] args) - { - return args[0].GetType().ToString().Substring(args[0].GetType().ToString().LastIndexOf(".", StringComparison.Ordinal) + 1); - } + public class MiscFunctions : ILibrary + { + [IntFunc("type", 1)] + public static HassiumObject Type(HassiumObject[] args) + { + return + args[0].GetType() + .ToString() + .Substring(args[0].GetType().ToString().LastIndexOf(".", StringComparison.Ordinal) + 1); + } - [IntFunc("throw", -1)] - public static HassiumObject Throw(HassiumObject[] args) - { - throw new Exception(String.Join("", args.Cast())); - } + [IntFunc("throw", -1)] + public static HassiumObject Throw(HassiumObject[] args) + { + throw new Exception(String.Join("", args.Cast())); + } [IntFunc("fill", 2)] public static HassiumObject Fill(HassiumObject[] args) { HassiumObject[] array = new HassiumObject[args[1].HInt().Value]; - var fillvalue = new[] { args[0] }; + var fillvalue = new[] {args[0]}; Array.Copy(fillvalue, array, 1); int arraylgt = array.Length / 2; - for(int i = 1; i < array.Length; i *= 2) + for (int i = 1; i < array.Length; i *= 2) { int cplength = i; - if(i > arraylgt) + if (i > arraylgt) { cplength = array.Length - i; } @@ -61,7 +64,7 @@ public static HassiumObject FillZero(HassiumObject[] args) return Enumerable.Repeat(zero, args[0].HInt().Value).ToArray(); } - [IntFunc("range", new []{2,3})] + [IntFunc("range", new[] {2, 3})] public static HassiumObject Range(HassiumObject[] args) { var from = args[0].HDouble().Value; @@ -80,53 +83,53 @@ public static HassiumObject Range(HassiumObject[] args) return list.ToArray().Select(x => new HassiumDouble(x)).ToArray(); } return from == to - ? new[] { from }.Select(x => new HassiumDouble(x)).ToArray() + ? new[] {from}.Select(x => new HassiumDouble(x)).ToArray() : (to < from - ? Enumerable.Range((int)to, (int)from).Reverse().Cast().ToArray() - : Enumerable.Range((int)from, (int)to).Select(x => new HassiumDouble(x)).ToArray()); + ? Enumerable.Range((int) to, (int) from).Reverse().Cast().ToArray() + : Enumerable.Range((int) from, (int) to).Select(x => new HassiumDouble(x)).ToArray()); } [IntFunc("runtimecall", -1)] - public static HassiumObject RuntimeCall(HassiumObject[] args) - { - string fullpath = args[0].ToString(); - string typename = fullpath.Substring(0, fullpath.LastIndexOf('.')); - string membername = fullpath.Split('.').Last(); - object[] margs = args.Skip(1).ToArray(); - Type t = System.Type.GetType(typename); - if(t == null) throw new ArgumentException("The type '" + typename + "' doesn't exist."); - object instance = null; - try - { - instance = Activator.CreateInstance(t); - } - catch (Exception) - { - } - var test = t.GetMember(membername).First(); - switch (test.MemberType) - { - case MemberTypes.Field: - var fv = t.GetField(membername).GetValue(null); - if(fv is double) return new HassiumDouble((double)fv); - if(fv is int) return new HassiumInt((int)fv); - if(fv is string) return new HassiumString((string)fv); - if(fv is Array) return new HassiumArray((Array)fv); - if(fv is IDictionary) return new HassiumDictionary((IDictionary)fv); - if (fv is bool) return new HassiumBool((bool) fv); - else return (HassiumObject)(object) fv; - case MemberTypes.Method: - case MemberTypes.Constructor: - var result = t.InvokeMember( - membername, - BindingFlags.InvokeMethod, - null, - instance, - margs - ); - return (HassiumObject)result; - } - return null; - } - } -} + public static HassiumObject RuntimeCall(HassiumObject[] args) + { + string fullpath = args[0].ToString(); + string typename = fullpath.Substring(0, fullpath.LastIndexOf('.')); + string membername = fullpath.Split('.').Last(); + object[] margs = args.Skip(1).ToArray(); + Type t = System.Type.GetType(typename); + if (t == null) throw new ArgumentException("The type '" + typename + "' doesn't exist."); + object instance = null; + try + { + instance = Activator.CreateInstance(t); + } + catch (Exception) + { + } + var test = t.GetMember(membername).First(); + switch (test.MemberType) + { + case MemberTypes.Field: + var fv = t.GetField(membername).GetValue(null); + if (fv is double) return new HassiumDouble((double) fv); + if (fv is int) return new HassiumInt((int) fv); + if (fv is string) return new HassiumString((string) fv); + if (fv is Array) return new HassiumArray((Array) fv); + if (fv is IDictionary) return new HassiumDictionary((IDictionary) fv); + if (fv is bool) return new HassiumBool((bool) fv); + else return (HassiumObject) (object) fv; + case MemberTypes.Method: + case MemberTypes.Constructor: + var result = t.InvokeMember( + membername, + BindingFlags.InvokeMethod, + null, + instance, + margs + ); + return (HassiumObject) result; + } + return null; + } + } +} \ No newline at end of file diff --git a/src/Hassium/Functions/SystemFunctions.cs b/src/Hassium/Functions/SystemFunctions.cs index 965620e..676f2e6 100644 --- a/src/Hassium/Functions/SystemFunctions.cs +++ b/src/Hassium/Functions/SystemFunctions.cs @@ -11,7 +11,6 @@ namespace Hassium.Functions { public class SystemFunctions : ILibrary { - [IntFunc("system", -1)] public static HassiumObject System(HassiumObject[] args) { @@ -27,14 +26,14 @@ public static HassiumObject System(HassiumObject[] args) } }; process.Start(); - + var output = process.StandardOutput.ReadToEnd(); process.WaitForExit(); return output; } - [IntFunc("date", new []{1, 3, 6, 0})] + [IntFunc("date", new[] {1, 3, 6, 0})] public static HassiumObject Date(HassiumObject[] args) { switch (args.Length) @@ -53,24 +52,27 @@ public static HassiumObject Date(HassiumObject[] args) } } - [IntFunc("dateParse", new []{1, 2})] + [IntFunc("dateParse", new[] {1, 2})] public static HassiumObject DateParse(HassiumObject[] args) { - return args.Length == 2 ? new HassiumDate(DateTime.ParseExact(args[0].ToString(), args[1].ToString(), CultureInfo.InvariantCulture)) : new HassiumDate(DateTime.Parse(args[0].ToString())); + return args.Length == 2 + ? new HassiumDate(DateTime.ParseExact(args[0].ToString(), args[1].ToString(), + CultureInfo.InvariantCulture)) + : new HassiumDate(DateTime.Parse(args[0].ToString())); } - [IntFunc("time", new []{0, 1, 3})] + [IntFunc("time", new[] {0, 1, 3})] public static HassiumObject Time(HassiumObject[] args) { - switch(args.Length) + switch (args.Length) { case 0: return new HassiumDate(DateTime.Now); case 1: - return new HassiumDate(DateTime.Now).toString(new HassiumObject[] { args[0].ToString() }); + return new HassiumDate(DateTime.Now).toString(new HassiumObject[] {args[0].ToString()}); default: return new HassiumDate(new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, - args[0].HDouble().ValueInt, args[1].HDouble().ValueInt, args[2].HDouble().ValueInt)); + args[0].HDouble().ValueInt, args[1].HDouble().ValueInt, args[2].HDouble().ValueInt)); } } @@ -102,4 +104,4 @@ public static HassiumObject Eval(HassiumObject[] args) return null; } } -} +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/Collections/HassiumStack.cs b/src/Hassium/HassiumObjects/Collections/HassiumStack.cs index 2b8882e..bb9062f 100644 --- a/src/Hassium/HassiumObjects/Collections/HassiumStack.cs +++ b/src/Hassium/HassiumObjects/Collections/HassiumStack.cs @@ -5,7 +5,7 @@ namespace Hassium.HassiumObjects.Collections { - public class HassiumStack: HassiumObject + public class HassiumStack : HassiumObject { public Stack Value { get; private set; } @@ -47,5 +47,4 @@ private HassiumObject push(HassiumObject[] args) return null; } } -} - +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/Conversion/HassiumConvert.cs b/src/Hassium/HassiumObjects/Conversion/HassiumConvert.cs index 8079398..21227c5 100644 --- a/src/Hassium/HassiumObjects/Conversion/HassiumConvert.cs +++ b/src/Hassium/HassiumObjects/Conversion/HassiumConvert.cs @@ -5,7 +5,7 @@ namespace Hassium.HassiumObjects.Conversion { - public class HassiumConvert: HassiumObject + public class HassiumConvert : HassiumObject { public HassiumConvert() { @@ -18,19 +18,19 @@ public static HassiumObject toNumber(HassiumObject[] args) { if (args[0] is HassiumString) { - var ret = Convert.ToDouble(((HassiumString)args[0]).Value); + var ret = Convert.ToDouble(((HassiumString) args[0]).Value); if (ret == System.Math.Truncate(ret)) - return new HassiumInt((int)ret); + return new HassiumInt((int) ret); else return new HassiumDouble(ret); } else if (args[0] is HassiumInt) { - return new HassiumDouble(((HassiumInt)args[0]).Value); + return new HassiumDouble(((HassiumInt) args[0]).Value); } else if (args[0] is HassiumDouble) { - return new HassiumInt(((HassiumDouble)args[0]).ValueInt ); + return new HassiumInt(((HassiumDouble) args[0]).ValueInt); } else { @@ -46,14 +46,13 @@ public static HassiumObject toString(HassiumObject[] args) public static HassiumObject toBool(HassiumObject[] args) { if (args[0] is HassiumString) - return new HassiumBool(Convert.ToBoolean(((HassiumString)args[0]).Value)); + return new HassiumBool(Convert.ToBoolean(((HassiumString) args[0]).Value)); else if (args[0] is HassiumDouble) - return new HassiumBool(Convert.ToBoolean(((HassiumDouble)args[0]).ValueInt)); + return new HassiumBool(Convert.ToBoolean(((HassiumDouble) args[0]).ValueInt)); else if (args[0] is HassiumInt) - return new HassiumBool(Convert.ToBoolean(((HassiumInt)args[0]).Value)); + return new HassiumBool(Convert.ToBoolean(((HassiumInt) args[0]).Value)); else throw new Exception("Unknown format for Convert.toBool"); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/Drawing/HassiumBitmap.cs b/src/Hassium/HassiumObjects/Drawing/HassiumBitmap.cs index 44632ba..c309f2e 100644 --- a/src/Hassium/HassiumObjects/Drawing/HassiumBitmap.cs +++ b/src/Hassium/HassiumObjects/Drawing/HassiumBitmap.cs @@ -12,20 +12,20 @@ namespace Hassium.HassiumObjects.Drawing { - public class HassiumBitmap: HassiumObject + public class HassiumBitmap : HassiumObject { public Bitmap Value { get; private set; } public HassiumBitmap(HassiumObject[] cctr) { if (cctr[0] is HassiumString) - Value = new Bitmap(((HassiumString)cctr).Value); + Value = new Bitmap(((HassiumString) cctr).Value); else if (cctr[0] is HassiumImage) - Value = new Bitmap(((HassiumImage)cctr).Value); + Value = new Bitmap(((HassiumImage) cctr).Value); else if (cctr[0] is HassiumInt) - Value = new Bitmap(((HassiumInt)cctr[0]).Value, ((HassiumInt)cctr[1]).Value); + Value = new Bitmap(((HassiumInt) cctr[0]).Value, ((HassiumInt) cctr[1]).Value); else if (cctr[0] is HassiumDouble) - Value = new Bitmap(((HassiumDouble)cctr[0]).ValueInt, ((HassiumDouble)cctr[1]).ValueInt); + Value = new Bitmap(((HassiumDouble) cctr[0]).ValueInt, ((HassiumDouble) cctr[1]).ValueInt); else throw new Exception("Unknown type " + cctr[0].GetType().ToString() + " in HassiumBitmap constructor"); @@ -51,28 +51,29 @@ private HassiumObject makeTransparent(HassiumObject[] args) if (args.Length <= 0) Value.MakeTransparent(); else - Value.MakeTransparent(((HassiumColor)args[0]).Value); + Value.MakeTransparent(((HassiumColor) args[0]).Value); return null; } private HassiumObject save(HassiumObject[] args) { - Value.Save(((HassiumString)args[0]).Value); + Value.Save(((HassiumString) args[0]).Value); return null; } private HassiumObject setPixel(HassiumObject[] args) { - Value.SetPixel(((HassiumDouble)args[0]).ValueInt, ((HassiumDouble)args[1]).ValueInt, ((HassiumColor)args[2]).Value); + Value.SetPixel(((HassiumDouble) args[0]).ValueInt, ((HassiumDouble) args[1]).ValueInt, + ((HassiumColor) args[2]).Value); return null; } private HassiumObject setResolution(HassiumObject[] args) { - Value.SetResolution(((float)((HassiumDouble)args[0]).Value), ((float)((HassiumDouble)args[1]).Value)); + Value.SetResolution(((float) ((HassiumDouble) args[0]).Value), ((float) ((HassiumDouble) args[1]).Value)); return null; } @@ -82,4 +83,4 @@ private HassiumObject toString(HassiumObject[] args) return new HassiumString(Value.ToString()); } } -} +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/Drawing/HassiumColor.cs b/src/Hassium/HassiumObjects/Drawing/HassiumColor.cs index 7e4d4b4..8f374dd 100644 --- a/src/Hassium/HassiumObjects/Drawing/HassiumColor.cs +++ b/src/Hassium/HassiumObjects/Drawing/HassiumColor.cs @@ -9,531 +9,531 @@ namespace Hassium.HassiumObjects.Drawing { - public class HassiumColor : HassiumObject - { - public Color Value { get; private set; } - - public HassiumColor(HassiumObject[] args) - { - switch (args.Length) - { - case 0: - throw new ParseException("Not enough arguments for HassiumColor constructor", -1); - case 1: - if (args[0] is HassiumString) - { - string col = args[0].ToString(); - Value = col.StartsWith("#") ? ColorTranslator.FromHtml(col) : Color.FromName(col.ToLower()); - } - break; - case 3: - Value = Color.FromArgb(args[0].HInt().Value, args[1].HInt().Value, args[2].HInt().Value); - break; - default: - if (args[0] is HassiumString) - { - string fmt = args[0].ToString().ToLower(); - switch (fmt) - { - case "hsl": - { - double hue = args[1].HDouble().Value / 360; - double saturation = args[2].HDouble().Value / 100; - double luminosity = args[3].HDouble().Value / 100; - - var red = luminosity; - var green = luminosity; - var blue = luminosity; - var v = (luminosity <= 0.5) - ? (luminosity * (1.0 + saturation)) - : (luminosity + saturation - luminosity * saturation); - if (v > 0) - { - var m = luminosity + luminosity - v; - var sv = (v - m) / v; - hue *= 6.0; - var sextant = (int) hue; - var fract = hue - sextant; - var vsf = v * sv * fract; - var mid1 = m + vsf; - var mid2 = v - vsf; - switch (sextant) - { - case 0: - red = v; - green = mid1; - blue = m; - break; - case 1: - red = mid2; - green = v; - blue = m; - break; - case 2: - red = m; - green = v; - blue = mid1; - break; - case 3: - red = m; - green = mid2; - blue = v; - break; - case 4: - red = mid1; - green = m; - blue = v; - break; - case 5: - red = v; - green = m; - blue = mid2; - break; - } - } - Value = Color.FromArgb(Convert.ToByte(red * 255.0f), Convert.ToByte(green * 255.0f), - Convert.ToByte(blue * 255.0f)); - } - break; - case "hsv": - case "hsb": - { - double hue = args[1].HDouble().Value; - double saturation = args[2].HDouble().Value / 100; - double value = args[3].HDouble().Value / 100; - - int hi = Convert.ToInt32(System.Math.Floor(hue / 60)) % 6; - double f = hue / 60 - System.Math.Floor(hue / 60); - - value = value * 255; - int v = Convert.ToInt32(value); - int p = Convert.ToInt32(value * (1 - saturation)); - int q = Convert.ToInt32(value * (1 - f * saturation)); - int t = Convert.ToInt32(value * (1 - (1 - f) * saturation)); - - switch (hi) - { - case 0: - Value = Color.FromArgb(255, v, t, p); - break; - case 1: - Value = Color.FromArgb(255, q, v, p); - break; - case 2: - Value = Color.FromArgb(255, p, v, t); - break; - case 3: - Value = Color.FromArgb(255, p, q, v); - break; - case 4: - Value = Color.FromArgb(255, t, p, v); - break; - default: - Value = Color.FromArgb(255, v, p, q); - break; - } - } - break; - case "rgbp": - case "rgbpercent": - Value = args.Length == 5 - ? Color.FromArgb(Convert.ToInt32(args[1].HDouble().Value * 255), - Convert.ToInt32(args[2].HDouble().Value * 255), - Convert.ToInt32(args[3].HDouble().Value * 255), - Convert.ToInt32(args[4].HDouble().Value * 255)) - : Color.FromArgb(Convert.ToInt32(args[1].HDouble().Value * 255), - Convert.ToInt32(args[2].HDouble().Value * 255), - Convert.ToInt32(args[3].HDouble().Value * 255)); - break; - case "cmyk": - { - double cyan = args[1].HDouble().Value / 100; - double magenta = args[2].HDouble().Value / 100; - double yellow = args[3].HDouble().Value / 100; - double black = args[4].HDouble().Value / 100; - - Value = - new HassiumColor(new HassiumObject[] - { - "cmy", - (cyan * (1 - black) + black) * 100, - (magenta * (1 - black) + black) * 100, - (yellow * (1 - black) + black) * 100 - }).Value; - break; - } - case "cmy": - { - double cyan = args[1].HDouble().Value / 100; - double magenta = args[2].HDouble().Value / 100; - double yellow = args[3].HDouble().Value / 100; - - Value = Color.FromArgb(Convert.ToInt32((1 - cyan) * 255.0), - Convert.ToInt32((1 - magenta) * 255.0), Convert.ToInt32((1 - yellow) * 255.0)); - break; - } - case "xyz": - { - double x = args[1].HDouble().Value / 100; - double y = args[2].HDouble().Value / 100; - double z = args[3].HDouble().Value / 100; - - var r = x * 3.2406 + y * -1.5372 + z * -0.4986; - var g = x * -0.9689 + y * 1.8758 + z * 0.0415; - var b = x * 0.0557 + y * -0.2040 + z * 1.0570; - - r = r > 0.0031308 ? 1.055 * System.Math.Pow(r, 1 / 2.4) - 0.055 : 12.92 * r; - g = g > 0.0031308 ? 1.055 * System.Math.Pow(g, 1 / 2.4) - 0.055 : 12.92 * g; - b = b > 0.0031308 ? 1.055 * System.Math.Pow(b, 1 / 2.4) - 0.055 : 12.92 * b; - - Value = Color.FromArgb(Convert.ToInt32(r * 255), Convert.ToInt32(g * 255), - Convert.ToInt32(b * 255)); - break; - } - case "lab": - case "cie-lab": - { - var _l = args[1].HDouble().Value; - var _a = args[2].HDouble().Value; - var _b = args[3].HDouble().Value; - - double _y = (_l + 16.0) / 116.0; - double _x = _a / 500.0 + _y; - double _z = _y - _b / 200.0; - - double _x3 = _x * _x * _x; - double _z3 = _z * _z * _z; - - - Value = - new HassiumColor(new HassiumObject[] - { - "xyz", - 95.047 * (_x3 > 0.00885645168 ? _x3 : (_x - 16.0 / 116.0) / 7.787), - 100.000 * - (_l > (903.296296 * 0.00885645168) - ? System.Math.Pow(((_l + 16.0) / 116.0), 3) - : _l / 903.296296), - 108.883 * (_z3 > 0.00885645168 ? _z3 : (_z - 16.0 / 116.0) / 7.787) - }).Value; - break; - } - case "lch": - case "cie-lch": - { - var _l = args[1].HDouble().Value; - var _c = args[2].HDouble().Value; - var _h = args[3].HDouble().Value; - var hrad = _h * System.Math.PI / 180.0; - - Value = new HassiumColor(new HassiumObject[] - { - "lab", - Convert.ToInt32(_l), - Convert.ToInt32(System.Math.Cos(hrad) * _c), - Convert.ToInt32(System.Math.Sin(hrad) * _c) - }).Value; - break; - } - case "luv": - case "cie-luv": - { - var _l = args[1].HDouble().Value; - var _u = args[2].HDouble().Value; - var _v = args[3].HDouble().Value; - - var uPrime = (4.0 * 95.047) / 1921.696; - var vPrime = (9.0 * 100.000) / 1921.696; - var a = (1.0 / 3.0) * ((52.0 * _l) / (_u + 13 * _l * uPrime) - 1.0); - var imteL_16_116 = (_l + 16.0) / 116.0; - var y = _l > 903.296296 * 0.00885645168 - ? imteL_16_116 * imteL_16_116 * imteL_16_116 - : _l / 903.296296; - var b = -5.0 * y; - var d = y * ((39.0 * _l) / (_v + 13.0 * _l * vPrime) - 5.0); - var x = (d - b) / (a - (-1.0 / 3.0)); - var z = x * a + b; - - Value = - new HassiumColor(new HassiumObject[] - { - "xyz", - 100 * x, - 100 * y, - 100 * z - }).Value; - break; - } - case "hunterlab": - case "hunter-lab": - case "hlab": - case "htlab": - { - var _l = args[1].HDouble().Value; - var _a = args[2].HDouble().Value; - var _b = args[3].HDouble().Value; - - var x = (_a / 17.5) * (_l / 10.0); - var itemL_10 = _l / 10.0; - var y = itemL_10 * itemL_10; - var z = _b / 7.0 * _l / 10.0; - - Value = - new HassiumColor(new HassiumObject[] - { - "xyz", - (x + y) / 1.02, - y, - -(z - y) / 0.847 - }).Value; - break; - } - } - } - else - { - Value = Color.FromArgb(args[0].HInt().Value, args[1].HInt().Value, args[2].HInt().Value, - args[3].HInt().Value); - } - break; - } - - Attributes.Add("alpha", new HassiumProperty("alpha", x => Value.A, x => - { - Value = Color.FromArgb(x[0].HInt().Value, Value.R, Value.G, Value.B); - return null; - })); - Attributes.Add("red", new HassiumProperty("red", x => Value.R, x => - { - Value = Color.FromArgb(Value.A, x[0].HInt().Value, Value.G, Value.B); - return null; - })); - Attributes.Add("green", new HassiumProperty("green", x => Value.G, x => - { - Value = Color.FromArgb(Value.A, Value.R, x[0].HInt().Value, Value.B); - return null; - })); - Attributes.Add("blue", new HassiumProperty("blue", x => Value.B, x => - { - Value = Color.FromArgb(Value.A, Value.R, Value.G, x[0].HInt().Value); - return null; - })); - Attributes.Add("argb", new HassiumProperty("argb", x => Value.ToArgb(), x => - { - Value = Color.FromArgb(x[0].HInt().Value); - return null; - })); - - Attributes.Add("toHsl", new InternalFunction(ToHSL, 0)); - Attributes.Add("toHsv", new InternalFunction(ToHSV, 0)); - Attributes.Add("toCmyk", new InternalFunction(ToCMYK, 0)); - Attributes.Add("toCmy", new InternalFunction(ToCMY, 0)); - Attributes.Add("toXyz", new InternalFunction(ToXYZ, 0)); - Attributes.Add("toLab", new InternalFunction(ToLAB, 0)); - Attributes.Add("toLch", new InternalFunction(ToLCH, 0)); - Attributes.Add("toLuv", new InternalFunction(ToLUV, 0)); - Attributes.Add("toHunterLab", new InternalFunction(ToHunterLab, 0)); + public class HassiumColor : HassiumObject + { + public Color Value { get; private set; } + + public HassiumColor(HassiumObject[] args) + { + switch (args.Length) + { + case 0: + throw new ParseException("Not enough arguments for HassiumColor constructor", -1); + case 1: + if (args[0] is HassiumString) + { + string col = args[0].ToString(); + Value = col.StartsWith("#") ? ColorTranslator.FromHtml(col) : Color.FromName(col.ToLower()); + } + break; + case 3: + Value = Color.FromArgb(args[0].HInt().Value, args[1].HInt().Value, args[2].HInt().Value); + break; + default: + if (args[0] is HassiumString) + { + string fmt = args[0].ToString().ToLower(); + switch (fmt) + { + case "hsl": + { + double hue = args[1].HDouble().Value / 360; + double saturation = args[2].HDouble().Value / 100; + double luminosity = args[3].HDouble().Value / 100; + + var red = luminosity; + var green = luminosity; + var blue = luminosity; + var v = (luminosity <= 0.5) + ? (luminosity * (1.0 + saturation)) + : (luminosity + saturation - luminosity * saturation); + if (v > 0) + { + var m = luminosity + luminosity - v; + var sv = (v - m) / v; + hue *= 6.0; + var sextant = (int) hue; + var fract = hue - sextant; + var vsf = v * sv * fract; + var mid1 = m + vsf; + var mid2 = v - vsf; + switch (sextant) + { + case 0: + red = v; + green = mid1; + blue = m; + break; + case 1: + red = mid2; + green = v; + blue = m; + break; + case 2: + red = m; + green = v; + blue = mid1; + break; + case 3: + red = m; + green = mid2; + blue = v; + break; + case 4: + red = mid1; + green = m; + blue = v; + break; + case 5: + red = v; + green = m; + blue = mid2; + break; + } + } + Value = Color.FromArgb(Convert.ToByte(red * 255.0f), Convert.ToByte(green * 255.0f), + Convert.ToByte(blue * 255.0f)); + } + break; + case "hsv": + case "hsb": + { + double hue = args[1].HDouble().Value; + double saturation = args[2].HDouble().Value / 100; + double value = args[3].HDouble().Value / 100; + + int hi = Convert.ToInt32(System.Math.Floor(hue / 60)) % 6; + double f = hue / 60 - System.Math.Floor(hue / 60); + + value = value * 255; + int v = Convert.ToInt32(value); + int p = Convert.ToInt32(value * (1 - saturation)); + int q = Convert.ToInt32(value * (1 - f * saturation)); + int t = Convert.ToInt32(value * (1 - (1 - f) * saturation)); + + switch (hi) + { + case 0: + Value = Color.FromArgb(255, v, t, p); + break; + case 1: + Value = Color.FromArgb(255, q, v, p); + break; + case 2: + Value = Color.FromArgb(255, p, v, t); + break; + case 3: + Value = Color.FromArgb(255, p, q, v); + break; + case 4: + Value = Color.FromArgb(255, t, p, v); + break; + default: + Value = Color.FromArgb(255, v, p, q); + break; + } + } + break; + case "rgbp": + case "rgbpercent": + Value = args.Length == 5 + ? Color.FromArgb(Convert.ToInt32(args[1].HDouble().Value * 255), + Convert.ToInt32(args[2].HDouble().Value * 255), + Convert.ToInt32(args[3].HDouble().Value * 255), + Convert.ToInt32(args[4].HDouble().Value * 255)) + : Color.FromArgb(Convert.ToInt32(args[1].HDouble().Value * 255), + Convert.ToInt32(args[2].HDouble().Value * 255), + Convert.ToInt32(args[3].HDouble().Value * 255)); + break; + case "cmyk": + { + double cyan = args[1].HDouble().Value / 100; + double magenta = args[2].HDouble().Value / 100; + double yellow = args[3].HDouble().Value / 100; + double black = args[4].HDouble().Value / 100; + + Value = + new HassiumColor(new HassiumObject[] + { + "cmy", + (cyan * (1 - black) + black) * 100, + (magenta * (1 - black) + black) * 100, + (yellow * (1 - black) + black) * 100 + }).Value; + break; + } + case "cmy": + { + double cyan = args[1].HDouble().Value / 100; + double magenta = args[2].HDouble().Value / 100; + double yellow = args[3].HDouble().Value / 100; + + Value = Color.FromArgb(Convert.ToInt32((1 - cyan) * 255.0), + Convert.ToInt32((1 - magenta) * 255.0), Convert.ToInt32((1 - yellow) * 255.0)); + break; + } + case "xyz": + { + double x = args[1].HDouble().Value / 100; + double y = args[2].HDouble().Value / 100; + double z = args[3].HDouble().Value / 100; + + var r = x * 3.2406 + y * -1.5372 + z * -0.4986; + var g = x * -0.9689 + y * 1.8758 + z * 0.0415; + var b = x * 0.0557 + y * -0.2040 + z * 1.0570; + + r = r > 0.0031308 ? 1.055 * System.Math.Pow(r, 1 / 2.4) - 0.055 : 12.92 * r; + g = g > 0.0031308 ? 1.055 * System.Math.Pow(g, 1 / 2.4) - 0.055 : 12.92 * g; + b = b > 0.0031308 ? 1.055 * System.Math.Pow(b, 1 / 2.4) - 0.055 : 12.92 * b; + + Value = Color.FromArgb(Convert.ToInt32(r * 255), Convert.ToInt32(g * 255), + Convert.ToInt32(b * 255)); + break; + } + case "lab": + case "cie-lab": + { + var _l = args[1].HDouble().Value; + var _a = args[2].HDouble().Value; + var _b = args[3].HDouble().Value; + + double _y = (_l + 16.0) / 116.0; + double _x = _a / 500.0 + _y; + double _z = _y - _b / 200.0; + + double _x3 = _x * _x * _x; + double _z3 = _z * _z * _z; + + + Value = + new HassiumColor(new HassiumObject[] + { + "xyz", + 95.047 * (_x3 > 0.00885645168 ? _x3 : (_x - 16.0 / 116.0) / 7.787), + 100.000 * + (_l > (903.296296 * 0.00885645168) + ? System.Math.Pow(((_l + 16.0) / 116.0), 3) + : _l / 903.296296), + 108.883 * (_z3 > 0.00885645168 ? _z3 : (_z - 16.0 / 116.0) / 7.787) + }).Value; + break; + } + case "lch": + case "cie-lch": + { + var _l = args[1].HDouble().Value; + var _c = args[2].HDouble().Value; + var _h = args[3].HDouble().Value; + var hrad = _h * System.Math.PI / 180.0; + + Value = new HassiumColor(new HassiumObject[] + { + "lab", + Convert.ToInt32(_l), + Convert.ToInt32(System.Math.Cos(hrad) * _c), + Convert.ToInt32(System.Math.Sin(hrad) * _c) + }).Value; + break; + } + case "luv": + case "cie-luv": + { + var _l = args[1].HDouble().Value; + var _u = args[2].HDouble().Value; + var _v = args[3].HDouble().Value; + + var uPrime = (4.0 * 95.047) / 1921.696; + var vPrime = (9.0 * 100.000) / 1921.696; + var a = (1.0 / 3.0) * ((52.0 * _l) / (_u + 13 * _l * uPrime) - 1.0); + var imteL_16_116 = (_l + 16.0) / 116.0; + var y = _l > 903.296296 * 0.00885645168 + ? imteL_16_116 * imteL_16_116 * imteL_16_116 + : _l / 903.296296; + var b = -5.0 * y; + var d = y * ((39.0 * _l) / (_v + 13.0 * _l * vPrime) - 5.0); + var x = (d - b) / (a - (-1.0 / 3.0)); + var z = x * a + b; + + Value = + new HassiumColor(new HassiumObject[] + { + "xyz", + 100 * x, + 100 * y, + 100 * z + }).Value; + break; + } + case "hunterlab": + case "hunter-lab": + case "hlab": + case "htlab": + { + var _l = args[1].HDouble().Value; + var _a = args[2].HDouble().Value; + var _b = args[3].HDouble().Value; + + var x = (_a / 17.5) * (_l / 10.0); + var itemL_10 = _l / 10.0; + var y = itemL_10 * itemL_10; + var z = _b / 7.0 * _l / 10.0; + + Value = + new HassiumColor(new HassiumObject[] + { + "xyz", + (x + y) / 1.02, + y, + -(z - y) / 0.847 + }).Value; + break; + } + } + } + else + { + Value = Color.FromArgb(args[0].HInt().Value, args[1].HInt().Value, args[2].HInt().Value, + args[3].HInt().Value); + } + break; + } + + Attributes.Add("alpha", new HassiumProperty("alpha", x => Value.A, x => + { + Value = Color.FromArgb(x[0].HInt().Value, Value.R, Value.G, Value.B); + return null; + })); + Attributes.Add("red", new HassiumProperty("red", x => Value.R, x => + { + Value = Color.FromArgb(Value.A, x[0].HInt().Value, Value.G, Value.B); + return null; + })); + Attributes.Add("green", new HassiumProperty("green", x => Value.G, x => + { + Value = Color.FromArgb(Value.A, Value.R, x[0].HInt().Value, Value.B); + return null; + })); + Attributes.Add("blue", new HassiumProperty("blue", x => Value.B, x => + { + Value = Color.FromArgb(Value.A, Value.R, Value.G, x[0].HInt().Value); + return null; + })); + Attributes.Add("argb", new HassiumProperty("argb", x => Value.ToArgb(), x => + { + Value = Color.FromArgb(x[0].HInt().Value); + return null; + })); + + Attributes.Add("toHsl", new InternalFunction(ToHSL, 0)); + Attributes.Add("toHsv", new InternalFunction(ToHSV, 0)); + Attributes.Add("toCmyk", new InternalFunction(ToCMYK, 0)); + Attributes.Add("toCmy", new InternalFunction(ToCMY, 0)); + Attributes.Add("toXyz", new InternalFunction(ToXYZ, 0)); + Attributes.Add("toLab", new InternalFunction(ToLAB, 0)); + Attributes.Add("toLch", new InternalFunction(ToLCH, 0)); + Attributes.Add("toLuv", new InternalFunction(ToLUV, 0)); + Attributes.Add("toHunterLab", new InternalFunction(ToHunterLab, 0)); //Attributes.Add("toYuv", new InternalFunction(ToYUV, 0)); } - public HassiumObject ToHSL(HassiumObject[] args) - { - var red = Value.R / 255.0; - var green = Value.G / 255.0; - var blue = Value.B / 255.0; - var v = System.Math.Max(System.Math.Max(red, green), blue); - var m = System.Math.Min(System.Math.Min(red, green), blue); - var hue = 0.0; - var saturation = 0.0; - var luminosity = (m + v) / 2.0; - if (luminosity > 0.0) - { - var vm = v - m; - saturation = vm; - if (saturation > 0.0) - { - saturation /= (luminosity <= 0.5) ? (v + m) : (2.0 - v - m); - var r2 = (v - red) / vm; - var g2 = (v - green) / vm; - var b2 = (v - blue) / vm; - if (red == v) - { - hue = (green == m ? 5.0 + b2 : 1.0 - g2); - } - else if (green == v) - { - hue = (blue == m ? 1.0 + r2 : 3.0 - b2); - } - else - { - hue = (red == m ? 3.0 + g2 : 5.0 - r2); - } - hue /= 6.0; - } - } - - return - new HassiumDictionary(new Dictionary - { - {"h", hue * 360}, - {"s", saturation * 100}, - {"l", luminosity * 100} - }); - } - - public HassiumObject ToHSV(HassiumObject[] args) - { - int max = System.Math.Max(Value.R, System.Math.Max(Value.G, Value.B)); - int min = System.Math.Min(Value.R, System.Math.Min(Value.G, Value.B)); - - double hue = Value.GetHue(); - double saturation = (max == 0) ? 0 : 1d - (1d * min / max); - double value = max / 255d; - - return - new HassiumDictionary(new Dictionary - { - {"h", hue}, - {"s", saturation * 100}, - {"v", value * 100} - }); - } - - public HassiumObject ToCMYK(HassiumObject[] args) - { - double red = Value.R / 255d; - double green = Value.G / 255d; - double blue = Value.B / 255d; - - double black = new[] {1 - red, 1 - green, 1 - blue}.Min(); - double cyan = (1 - red - black) / (1 - black); - double magenta = (1 - green - black) / (1 - black); - double yellow = (1 - blue - black) / (1 - black); - - return - new HassiumDictionary(new Dictionary - { - {"c", 100 * cyan}, - {"m", 100 * magenta}, - {"y", 100 * yellow}, - {"k", 100 * black} - }); - } - - public HassiumObject ToCMY(HassiumObject[] args) - { - return - new HassiumDictionary(new Dictionary - { - {"c", (1 - Value.R / 255.0) * 100.0}, - {"m", (1 - Value.G / 255.0) * 100.0}, - {"y", (1 - Value.B / 255.0) * 100.0} - }); - } - - public HassiumObject ToXYZ(HassiumObject[] args) - { - double red = Value.R / 255d; - double green = Value.G / 255d; - double blue = Value.B / 255d; - - double r = (red > 0.04045) ? System.Math.Pow((red + 0.055) / 1.055, 2.4) : (red / 12.92); - double g = (green > 0.04045) ? System.Math.Pow((green + 0.055) / 1.055, 2.4) : (green / 12.92); - double b = (blue > 0.04045) ? System.Math.Pow((blue + 0.055) / 1.055, 2.4) : (blue / 12.92); - - return - new HassiumDictionary(new Dictionary - { - {"x", (r * 0.4124 + g * 0.3576 + b * 0.1805) * 100.0}, - {"y", (r * 0.2126 + g * 0.7152 + b * 0.0722) * 100.0}, - {"z", (r * 0.0193 + g * 0.1192 + b * 0.9505) * 100.0} - }); - } - - public HassiumObject ToLAB(HassiumObject[] args) - { - double[] xyz = ToXYZ(args).HDict().Value.Select(k => k.Value.HDouble().Value).ToArray(); - Func pivot = - n => n > 0.00885645168 ? System.Math.Pow(n, 1.0 / 3.0) : (903.296296 * n + 16) / 116; - double x = pivot(xyz[0] / 95.047); - double y = pivot(xyz[1] / 100.000); - double z = pivot(xyz[2] / 108.883); - - return - new HassiumDictionary(new Dictionary - { - {"l", System.Math.Max(0, 116 * y - 16)}, - {"a", 500 * (x - y)}, - {"b", 200 * (y - z)} - }); - } - - public HassiumObject ToLCH(HassiumObject[] args) - { - double[] lab = ToLAB(args).HDict().Value.Select(k => k.Value.HDouble().Value).ToArray(); - double h = System.Math.Atan2(lab[2], lab[1]); - - if(h > 0) - { - h = (h / System.Math.PI) * 180.0; - } - else - { - h = 360 - (System.Math.Abs(h) / System.Math.PI) * 180.0; - } - - if(h < 0) - { - h += 360.0; - } - else if(h >= 360) - { - h -= 360.0; - } - - return - new HassiumDictionary(new Dictionary - { - {"l", lab[0]}, - {"c", System.Math.Sqrt(lab[1] * lab[1] + lab[2] * lab[2])}, - {"h", h} - }); - } - - public HassiumObject ToLUV(HassiumObject[] args) - { - double[] xyz = ToXYZ(args).HDict().Value.Select(k => k.Value.HDouble().Value).ToArray(); - - var y = xyz[1] / 100.000; - var L = y > 0.00885645168 ? 116.0 * System.Math.Pow(y, 1.0 / 3.0) - 16.0 : 903.296296 * y; - var target = xyz[0] + 15.0 * xyz[1] + 3.0 * xyz[2]; - var reference = 1921.696; - - var xtarget = target == 0 ? 0 : ((4.0 * xyz[0] / target) - (4.0 * 95.047 / reference)); - var ytarget = target == 0 ? 0 : ((9.0 * xyz[1] / target) - (9.0 * 100.000 / reference)); - - return - new HassiumDictionary(new Dictionary - { - {"l", L}, - {"u", 13.0 * L * xtarget}, - {"v", 13.0 * L * ytarget} - }); - } - - public HassiumObject ToHunterLab(HassiumObject[] args) - { - double[] xyz = ToXYZ(args).HDict().Value.Select(k => k.Value.HDouble().Value).ToArray(); - - - return - new HassiumDictionary(new Dictionary - { - {"l", 10.0 * System.Math.Sqrt(xyz[1])}, - {"a", xyz[1] != 0 ? 17.5 * (((1.02 * xyz[0]) - xyz[1]) / System.Math.Sqrt(xyz[1])) : 0}, - {"b", xyz[1] != 0 ? 7.0 * ((xyz[1] - (0.847 * xyz[2])) / System.Math.Sqrt(xyz[1])) : 0} - }); - } + public HassiumObject ToHSL(HassiumObject[] args) + { + var red = Value.R / 255.0; + var green = Value.G / 255.0; + var blue = Value.B / 255.0; + var v = System.Math.Max(System.Math.Max(red, green), blue); + var m = System.Math.Min(System.Math.Min(red, green), blue); + var hue = 0.0; + var saturation = 0.0; + var luminosity = (m + v) / 2.0; + if (luminosity > 0.0) + { + var vm = v - m; + saturation = vm; + if (saturation > 0.0) + { + saturation /= (luminosity <= 0.5) ? (v + m) : (2.0 - v - m); + var r2 = (v - red) / vm; + var g2 = (v - green) / vm; + var b2 = (v - blue) / vm; + if (red == v) + { + hue = (green == m ? 5.0 + b2 : 1.0 - g2); + } + else if (green == v) + { + hue = (blue == m ? 1.0 + r2 : 3.0 - b2); + } + else + { + hue = (red == m ? 3.0 + g2 : 5.0 - r2); + } + hue /= 6.0; + } + } + + return + new HassiumDictionary(new Dictionary + { + {"h", hue * 360}, + {"s", saturation * 100}, + {"l", luminosity * 100} + }); + } + + public HassiumObject ToHSV(HassiumObject[] args) + { + int max = System.Math.Max(Value.R, System.Math.Max(Value.G, Value.B)); + int min = System.Math.Min(Value.R, System.Math.Min(Value.G, Value.B)); + + double hue = Value.GetHue(); + double saturation = (max == 0) ? 0 : 1d - (1d * min / max); + double value = max / 255d; + + return + new HassiumDictionary(new Dictionary + { + {"h", hue}, + {"s", saturation * 100}, + {"v", value * 100} + }); + } + + public HassiumObject ToCMYK(HassiumObject[] args) + { + double red = Value.R / 255d; + double green = Value.G / 255d; + double blue = Value.B / 255d; + + double black = new[] {1 - red, 1 - green, 1 - blue}.Min(); + double cyan = (1 - red - black) / (1 - black); + double magenta = (1 - green - black) / (1 - black); + double yellow = (1 - blue - black) / (1 - black); + + return + new HassiumDictionary(new Dictionary + { + {"c", 100 * cyan}, + {"m", 100 * magenta}, + {"y", 100 * yellow}, + {"k", 100 * black} + }); + } + + public HassiumObject ToCMY(HassiumObject[] args) + { + return + new HassiumDictionary(new Dictionary + { + {"c", (1 - Value.R / 255.0) * 100.0}, + {"m", (1 - Value.G / 255.0) * 100.0}, + {"y", (1 - Value.B / 255.0) * 100.0} + }); + } + + public HassiumObject ToXYZ(HassiumObject[] args) + { + double red = Value.R / 255d; + double green = Value.G / 255d; + double blue = Value.B / 255d; + + double r = (red > 0.04045) ? System.Math.Pow((red + 0.055) / 1.055, 2.4) : (red / 12.92); + double g = (green > 0.04045) ? System.Math.Pow((green + 0.055) / 1.055, 2.4) : (green / 12.92); + double b = (blue > 0.04045) ? System.Math.Pow((blue + 0.055) / 1.055, 2.4) : (blue / 12.92); + + return + new HassiumDictionary(new Dictionary + { + {"x", (r * 0.4124 + g * 0.3576 + b * 0.1805) * 100.0}, + {"y", (r * 0.2126 + g * 0.7152 + b * 0.0722) * 100.0}, + {"z", (r * 0.0193 + g * 0.1192 + b * 0.9505) * 100.0} + }); + } + + public HassiumObject ToLAB(HassiumObject[] args) + { + double[] xyz = ToXYZ(args).HDict().Value.Select(k => k.Value.HDouble().Value).ToArray(); + Func pivot = + n => n > 0.00885645168 ? System.Math.Pow(n, 1.0 / 3.0) : (903.296296 * n + 16) / 116; + double x = pivot(xyz[0] / 95.047); + double y = pivot(xyz[1] / 100.000); + double z = pivot(xyz[2] / 108.883); + + return + new HassiumDictionary(new Dictionary + { + {"l", System.Math.Max(0, 116 * y - 16)}, + {"a", 500 * (x - y)}, + {"b", 200 * (y - z)} + }); + } + + public HassiumObject ToLCH(HassiumObject[] args) + { + double[] lab = ToLAB(args).HDict().Value.Select(k => k.Value.HDouble().Value).ToArray(); + double h = System.Math.Atan2(lab[2], lab[1]); + + if (h > 0) + { + h = (h / System.Math.PI) * 180.0; + } + else + { + h = 360 - (System.Math.Abs(h) / System.Math.PI) * 180.0; + } + + if (h < 0) + { + h += 360.0; + } + else if (h >= 360) + { + h -= 360.0; + } + + return + new HassiumDictionary(new Dictionary + { + {"l", lab[0]}, + {"c", System.Math.Sqrt(lab[1] * lab[1] + lab[2] * lab[2])}, + {"h", h} + }); + } + + public HassiumObject ToLUV(HassiumObject[] args) + { + double[] xyz = ToXYZ(args).HDict().Value.Select(k => k.Value.HDouble().Value).ToArray(); + + var y = xyz[1] / 100.000; + var L = y > 0.00885645168 ? 116.0 * System.Math.Pow(y, 1.0 / 3.0) - 16.0 : 903.296296 * y; + var target = xyz[0] + 15.0 * xyz[1] + 3.0 * xyz[2]; + var reference = 1921.696; + + var xtarget = target == 0 ? 0 : ((4.0 * xyz[0] / target) - (4.0 * 95.047 / reference)); + var ytarget = target == 0 ? 0 : ((9.0 * xyz[1] / target) - (9.0 * 100.000 / reference)); + + return + new HassiumDictionary(new Dictionary + { + {"l", L}, + {"u", 13.0 * L * xtarget}, + {"v", 13.0 * L * ytarget} + }); + } + + public HassiumObject ToHunterLab(HassiumObject[] args) + { + double[] xyz = ToXYZ(args).HDict().Value.Select(k => k.Value.HDouble().Value).ToArray(); + + + return + new HassiumDictionary(new Dictionary + { + {"l", 10.0 * System.Math.Sqrt(xyz[1])}, + {"a", xyz[1] != 0 ? 17.5 * (((1.02 * xyz[0]) - xyz[1]) / System.Math.Sqrt(xyz[1])) : 0}, + {"b", xyz[1] != 0 ? 7.0 * ((xyz[1] - (0.847 * xyz[2])) / System.Math.Sqrt(xyz[1])) : 0} + }); + } /*public HassiumObject ToYUV(HassiumObject[] args) { @@ -554,4 +554,4 @@ public HassiumObject ToHunterLab(HassiumObject[] args) }); }*/ } -} +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/Drawing/HassiumImage.cs b/src/Hassium/HassiumObjects/Drawing/HassiumImage.cs index 39441e0..a5f5df4 100644 --- a/src/Hassium/HassiumObjects/Drawing/HassiumImage.cs +++ b/src/Hassium/HassiumObjects/Drawing/HassiumImage.cs @@ -11,7 +11,7 @@ namespace Hassium.HassiumObjects.Drawing { - public class HassiumImage: HassiumObject + public class HassiumImage : HassiumObject { public Image Value { get; private set; } @@ -31,9 +31,9 @@ private HassiumObject dispose(HassiumObject[] args) private HassiumObject save(HassiumObject[] args) { - Value.Save(((HassiumString)args[0]).ToString()); + Value.Save(((HassiumString) args[0]).ToString()); return null; } } -} +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/HassiumClass.cs b/src/Hassium/HassiumObjects/HassiumClass.cs index 514c8b9..7fd0433 100644 --- a/src/Hassium/HassiumObjects/HassiumClass.cs +++ b/src/Hassium/HassiumObjects/HassiumClass.cs @@ -6,7 +6,7 @@ namespace Hassium { - public class HassiumClass: HassiumObject + public class HassiumClass : HassiumObject { public HassiumClass(ClassNode value, Interpreter.Interpreter interpreter) { @@ -31,11 +31,10 @@ KeyValuePair attrib in interpreter.SymbolTable.ChildScopes[((ClassNode) value).Name + "." + fnode.Name], this)); } - foreach(var pnode in value.Children[0].Children.OfType().Select(node => node)) + foreach (var pnode in value.Children[0].Children.OfType().Select(node => node)) { - SetAttribute(pnode.Name, (HassiumProperty)pnode.Visit(interpreter)); + SetAttribute(pnode.Name, (HassiumProperty) pnode.Visit(interpreter)); } } } -} - +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/HassiumModule.cs b/src/Hassium/HassiumObjects/HassiumModule.cs index b8db161..cdf650e 100644 --- a/src/Hassium/HassiumObjects/HassiumModule.cs +++ b/src/Hassium/HassiumObjects/HassiumModule.cs @@ -8,9 +8,10 @@ namespace Hassium.HassiumObjects public class HassiumModule : HassiumObject { public string Name { get; private set; } + public HassiumModule(string name) { Name = name; } } -} +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/HassiumObject.cs b/src/Hassium/HassiumObjects/HassiumObject.cs index bfc8538..bd5c66e 100644 --- a/src/Hassium/HassiumObjects/HassiumObject.cs +++ b/src/Hassium/HassiumObjects/HassiumObject.cs @@ -35,8 +35,8 @@ public void SetAttribute(string name, HassiumObject value) } if (_attributes.ContainsKey(name) && _attributes[name] is HassiumProperty) { - var prop = ((HassiumProperty)_attributes[name]); - if(prop.ReadOnly) throw new ParseException("The property " + prop.Name + " is read-only", -1); + var prop = ((HassiumProperty) _attributes[name]); + if (prop.ReadOnly) throw new ParseException("The property " + prop.Name + " is read-only", -1); prop.SetValue(this, value); } else _attributes[name] = value; @@ -44,14 +44,21 @@ public void SetAttribute(string name, HassiumObject value) public HassiumObject GetAttribute(string name, int pos) { - if(!_attributes.ContainsKey(name)) throw new ParseException("The attribute '" + name + "' doesn't exist for the specified object.", pos); - if (_attributes.ContainsKey(name) && _attributes[name] is HassiumProperty) return ((HassiumProperty)_attributes[name]).GetValue(this); + if (!_attributes.ContainsKey(name)) + throw new ParseException("The attribute '" + name + "' doesn't exist for the specified object.", pos); + if (_attributes.ContainsKey(name) && _attributes[name] is HassiumProperty) + return ((HassiumProperty) _attributes[name]).GetValue(this); else return _attributes[name]; } public override string ToString() { - if (Attributes.Any(x => x.Key == "toString" && (x.Value is InternalFunction && ((InternalFunction)x.Value).Arguments.Length == 0))) return GetAttribute("toString", -1).Invoke(); + if ( + Attributes.Any( + x => + x.Key == "toString" && + (x.Value is InternalFunction && ((InternalFunction) x.Value).Arguments.Length == 0))) + return GetAttribute("toString", -1).Invoke(); return ""; } @@ -61,6 +68,7 @@ public virtual HassiumObject Invoke(params HassiumObject[] args) } #region Cast + public static implicit operator int(HassiumObject obj) { return ((HassiumInt) obj).Value; @@ -71,9 +79,9 @@ public static implicit operator HassiumObject(int i) return new HassiumInt(i); } - public static implicit operator double (HassiumObject obj) + public static implicit operator double(HassiumObject obj) { - return ((HassiumDouble)obj).Value; + return ((HassiumDouble) obj).Value; } public static implicit operator HassiumObject(double d) @@ -81,7 +89,7 @@ public static implicit operator HassiumObject(double d) return new HassiumDouble(d); } - public static implicit operator string (HassiumObject obj) + public static implicit operator string(HassiumObject obj) { return obj.ToString(); } @@ -91,7 +99,7 @@ public static implicit operator HassiumObject(string s) return new HassiumString(s); } - public static implicit operator char (HassiumObject obj) + public static implicit operator char(HassiumObject obj) { return ((HassiumChar) obj).Value; } @@ -101,9 +109,9 @@ public static implicit operator HassiumObject(char s) return new HassiumChar(s); } - public static implicit operator object[] (HassiumObject obj) + public static implicit operator object[](HassiumObject obj) { - return ((HassiumArray)obj).Value.Cast().ToArray(); + return ((HassiumArray) obj).Value.Cast().ToArray(); } public static implicit operator Dictionary(HassiumObject obj) @@ -118,7 +126,7 @@ public static implicit operator HassiumObject(Dictionary(HassiumObject obj) { - return ((HassiumDictionary)obj).Value.ToDictionary(x => (object)x.Key, x => (object)x.Value); + return ((HassiumDictionary) obj).Value.ToDictionary(x => (object) x.Key, x => (object) x.Value); } public static implicit operator HassiumObject(Dictionary dict) @@ -148,15 +156,16 @@ public static implicit operator HassiumObject(object[] arr) public static HassiumObject ToHassiumObject(object fv) { - if (fv is double) return new HassiumDouble((double)fv); - if (fv is int) return new HassiumDouble((int)fv); - if (fv is string) return new HassiumString((string)fv); - if(fv is char) return new HassiumChar((char)fv); - if (fv is Array) return new HassiumArray((Array)fv); - if (fv is IDictionary) return new HassiumDictionary((IDictionary)fv); - if (fv is bool) return new HassiumBool((bool)fv); - if (fv is KeyValuePair) return HKvp((KeyValuePair)fv); - else return (HassiumObject)(object)fv; + if (fv is double) return new HassiumDouble((double) fv); + if (fv is int) return new HassiumDouble((int) fv); + if (fv is string) return new HassiumString((string) fv); + if (fv is char) return new HassiumChar((char) fv); + if (fv is Array) return new HassiumArray((Array) fv); + if (fv is IDictionary) return new HassiumDictionary((IDictionary) fv); + if (fv is bool) return new HassiumBool((bool) fv); + if (fv is KeyValuePair) + return HKvp((KeyValuePair) fv); + else return (HassiumObject) (object) fv; } public KeyValuePair HKvp(HassiumKeyValuePair kvp) @@ -171,17 +180,17 @@ public static HassiumKeyValuePair HKvp(KeyValuePair GetBackground(new HassiumObject[] {}), SetBackground)); - Attributes.Add("foregroundColor", new HassiumProperty("foregroundColor", x => GetForeground(new HassiumObject[] { }), SetForeground)); + Attributes.Add("beep", new InternalFunction(Beep, new[] {0, 2})); + Attributes.Add("backgroundColor", + new HassiumProperty("backgroundColor", x => GetBackground(new HassiumObject[] {}), SetBackground)); + Attributes.Add("foregroundColor", + new HassiumProperty("foregroundColor", x => GetForeground(new HassiumObject[] {}), SetForeground)); Attributes.Add("title", new HassiumProperty("title", x => Console.Title, x => { Console.Title = x[0].ToString(); @@ -82,10 +84,10 @@ public HassiumConsole() /// Nothing public HassiumObject SetForeground(HassiumObject[] args) { - Console.ForegroundColor = parseColor(args[0].ToString()); return null; } + /// /// Sets the console background color. /// @@ -148,6 +150,7 @@ public HassiumObject SetBackground(HassiumObject[] args) Console.BackgroundColor = parseColor(args[0].ToString()); return null; } + /// /// Gets the console foreground color. /// @@ -208,6 +211,7 @@ public HassiumObject GetForeground(HassiumObject[] args) { return Console.ForegroundColor.ToString(); } + /// /// Gets the console background color. /// @@ -272,9 +276,12 @@ public HassiumObject GetBackground(HassiumObject[] args) public HassiumObject GetCursorPos(HassiumObject[] args) { var ret = new HassiumObject(); - ret.SetAttribute("left", new HassiumProperty("left", x => Console.CursorLeft, x => Console.CursorLeft = x[1].HInt().Value)); - ret.SetAttribute("top", new HassiumProperty("top", x => Console.CursorTop, x => Console.CursorTop = x[1].HInt().Value)); - ret.SetAttribute("toString", new InternalFunction(x => "{" + Console.CursorLeft + ", " + Console.CursorTop + "}", 0)); + ret.SetAttribute("left", + new HassiumProperty("left", x => Console.CursorLeft, x => Console.CursorLeft = x[1].HInt().Value)); + ret.SetAttribute("top", + new HassiumProperty("top", x => Console.CursorTop, x => Console.CursorTop = x[1].HInt().Value)); + ret.SetAttribute("toString", + new InternalFunction(x => "{" + Console.CursorLeft + ", " + Console.CursorTop + "}", 0)); return ret; } @@ -339,4 +346,4 @@ private ConsoleColor parseColor(string color) } } } -} +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/IO/HassiumDirectory.cs b/src/Hassium/HassiumObjects/IO/HassiumDirectory.cs index 277111e..41545e9 100644 --- a/src/Hassium/HassiumObjects/IO/HassiumDirectory.cs +++ b/src/Hassium/HassiumObjects/IO/HassiumDirectory.cs @@ -63,7 +63,7 @@ public HassiumObject Copy(HassiumObject[] args) public HassiumObject Move(HassiumObject[] args) { - Copy(new HassiumObject[]{args[0], args[1].ToString()}); + Copy(new HassiumObject[] {args[0], args[1].ToString()}); Directory.Delete(args[0], true); args[0] = args[1].ToString(); return null; @@ -72,7 +72,7 @@ public HassiumObject Move(HassiumObject[] args) public HassiumObject Rename(HassiumObject[] args) { var dname = Path.GetDirectoryName(args[0]); - if(dname != null) Move(new HassiumObject[] { args[0], Path.Combine(dname, args[1].ToString())}); + if (dname != null) Move(new HassiumObject[] {args[0], Path.Combine(dname, args[1].ToString())}); return null; } @@ -99,19 +99,19 @@ public HassiumObject GetLastWriteTime(HassiumObject[] args) public HassiumObject SetLastAccessTime(HassiumObject[] args) { - Directory.SetLastAccessTime(args[0].ToString(), ((HassiumDate)args[1]).Value); + Directory.SetLastAccessTime(args[0].ToString(), ((HassiumDate) args[1]).Value); return null; } public HassiumObject SetLastWriteTime(HassiumObject[] args) { - Directory.SetLastWriteTime(args[0].ToString(), ((HassiumDate)args[1]).Value); + Directory.SetLastWriteTime(args[0].ToString(), ((HassiumDate) args[1]).Value); return null; } public HassiumObject SetCreationTime(HassiumObject[] args) { - Directory.SetCreationTime(args[0].ToString(), ((HassiumDate)args[1]).Value); + Directory.SetCreationTime(args[0].ToString(), ((HassiumDate) args[1]).Value); return null; } @@ -147,4 +147,4 @@ public HassiumObject GetDirectories(HassiumObject[] args) return Directory.GetDirectories(args[0].ToString()); } } -} +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/IO/HassiumFile.cs b/src/Hassium/HassiumObjects/IO/HassiumFile.cs index de39672..0790afd 100644 --- a/src/Hassium/HassiumObjects/IO/HassiumFile.cs +++ b/src/Hassium/HassiumObjects/IO/HassiumFile.cs @@ -11,7 +11,6 @@ public class HassiumFile : HassiumObject //public string FilePath { get; set; } ///public bool Exists { get { return File.Exists(FilePath); } } - public HassiumFile() { Attributes.Add("writeText", new InternalFunction(PutContent, 2)); @@ -25,7 +24,7 @@ public HassiumFile() Attributes.Add("move", new InternalFunction(Move, 2)); Attributes.Add("rename", new InternalFunction(Rename, 2)); Attributes.Add("deleteFile", new InternalFunction(DeleteFile, 1)); - + Attributes.Add("createText", new InternalFunction(createText, 1)); Attributes.Add("openText", new InternalFunction(openText, 1)); Attributes.Add("getCreationTime", new InternalFunction(getCreationTime, 1)); @@ -34,7 +33,7 @@ public HassiumFile() Attributes.Add("setLastAccessTime", new InternalFunction(setLastWriteTime, 2)); Attributes.Add("setCreationTime", new InternalFunction(setCreationTime, 2)); } - + public HassiumObject Create(HassiumObject[] args) { @@ -62,7 +61,7 @@ public HassiumObject Exists(HassiumObject[] args) public HassiumObject Rename(HassiumObject[] args) { var dname = Path.GetDirectoryName(args[0].ToString()); - if(dname != null) File.Move(args[0].ToString(), Path.Combine(dname, args[1].ToString())); + if (dname != null) File.Move(args[0].ToString(), Path.Combine(dname, args[1].ToString())); return null; } @@ -100,7 +99,6 @@ public HassiumObject DeleteFile(HassiumObject[] args) return null; } - public HassiumObject createText(HassiumObject[] args) { @@ -129,20 +127,20 @@ public HassiumObject getLastWriteTime(HassiumObject[] args) public HassiumObject setLastAccessTime(HassiumObject[] args) { - File.SetLastAccessTime(args[0].ToString(), ((HassiumDate)args[1]).Value); + File.SetLastAccessTime(args[0].ToString(), ((HassiumDate) args[1]).Value); return null; } public HassiumObject setLastWriteTime(HassiumObject[] args) { - File.SetLastWriteTime(args[0].ToString(), ((HassiumDate)args[1]).Value); + File.SetLastWriteTime(args[0].ToString(), ((HassiumDate) args[1]).Value); return null; } public HassiumObject setCreationTime(HassiumObject[] args) { - File.SetCreationTime(args[0].ToString(), ((HassiumDate)args[1]).Value); + File.SetCreationTime(args[0].ToString(), ((HassiumDate) args[1]).Value); return null; } } -} +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/IO/HassiumFileStream.cs b/src/Hassium/HassiumObjects/IO/HassiumFileStream.cs index ec80c3d..55eed62 100644 --- a/src/Hassium/HassiumObjects/IO/HassiumFileStream.cs +++ b/src/Hassium/HassiumObjects/IO/HassiumFileStream.cs @@ -3,11 +3,11 @@ namespace Hassium.HassiumObjects.IO { - public class HassiumFileStream: HassiumStream + public class HassiumFileStream : HassiumStream { public new FileStream Value { - get { return (FileStream)base.Value; } + get { return (FileStream) base.Value; } set { base.Value = value; } } @@ -21,5 +21,4 @@ public HassiumObject ReadChar(HassiumObject[] args) return ((char) Value.ReadByte()).ToString(); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/IO/HassiumIO.cs b/src/Hassium/HassiumObjects/IO/HassiumIO.cs index aec2a68..81cfcc5 100644 --- a/src/Hassium/HassiumObjects/IO/HassiumIO.cs +++ b/src/Hassium/HassiumObjects/IO/HassiumIO.cs @@ -15,4 +15,4 @@ public HassiumIO() Attributes.Add("stderr", new InternalFunction(x => new HassiumStream(Console.OpenStandardError()), 0, true)); } } -} +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/IO/HassiumPath.cs b/src/Hassium/HassiumObjects/IO/HassiumPath.cs index 341b5a8..316b3d7 100644 --- a/src/Hassium/HassiumObjects/IO/HassiumPath.cs +++ b/src/Hassium/HassiumObjects/IO/HassiumPath.cs @@ -4,7 +4,7 @@ namespace Hassium.HassiumObjects.IO { - public class HassiumPath: HassiumObject + public class HassiumPath : HassiumObject { public HassiumPath() { @@ -76,5 +76,4 @@ public HassiumObject isPathRooted(HassiumObject[] args) return new HassiumBool(Path.IsPathRooted(args[0].ToString())); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/IO/HassiumStream.cs b/src/Hassium/HassiumObjects/IO/HassiumStream.cs index 8f5531b..849ff3b 100644 --- a/src/Hassium/HassiumObjects/IO/HassiumStream.cs +++ b/src/Hassium/HassiumObjects/IO/HassiumStream.cs @@ -1,4 +1,3 @@ - using System.IO; using System.Linq; using Hassium.Functions; @@ -6,7 +5,7 @@ namespace Hassium.HassiumObjects.IO { - public class HassiumStream: HassiumObject + public class HassiumStream : HassiumObject { public Stream Value { get; protected set; } @@ -22,7 +21,7 @@ public HassiumStream(Stream value) Attributes.Add("flush", new InternalFunction(Flush, 0)); Attributes.Add("close", new InternalFunction(Close, 0)); Attributes.Add("read", new InternalFunction(Read, 0)); - Attributes.Add("seek", new InternalFunction(Seek, new []{0,1})); + Attributes.Add("seek", new InternalFunction(Seek, new[] {0, 1})); Attributes.Add("write", new InternalFunction(Write, 1)); } @@ -55,7 +54,7 @@ public HassiumObject Write(HassiumObject[] args) var buffer = args[0].ToString().ToCharArray().Select(x => (byte) x).ToArray(); Value.Write(buffer, 0, buffer.Length); } - else Value.WriteByte((byte)args[0].HDouble().ValueInt); + else Value.WriteByte((byte) args[0].HDouble().ValueInt); return null; } } diff --git a/src/Hassium/HassiumObjects/IO/HassiumStreamReader.cs b/src/Hassium/HassiumObjects/IO/HassiumStreamReader.cs index 7b70575..adb8ed0 100644 --- a/src/Hassium/HassiumObjects/IO/HassiumStreamReader.cs +++ b/src/Hassium/HassiumObjects/IO/HassiumStreamReader.cs @@ -2,9 +2,9 @@ using Hassium.Functions; using Hassium.HassiumObjects.Types; -namespace Hassium.HassiumObjects.IO +namespace Hassium.HassiumObjects.IO { - public class HassiumStreamReader: HassiumObject + public class HassiumStreamReader : HassiumObject { public StreamReader Value { get; private set; } @@ -38,12 +38,12 @@ private HassiumObject dispose(HassiumObject[] args) private HassiumObject peek(HassiumObject[] args) { - return new HassiumChar(((char)Value.Peek())); + return new HassiumChar(((char) Value.Peek())); } private HassiumObject read(HassiumObject[] args) { - return new HassiumChar(((char)Value.Read())); + return new HassiumChar(((char) Value.Read())); } private HassiumObject readToEnd(HassiumObject[] args) @@ -51,5 +51,4 @@ private HassiumObject readToEnd(HassiumObject[] args) return new HassiumString(Value.ReadToEnd()); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/IO/HassiumStreamWriter.cs b/src/Hassium/HassiumObjects/IO/HassiumStreamWriter.cs index 0be1c0a..00c8543 100644 --- a/src/Hassium/HassiumObjects/IO/HassiumStreamWriter.cs +++ b/src/Hassium/HassiumObjects/IO/HassiumStreamWriter.cs @@ -3,7 +3,7 @@ namespace Hassium.HassiumObjects.IO { - public class HassiumStreamWriter: HassiumObject + public class HassiumStreamWriter : HassiumObject { public StreamWriter Value { get; private set; } @@ -47,5 +47,4 @@ private HassiumObject writeLine(HassiumObject[] args) return null; } } -} - +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/Interactive/HassiumDebug.cs b/src/Hassium/HassiumObjects/Interactive/HassiumDebug.cs index 931ba08..d75b684 100644 --- a/src/Hassium/HassiumObjects/Interactive/HassiumDebug.cs +++ b/src/Hassium/HassiumObjects/Interactive/HassiumDebug.cs @@ -9,20 +9,28 @@ namespace Hassium.HassiumObjects.Debug { - public class HassiumDebug: HassiumObject + public class HassiumDebug : HassiumObject { public HassiumDebug() { - Attributes.Add("localVariables", new HassiumProperty("localVariables", x => localVariables(new HassiumObject[] {}), null, true)); - Attributes.Add("globalVariables", new HassiumProperty("globalVariables", x => globalVariables(new HassiumObject[] { }), null, true)); - Attributes.Add("fileName", new HassiumProperty("fileName", x => HassiumInterpreter.options.FilePath, null, true)); - Attributes.Add("secureMode", new HassiumProperty("secureMode", x => HassiumInterpreter.options.Secure, null, true)); - Attributes.Add("sourceCode", new HassiumProperty("sourceCode", x => HassiumInterpreter.options.Code, null, true)); + Attributes.Add("localVariables", + new HassiumProperty("localVariables", x => localVariables(new HassiumObject[] {}), null, true)); + Attributes.Add("globalVariables", + new HassiumProperty("globalVariables", x => globalVariables(new HassiumObject[] {}), null, true)); + Attributes.Add("fileName", + new HassiumProperty("fileName", x => HassiumInterpreter.options.FilePath, null, true)); + Attributes.Add("secureMode", + new HassiumProperty("secureMode", x => HassiumInterpreter.options.Secure, null, true)); + Attributes.Add("sourceCode", + new HassiumProperty("sourceCode", x => HassiumInterpreter.options.Code, null, true)); } private HassiumObject localVariables(HassiumObject[] args) { - return new HassiumDictionary(HassiumInterpreter.CurrentInterpreter.CallStack.SelectMany(x => x.Locals).ToDictionary(x => new HassiumString(x.Key), x => x.Value)); + return + new HassiumDictionary( + HassiumInterpreter.CurrentInterpreter.CallStack.SelectMany(x => x.Locals) + .ToDictionary(x => new HassiumString(x.Key), x => x.Value)); } private HassiumObject globalVariables(HassiumObject[] args) @@ -36,5 +44,4 @@ private HassiumObject globalVariables(HassiumObject[] args) return new HassiumDictionary(res.ToDictionary(x => new HassiumString(x.Key), x => x.Value)); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/Math/HassiumMath.cs b/src/Hassium/HassiumObjects/Math/HassiumMath.cs index cd24233..e32f56c 100644 --- a/src/Hassium/HassiumObjects/Math/HassiumMath.cs +++ b/src/Hassium/HassiumObjects/Math/HassiumMath.cs @@ -8,7 +8,7 @@ namespace Hassium.HassiumObjects.Math { - public class HassiumMath: HassiumObject + public class HassiumMath : HassiumObject { public HassiumMath() { @@ -32,7 +32,7 @@ public HassiumMath() Attributes.Add("log10", new InternalFunction(Log10, 1)); Attributes.Add("max", new InternalFunction(Max, 2)); Attributes.Add("min", new InternalFunction(Min, 2)); - Attributes.Add("round", new InternalFunction(Round, new []{1,2})); + Attributes.Add("round", new InternalFunction(Round, new[] {1, 2})); Attributes.Add("sin", new InternalFunction(Sin, 1)); Attributes.Add("sinh", new InternalFunction(Sinh, 1)); Attributes.Add("tan", new InternalFunction(Tan, 1)); @@ -42,7 +42,8 @@ public HassiumMath() public HassiumObject Hash(HassiumObject[] args) { byte[] encodedText = new UTF8Encoding().GetBytes(args[1].ToString()); - byte[] hash = ((HashAlgorithm)CryptoConfig.CreateFromName(args[0].ToString().ToUpper())).ComputeHash(encodedText); + byte[] hash = + ((HashAlgorithm) CryptoConfig.CreateFromName(args[0].ToString().ToUpper())).ComputeHash(encodedText); return BitConverter.ToString(hash).Replace("-", string.Empty).ToLower(); } @@ -128,12 +129,14 @@ public HassiumObject Max(HassiumObject[] args) public HassiumObject Min(HassiumObject[] args) { - return new HassiumDouble(System.Math.Min(args[0].HDouble().Value, ((HassiumDouble)args[1].HDouble().Value))); + return new HassiumDouble(System.Math.Min(args[0].HDouble().Value, ((HassiumDouble) args[1].HDouble().Value))); } public HassiumObject Round(HassiumObject[] args) { - return args.Count() > 1 ? new HassiumDouble(System.Math.Round(args[0].HDouble().Value, args[1].HInt().Value)) : new HassiumDouble(System.Math.Round(args[0].HDouble().Value)); + return args.Count() > 1 + ? new HassiumDouble(System.Math.Round(args[0].HDouble().Value, args[1].HInt().Value)) + : new HassiumDouble(System.Math.Round(args[0].HDouble().Value)); } public HassiumObject Sin(HassiumObject[] args) @@ -156,4 +159,4 @@ public HassiumObject Tanh(HassiumObject[] args) return new HassiumDouble(System.Math.Tanh(args[0].HDouble().Value)); } } -} +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/Networking/HTTP/HassiumHttpListener.cs b/src/Hassium/HassiumObjects/Networking/HTTP/HassiumHttpListener.cs index bb975bf..a4d2277 100644 --- a/src/Hassium/HassiumObjects/Networking/HTTP/HassiumHttpListener.cs +++ b/src/Hassium/HassiumObjects/Networking/HTTP/HassiumHttpListener.cs @@ -3,7 +3,7 @@ namespace Hassium.HassiumObjects.Networking.HTTP { - public class HassiumHttpListener: HassiumObject + public class HassiumHttpListener : HassiumObject { public HttpListener Value { get; private set; } @@ -53,5 +53,4 @@ private HassiumObject getContext(HassiumObject[] args) return new HassiumHttpListenerContext(Value.GetContext()); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/Networking/HTTP/HassiumHttpListenerContext.cs b/src/Hassium/HassiumObjects/Networking/HTTP/HassiumHttpListenerContext.cs index 09559a1..bc7888e 100644 --- a/src/Hassium/HassiumObjects/Networking/HTTP/HassiumHttpListenerContext.cs +++ b/src/Hassium/HassiumObjects/Networking/HTTP/HassiumHttpListenerContext.cs @@ -3,7 +3,7 @@ namespace Hassium.HassiumObjects.Networking.HTTP { - public class HassiumHttpListenerContext: HassiumObject + public class HassiumHttpListenerContext : HassiumObject { public HttpListenerContext Value { get; private set; } @@ -24,5 +24,4 @@ private HassiumObject response(HassiumObject[] args) return new HassiumHttpListenerResponse(Value.Response); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/Networking/HTTP/HassiumHttpListenerRequest.cs b/src/Hassium/HassiumObjects/Networking/HTTP/HassiumHttpListenerRequest.cs index b77143a..982d67f 100644 --- a/src/Hassium/HassiumObjects/Networking/HTTP/HassiumHttpListenerRequest.cs +++ b/src/Hassium/HassiumObjects/Networking/HTTP/HassiumHttpListenerRequest.cs @@ -5,7 +5,7 @@ namespace Hassium.HassiumObjects.Networking.HTTP { - public class HassiumHttpListenerRequest: HassiumObject + public class HassiumHttpListenerRequest : HassiumObject { public HttpListenerRequest Value { get; private set; } @@ -68,5 +68,4 @@ private HassiumObject userAgent(HassiumObject[] args) return Value.UserAgent; } } -} - +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/Networking/HTTP/HassiumHttpListenerResponse.cs b/src/Hassium/HassiumObjects/Networking/HTTP/HassiumHttpListenerResponse.cs index d3ca2d7..59027b9 100644 --- a/src/Hassium/HassiumObjects/Networking/HTTP/HassiumHttpListenerResponse.cs +++ b/src/Hassium/HassiumObjects/Networking/HTTP/HassiumHttpListenerResponse.cs @@ -7,7 +7,7 @@ namespace Hassium.HassiumObjects.Networking.HTTP { - public class HassiumHttpListenerResponse: HassiumObject + public class HassiumHttpListenerResponse : HassiumObject { public HttpListenerResponse Value { get; private set; } @@ -22,11 +22,11 @@ public HassiumHttpListenerResponse(HttpListenerResponse value) return null; })); Attributes.Add("contentLength", new HassiumProperty("contentLength", x => value.ContentLength64, - x => - { - value.ContentLength64 = x[0].HInt().Value; - return null; - })); + x => + { + value.ContentLength64 = x[0].HInt().Value; + return null; + })); Attributes.Add("outputStream", new InternalFunction(outputStream, 0, true)); Attributes.Add("abort", new InternalFunction(abort, 0)); Attributes.Add("appendHeader", new InternalFunction(appendHeader, 2)); @@ -63,5 +63,4 @@ private HassiumObject redirect(HassiumObject[] args) return null; } } -} - +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/Networking/HassiumNetworkStream.cs b/src/Hassium/HassiumObjects/Networking/HassiumNetworkStream.cs index 2ef0b3a..d71b046 100644 --- a/src/Hassium/HassiumObjects/Networking/HassiumNetworkStream.cs +++ b/src/Hassium/HassiumObjects/Networking/HassiumNetworkStream.cs @@ -4,7 +4,7 @@ namespace Hassium.HassiumObjects.Networking { - public class HassiumNetworkStream: HassiumStream + public class HassiumNetworkStream : HassiumStream { public new NetworkStream Value { @@ -15,7 +15,5 @@ public class HassiumNetworkStream: HassiumStream public HassiumNetworkStream(Stream s) : base(s) { } - } -} - +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/Networking/HassiumSocket.cs b/src/Hassium/HassiumObjects/Networking/HassiumSocket.cs index c979ad0..4481ff5 100644 --- a/src/Hassium/HassiumObjects/Networking/HassiumSocket.cs +++ b/src/Hassium/HassiumObjects/Networking/HassiumSocket.cs @@ -6,7 +6,7 @@ namespace Hassium.HassiumObjects.Networking { - public class HassiumSocket: HassiumObject + public class HassiumSocket : HassiumObject { public Socket Value { get; private set; } @@ -73,5 +73,4 @@ private HassiumObject send(HassiumObject[] args) return Value.Send(Encoding.ASCII.GetBytes(args[0].ToString())); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/Networking/HassiumTcpClient.cs b/src/Hassium/HassiumObjects/Networking/HassiumTcpClient.cs index 3471599..e788618 100644 --- a/src/Hassium/HassiumObjects/Networking/HassiumTcpClient.cs +++ b/src/Hassium/HassiumObjects/Networking/HassiumTcpClient.cs @@ -5,7 +5,7 @@ namespace Hassium.HassiumObjects.Networking { - public class HassiumTcpClient: HassiumObject + public class HassiumTcpClient : HassiumObject { public TcpClient Value { get; private set; } @@ -36,5 +36,4 @@ private HassiumObject close(HassiumObject[] args) return null; } } -} - +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/Networking/HassiumWebClient.cs b/src/Hassium/HassiumObjects/Networking/HassiumWebClient.cs index 26c0c9c..1e97faa 100644 --- a/src/Hassium/HassiumObjects/Networking/HassiumWebClient.cs +++ b/src/Hassium/HassiumObjects/Networking/HassiumWebClient.cs @@ -5,7 +5,7 @@ namespace Hassium.HassiumObjects.Networking { - public class HassiumWebClient: HassiumObject + public class HassiumWebClient : HassiumObject { public WebClient Value { get; private set; } @@ -32,7 +32,9 @@ private HassiumObject upfile(HassiumObject[] args) { ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; Value.Headers.Add("Content-Type", "binary/octet-stream"); - return new HassiumString(Encoding.ASCII.GetString(Value.UploadFile(args[0].ToString(), "POST", args[1].ToString()))); + return + new HassiumString( + Encoding.ASCII.GetString(Value.UploadFile(args[0].ToString(), "POST", args[1].ToString()))); } public override string ToString() @@ -40,5 +42,4 @@ public override string ToString() return Value.ToString(); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/Random/HassiumRandom.cs b/src/Hassium/HassiumObjects/Random/HassiumRandom.cs index 766c106..80490d6 100644 --- a/src/Hassium/HassiumObjects/Random/HassiumRandom.cs +++ b/src/Hassium/HassiumObjects/Random/HassiumRandom.cs @@ -3,14 +3,14 @@ namespace Hassium.HassiumObjects.Random { - public class HassiumRandom: HassiumObject + public class HassiumRandom : HassiumObject { public System.Random Value { get; private set; } public HassiumRandom(System.Random value) { Value = value; - Attributes.Add("next", new InternalFunction(next, new []{0,1,2})); + Attributes.Add("next", new InternalFunction(next, new[] {0, 1, 2})); Attributes.Add("nextDouble", new InternalFunction(nextDouble, 0)); Attributes.Add("toString", new InternalFunction(toString, 0)); } @@ -38,5 +38,4 @@ private HassiumObject toString(HassiumObject[] args) return Value.ToString(); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/Reflection/HassiumAssembly.cs b/src/Hassium/HassiumObjects/Reflection/HassiumAssembly.cs index 223237f..075abd0 100644 --- a/src/Hassium/HassiumObjects/Reflection/HassiumAssembly.cs +++ b/src/Hassium/HassiumObjects/Reflection/HassiumAssembly.cs @@ -6,7 +6,7 @@ namespace Hassium.HassiumObjects.Reflection { - public class HassiumAssembly: HassiumObject + public class HassiumAssembly : HassiumObject { public Assembly Value { get; private set; } @@ -53,5 +53,4 @@ private HassiumObject toString(HassiumObject[] args) return Value.ToString(); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/Reflection/HassiumModule.cs b/src/Hassium/HassiumObjects/Reflection/HassiumModule.cs index 313838f..d5c01fc 100644 --- a/src/Hassium/HassiumObjects/Reflection/HassiumModule.cs +++ b/src/Hassium/HassiumObjects/Reflection/HassiumModule.cs @@ -4,7 +4,7 @@ namespace Hassium.HassiumObjects.Reflection { - public class HassiumModule: HassiumObject + public class HassiumModule : HassiumObject { public Module Value { get; private set; } @@ -14,8 +14,8 @@ public HassiumModule(Module module) Attributes.Add("assembly", new InternalFunction(x => new HassiumAssembly(Value.Assembly), 0, true)); Attributes.Add("name", new InternalFunction(x => new HassiumString(Value.Name), 0, true)); Attributes.Add("scopeName", new InternalFunction(x => new HassiumString(Value.ScopeName), 0, true)); - Attributes.Add("fullyQualifiedName", new InternalFunction(x => new HassiumString(Value.FullyQualifiedName), 0, true)); + Attributes.Add("fullyQualifiedName", + new InternalFunction(x => new HassiumString(Value.FullyQualifiedName), 0, true)); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/Text/HassiumEncoding.cs b/src/Hassium/HassiumObjects/Text/HassiumEncoding.cs index 55898e8..3ce153d 100644 --- a/src/Hassium/HassiumObjects/Text/HassiumEncoding.cs +++ b/src/Hassium/HassiumObjects/Text/HassiumEncoding.cs @@ -6,7 +6,7 @@ namespace Hassium.HassiumObjects.Text { - public class HassiumEncoding: HassiumObject + public class HassiumEncoding : HassiumObject { public Encoding Value { get; private set; } @@ -66,8 +66,7 @@ private HassiumObject getBytes(HassiumObject[] args) private HassiumObject getByte(HassiumObject[] args) { - return (int)(Value.GetBytes(args[0].HString().Value)[0]); + return (int) (Value.GetBytes(args[0].HString().Value)[0]); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/Text/HassiumStringBuilder.cs b/src/Hassium/HassiumObjects/Text/HassiumStringBuilder.cs index a2e91a7..da3aaad 100644 --- a/src/Hassium/HassiumObjects/Text/HassiumStringBuilder.cs +++ b/src/Hassium/HassiumObjects/Text/HassiumStringBuilder.cs @@ -4,7 +4,7 @@ namespace Hassium.HassiumObjects.Text { - public class HassiumStringBuilder: HassiumObject + public class HassiumStringBuilder : HassiumObject { public StringBuilder Value { get; private set; } @@ -62,5 +62,4 @@ private HassiumObject toString(HassiumObject[] args) return Value.ToString(); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/Text/HassiumTextReader.cs b/src/Hassium/HassiumObjects/Text/HassiumTextReader.cs index f3d8d54..1ab640b 100644 --- a/src/Hassium/HassiumObjects/Text/HassiumTextReader.cs +++ b/src/Hassium/HassiumObjects/Text/HassiumTextReader.cs @@ -5,7 +5,7 @@ namespace Hassium.HassiumObjects.Text { - public class HassiumTextReader: HassiumObject + public class HassiumTextReader : HassiumObject { public TextReader Value { get; private set; } @@ -35,12 +35,12 @@ private HassiumObject dispose(HassiumObject[] args) private HassiumObject peek(HassiumObject[] args) { - return new HassiumString(Convert.ToString(((char)Value.Peek()))); + return new HassiumString(Convert.ToString(((char) Value.Peek()))); } private HassiumObject read(HassiumObject[] args) { - return new HassiumString(Convert.ToString(((char)Value.Read()))); + return new HassiumString(Convert.ToString(((char) Value.Read()))); } private HassiumObject readLine(HassiumObject[] args) @@ -58,5 +58,4 @@ private HassiumObject toString(HassiumObject[] args) return new HassiumString(Value.ToString()); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/Text/HassiumTextWriter.cs b/src/Hassium/HassiumObjects/Text/HassiumTextWriter.cs index 97fd588..707f5d8 100644 --- a/src/Hassium/HassiumObjects/Text/HassiumTextWriter.cs +++ b/src/Hassium/HassiumObjects/Text/HassiumTextWriter.cs @@ -4,7 +4,7 @@ namespace Hassium.HassiumObjects.Text { - public class HassiumTextWriter: HassiumObject + public class HassiumTextWriter : HassiumObject { public TextWriter Value { get; private set; } @@ -54,5 +54,4 @@ private HassiumObject toString(HassiumObject[] args) return new HassiumString(Value.ToString()); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/Types/HassiumArray.cs b/src/Hassium/HassiumObjects/Types/HassiumArray.cs index abb1873..f019ea7 100644 --- a/src/Hassium/HassiumObjects/Types/HassiumArray.cs +++ b/src/Hassium/HassiumObjects/Types/HassiumArray.cs @@ -24,7 +24,7 @@ public HassiumArray(IEnumerable value) Attributes.Add("remove", new InternalFunction(Remove, 1)); Attributes.Add("resize", new InternalFunction(ResizeArr, 1)); - Attributes.Add("join", new InternalFunction(ArrayJoin, new []{0,1})); + Attributes.Add("join", new InternalFunction(ArrayJoin, new[] {0, 1})); Attributes.Add("reverse", new InternalFunction(ArrayReverse, 0)); Attributes.Add("contains", new InternalFunction(ArrayContains, 1)); @@ -84,7 +84,6 @@ public static implicit operator HassiumArray(object[] arr) } - private HassiumObject toString(HassiumObject[] args) { return ToString(); @@ -110,6 +109,7 @@ public HassiumObject ArrayJoin(HassiumObject[] args) return string.Join(separator, objarr.Select(x => x.ToString())); } + /* TODO: Implement static methods public static HassiumObject ArrayFill(HassiumObject[] args) { @@ -167,8 +167,5 @@ public HassiumObject ArrayZip(HassiumObject[] args) } #endregion - - } -} - +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/Types/HassiumBool.cs b/src/Hassium/HassiumObjects/Types/HassiumBool.cs index 2e100b6..cb88659 100644 --- a/src/Hassium/HassiumObjects/Types/HassiumBool.cs +++ b/src/Hassium/HassiumObjects/Types/HassiumBool.cs @@ -3,7 +3,7 @@ namespace Hassium.HassiumObjects.Types { - public class HassiumBool: HassiumObject, IConvertible + public class HassiumBool : HassiumObject, IConvertible { public bool Value { get; private set; } @@ -24,6 +24,7 @@ private HassiumObject tostring(HassiumObject[] args) } #region IConvertible stuff + public TypeCode GetTypeCode() { return TypeCode.Boolean; @@ -108,7 +109,7 @@ ulong IConvertible.ToUInt64(IFormatProvider provider) { return Convert.ToUInt64(Value); } + #endregion } -} - +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/Types/HassiumByte.cs b/src/Hassium/HassiumObjects/Types/HassiumByte.cs index 207a36a..995694f 100644 --- a/src/Hassium/HassiumObjects/Types/HassiumByte.cs +++ b/src/Hassium/HassiumObjects/Types/HassiumByte.cs @@ -1,6 +1,6 @@ namespace Hassium.HassiumObjects.Types { - public class HassiumByte: HassiumObject + public class HassiumByte : HassiumObject { public byte Value { get; private set; } @@ -14,5 +14,4 @@ public override string ToString() return Value.ToString("X2"); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/Types/HassiumChar.cs b/src/Hassium/HassiumObjects/Types/HassiumChar.cs index f6fa726..84a98c3 100644 --- a/src/Hassium/HassiumObjects/Types/HassiumChar.cs +++ b/src/Hassium/HassiumObjects/Types/HassiumChar.cs @@ -3,7 +3,7 @@ namespace Hassium.HassiumObjects.Types { - public class HassiumChar: HassiumObject + public class HassiumChar : HassiumObject { public char Value { get; private set; } @@ -28,5 +28,4 @@ public static implicit operator HassiumString(HassiumChar c) return new HassiumString(c.ToString()); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/Types/HassiumDate.cs b/src/Hassium/HassiumObjects/Types/HassiumDate.cs index 06fd44e..bba60b0 100644 --- a/src/Hassium/HassiumObjects/Types/HassiumDate.cs +++ b/src/Hassium/HassiumObjects/Types/HassiumDate.cs @@ -20,13 +20,13 @@ public HassiumDate(DateTime value) Attributes.Add("minute", new InternalFunction(x => Value.Minute, 0, true)); Attributes.Add("second", new InternalFunction(x => Value.Second, 0, true)); Attributes.Add("isLeapYear", new InternalFunction(x => DateTime.IsLeapYear(Value.Year), 0, true)); - Attributes.Add("timeStamp", new InternalFunction(x => GetTimestamp(new HassiumObject[]{}), 0, true)); - Attributes.Add("toString", new InternalFunction(toString, new []{0,1})); + Attributes.Add("timeStamp", new InternalFunction(x => GetTimestamp(new HassiumObject[] {}), 0, true)); + Attributes.Add("toString", new InternalFunction(toString, new[] {0, 1})); } public HassiumObject GetTimestamp(HassiumObject[] args) { - return new HassiumInt((int)(Value - new DateTime(1970, 1, 1)).TotalSeconds); + return new HassiumInt((int) (Value - new DateTime(1970, 1, 1)).TotalSeconds); } public override string ToString() @@ -59,10 +59,10 @@ public HassiumObject toString(HassiumObject[] args) ta = n.DayOfWeek.ToString(); break; case 'N': - ta = (int)(n.DayOfWeek + 6) % 7 + ""; + ta = (int) (n.DayOfWeek + 6) % 7 + ""; break; case 'w': - ta = (int)(n.DayOfWeek) + ""; + ta = (int) (n.DayOfWeek) + ""; break; case 'z': ta = n.DayOfYear.ToString(); @@ -70,7 +70,7 @@ public HassiumObject toString(HassiumObject[] args) case 'W': ta = CultureInfo.InvariantCulture.Calendar.GetWeekOfYear( - n.AddDays(4 - ((int)(n.DayOfWeek + 6) % 7 == 0 ? 7 : (int)(n.DayOfWeek + 6) % 7)), + n.AddDays(4 - ((int) (n.DayOfWeek + 6) % 7 == 0 ? 7 : (int) (n.DayOfWeek + 6) % 7)), CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday).ToString(); break; case 'F': @@ -86,7 +86,7 @@ public HassiumObject toString(HassiumObject[] args) ta = n.Month.ToString(); break; case 't': - ta = new[] { 31, 30, 28, 30, 31, 30, 31, 31, 30, 31, 30, 31 }[n.Month - 1].ToString(); + ta = new[] {31, 30, 28, 30, 31, 30, 31, 31, 30, 31, 30, 31}[n.Month - 1].ToString(); break; case 'L': ta = DateTime.IsLeapYear(n.Year) ? "1" : "0"; @@ -110,7 +110,8 @@ public HassiumObject toString(HassiumObject[] args) ta = n.Hour > 12 ? "P.M." : "A.M."; break; case 'B': - ta = ((int)(n.ToUniversalTime().AddHours(1).TimeOfDay.TotalMilliseconds / 86400d)).ToString(); + ta = + ((int) (n.ToUniversalTime().AddHours(1).TimeOfDay.TotalMilliseconds / 86400d)).ToString(); break; case 'g': ta = (n.Hour > 12 ? n.Hour - 12 : n.Hour).ToString(); @@ -143,13 +144,14 @@ public HassiumObject toString(HassiumObject[] args) offstr = "-" + offset.ToString().Replace(":", "").Substring(1, 4); else offstr = "+" + offset.ToString().Replace(":", "").Substring(0, 4); - ta = string.Format("{0}T{1}{2}", n.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture), n.ToString("HH:mm:ss", CultureInfo.InvariantCulture), offstr); + ta = string.Format("{0}T{1}{2}", n.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture), + n.ToString("HH:mm:ss", CultureInfo.InvariantCulture), offstr); break; case 'r': ta = n.ToString("ddd, dd MMM yyyy hh:mm:ss ") + "GMT"; break; case 'U': - ta = (int)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds + ""; + ta = (int) (DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds + ""; break; default: ta = cur.ToString(); @@ -165,7 +167,8 @@ public HassiumObject toString(HassiumObject[] args) { var a = d1.Value; var b = d2.Value; - var c = new HassiumDate(a + new TimeSpan(b.Year * 365 + b.DayOfYear, b.Hour, b.Minute, b.Second, b.Millisecond)); + var c = + new HassiumDate(a + new TimeSpan(b.Year * 365 + b.DayOfYear, b.Hour, b.Minute, b.Second, b.Millisecond)); return c; } @@ -174,4 +177,4 @@ public HassiumObject toString(HassiumObject[] args) return new HassiumDate(new DateTime().Add(d1.Value - d2.Value)); } } -} +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/Types/HassiumDictionary.cs b/src/Hassium/HassiumObjects/Types/HassiumDictionary.cs index 22c31ff..73f520e 100644 --- a/src/Hassium/HassiumObjects/Types/HassiumDictionary.cs +++ b/src/Hassium/HassiumObjects/Types/HassiumDictionary.cs @@ -7,21 +7,22 @@ namespace Hassium.HassiumObjects.Types { - public class HassiumDictionary: HassiumObject, IEnumerable + public class HassiumDictionary : HassiumObject, IEnumerable { public List Value { get; private set; } - public HassiumDictionary(Dictionary value) : this(value.Select(x => (HassiumKeyValuePair)x).ToList()) + public HassiumDictionary(Dictionary value) + : this(value.Select(x => (HassiumKeyValuePair) x).ToList()) { Attributes.Add("length", new InternalFunction(x => Value.Count, 0, true)); Attributes.Add("toString", new InternalFunction(tostring, 0)); Attributes.Add("keys", new HassiumProperty("keys", - x => new HassiumArray(Value.Select(y => y.Key)), x => null, true)); + x => new HassiumArray(Value.Select(y => y.Key)), x => null, true)); Attributes.Add("values", - new HassiumProperty("values", - x => new HassiumArray(Value.Select(y => y.Value)), x => null, true)); + new HassiumProperty("values", + x => new HassiumArray(Value.Select(y => y.Value)), x => null, true)); Attributes.Add("resize", new InternalFunction(ResizeArr, 1)); Attributes.Add("reverse", new InternalFunction(ArrayReverse, 0)); @@ -91,10 +92,11 @@ public HassiumDictionary(Dictionary value) } public HassiumDictionary(IDictionary value) : this(value.Keys.Cast() - .Zip(value.Values.Cast(), (a, b) => new KeyValuePair(a, b)) - .Select(x => new HassiumKeyValuePair((HassiumObject)x.Key, (HassiumObject)x.Value)).ToList()) + .Zip(value.Values.Cast(), (a, b) => new KeyValuePair(a, b)) + .Select(x => new HassiumKeyValuePair((HassiumObject) x.Key, (HassiumObject) x.Value)).ToList()) { } + public IEnumerator GetEnumerator() { return Value.GetEnumerator(); @@ -118,7 +120,7 @@ public HassiumObject ArrayReverse(HassiumObject[] args) public HassiumObject ArrayOp(HassiumObject[] args) { - return Value.Aggregate((a, b) => (HassiumKeyValuePair)args[0].Invoke(a, b)); + return Value.Aggregate((a, b) => (HassiumKeyValuePair) args[0].Invoke(a, b)); } #region LINQ-like functions @@ -191,6 +193,7 @@ public override string ToString() } #region IConvertible stuff + public TypeCode GetTypeCode() { return TypeCode.Single; @@ -198,57 +201,57 @@ public TypeCode GetTypeCode() bool IConvertible.ToBoolean(IFormatProvider provider) { - return Convert.ToBoolean((object)Value); + return Convert.ToBoolean((object) Value); } byte IConvertible.ToByte(IFormatProvider provider) { - return Convert.ToByte((object)Value); + return Convert.ToByte((object) Value); } char IConvertible.ToChar(IFormatProvider provider) { - return Convert.ToChar((object)Value); + return Convert.ToChar((object) Value); } DateTime IConvertible.ToDateTime(IFormatProvider provider) { - return Convert.ToDateTime((object)Value); + return Convert.ToDateTime((object) Value); } decimal IConvertible.ToDecimal(IFormatProvider provider) { - return Convert.ToDecimal((object)Value); + return Convert.ToDecimal((object) Value); } double IConvertible.ToDouble(IFormatProvider provider) { - return Convert.ToDouble((object)Value); + return Convert.ToDouble((object) Value); } short IConvertible.ToInt16(IFormatProvider provider) { - return Convert.ToInt16((object)Value); + return Convert.ToInt16((object) Value); } int IConvertible.ToInt32(IFormatProvider provider) { - return Convert.ToInt32((object)Value); + return Convert.ToInt32((object) Value); } long IConvertible.ToInt64(IFormatProvider provider) { - return Convert.ToInt64((object)Value); + return Convert.ToInt64((object) Value); } sbyte IConvertible.ToSByte(IFormatProvider provider) { - return Convert.ToSByte((object)Value); + return Convert.ToSByte((object) Value); } float IConvertible.ToSingle(IFormatProvider provider) { - return Convert.ToSingle((object)Value); + return Convert.ToSingle((object) Value); } string IConvertible.ToString(IFormatProvider provider) @@ -263,19 +266,19 @@ object IConvertible.ToType(Type conversionType, IFormatProvider provider) ushort IConvertible.ToUInt16(IFormatProvider provider) { - return Convert.ToUInt16((object)Value); + return Convert.ToUInt16((object) Value); } uint IConvertible.ToUInt32(IFormatProvider provider) { - return Convert.ToUInt32((object)Value); + return Convert.ToUInt32((object) Value); } ulong IConvertible.ToUInt64(IFormatProvider provider) { - return Convert.ToUInt64((object)Value); + return Convert.ToUInt64((object) Value); } + #endregion } -} - +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/Types/HassiumDouble.cs b/src/Hassium/HassiumObjects/Types/HassiumDouble.cs index 61385f2..55d4350 100644 --- a/src/Hassium/HassiumObjects/Types/HassiumDouble.cs +++ b/src/Hassium/HassiumObjects/Types/HassiumDouble.cs @@ -3,17 +3,21 @@ namespace Hassium.HassiumObjects.Types { - public class HassiumDouble: HassiumObject, IConvertible + public class HassiumDouble : HassiumObject, IConvertible { public double Value { get; set; } - public int ValueInt { get { return Convert.ToInt32(Value); } set { Value = (double) value; } } + public int ValueInt + { + get { return Convert.ToInt32(Value); } + set { Value = (double) value; } + } public HassiumDouble(double value) { Attributes.Add("toString", new InternalFunction(tostring, 0)); Attributes.Add("compare", new InternalFunction(compare, 1)); - Attributes.Add("isBetween", new InternalFunction(isBetween, new []{2,3})); + Attributes.Add("isBetween", new InternalFunction(isBetween, new[] {2, 3})); Value = value; } @@ -29,7 +33,8 @@ public HassiumObject tostring(HassiumObject[] args) public HassiumObject isBetween(HassiumObject[] args) { - if(args.Length == 3) if(args[2].HBool().Value) return Value >= args[0].HDouble().Value && Value <= args[1].HDouble().Value; + if (args.Length == 3) + if (args[2].HBool().Value) return Value >= args[0].HDouble().Value && Value <= args[1].HDouble().Value; return Value > args[0].HDouble().Value && Value < args[1].HDouble().Value; } @@ -45,6 +50,7 @@ public static implicit operator HassiumString(HassiumDouble str) } #region IConvertible stuff + public TypeCode GetTypeCode() { return TypeCode.Double; @@ -129,7 +135,7 @@ ulong IConvertible.ToUInt64(IFormatProvider provider) { return Convert.ToUInt64(Value); } -#endregion - } -} + #endregion + } +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/Types/HassiumEvent.cs b/src/Hassium/HassiumObjects/Types/HassiumEvent.cs index 18231c5..f61a8b0 100644 --- a/src/Hassium/HassiumObjects/Types/HassiumEvent.cs +++ b/src/Hassium/HassiumObjects/Types/HassiumEvent.cs @@ -30,4 +30,4 @@ public override HassiumObject Invoke(params HassiumObject[] args) return EventHandler(args); } } -} +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/Types/HassiumInt.cs b/src/Hassium/HassiumObjects/Types/HassiumInt.cs index 7c25b64..b2b4dd3 100644 --- a/src/Hassium/HassiumObjects/Types/HassiumInt.cs +++ b/src/Hassium/HassiumObjects/Types/HassiumInt.cs @@ -3,7 +3,7 @@ namespace Hassium.HassiumObjects.Types { - public class HassiumInt: HassiumObject, IConvertible + public class HassiumInt : HassiumObject, IConvertible { public int Value { get; set; } @@ -11,7 +11,7 @@ public HassiumInt(int value) { Attributes.Add("toString", new InternalFunction(tostring, 0)); Attributes.Add("compare", new InternalFunction(compare, 1)); - Attributes.Add("isBetween", new InternalFunction(isBetween, new []{2,3})); + Attributes.Add("isBetween", new InternalFunction(isBetween, new[] {2, 3})); Value = value; } @@ -27,7 +27,8 @@ public HassiumObject tostring(HassiumObject[] args) public HassiumObject isBetween(HassiumObject[] args) { - if(args.Length == 3) if(args[2].HBool().Value) return Value >= args[0].HDouble().Value && Value <= args[1].HDouble().Value; + if (args.Length == 3) + if (args[2].HBool().Value) return Value >= args[0].HDouble().Value && Value <= args[1].HDouble().Value; return Value > args[0].HDouble().Value && Value < args[1].HDouble().Value; } @@ -48,6 +49,7 @@ public static implicit operator HassiumDouble(HassiumInt val) } #region IConvertible stuff + public TypeCode GetTypeCode() { return TypeCode.Int32; @@ -132,7 +134,7 @@ ulong IConvertible.ToUInt64(IFormatProvider provider) { return Convert.ToUInt64(Value); } -#endregion - } -} + #endregion + } +} \ No newline at end of file diff --git a/src/Hassium/HassiumObjects/Types/HassiumString.cs b/src/Hassium/HassiumObjects/Types/HassiumString.cs index ffdf428..3579f9e 100644 --- a/src/Hassium/HassiumObjects/Types/HassiumString.cs +++ b/src/Hassium/HassiumObjects/Types/HassiumString.cs @@ -5,7 +5,7 @@ namespace Hassium.HassiumObjects.Types { - public class HassiumString: HassiumObject, IConvertible + public class HassiumString : HassiumObject, IConvertible { protected bool Equals(HassiumString other) { @@ -27,7 +27,7 @@ public HassiumString(string value) Attributes.Add("begins", new InternalFunction(begins, 1)); Attributes.Add("ends", new InternalFunction(ends, 1)); Attributes.Add("getAt", new InternalFunction(getat, 1)); - Attributes.Add("substring", new InternalFunction(substr, new []{1,2})); + Attributes.Add("substring", new InternalFunction(substr, new[] {1, 2})); Attributes.Add("concat", new InternalFunction(concat, 1)); Attributes.Add("contains", new InternalFunction(contains, 1)); Attributes.Add("split", new InternalFunction(split, 1)); @@ -175,6 +175,7 @@ public override bool Equals(object obj) } #region IConvertible stuff + public TypeCode GetTypeCode() { return TypeCode.String; @@ -264,7 +265,7 @@ ulong IConvertible.ToUInt64(IFormatProvider provider) { return Convert.ToUInt64(Value); } + #endregion } -} - +} \ No newline at end of file diff --git a/src/Hassium/Interpreter/HassiumMethod.cs b/src/Hassium/Interpreter/HassiumMethod.cs index 0a57c34..98d784d 100644 --- a/src/Hassium/Interpreter/HassiumMethod.cs +++ b/src/Hassium/Interpreter/HassiumMethod.cs @@ -16,11 +16,20 @@ public class HassiumMethod : HassiumObject public FuncNode FuncNode; public StackFrame stackFrame; - public string Name { get { return FuncNode.Name; } } + public string Name + { + get { return FuncNode.Name; } + } - public bool IsStatic { get { return !FuncNode.Parameters.Contains("this"); } } + public bool IsStatic + { + get { return !FuncNode.Parameters.Contains("this"); } + } - public bool IsConstructor { get { return Name == "new"; } } + public bool IsConstructor + { + get { return Name == "new"; } + } public HassiumMethod(Interpreter interpreter, FuncNode funcNode, LocalScope localScope, HassiumObject self) { @@ -47,7 +56,8 @@ public static implicit operator HassiumEventHandler(HassiumMethod mt) public override HassiumObject Invoke(params HassiumObject[] args) { - if (stackFrame == null || (stackFrame.Locals.Count == 0 || FuncNode.Parameters.Any(x => stackFrame.Locals.ContainsKey(x)))) + if (stackFrame == null || + (stackFrame.Locals.Count == 0 || FuncNode.Parameters.Any(x => stackFrame.Locals.ContainsKey(x)))) stackFrame = new StackFrame(LocalScope, (IsStatic && !IsConstructor) ? null : SelfReference); if (!IsStatic || IsConstructor) stackFrame.Locals["this"] = SelfReference; @@ -73,7 +83,10 @@ public override HassiumObject Invoke(params HassiumObject[] args) Interpreter.CallStack.Pop(); - if (ret is HassiumArray) ret = ((HassiumArray) ret).Cast().Select((s, i) => new {s, i}).ToDictionary(x => (object)x.i, x => (object)x.s); + if (ret is HassiumArray) + ret = ((HassiumArray) ret).Cast() + .Select((s, i) => new {s, i}) + .ToDictionary(x => (object) x.i, x => (object) x.s); Interpreter.inFunc--; Interpreter.returnFunc = false; return ret; @@ -81,7 +94,8 @@ public override HassiumObject Invoke(params HassiumObject[] args) public override string ToString() { - return string.Format("[HassiumMethod: {0}`{1} SelfReference={2}]", Name, FuncNode.Parameters.Count, SelfReference ?? "null"); + return string.Format("[HassiumMethod: {0}`{1} SelfReference={2}]", Name, FuncNode.Parameters.Count, + SelfReference ?? "null"); } /// @@ -129,10 +143,10 @@ public static Func GetFunc2(Hassium /// /// The to convert /// The resulting - public static Func GetFunc3(HassiumObject internalFunction) + public static Func GetFunc3( + HassiumObject internalFunction) { return (arg1, arg2, arg3) => (internalFunction).Invoke(arg1, arg2, arg3); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/Interpreter/HassiumProperty.cs b/src/Hassium/Interpreter/HassiumProperty.cs index f0e7182..03a7f50 100644 --- a/src/Hassium/Interpreter/HassiumProperty.cs +++ b/src/Hassium/Interpreter/HassiumProperty.cs @@ -26,4 +26,4 @@ public override HassiumObject Invoke(params HassiumObject[] args) return GetValue(args); } } -} +} \ No newline at end of file diff --git a/src/Hassium/Interpreter/IVisitor.cs b/src/Hassium/Interpreter/IVisitor.cs index 7890b27..90dce6e 100644 --- a/src/Hassium/Interpreter/IVisitor.cs +++ b/src/Hassium/Interpreter/IVisitor.cs @@ -39,4 +39,4 @@ public interface IVisitor object Accept(WhileNode node); object Accept(UseNode node); } -} +} \ No newline at end of file diff --git a/src/Hassium/Interpreter/Interpreter.cs b/src/Hassium/Interpreter/Interpreter.cs index d3a0948..07dc7fd 100644 --- a/src/Hassium/Interpreter/Interpreter.cs +++ b/src/Hassium/Interpreter/Interpreter.cs @@ -33,6 +33,7 @@ namespace Hassium.Interpreter { public delegate void ExitEventHandler(int code); + /// /// Interpreter. /// @@ -62,7 +63,10 @@ public HassiumObject GetVariable(string name, AstNode node) if (CallStack.Count > 0 && CallStack.Any(x => x.Locals.ContainsKey(name))) return CallStack.First(x => x.Locals.ContainsKey(name)).Locals[name]; if (CallStack.Count > 0 && CallStack.Any(x => x.Locals.Any(y => y.Key.StartsWith(name)))) - return CallStack.First(x => x.Locals.Any(y => y.Key.StartsWith(name))).Locals.First(x => x.Key.StartsWith(name)).Value; + return + CallStack.First(x => x.Locals.Any(y => y.Key.StartsWith(name))) + .Locals.First(x => x.Key.StartsWith(name)) + .Value; if (Globals.ContainsKey(name)) return Globals[name]; if (Globals.Any(x => x.Key.StartsWith(name))) @@ -76,7 +80,8 @@ public bool HasVariable(string name, bool onlyglobal = false) return onlyglobal ? Globals.ContainsKey(name) || Constants.ContainsKey(name) : Globals.ContainsKey(name) || Constants.ContainsKey(name) || - (CallStack.Count > 0 && (CallStack.Peek().Scope.Symbols.Contains(name) || CallStack.Any(x => x.Locals.ContainsKey(name)))); + (CallStack.Count > 0 && + (CallStack.Peek().Scope.Symbols.Contains(name) || CallStack.Any(x => x.Locals.ContainsKey(name)))); } public void SetGlobalVariable(string name, HassiumObject value, AstNode node) @@ -93,7 +98,6 @@ public void SetLocalVariable(string name, HassiumObject value, AstNode node) throw new ParseException("Can't change the value of the internal constant '" + name + "'.", node); - if (CallStack.Count > 0) { if (CallStack.Any(x => x.Locals.ContainsKey(name))) @@ -102,9 +106,13 @@ public void SetLocalVariable(string name, HassiumObject value, AstNode node) } } - public void SetVariable(string name, HassiumObject value, AstNode node, bool forceglobal = false, bool onlyexist = false) + public void SetVariable(string name, HassiumObject value, AstNode node, bool forceglobal = false, + bool onlyexist = false) { - if (!forceglobal && CallStack.Count > 0 && (!onlyexist || (CallStack.Peek().Scope.Symbols.Contains(name) || CallStack.Any(x => x.Locals.ContainsKey(name)))) && !Globals.ContainsKey(name)) + if (!forceglobal && CallStack.Count > 0 && + (!onlyexist || + (CallStack.Peek().Scope.Symbols.Contains(name) || CallStack.Any(x => x.Locals.ContainsKey(name)))) && + !Globals.ContainsKey(name)) SetLocalVariable(name, value, node); else SetGlobalVariable(name, value, node); @@ -112,17 +120,21 @@ public void SetVariable(string name, HassiumObject value, AstNode node, bool for public void FreeVariable(string name, AstNode node, bool forceglobal = false) { - if (Constants.ContainsKey(name)) throw new ParseException("Can't delete internal constant '" + name + "'.", node); + if (Constants.ContainsKey(name)) + throw new ParseException("Can't delete internal constant '" + name + "'.", node); if (forceglobal) { - if (!Globals.ContainsKey(name)) throw new ParseException("The global variable '" + name + "' doesn't exist.", node); + if (!Globals.ContainsKey(name)) + throw new ParseException("The global variable '" + name + "' doesn't exist.", node); Globals.Remove(name); } else { if (!HasVariable(name)) throw new ParseException("The variable '" + name + "' doesn't exist.", node); - if (CallStack.Count > 0 && (CallStack.Peek().Scope.Symbols.Contains(name) || CallStack.Any(x => x.Locals.ContainsKey(name)))) - CallStack.First(x => x.Locals.ContainsKey(name) || x.Scope.Symbols.Contains(name)).Locals.Remove(name); + if (CallStack.Count > 0 && + (CallStack.Peek().Scope.Symbols.Contains(name) || CallStack.Any(x => x.Locals.ContainsKey(name)))) + CallStack.First(x => x.Locals.ContainsKey(name) || x.Scope.Symbols.Contains(name)) + .Locals.Remove(name); else Globals.Remove(name); } @@ -143,7 +155,7 @@ public IFunction GetFunction(string name, int parm, AstNode node) { return GetVariable(name + "`i", node); } - if(HasVariable(name)) + if (HasVariable(name)) { return GetVariable(name, node); } @@ -187,13 +199,14 @@ public void Execute(bool repl = false) { if (node is FuncNode && firstExecute) { - var fnode = ((FuncNode)node); + var fnode = ((FuncNode) node); var scope = SymbolTable.ChildScopes[fnode.Name + "`" + fnode.Parameters.Count]; - SetVariable(fnode.Name + "`" + fnode.Parameters.Count, new HassiumMethod(this, fnode, scope, null), node); + SetVariable(fnode.Name + "`" + fnode.Parameters.Count, new HassiumMethod(this, fnode, scope, null), + node); } else if (node is ClassNode) { - var cnode = ((ClassNode)node); + var cnode = ((ClassNode) node); if (!Globals.ContainsKey(cnode.Name)) Globals.Add(cnode.Name, new HassiumClass(cnode, this)); } @@ -211,7 +224,7 @@ public void Execute(bool repl = false) if (exit) return; if (node is FuncNode) { - var fnode = ((FuncNode)node); + var fnode = ((FuncNode) node); var scope = SymbolTable.ChildScopes[fnode.Name + "`" + fnode.Parameters.Count]; //If there is a main, let it be the main entry point of the program if (fnode.Name == "main") @@ -238,12 +251,13 @@ private HassiumObject interpretBinaryOp(BinOpNode node) { if (node.Left is ArrayGetNode) { - var call = (ArrayGetNode)(node.Left); + var call = (ArrayGetNode) (node.Left); if (!call.Target.CanBeIndexed) - throw new ParseException("The [] operator only applies to objects of type Array, Dictionary or String.", node); + throw new ParseException( + "The [] operator only applies to objects of type Array, Dictionary or String.", node); - if(!call.Target.CanBeModified) + if (!call.Target.CanBeModified) throw new ParseException("The specified target cannot be modified.", node); var evaluated = call.Target.Visit(this); @@ -282,7 +296,7 @@ private HassiumObject interpretBinaryOp(BinOpNode node) else if (evaluated is HassiumArray || evaluated is HassiumString) { HassiumArray theArray = null; - if(evaluated is HassiumString) + if (evaluated is HassiumString) { theArray = new HassiumArray(evaluated.ToString().ToCharArray().Cast()); } @@ -300,7 +314,7 @@ private HassiumObject interpretBinaryOp(BinOpNode node) ? interpretBinaryOp(theArray[arid], right, node.AssignOperation) : right; - if(append) + if (append) theArray.Add(new[] {theValue}); else { @@ -314,9 +328,9 @@ private HassiumObject interpretBinaryOp(BinOpNode node) } else { - throw new ParseException("The [] operator only applies to objects of type Array, Dictionary or String.", node); + throw new ParseException( + "The [] operator only applies to objects of type Array, Dictionary or String.", node); } - } else if (node.Left is MemberAccessNode) { @@ -330,13 +344,13 @@ private HassiumObject interpretBinaryOp(BinOpNode node) throw new ParseException("Not a valid identifier", node); SetVariable(node.Left.ToString(), node.IsOpAssign - ? interpretBinaryOp(new BinOpNode(node.Position, node.AssignOperation, node.Left, node.Right)) - : right, node); + ? interpretBinaryOp(new BinOpNode(node.Position, node.AssignOperation, node.Left, node.Right)) + : right, node); } return right; } var left = node.Left.Visit(this); - if(node.BinOp == BinaryOperation.Is) + if (node.BinOp == BinaryOperation.Is) { var target = right; Type ttype = null; @@ -356,11 +370,12 @@ private HassiumObject interpretBinaryOp(BinOpNode node) /// The result of the operation private HassiumObject interpretBinaryOp(object left, object right, BinaryOperation _op, int pos = -1) { - if(left == null && _op != BinaryOperation.NullCoalescing) throw new ParseException("Left operand can't be null", pos); + if (left == null && _op != BinaryOperation.NullCoalescing) + throw new ParseException("Left operand can't be null", pos); if (left is AstNode) left = ((AstNode) left).Visit(this); - if (left is int) left = (double) (int) left; - if (right is AstNode) right = ((AstNode)right).Visit(this); - if (right is int) right = (double)(int)right; + if (left is int) left = (double) (int) left; + if (right is AstNode) right = ((AstNode) right).Visit(this); + if (right is int) right = (double) (int) right; switch (_op) { case BinaryOperation.Addition: @@ -370,12 +385,12 @@ private HassiumObject interpretBinaryOp(object left, object right, BinaryOperati return (HassiumDate) left + (HassiumDate) right; if (left is HassiumKeyValuePair || right is HassiumKeyValuePair) return new HassiumString(left + right.ToString()); - if(left is HassiumInt && right is HassiumInt) + if (left is HassiumInt && right is HassiumInt) return new HassiumInt(Convert.ToInt32(left) + Convert.ToInt32(right)); - if(left is HassiumEvent && right is HassiumMethod) + if (left is HassiumEvent && right is HassiumMethod) { var ev = (HassiumEvent) left; - ev.AddHandler((HassiumMethod)right); + ev.AddHandler((HassiumMethod) right); return ev; } return new HassiumDouble(Convert.ToDouble(left) + Convert.ToDouble(right)); @@ -384,13 +399,13 @@ private HassiumObject interpretBinaryOp(object left, object right, BinaryOperati return new HassiumInt(Convert.ToInt32(left) - Convert.ToInt32(right)); if (left is HassiumEvent && right is HassiumMethod) { - var ev = (HassiumEvent)left; - ev.RemoveHandler((HassiumMethod)right); + var ev = (HassiumEvent) left; + ev.RemoveHandler((HassiumMethod) right); return ev; } return new HassiumDouble(Convert.ToDouble(left) - Convert.ToDouble(right)); case BinaryOperation.Division: - if(Convert.ToDouble(right) == 0.0) throw new ParseException("Cannot divide by zero", pos); + if (Convert.ToDouble(right) == 0.0) throw new ParseException("Cannot divide by zero", pos); if (left is HassiumInt && right is HassiumInt) return new HassiumInt(Convert.ToInt32(left) / Convert.ToInt32(right)); return new HassiumDouble(Convert.ToDouble(left) / Convert.ToDouble(right)); @@ -423,8 +438,11 @@ private HassiumObject interpretBinaryOp(object left, object right, BinaryOperati case BinaryOperation.LesserOrEqual: return new HassiumBool(Convert.ToDouble(left) <= Convert.ToDouble(right)); case BinaryOperation.CombinedComparison: - if (new HassiumBool(interpretBinaryOp(left, right, BinaryOperation.GreaterThan))) return new HassiumInt(1); - return new HassiumBool(interpretBinaryOp(left, right, BinaryOperation.LessThan)) ? new HassiumInt(-1) : new HassiumInt(0); + if (new HassiumBool(interpretBinaryOp(left, right, BinaryOperation.GreaterThan))) + return new HassiumInt(1); + return new HassiumBool(interpretBinaryOp(left, right, BinaryOperation.LessThan)) + ? new HassiumInt(-1) + : new HassiumInt(0); case BinaryOperation.Xor: return new HassiumInt(Convert.ToInt32(left) ^ Convert.ToInt32(right)); case BinaryOperation.BitwiseAnd: @@ -440,7 +458,7 @@ private HassiumObject interpretBinaryOp(object left, object right, BinaryOperati case BinaryOperation.Pow: if (left is HassiumInt && right is HassiumInt) - return new HassiumInt((int)Math.Pow(Convert.ToInt32(left), Convert.ToInt32(right))); + return new HassiumInt((int) Math.Pow(Convert.ToInt32(left), Convert.ToInt32(right))); return new HassiumDouble(Math.Pow(Convert.ToDouble(left), Convert.ToDouble(right))); case BinaryOperation.Root: /*if (left is HassiumInt && right is HassiumInt) @@ -453,6 +471,7 @@ private HassiumObject interpretBinaryOp(object left, object right, BinaryOperati // Raise error return new HassiumInt(-1); } + /// /// Interprets the unary op. /// @@ -469,7 +488,7 @@ private HassiumObject interpretUnaryOp(UnaryOpNode node) if (value is int) return -(int) value; return -Convert.ToDouble(value); case UnaryOperation.Complement: - return ~(int)Convert.ToDouble(value); + return ~(int) Convert.ToDouble(value); } //Raise error return -1; @@ -495,17 +514,17 @@ public static Dictionary GetFunctions(string path = "" var theattr1 = myfunc.GetCustomAttributes(typeof (IntFunc), true); foreach (var theattr in theattr1.OfType()) { - for(int i=0; i < theattr.Arguments.Length; i++) + for (int i = 0; i < theattr.Arguments.Length; i++) { var rfunc = new InternalFunction( - (HassiumFunctionDelegate) - Delegate.CreateDelegate(typeof(HassiumFunctionDelegate), myfunc), theattr.Arguments[i], false, theattr.Constructor); + (HassiumFunctionDelegate) + Delegate.CreateDelegate(typeof (HassiumFunctionDelegate), myfunc), + theattr.Arguments[i], false, theattr.Constructor); int an = theattr.Arguments[i]; result.Add(theattr.Name + "`" + (an == -1 ? "i" : an.ToString()), rfunc); - if (theattr.Alias != "") result.Add(theattr.Alias + "`" + (an == -1 ? "i" : an.ToString()), rfunc); + if (theattr.Alias != "") + result.Add(theattr.Alias + "`" + (an == -1 ? "i" : an.ToString()), rfunc); } - - } } } @@ -524,7 +543,7 @@ public object Accept(ArgListNode node) var arguments = new HassiumObject[node.Children.Count]; for (var x = 0; x < node.Children.Count; x++) { - arguments[x] = (HassiumObject)node.Children[x].Visit(this); + arguments[x] = (HassiumObject) node.Children[x].Visit(this); } return arguments; } @@ -534,22 +553,25 @@ public object Accept(ArrayGetNode node) var call = node; if (!call.Target.CanBeIndexed) - throw new ParseException("The [] operator only applies to objects of type Array, Dictionary or String.", node); + throw new ParseException( + "The [] operator only applies to objects of type Array, Dictionary or String.", node); - var evaluated = (HassiumObject)call.Target.Visit(this); + var evaluated = (HassiumObject) call.Target.Visit(this); if (evaluated is HassiumDictionary) { - var theArray = ((HassiumDictionary)evaluated); + var theArray = ((HassiumDictionary) evaluated); HassiumObject arid = null; if (call.Arguments.Children.Count > 0) - arid = (HassiumObject)call.Arguments.Children[0].Visit(this); + arid = (HassiumObject) call.Arguments.Children[0].Visit(this); if (arid == null) return theArray.Value.Last().Value; else { - return theArray.Value.Any(cur => cur.Key.ToString() == arid.ToString()) ? theArray.Value.First(cur => cur.Key.ToString() == arid.ToString()).Value : theArray[arid]; + return theArray.Value.Any(cur => cur.Key.ToString() == arid.ToString()) + ? theArray.Value.First(cur => cur.Key.ToString() == arid.ToString()).Value + : theArray[arid]; } } else if (evaluated is HassiumArray || evaluated is HassiumString) @@ -559,17 +581,17 @@ public object Accept(ArrayGetNode node) { theArray = new HassiumArray(evaluated.ToString().ToCharArray().Cast()); } - else theArray = ((HassiumArray)evaluated); + else theArray = ((HassiumArray) evaluated); int arid = -1; bool append = false; if (call.Arguments.Children.Count > 0) - arid = (HassiumObject)call.Arguments.Children[0].Visit(this); + arid = (HassiumObject) call.Arguments.Children[0].Visit(this); else append = true; - int count = (HassiumObject)call.Count.Visit(this); + int count = (HassiumObject) call.Count.Visit(this); if (append) return theArray.Value.Last(); @@ -584,7 +606,8 @@ public object Accept(ArrayGetNode node) } else { - throw new ParseException("The [] operator only applies to objects of type Array, Dictionary or String.", node); + throw new ParseException( + "The [] operator only applies to objects of type Array, Dictionary or String.", node); } } @@ -600,16 +623,18 @@ public object Accept(ArrayInitializerNode node) if (ainode.IsDictionary) return new HassiumDictionary(content.Select( pair => - new KeyValuePair( - pair.Key is AstNode - ? (HassiumObject)((AstNode)(pair.Key)).Visit(this) - : HassiumObject.ToHassiumObject(pair.Key), - pair.Value is AstNode - ? (HassiumObject)((AstNode)(pair.Value)).Visit(this) - : HassiumObject.ToHassiumObject(pair.Value))) + new KeyValuePair( + pair.Key is AstNode + ? (HassiumObject) ((AstNode) (pair.Key)).Visit(this) + : HassiumObject.ToHassiumObject(pair.Key), + pair.Value is AstNode + ? (HassiumObject) ((AstNode) (pair.Value)).Visit(this) + : HassiumObject.ToHassiumObject(pair.Value))) .ToDictionary(x => x.Key, x => x.Value)); else - return new HassiumArray(content.Values.Select(x => x is AstNode ? (HassiumObject)((AstNode)x).Visit(this) : x)); + return + new HassiumArray( + content.Values.Select(x => x is AstNode ? (HassiumObject) ((AstNode) x).Visit(this) : x)); } public object Accept(BinOpNode node) @@ -647,7 +672,7 @@ public object Accept(CodeBlock node) public object Accept(ConditionalOpNode node) { var ifStmt = node; - if ((HassiumBool)(ifStmt.Predicate.Visit(this))) + if ((HassiumBool) (ifStmt.Predicate.Visit(this))) { return ifStmt.Body.Visit(this); } @@ -673,14 +698,14 @@ public object Accept(ForEachNode node) inLoop++; if (haystackstmt is HassiumDictionary) { - var theArray = ((HassiumDictionary)haystackstmt); + var theArray = ((HassiumDictionary) haystackstmt); var keyvname = ""; var valvname = ""; if (needlestmt is ArrayInitializerNode) { - keyvname = ((ArrayInitializerNode)needlestmt).Value[0].ToString(); - valvname = ((ArrayInitializerNode)needlestmt).Value[1].ToString(); + keyvname = ((ArrayInitializerNode) needlestmt).Value[0].ToString(); + valvname = ((ArrayInitializerNode) needlestmt).Value[1].ToString(); } else { @@ -688,10 +713,13 @@ public object Accept(ForEachNode node) } if (keyvname != "") SetVariable(keyvname, null, forStmt); SetVariable(valvname, null, forStmt); - foreach (var needle in (keyvname != "" ? theArray : (IEnumerable)(theArray.Value.Select(x => x.Value)))) + foreach (var needle in (keyvname != "" ? theArray : (IEnumerable) (theArray.Value.Select(x => x.Value))) + ) { - if (keyvname != "") SetVariable(keyvname, ((HassiumKeyValuePair)needle).Key, forStmt); - SetVariable(valvname, keyvname != "" ? ((HassiumKeyValuePair)needle).Value : HassiumObject.ToHassiumObject(needle), forStmt); + if (keyvname != "") SetVariable(keyvname, ((HassiumKeyValuePair) needle).Key, forStmt); + SetVariable(valvname, + keyvname != "" ? ((HassiumKeyValuePair) needle).Value : HassiumObject.ToHassiumObject(needle), + forStmt); forStmt.Body.Visit(this); if (continueLoop) continueLoop = false; if (breakLoop) @@ -711,7 +739,7 @@ public object Accept(ForEachNode node) { theArray = new HassiumArray(haystackstmt.ToString().ToCharArray().Cast()); } - else theArray = ((HassiumArray)haystackstmt); + else theArray = ((HassiumArray) haystackstmt); var valvname = needlestmt.ToString(); @@ -733,7 +761,8 @@ public object Accept(ForEachNode node) else { inLoop--; - throw new ParseException("Foreach can only be used with objects of type Array, Dictionary or String.", node); + throw new ParseException("Foreach can only be used with objects of type Array, Dictionary or String.", + node); } return null; } @@ -777,7 +806,6 @@ public object Accept(FuncNode node) } - public object Accept(FunctionCallNode node) { var call = node; @@ -804,18 +832,19 @@ public object Accept(FunctionCallNode node) if (call.Target is MemberAccessNode) { var man = (MemberAccessNode) call.Target; - var targ = (HassiumObject)man.Left.Visit(this); - if(targ.Attributes.ContainsKey(man.Member + "`" + call.Arguments.Children.Count)) + var targ = (HassiumObject) man.Left.Visit(this); + if (targ.Attributes.ContainsKey(man.Member + "`" + call.Arguments.Children.Count)) { target = targ.GetAttribute(man.Member + "`" + call.Arguments.Children.Count, node.Position); } - else if(targ.Attributes.ContainsKey(man.Member)) + else if (targ.Attributes.ContainsKey(man.Member)) { target = targ.GetAttribute(man.Member, node.Position); } else { - throw new ParseException("The function " + man.Member + " doesn't exist for the object " + man.Left, node); + throw new ParseException( + "The function " + man.Member + " doesn't exist for the object " + man.Left, node); } } else @@ -823,12 +852,13 @@ public object Accept(FunctionCallNode node) break; } - if(HassiumInterpreter.options.Secure) + if (HassiumInterpreter.options.Secure) { var forbidden = new List {"system", "runtimecall", "input"}; - if(forbidden.Contains(call.Target.ToString())) + if (forbidden.Contains(call.Target.ToString())) { - throw new ParseException("The " + call.Target + "() function is disabled for security reasons.", node); + throw new ParseException("The " + call.Target + "() function is disabled for security reasons.", + node); } } @@ -836,16 +866,15 @@ public object Accept(FunctionCallNode node) if (target is InternalFunction && (target as InternalFunction).IsConstructor) throw new ParseException("Attempt to run a constructor without the 'new' operator", node); - if(target is HassiumMethod) + if (target is HassiumMethod) { var th = target as HassiumMethod; if (!th.IsStatic) { - if(call.Target is MemberAccessNode) + if (call.Target is MemberAccessNode) { - var man = (MemberAccessNode) call.Target; - if(!((HassiumObject)man.Left.Visit(this)).IsInstance) + if (!((HassiumObject) man.Left.Visit(this)).IsInstance) { throw new ParseException("Non-static method can only be used with instance of class", call); } @@ -856,7 +885,9 @@ public object Accept(FunctionCallNode node) var arguments = new HassiumObject[call.Arguments.Children.Count]; for (var x = 0; x < call.Arguments.Children.Count; x++) { - arguments[x] = dontEval ? new HassiumString(call.Arguments.Children[x].ToString()) : (HassiumObject)call.Arguments.Children[x].Visit(this); + arguments[x] = dontEval + ? new HassiumString(call.Arguments.Children[x].ToString()) + : (HassiumObject) call.Arguments.Children[x].Visit(this); } switch (call.Target.ToString()) @@ -871,7 +902,6 @@ public object Accept(FunctionCallNode node) } - HassiumObject ret = target.Invoke(arguments); if (returnFunc) returnFunc = false; @@ -887,7 +917,7 @@ public object Accept(IdentifierNode node) public object Accept(IfNode node) { var ifStmt = node; - if ((HassiumBool)(ifStmt.Predicate.Visit(this))) + if ((HassiumBool) (ifStmt.Predicate.Visit(this))) { ifStmt.Body.Visit(this); } @@ -907,9 +937,9 @@ public object Accept(InstanceNode node) HassiumObject theVar = null; if (fcall.Target is MemberAccessNode) { - theVar = (HassiumObject)fcall.Target.Visit(this); + theVar = (HassiumObject) fcall.Target.Visit(this); } - else theVar = (HassiumObject)GetFunction(fcall.Target.ToString(), fcall.Arguments.Children.Count, node); + else theVar = (HassiumObject) GetFunction(fcall.Target.ToString(), fcall.Arguments.Children.Count, node); if (theVar is InternalFunction) { @@ -949,17 +979,17 @@ public object Accept(LambdaFuncNode node) return true; }); } - return new HassiumMethod(this, (FuncNode)funcNode, stackFrame, null); + return new HassiumMethod(this, (FuncNode) funcNode, stackFrame, null); } public object Accept(MemberAccessNode node) { var accessor = node; - var target = (HassiumObject)accessor.Left.Visit(this); + var target = (HassiumObject) accessor.Left.Visit(this); var attr = target.GetAttribute(accessor.Member, node.Position + 1); - if (attr is InternalFunction && ((InternalFunction)attr).IsProperty) + if (attr is InternalFunction && ((InternalFunction) attr).IsProperty) { - return ((InternalFunction)attr).Invoke(); + return ((InternalFunction) attr).Invoke(); } else { @@ -970,15 +1000,17 @@ public object Accept(MemberAccessNode node) public object Accept(MentalNode node) { var mnode = node; - if (!HasVariable(mnode.Name)) throw new ParseException("The operand of an increment or decrement operator must be a variable, property or indexer", mnode); + if (!HasVariable(mnode.Name)) + throw new ParseException( + "The operand of an increment or decrement operator must be a variable, property or indexer", mnode); var oldValue = GetVariable(mnode.Name, mnode); switch (mnode.OpType) { case "++": - SetVariable(mnode.Name, Convert.ToInt32((object)GetVariable(mnode.Name, mnode)) + 1, mnode); + SetVariable(mnode.Name, Convert.ToInt32((object) GetVariable(mnode.Name, mnode)) + 1, mnode); break; case "--": - SetVariable(mnode.Name, Convert.ToInt32((object)GetVariable(mnode.Name, mnode)) - 1, mnode); + SetVariable(mnode.Name, Convert.ToInt32((object) GetVariable(mnode.Name, mnode)) - 1, mnode); break; default: throw new ParseException("Unknown operation " + mnode.OpType, mnode); @@ -997,7 +1029,8 @@ public object Accept(NumberNode node) public object Accept(PropertyNode node) { - var prop = new HassiumProperty(node.Name, x => GetPropVal(node, x[0]), x => SetPropVal(node, x[1], x[0]), node.SetNode == null); + var prop = new HassiumProperty(node.Name, x => GetPropVal(node, x[0]), x => SetPropVal(node, x[1], x[0]), + node.SetNode == null); SetVariable(node.Name, prop, node); return prop; } @@ -1005,14 +1038,15 @@ public object Accept(PropertyNode node) private HassiumObject GetPropVal(PropertyNode node, HassiumObject self) { var funcnode = new HassiumMethod(this, - new FuncNode(node.GetNode.Position, "__getprop__" + node.Name + "`1", new List { "this" }, + new FuncNode(node.GetNode.Position, "__getprop__" + node.Name + "`1", new List {"this"}, node.GetNode.Body), SymbolTable.ChildScopes["__getprop__" + node.Name + "`1"], self); return funcnode.Invoke(); } private HassiumObject SetPropVal(PropertyNode node, HassiumObject value, HassiumObject self) { - if(node.SetNode == null) throw new ParseException("The property is read-only, it cannot be modified.", node); + if (node.SetNode == null) + throw new ParseException("The property is read-only, it cannot be modified.", node); var funcnode = new HassiumMethod(this, new FuncNode(node.SetNode.Position, "__setprop__" + node.Name + "`2", new List {"this", "value"}, node.SetNode.Body), SymbolTable.ChildScopes["__setprop__" + node.Name + "`2"], self); @@ -1024,10 +1058,11 @@ public object Accept(ReturnNode node) { if (inFunc == 0) throw new ParseException("'return' cannot be used outside a function", node); var returnStmt = node; - if (returnStmt.Value != null && !returnStmt.Value.ReturnsValue) throw new ParseException("This node type doesn't return a value.", returnStmt.Value); + if (returnStmt.Value != null && !returnStmt.Value.ReturnsValue) + throw new ParseException("This node type doesn't return a value.", returnStmt.Value); var ret = returnStmt.Value.Visit(this); returnFunc = true; - CallStack.Peek().ReturnValue = (HassiumObject)ret; + CallStack.Peek().ReturnValue = (HassiumObject) ret; return ret; } @@ -1087,11 +1122,30 @@ public object Accept(UseNode node) Constants.Add("Directory", new HassiumDirectory()); Constants.Add("Path", new HassiumPath()); Constants.Add("IO", new HassiumIO()); - Constants.Add("StreamWriter", new InternalFunction(x => new HassiumStreamWriter(x[0] is HassiumStream ? new StreamWriter(((HassiumStream)x[0]).Value) : new StreamWriter(x[0].ToString())), 1, false, true)); - Constants.Add("StreamReader", new InternalFunction(x => new HassiumStreamReader(x[0] is HassiumStream ? new StreamReader(((HassiumStream)x[0]).Value) : new StreamReader(x[0].ToString())), 1, false, true)); - Constants.Add("FileStream", new InternalFunction(x => new HassiumFileStream(new FileStream(x[0].ToString(), FileMode.OpenOrCreate)), 1, false, true)); - Constants.Add("BinaryWriter", new InternalFunction(x => new HassiumBinaryWriter(new BinaryWriter(((HassiumStream)x[0]).Value)), 1, false, true)); - Constants.Add("BinaryReader", new InternalFunction(x => new HassiumBinaryReader(new BinaryReader(((HassiumStream)x[0]).Value)), 1, false, true)); + Constants.Add("StreamWriter", + new InternalFunction( + x => + new HassiumStreamWriter(x[0] is HassiumStream + ? new StreamWriter(((HassiumStream) x[0]).Value) + : new StreamWriter(x[0].ToString())), 1, false, true)); + Constants.Add("StreamReader", + new InternalFunction( + x => + new HassiumStreamReader(x[0] is HassiumStream + ? new StreamReader(((HassiumStream) x[0]).Value) + : new StreamReader(x[0].ToString())), 1, false, true)); + Constants.Add("FileStream", + new InternalFunction( + x => new HassiumFileStream(new FileStream(x[0].ToString(), FileMode.OpenOrCreate)), 1, + false, true)); + Constants.Add("BinaryWriter", + new InternalFunction( + x => new HassiumBinaryWriter(new BinaryWriter(((HassiumStream) x[0]).Value)), 1, false, + true)); + Constants.Add("BinaryReader", + new InternalFunction( + x => new HassiumBinaryReader(new BinaryReader(((HassiumStream) x[0]).Value)), 1, false, + true)); break; case "math": Constants.Add("Math", new HassiumMath()); @@ -1100,19 +1154,35 @@ public object Accept(UseNode node) Constants.Add("Debug", new HassiumDebug()); break; case "collections": - Constants.Add("Stack", new InternalFunction(x => new HassiumStack(x.Length == 0 ? new Stack() : new Stack(x[0].HInt().Value)), new []{0,1}, false, true)); - Constants.Add("Dictionary", new InternalFunction(x => new HassiumDictionary(new Dictionary()), 0, false, true)); + Constants.Add("Stack", + new InternalFunction( + x => + new HassiumStack(x.Length == 0 + ? new Stack() + : new Stack(x[0].HInt().Value)), new[] {0, 1}, false, true)); + Constants.Add("Dictionary", + new InternalFunction( + x => new HassiumDictionary(new Dictionary()), 0, false, + true)); break; case "net": case "network": - Constants.Add("WebClient", new InternalFunction(x => new HassiumWebClient(new WebClient()), 0, false, true)); - Constants.Add("TcpClient", new InternalFunction(x => new HassiumTcpClient(new TcpClient()), 0, false, true)); - Constants.Add("NetworkStream", new InternalFunction(x => new HassiumNetworkStream(new NetworkStream(((HassiumSocket)x[0]).Value)), 1, false, true)); - Constants.Add("HttpListener", new InternalFunction(x => new HassiumHttpListener(new HttpListener()), 0, false, true)); + Constants.Add("WebClient", + new InternalFunction(x => new HassiumWebClient(new WebClient()), 0, false, true)); + Constants.Add("TcpClient", + new InternalFunction(x => new HassiumTcpClient(new TcpClient()), 0, false, true)); + Constants.Add("NetworkStream", + new InternalFunction( + x => new HassiumNetworkStream(new NetworkStream(((HassiumSocket) x[0]).Value)), 1, false, + true)); + Constants.Add("HttpListener", + new InternalFunction(x => new HassiumHttpListener(new HttpListener()), 0, false, true)); break; case "text": - Constants.Add("StringBuilder", new InternalFunction(x => new HassiumStringBuilder(new StringBuilder()), 0, false, true)); - Constants.Add("Encoding", new InternalFunction(x => new HassiumEncoding(x[0].HString()), 1, false, true)); + Constants.Add("StringBuilder", + new InternalFunction(x => new HassiumStringBuilder(new StringBuilder()), 0, false, true)); + Constants.Add("Encoding", + new InternalFunction(x => new HassiumEncoding(x[0].HString()), 1, false, true)); if (!HassiumInterpreter.options.Secure) { Constants.Add("TextWriter", @@ -1124,9 +1194,12 @@ public object Accept(UseNode node) } break; case "drawing": - Constants.Add("Color", new InternalFunction(x => new HassiumColor(x), new []{1, 3, 4, 5}, false, true)); - Constants.Add("Bitmap", new InternalFunction(x => new HassiumBitmap(x), new []{1,2}, false, true)); - Constants.Add("Image", new InternalFunction(x => new HassiumImage(x[0].HString()), 1, false, true)); + Constants.Add("Color", + new InternalFunction(x => new HassiumColor(x), new[] {1, 3, 4, 5}, false, true)); + Constants.Add("Bitmap", + new InternalFunction(x => new HassiumBitmap(x), new[] {1, 2}, false, true)); + Constants.Add("Image", + new InternalFunction(x => new HassiumImage(x[0].HString()), 1, false, true)); break; default: throw new Exception("Unknown Module: " + node.Path); @@ -1182,7 +1255,7 @@ public object Accept(TryNode node) } finally { - if(tryStmt.FinallyBody != null) + if (tryStmt.FinallyBody != null) tryStmt.FinallyBody.Visit(this); } return null; @@ -1221,7 +1294,7 @@ public object Accept(WhileNode node) if (counter == 0) { - if(whileStmt.ElseBody != null) + if (whileStmt.ElseBody != null) whileStmt.ElseBody.Visit(this); } inLoop--; @@ -1230,11 +1303,11 @@ public object Accept(WhileNode node) private void VisitSubnodes(AstNode node) { - foreach(var nd in node.Children) + foreach (var nd in node.Children) { nd.Visit(this); if (continueLoop || breakLoop || returnFunc || exit) break; } } } -} +} \ No newline at end of file diff --git a/src/Hassium/Interpreter/ParseException.cs b/src/Hassium/Interpreter/ParseException.cs index ee199b3..7614a91 100644 --- a/src/Hassium/Interpreter/ParseException.cs +++ b/src/Hassium/Interpreter/ParseException.cs @@ -19,4 +19,4 @@ public ParseException(string message, int position) : base(message) Position = position; } } -} +} \ No newline at end of file diff --git a/src/Hassium/Interpreter/StackFrame.cs b/src/Hassium/Interpreter/StackFrame.cs index 17c9a4b..ce6ec49 100644 --- a/src/Hassium/Interpreter/StackFrame.cs +++ b/src/Hassium/Interpreter/StackFrame.cs @@ -6,11 +6,9 @@ namespace Hassium.Interpreter { public class StackFrame { - public HassiumObject Self { - get - { - return Locals.ContainsKey("this") ? Locals["this"] : null; - } + public HassiumObject Self + { + get { return Locals.ContainsKey("this") ? Locals["this"] : null; } } public LocalScope Scope { get; private set; } @@ -27,5 +25,4 @@ public StackFrame(LocalScope scope, HassiumObject self = null) Locals["this"] = self; } } -} - +} \ No newline at end of file diff --git a/src/Hassium/Lexer/IFunction.cs b/src/Hassium/Lexer/IFunction.cs index 1c0a65f..34209eb 100644 --- a/src/Hassium/Lexer/IFunction.cs +++ b/src/Hassium/Lexer/IFunction.cs @@ -6,5 +6,4 @@ public interface IFunction { HassiumObject Invoke(HassiumObject[] args); } -} - +} \ No newline at end of file diff --git a/src/Hassium/Lexer/Lexer.cs b/src/Hassium/Lexer/Lexer.cs index 1477643..1f1955d 100644 --- a/src/Hassium/Lexer/Lexer.cs +++ b/src/Hassium/Lexer/Lexer.cs @@ -35,13 +35,13 @@ public static string Minimize(string code) { char c = code[index]; - if((char.IsLetter(c) || "_".Contains(c))) + if ((char.IsLetter(c) || "_".Contains(c))) { isIdentifier = true; result += c; continue; } - else if(" \r\n".Contains(c) && !isIdentifier) + else if (" \r\n".Contains(c) && !isIdentifier) { continue; } @@ -50,17 +50,17 @@ public static string Minimize(string code) isIdentifier = false; } - if(c == '#') + if (c == '#') { - while(code[index++] != '\n') + while (code[index++] != '\n') { } continue; } - if(c == ';') + if (c == ';') { - if(index != 0 && !char.IsLetterOrDigit(code[index - 1])) + if (index != 0 && !char.IsLetterOrDigit(code[index - 1])) { continue; } @@ -78,7 +78,7 @@ public static string Minimize(string code) public List Tokenize() { EatWhiteSpaces(); - + while (HasChar()) { ReadToken(); @@ -246,6 +246,7 @@ private void ScanComment(bool oneLine) if (!oneLine && HasChar()) ReadChar(); } + /// /// Scans the string. /// @@ -263,7 +264,7 @@ private void ScanString(bool isVerbatim = false) while (HasChar() && (isEscaping || PeekChar() != quote)) { var currentChar = ReadChar(); - if(currentChar == '#' && !isEscaping && !isVerbatim && PeekChar() == '{') + if (currentChar == '#' && !isEscaping && !isVerbatim && PeekChar() == '{') { ReadChar(); if (PeekChar() == '}') @@ -334,15 +335,17 @@ private void ScanString(bool isVerbatim = false) } } - if(HasChar()) ReadChar(); + if (HasChar()) ReadChar(); else throw new ParseException("Unfinished string", position); Add(new Token(TokenType.String, stringBuilder)); } + private static bool IsHexChar(char c) { return "abcdefABCDEF".Contains(c); } + /// /// Scans a number /// @@ -368,7 +371,7 @@ private Token ScanNumber() var finalNumber = stringBuilder.ToString(); var baseName = ""; var baseSize = 0; - if(finalNumber.StartsWith("0x")) + if (finalNumber.StartsWith("0x")) { baseName = "hex"; baseSize = 16; @@ -383,9 +386,10 @@ private Token ScanNumber() baseName = "octal"; baseSize = 8; } - if(baseName != "") + if (baseName != "") { - if (finalNumber.Length == 2) throw new ParseException("Invalid " + baseName + " number: " + finalNumber, position); + if (finalNumber.Length == 2) + throw new ParseException("Invalid " + baseName + " number: " + finalNumber, position); try { return new Token(TokenType.Number, Convert.ToInt32(finalNumber.Substring(2), baseSize).ToString()); @@ -398,9 +402,11 @@ private Token ScanNumber() else { double temp = 0; - if(double.TryParse(finalNumber, NumberStyles.Any, CultureInfo.InvariantCulture, out temp)) + if (double.TryParse(finalNumber, NumberStyles.Any, CultureInfo.InvariantCulture, out temp)) { - return (temp == Math.Truncate(temp) && !finalNumber.Contains('.')) ? new Token(TokenType.Number, (int)temp) : new Token(TokenType.Number, temp); + return (temp == Math.Truncate(temp) && !finalNumber.Contains('.')) + ? new Token(TokenType.Number, (int) temp) + : new Token(TokenType.Number, temp); } else { @@ -420,19 +426,20 @@ private Token ScanIdentifier() { stringBuilder.Append(ReadChar()); } - if(PeekChar() == '`' && "0123456789i".Contains(PeekChar(1))) + if (PeekChar() == '`' && "0123456789i".Contains(PeekChar(1))) { stringBuilder.Append(ReadChar()); stringBuilder.Append(ReadChar()); } var finalId = stringBuilder.ToString(); - if (finalId.Contains('.')) throw new ParseException("Invalid character in Identifier: . (period)", position); + if (finalId.Contains('.')) + throw new ParseException("Invalid character in Identifier: . (period)", position); return new Token(TokenType.Identifier, finalId); } private void EatWhiteSpaces() { - while(HasChar() && char.IsWhiteSpace(PeekChar())) ReadChar(); + while (HasChar() && char.IsWhiteSpace(PeekChar())) ReadChar(); } private char PeekChar(int n = 0) @@ -450,5 +457,4 @@ private char ReadChar() return code[position++]; } } -} - +} \ No newline at end of file diff --git a/src/Hassium/Lexer/Token.cs b/src/Hassium/Lexer/Token.cs index 36d8a41..f028055 100644 --- a/src/Hassium/Lexer/Token.cs +++ b/src/Hassium/Lexer/Token.cs @@ -35,5 +35,4 @@ public Token(TokenType type, object value, int pos = -1) Position = pos; } } -} - +} \ No newline at end of file diff --git a/src/Hassium/Parser/Ast/ArgListNode.cs b/src/Hassium/Parser/Ast/ArgListNode.cs index 9218733..1581d38 100644 --- a/src/Hassium/Parser/Ast/ArgListNode.cs +++ b/src/Hassium/Parser/Ast/ArgListNode.cs @@ -2,7 +2,7 @@ namespace Hassium.Parser.Ast { - public class ArgListNode: AstNode + public class ArgListNode : AstNode { public ArgListNode(int position) : base(position) { @@ -13,5 +13,4 @@ public override object Visit(IVisitor visitor) return visitor.Accept(this); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/Parser/Ast/ArrayGetNode.cs b/src/Hassium/Parser/Ast/ArrayGetNode.cs index e906a50..ec23424 100644 --- a/src/Hassium/Parser/Ast/ArrayGetNode.cs +++ b/src/Hassium/Parser/Ast/ArrayGetNode.cs @@ -2,22 +2,16 @@ namespace Hassium.Parser.Ast { - public class ArrayGetNode: AstNode + public class ArrayGetNode : AstNode { public AstNode Target { - get - { - return Children[0]; - } + get { return Children[0]; } } public AstNode Arguments { - get - { - return Children[1]; - } + get { return Children[1]; } } public AstNode Count @@ -38,5 +32,4 @@ public override object Visit(IVisitor visitor) return visitor.Accept(this); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/Parser/Ast/ArrayInitializerNode.cs b/src/Hassium/Parser/Ast/ArrayInitializerNode.cs index 138d224..1e76e0e 100644 --- a/src/Hassium/Parser/Ast/ArrayInitializerNode.cs +++ b/src/Hassium/Parser/Ast/ArrayInitializerNode.cs @@ -38,4 +38,4 @@ public override object Visit(IVisitor visitor) return visitor.Accept(this); } } -} +} \ No newline at end of file diff --git a/src/Hassium/Parser/Ast/BinaryOpNode.cs b/src/Hassium/Parser/Ast/BinaryOpNode.cs index ed14b4f..4543bf3 100644 --- a/src/Hassium/Parser/Ast/BinaryOpNode.cs +++ b/src/Hassium/Parser/Ast/BinaryOpNode.cs @@ -36,20 +36,15 @@ public class BinOpNode : AstNode public BinaryOperation BinOp { get; set; } public BinaryOperation AssignOperation { get; set; } public bool IsOpAssign { get; set; } + public AstNode Left { - get - { - return Children [0]; - } + get { return Children[0]; } } public AstNode Right { - get - { - return Children [1]; - } + get { return Children[1]; } } public BinOpNode(int position, BinaryOperation type, AstNode left, AstNode right) : base(position) @@ -60,7 +55,8 @@ public BinOpNode(int position, BinaryOperation type, AstNode left, AstNode right Children.Add(right); } - public BinOpNode(int position, BinaryOperation type, BinaryOperation assign, AstNode left, AstNode right) : base(position) + public BinOpNode(int position, BinaryOperation type, BinaryOperation assign, AstNode left, AstNode right) + : base(position) { BinOp = type; AssignOperation = assign; @@ -74,5 +70,4 @@ public override object Visit(IVisitor visitor) return visitor.Accept(this); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/Parser/Ast/BreakNode.cs b/src/Hassium/Parser/Ast/BreakNode.cs index 5173729..e5de33a 100644 --- a/src/Hassium/Parser/Ast/BreakNode.cs +++ b/src/Hassium/Parser/Ast/BreakNode.cs @@ -13,4 +13,4 @@ public override object Visit(IVisitor visitor) return visitor.Accept(this); } } -} +} \ No newline at end of file diff --git a/src/Hassium/Parser/Ast/ClassNode.cs b/src/Hassium/Parser/Ast/ClassNode.cs index e7cbf02..dabe3ac 100644 --- a/src/Hassium/Parser/Ast/ClassNode.cs +++ b/src/Hassium/Parser/Ast/ClassNode.cs @@ -2,16 +2,13 @@ namespace Hassium.Parser.Ast { - public class ClassNode: AstNode + public class ClassNode : AstNode { public string Name { get; private set; } public AstNode Body { - get - { - return Children[0]; - } + get { return Children[0]; } } public string Extends { get; private set; } @@ -28,5 +25,4 @@ public override object Visit(IVisitor visitor) return visitor.Accept(this); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/Parser/Ast/CodeBlock.cs b/src/Hassium/Parser/Ast/CodeBlock.cs index 21d9489..6bd0307 100644 --- a/src/Hassium/Parser/Ast/CodeBlock.cs +++ b/src/Hassium/Parser/Ast/CodeBlock.cs @@ -2,7 +2,7 @@ namespace Hassium.Parser.Ast { - public class CodeBlock: AstNode + public class CodeBlock : AstNode { public CodeBlock(int codePos) : base(codePos) { @@ -13,5 +13,4 @@ public override object Visit(IVisitor visitor) return visitor.Accept(this); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/Parser/Ast/ConditionalOpNode.cs b/src/Hassium/Parser/Ast/ConditionalOpNode.cs index a7e1742..db0e0c1 100644 --- a/src/Hassium/Parser/Ast/ConditionalOpNode.cs +++ b/src/Hassium/Parser/Ast/ConditionalOpNode.cs @@ -6,27 +6,21 @@ public class ConditionalOpNode : AstNode { public AstNode Predicate { - get - { - return Children[0]; - } + get { return Children[0]; } } + public AstNode Body { - get - { - return Children[1]; - } + get { return Children[1]; } } + public AstNode ElseBody { - get - { - return Children[2]; - } + get { return Children[2]; } } - public ConditionalOpNode(int position, AstNode predicate, AstNode body) : this(position, predicate, body, new CodeBlock(position)) + public ConditionalOpNode(int position, AstNode predicate, AstNode body) + : this(position, predicate, body, new CodeBlock(position)) { } @@ -42,4 +36,4 @@ public override object Visit(IVisitor visitor) return visitor.Accept(this); } } -} +} \ No newline at end of file diff --git a/src/Hassium/Parser/Ast/ContinueNode.cs b/src/Hassium/Parser/Ast/ContinueNode.cs index 8999463..3cb4500 100644 --- a/src/Hassium/Parser/Ast/ContinueNode.cs +++ b/src/Hassium/Parser/Ast/ContinueNode.cs @@ -13,4 +13,4 @@ public override object Visit(IVisitor visitor) return visitor.Accept(this); } } -} +} \ No newline at end of file diff --git a/src/Hassium/Parser/Ast/Expression.cs b/src/Hassium/Parser/Ast/Expression.cs index 3d994ef..99f8742 100644 --- a/src/Hassium/Parser/Ast/Expression.cs +++ b/src/Hassium/Parser/Ast/Expression.cs @@ -14,4 +14,4 @@ public override object Visit(IVisitor visitor) return visitor.Accept(this); } } -} +} \ No newline at end of file diff --git a/src/Hassium/Parser/Ast/ForEachNode.cs b/src/Hassium/Parser/Ast/ForEachNode.cs index a4b33a2..21b89db 100644 --- a/src/Hassium/Parser/Ast/ForEachNode.cs +++ b/src/Hassium/Parser/Ast/ForEachNode.cs @@ -6,26 +6,17 @@ public class ForEachNode : AstNode { public AstNode Needle { - get - { - return Children[0]; - } + get { return Children[0]; } } public AstNode Haystack { - get - { - return Children[1]; - } + get { return Children[1]; } } public AstNode Body { - get - { - return Children[2]; - } + get { return Children[2]; } } public ForEachNode(int position, AstNode needle, AstNode haystack, AstNode body) : base(position) @@ -40,5 +31,4 @@ public override object Visit(IVisitor visitor) return visitor.Accept(this); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/Parser/Ast/ForNode.cs b/src/Hassium/Parser/Ast/ForNode.cs index 7e23830..ac5e04f 100644 --- a/src/Hassium/Parser/Ast/ForNode.cs +++ b/src/Hassium/Parser/Ast/ForNode.cs @@ -2,38 +2,26 @@ namespace Hassium.Parser.Ast { - public class ForNode: AstNode + public class ForNode : AstNode { public AstNode Left { - get - { - return Children[0]; - } + get { return Children[0]; } } public AstNode Predicate { - get - { - return Children[1]; - } + get { return Children[1]; } } public AstNode Right { - get - { - return Children[2]; - } + get { return Children[2]; } } public AstNode Body { - get - { - return Children[3]; - } + get { return Children[3]; } } public ForNode(int position, AstNode left, AstNode predicate, AstNode right, AstNode body) : base(position) @@ -49,5 +37,4 @@ public override object Visit(IVisitor visitor) return visitor.Accept(this); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/Parser/Ast/FuncNode.cs b/src/Hassium/Parser/Ast/FuncNode.cs index b9ede88..350ecb6 100644 --- a/src/Hassium/Parser/Ast/FuncNode.cs +++ b/src/Hassium/Parser/Ast/FuncNode.cs @@ -3,17 +3,14 @@ namespace Hassium.Parser.Ast { - public class FuncNode: AstNode + public class FuncNode : AstNode { public string Name { get; private set; } public List Parameters { get; private set; } public AstNode Body { - get - { - return Children[0]; - } + get { return Children[0]; } } public FuncNode(int position, string name, List parameters, AstNode body) : base(position) @@ -33,5 +30,4 @@ public override object Visit(IVisitor visitor) return visitor.Accept(this); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/Parser/Ast/FunctionCallNode.cs b/src/Hassium/Parser/Ast/FunctionCallNode.cs index dfaa6b8..cdd40de 100644 --- a/src/Hassium/Parser/Ast/FunctionCallNode.cs +++ b/src/Hassium/Parser/Ast/FunctionCallNode.cs @@ -2,21 +2,16 @@ namespace Hassium.Parser.Ast { - public class FunctionCallNode: AstNode + public class FunctionCallNode : AstNode { public AstNode Target { - get - { - return Children[0]; - } + get { return Children[0]; } } + public AstNode Arguments { - get - { - return Children[1]; - } + get { return Children[1]; } } public FunctionCallNode(int position, AstNode target, AstNode arguments) : base(position) @@ -30,5 +25,4 @@ public override object Visit(IVisitor visitor) return visitor.Accept(this); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/Parser/Ast/IdentifierNode.cs b/src/Hassium/Parser/Ast/IdentifierNode.cs index e91ba86..fb8f610 100644 --- a/src/Hassium/Parser/Ast/IdentifierNode.cs +++ b/src/Hassium/Parser/Ast/IdentifierNode.cs @@ -2,7 +2,7 @@ namespace Hassium.Parser.Ast { - public class IdentifierNode: AstNode + public class IdentifierNode : AstNode { public string Identifier { get; private set; } @@ -21,5 +21,4 @@ public override object Visit(IVisitor visitor) return visitor.Accept(this); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/Parser/Ast/IfNode.cs b/src/Hassium/Parser/Ast/IfNode.cs index 8238fb9..e0c8f89 100644 --- a/src/Hassium/Parser/Ast/IfNode.cs +++ b/src/Hassium/Parser/Ast/IfNode.cs @@ -2,31 +2,25 @@ namespace Hassium.Parser.Ast { - public class IfNode: AstNode + public class IfNode : AstNode { public AstNode Predicate { - get - { - return Children[0]; - } + get { return Children[0]; } } + public AstNode Body { - get - { - return Children[1]; - } + get { return Children[1]; } } + public AstNode ElseBody { - get - { - return Children[2]; - } + get { return Children[2]; } } - public IfNode(int position, AstNode predicate, AstNode body) : this(position, predicate, body, new CodeBlock(position)) + public IfNode(int position, AstNode predicate, AstNode body) + : this(position, predicate, body, new CodeBlock(position)) { } @@ -42,5 +36,4 @@ public override object Visit(IVisitor visitor) return visitor.Accept(this); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/Parser/Ast/InstanceNode.cs b/src/Hassium/Parser/Ast/InstanceNode.cs index 665cb89..fed5def 100644 --- a/src/Hassium/Parser/Ast/InstanceNode.cs +++ b/src/Hassium/Parser/Ast/InstanceNode.cs @@ -19,4 +19,4 @@ public override object Visit(IVisitor visitor) return visitor.Accept(this); } } -} +} \ No newline at end of file diff --git a/src/Hassium/Parser/Ast/LambdaFuncNode.cs b/src/Hassium/Parser/Ast/LambdaFuncNode.cs index 6a1296c..84fec07 100644 --- a/src/Hassium/Parser/Ast/LambdaFuncNode.cs +++ b/src/Hassium/Parser/Ast/LambdaFuncNode.cs @@ -9,10 +9,7 @@ public class LambdaFuncNode : AstNode public AstNode Body { - get - { - return Children[0]; - } + get { return Children[0]; } } public LambdaFuncNode(int position, List paramaters, AstNode body) : base(position) @@ -21,7 +18,7 @@ public LambdaFuncNode(int position, List paramaters, AstNode body) : bas Children.Add(body); } - public static explicit operator FuncNode (LambdaFuncNode funcNode) + public static explicit operator FuncNode(LambdaFuncNode funcNode) { return new FuncNode(funcNode.Position, "", funcNode.Parameters, funcNode.Body); } @@ -31,4 +28,4 @@ public override object Visit(IVisitor visitor) return visitor.Accept(this); } } -} +} \ No newline at end of file diff --git a/src/Hassium/Parser/Ast/MemberAccessNode.cs b/src/Hassium/Parser/Ast/MemberAccessNode.cs index 0e62a4f..caed152 100644 --- a/src/Hassium/Parser/Ast/MemberAccessNode.cs +++ b/src/Hassium/Parser/Ast/MemberAccessNode.cs @@ -6,17 +6,10 @@ public class MemberAccessNode : AstNode { public AstNode Left { - get - { - return Children[0]; - } + get { return Children[0]; } } - public string Member - { - private set; - get; - } + public string Member { private set; get; } public MemberAccessNode(int position, AstNode left, string identifier) : base(position) { @@ -34,5 +27,4 @@ public override object Visit(IVisitor visitor) return visitor.Accept(this); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/Parser/Ast/MentalNode.cs b/src/Hassium/Parser/Ast/MentalNode.cs index 6af5a85..cf0f370 100644 --- a/src/Hassium/Parser/Ast/MentalNode.cs +++ b/src/Hassium/Parser/Ast/MentalNode.cs @@ -2,7 +2,7 @@ namespace Hassium.Parser.Ast { - public class MentalNode: AstNode + public class MentalNode : AstNode { public string OpType { get; private set; } @@ -22,5 +22,4 @@ public override object Visit(IVisitor visitor) return visitor.Accept(this); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/Parser/Ast/NumberNode.cs b/src/Hassium/Parser/Ast/NumberNode.cs index f343de6..0e5575a 100644 --- a/src/Hassium/Parser/Ast/NumberNode.cs +++ b/src/Hassium/Parser/Ast/NumberNode.cs @@ -2,12 +2,12 @@ namespace Hassium.Parser.Ast { - public class NumberNode: AstNode + public class NumberNode : AstNode { public double Value { private set; get; } public bool IsInt { get; private set; } - public NumberNode (int position, double value, bool isint) : base(position) + public NumberNode(int position, double value, bool isint) : base(position) { Value = value; IsInt = isint; @@ -18,5 +18,4 @@ public override object Visit(IVisitor visitor) return visitor.Accept(this); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/Parser/Ast/PropertyNode.cs b/src/Hassium/Parser/Ast/PropertyNode.cs index 165fc49..50c1d94 100644 --- a/src/Hassium/Parser/Ast/PropertyNode.cs +++ b/src/Hassium/Parser/Ast/PropertyNode.cs @@ -26,4 +26,4 @@ public override object Visit(IVisitor visitor) return visitor.Accept(this); } } -} +} \ No newline at end of file diff --git a/src/Hassium/Parser/Ast/ReturnNode.cs b/src/Hassium/Parser/Ast/ReturnNode.cs index 4d4da5a..5dc3e22 100644 --- a/src/Hassium/Parser/Ast/ReturnNode.cs +++ b/src/Hassium/Parser/Ast/ReturnNode.cs @@ -2,18 +2,16 @@ namespace Hassium.Parser.Ast { - public class ReturnNode: AstNode + public class ReturnNode : AstNode { public AstNode Value { - get { - return Children.Count == 0 ? null : Children[0]; - } + get { return Children.Count == 0 ? null : Children[0]; } } public ReturnNode(int position, AstNode value) : base(position) { - if(value != null) Children.Add(value); + if (value != null) Children.Add(value); } public override object Visit(IVisitor visitor) @@ -21,5 +19,4 @@ public override object Visit(IVisitor visitor) return visitor.Accept(this); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/Parser/Ast/StatementNode.cs b/src/Hassium/Parser/Ast/StatementNode.cs index ee5f89f..70917d8 100644 --- a/src/Hassium/Parser/Ast/StatementNode.cs +++ b/src/Hassium/Parser/Ast/StatementNode.cs @@ -2,12 +2,11 @@ namespace Hassium.Parser.Ast { - public class StatementNode: AstNode + public class StatementNode : AstNode { public override object Visit(IVisitor visitor) { return visitor.Accept(this); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/Parser/Ast/StringNode.cs b/src/Hassium/Parser/Ast/StringNode.cs index da9cda9..575fbee 100644 --- a/src/Hassium/Parser/Ast/StringNode.cs +++ b/src/Hassium/Parser/Ast/StringNode.cs @@ -2,7 +2,7 @@ namespace Hassium.Parser.Ast { - public class StringNode: AstNode + public class StringNode : AstNode { public string Value { get; private set; } @@ -16,5 +16,4 @@ public override object Visit(IVisitor visitor) return visitor.Accept(this); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/Parser/Ast/SwitchNode.cs b/src/Hassium/Parser/Ast/SwitchNode.cs index defd8e7..01a4c37 100644 --- a/src/Hassium/Parser/Ast/SwitchNode.cs +++ b/src/Hassium/Parser/Ast/SwitchNode.cs @@ -7,15 +7,14 @@ public class SwitchNode : AstNode { public AstNode Predicate { - get - { - return Children[0]; - } + get { return Children[0]; } } + public List Body { get; set; } public AstNode DefaultBody { get; set; } - public SwitchNode(int position, AstNode predicate, List body, AstNode defaultBody = null) : base(position) + public SwitchNode(int position, AstNode predicate, List body, AstNode defaultBody = null) + : base(position) { Children.Add(predicate); Body = body; @@ -30,7 +29,7 @@ public override object Visit(IVisitor visitor) public class CaseNode : AstNode { - public List Values { get; set; } + public List Values { get; set; } public AstNode Body { get; set; } public CaseNode(int position, List predicate, AstNode body) : base(position) @@ -44,4 +43,4 @@ public override object Visit(IVisitor visitor) return visitor.Accept(this); } } -} +} \ No newline at end of file diff --git a/src/Hassium/Parser/Ast/ThreadNode.cs b/src/Hassium/Parser/Ast/ThreadNode.cs index 13572fb..14cdd03 100644 --- a/src/Hassium/Parser/Ast/ThreadNode.cs +++ b/src/Hassium/Parser/Ast/ThreadNode.cs @@ -2,14 +2,11 @@ namespace Hassium.Parser.Ast { - public class ThreadNode: AstNode + public class ThreadNode : AstNode { public AstNode Node { - get - { - return Children[0]; - } + get { return Children[0]; } } public ThreadNode(int position, AstNode node) : base(position) diff --git a/src/Hassium/Parser/Ast/TryNode.cs b/src/Hassium/Parser/Ast/TryNode.cs index a1dc8b7..b2794a9 100644 --- a/src/Hassium/Parser/Ast/TryNode.cs +++ b/src/Hassium/Parser/Ast/TryNode.cs @@ -2,30 +2,21 @@ namespace Hassium.Parser.Ast { - public class TryNode: AstNode + public class TryNode : AstNode { public AstNode Body { - get - { - return Children[0]; - } + get { return Children[0]; } } public AstNode CatchBody { - get - { - return Children[1]; - } + get { return Children[1]; } } public AstNode FinallyBody { - get - { - return Children.Count < 3 ? null : Children[2]; - } + get { return Children.Count < 3 ? null : Children[2]; } } public TryNode(int position, AstNode body, AstNode catchBody) : this(position, body, catchBody, null) @@ -36,7 +27,7 @@ public TryNode(int position, AstNode body, AstNode catchBody, AstNode finallyBod { Children.Add(body); Children.Add(catchBody); - if(finallyBody != null) Children.Add(finallyBody); + if (finallyBody != null) Children.Add(finallyBody); } public override object Visit(IVisitor visitor) @@ -44,5 +35,4 @@ public override object Visit(IVisitor visitor) return visitor.Accept(this); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/Parser/Ast/UnaryOpNode.cs b/src/Hassium/Parser/Ast/UnaryOpNode.cs index 8b12cc8..eede1e1 100644 --- a/src/Hassium/Parser/Ast/UnaryOpNode.cs +++ b/src/Hassium/Parser/Ast/UnaryOpNode.cs @@ -10,16 +10,13 @@ public enum UnaryOperation } //Class for the urinary operations - public class UnaryOpNode: AstNode + public class UnaryOpNode : AstNode { public UnaryOperation UnOp { get; set; } public AstNode Value { - get - { - return Children[0]; - } + get { return Children[0]; } } public UnaryOpNode(int position, UnaryOperation type, AstNode value) : base(position) @@ -33,5 +30,4 @@ public override object Visit(IVisitor visitor) return visitor.Accept(this); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/Parser/Ast/UncheckedNode.cs b/src/Hassium/Parser/Ast/UncheckedNode.cs index 949511c..307c98b 100644 --- a/src/Hassium/Parser/Ast/UncheckedNode.cs +++ b/src/Hassium/Parser/Ast/UncheckedNode.cs @@ -2,14 +2,11 @@ namespace Hassium.Parser.Ast { - public class UncheckedNode: AstNode + public class UncheckedNode : AstNode { public AstNode Node { - get - { - return Children[0]; - } + get { return Children[0]; } } public UncheckedNode(int position, AstNode node) : base(position) diff --git a/src/Hassium/Parser/Ast/UseNode.cs b/src/Hassium/Parser/Ast/UseNode.cs index 1156bdc..07ca5b3 100644 --- a/src/Hassium/Parser/Ast/UseNode.cs +++ b/src/Hassium/Parser/Ast/UseNode.cs @@ -2,7 +2,7 @@ namespace Hassium.Parser.Ast { - public class UseNode: AstNode + public class UseNode : AstNode { public string Path { get; private set; } public string Name { get; private set; } @@ -10,7 +10,8 @@ public class UseNode: AstNode public bool IsModule { get; private set; } public bool IsLibrary { get; private set; } - public UseNode(int position, string path, string name, bool global, bool module, bool library = false) : base(position) + public UseNode(int position, string path, string name, bool global, bool module, bool library = false) + : base(position) { Path = path; Name = name; @@ -24,5 +25,4 @@ public override object Visit(IVisitor visitor) return visitor.Accept(this); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/Parser/Ast/WhileNode.cs b/src/Hassium/Parser/Ast/WhileNode.cs index 64add0d..bddb9fb 100644 --- a/src/Hassium/Parser/Ast/WhileNode.cs +++ b/src/Hassium/Parser/Ast/WhileNode.cs @@ -2,31 +2,25 @@ namespace Hassium.Parser.Ast { - public class WhileNode: AstNode + public class WhileNode : AstNode { public AstNode Predicate { - get - { - return Children[0]; - } + get { return Children[0]; } } + public AstNode Body { - get - { - return Children[1]; - } + get { return Children[1]; } } + public AstNode ElseBody { - get - { - return Children[2]; - } + get { return Children[2]; } } - public WhileNode(int position, AstNode predicate, AstNode body) : this(position, predicate, body, new CodeBlock(position)) + public WhileNode(int position, AstNode predicate, AstNode body) + : this(position, predicate, body, new CodeBlock(position)) { } @@ -42,5 +36,4 @@ public override object Visit(IVisitor visitor) return visitor.Accept(this); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/Parser/AstNode.cs b/src/Hassium/Parser/AstNode.cs index 60f4134..8c34032 100644 --- a/src/Hassium/Parser/AstNode.cs +++ b/src/Hassium/Parser/AstNode.cs @@ -8,11 +8,7 @@ namespace Hassium.Parser { public abstract class AstNode { - public List Children - { - get; - private set; - } + public List Children { get; private set; } public int Position { get; set; } @@ -20,7 +16,8 @@ public bool CanBeIndexed { get { - return this is IdentifierNode || this is MemberAccessNode || this is FunctionCallNode || this is StringNode || + return this is IdentifierNode || this is MemberAccessNode || this is FunctionCallNode || + this is StringNode || this is InstanceNode || this is ArrayInitializerNode || this is ArrayGetNode; } } @@ -29,9 +26,11 @@ public bool ReturnsValue { get { - return this is IdentifierNode || this is MemberAccessNode || this is FunctionCallNode || this is StringNode || + return this is IdentifierNode || this is MemberAccessNode || this is FunctionCallNode || + this is StringNode || this is InstanceNode || this is ArrayInitializerNode || this is ArrayGetNode || this is BinOpNode || - this is ConditionalOpNode || this is LambdaFuncNode || this is NumberNode || this is UnaryOpNode || (this is CodeBlock && this.Children.Any(x => x.ReturnsValue)); + this is ConditionalOpNode || this is LambdaFuncNode || this is NumberNode || this is UnaryOpNode || + (this is CodeBlock && this.Children.Any(x => x.ReturnsValue)); } } @@ -56,8 +55,8 @@ public bool CanBeModified get { return this is IdentifierNode || this is MemberAccessNode; } } - protected AstNode () : this(-1) - { + protected AstNode() : this(-1) + { } protected AstNode(int position) @@ -66,5 +65,4 @@ protected AstNode(int position) Position = position; } } -} - +} \ No newline at end of file diff --git a/src/Hassium/Parser/Parser.cs b/src/Hassium/Parser/Parser.cs index 318a7a7..bb19fa2 100644 --- a/src/Hassium/Parser/Parser.cs +++ b/src/Hassium/Parser/Parser.cs @@ -15,14 +15,14 @@ public class Parser private List tokens; public int position; - public int codePos { get { return CurrentToken().Position; } } + public int codePos + { + get { return CurrentToken().Position; } + } public bool EndOfStream { - get - { - return tokens.Count <= position; - } + get { return tokens.Count <= position; } } public Parser(List tokens) @@ -60,7 +60,8 @@ public bool MatchToken(TokenType clazz) public bool MatchToken(TokenType clazz, string value) { - return position < tokens.Count && tokens[position].TokenClass == clazz && tokens[position].Value.ToString() == value; + return position < tokens.Count && tokens[position].TokenClass == clazz && + tokens[position].Value.ToString() == value; } public bool AcceptToken(TokenType clazz) @@ -92,7 +93,9 @@ public Token ExpectToken(TokenType clazz) public Token ExpectToken(TokenType clazz, string value) { - return MatchToken(clazz, value) ? tokens[position++] : new Token(TokenType.Exception, "Tokens did not match"); + return MatchToken(clazz, value) + ? tokens[position++] + : new Token(TokenType.Exception, "Tokens did not match"); } public static AstNode ParseStatement(Parser parser) @@ -213,7 +216,8 @@ public static AstNode ParseProperty(Parser parser) getBody.Children.Add(new ReturnNode(parser.codePos, ParseExpression(parser))); setBody = new CodeBlock(parser.codePos); - setBody.Children.Add(new BinOpNode(parser.codePos, BinaryOperation.Assignment, ParseExpression(parser), new IdentifierNode(parser.codePos, "value"))); + setBody.Children.Add(new BinOpNode(parser.codePos, BinaryOperation.Assignment, ParseExpression(parser), + new IdentifierNode(parser.codePos, "value"))); parser.ExpectToken(TokenType.EndOfLine); } else @@ -227,7 +231,8 @@ public static AstNode ParseProperty(Parser parser) autoProp = true; getBody = new CodeBlock(parser.codePos); getBody.Children.Add(new ReturnNode(parser.codePos, - new MemberAccessNode(parser.codePos, new IdentifierNode(parser.codePos, "this"), "__prop__" + name))); + new MemberAccessNode(parser.codePos, new IdentifierNode(parser.codePos, "this"), + "__prop__" + name))); } else getBody = ParseCodeBlock(parser); @@ -235,12 +240,12 @@ public static AstNode ParseProperty(Parser parser) if (parser.AcceptToken(TokenType.Identifier, "set")) { - if (parser.AcceptToken(TokenType.EndOfLine, ";")) { setBody = new CodeBlock(parser.codePos); setBody.Children.Add(new BinOpNode(parser.codePos, BinaryOperation.Assignment, - new MemberAccessNode(parser.codePos, new IdentifierNode(parser.codePos, "this"), "__prop__" + name), + new MemberAccessNode(parser.codePos, new IdentifierNode(parser.codePos, "this"), + "__prop__" + name), new IdentifierNode(parser.codePos, "value"))); } else @@ -259,8 +264,10 @@ public static AstNode ParseProperty(Parser parser) parser.ExpectToken(TokenType.Brace, "}"); } - getnode = new FuncNode(parser.codePos, "__getprop__" + name, new List { "this" }, getBody); - setnode = setBody == null ? null : new FuncNode(parser.codePos, "__setprop__" + name, new List { "this", "value" }, setBody); + getnode = new FuncNode(parser.codePos, "__getprop__" + name, new List {"this"}, getBody); + setnode = setBody == null + ? null + : new FuncNode(parser.codePos, "__setprop__" + name, new List {"this", "value"}, setBody); return new PropertyNode(pos, name, getnode, setnode); } @@ -335,7 +342,6 @@ public static AstNode ParseUse(Parser parser) return ret; } - public static AstNode ParseTryCatch(Parser parser) { @@ -391,7 +397,7 @@ public static AstNode ParseSwitch(Parser parser) { int cpos = parser.codePos; parser.ExpectToken(TokenType.Identifier, "case"); - var pred = new List { ParseExpression(parser) }; + var pred = new List {ParseExpression(parser)}; parser.ExpectToken(TokenType.Identifier, ":"); while (parser.MatchToken(TokenType.Identifier, "case")) { @@ -447,7 +453,9 @@ public static AstNode ParseForEach(Parser parser) parser.ExpectToken(TokenType.Identifier, "foreach"); parser.ExpectToken(TokenType.Parentheses, "("); AstNode needle = null; - needle = parser.CurrentToken().Value.ToString() == "[" ? ParseArrayInitializer(parser) : ParseIdentifier(parser); + needle = parser.CurrentToken().Value.ToString() == "[" + ? ParseArrayInitializer(parser) + : ParseIdentifier(parser); parser.ExpectToken(TokenType.Identifier, "in"); AstNode haystack = ParseStatement(parser); parser.ExpectToken(TokenType.Parentheses, ")"); @@ -573,7 +581,7 @@ private static AstNode ParseAssignment(Parser parser) AstNode left = ParseConditional(parser); while (parser.CurrentToken().TokenClass == TokenType.Assignment || - parser.CurrentToken().TokenClass == TokenType.OpAssign) + parser.CurrentToken().TokenClass == TokenType.OpAssign) { if (parser.AcceptToken(TokenType.Assignment)) { @@ -616,8 +624,9 @@ private static AstNode ParseLogicalOr(Parser parser) AstNode left = ParseLogicalAnd(parser); - while (parser.CurrentToken().TokenClass == TokenType.Comparison || parser.CurrentToken().TokenClass == TokenType.Operation) - { + while (parser.CurrentToken().TokenClass == TokenType.Comparison || + parser.CurrentToken().TokenClass == TokenType.Operation) + { if (parser.AcceptToken(TokenType.Comparison, "||")) { var right = ParseLogicalAnd(parser); @@ -650,7 +659,7 @@ private static AstNode ParseLogicalAnd(Parser parser) return left; } - + private static AstNode ParseEquality(Parser parser) { int pos = parser.codePos; @@ -658,7 +667,7 @@ private static AstNode ParseEquality(Parser parser) AstNode left = ParseComparison(parser); while (parser.CurrentToken().TokenClass == TokenType.Comparison || - parser.CurrentToken().TokenClass == TokenType.Operation) + parser.CurrentToken().TokenClass == TokenType.Operation) { if (parser.AcceptToken(TokenType.Comparison, "==")) { @@ -683,7 +692,8 @@ private static AstNode ParseComparison(Parser parser) AstNode left = ParseOr(parser); - while (parser.CurrentToken().TokenClass == TokenType.Comparison || parser.CurrentToken().Value.ToString() == "is") + while (parser.CurrentToken().TokenClass == TokenType.Comparison || + parser.CurrentToken().Value.ToString() == "is") { if (parser.AcceptToken(TokenType.Comparison, "<")) { @@ -812,7 +822,6 @@ private static AstNode ParseAdditive(Parser parser) } else break; - } return left; } @@ -822,7 +831,7 @@ private static AstNode ParseMultiplicative(Parser parser) int pos = parser.codePos; AstNode left = ParseExponent(parser); - + /*if(parser.AcceptToken(TokenType.Operation, "**")) { AstNode right = ParseMultiplicative(parser); @@ -859,8 +868,9 @@ private static AstNode ParseMultiplicative(Parser parser) parser.ExpectToken(TokenType.EndOfLine); if (left is ArrayInitializerNode) - return new LambdaFuncNode(pos, ((ArrayInitializerNode)left).Value.Values.Select(x => x.ToString()).ToList(), body); - return new LambdaFuncNode(pos, new List() { left.ToString() }, body); + return new LambdaFuncNode(pos, + ((ArrayInitializerNode) left).Value.Values.Select(x => x.ToString()).ToList(), body); + return new LambdaFuncNode(pos, new List() {left.ToString()}, body); } else { @@ -990,7 +1000,8 @@ private static AstNode ParseTerm(Parser parser) var curt = parser.CurrentToken(); if (curt.TokenClass == TokenType.Number) { - return new NumberNode(pos, Convert.ToDouble(parser.ExpectToken(TokenType.Number).Value), parser.PreviousToken().Value is int); + return new NumberNode(pos, Convert.ToDouble(parser.ExpectToken(TokenType.Number).Value), + parser.PreviousToken().Value is int); } else if (curt.TokenClass == TokenType.Parentheses) { @@ -1023,7 +1034,6 @@ private static AstNode ParseTerm(Parser parser) { throw new ParseException("Unexpected " + curt.Value, pos); } - } #endregion @@ -1041,7 +1051,7 @@ public static ArrayIndexerNode ParseArrayIndexer(Parser parser) ret.Children.Add(ParseExpression(parser)); } if (ret.Children.Count == 2 && ret.Children[1].ToString() == ":") - throw new ParseException("Expected slice number", parser.codePos); + throw new ParseException("Expected slice number", parser.codePos); parser.ExpectToken(TokenType.Bracket, "]"); return ret; @@ -1054,7 +1064,7 @@ public static ArrayInitializerNode ParseArrayInitializer(Parser parser) ret.IsDictionary = false; while (!parser.MatchToken(TokenType.Bracket, "]")) - { + { var ct1 = ParseExpression(parser); if (parser.AcceptToken(TokenType.Identifier, ":")) { @@ -1111,10 +1121,11 @@ public static AstNode ParseInstance(Parser parser) AstNode res = new IdentifierNode(parser.codePos, target); while (parser.AcceptToken(TokenType.Dot, ".")) { - res = new MemberAccessNode(parser.codePos, res, parser.ExpectToken(TokenType.Identifier).Value.ToString()); + res = new MemberAccessNode(parser.codePos, res, + parser.ExpectToken(TokenType.Identifier).Value.ToString()); } parser.ExpectToken(TokenType.Parentheses, "("); - + return new InstanceNode(pos, new FunctionCallNode(parser.codePos, res, ParseArgList(parser))); } @@ -1163,5 +1174,4 @@ public static UncheckedNode ParseUnchecked(Parser parser) return new UncheckedNode(pos, node); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/Program.cs b/src/Hassium/Program.cs index e56f4f7..7dfe5da 100644 --- a/src/Hassium/Program.cs +++ b/src/Hassium/Program.cs @@ -79,7 +79,7 @@ public static void Main(string[] args) Console.WriteLine(); if (e is ParseException) { - printErr(options.Code, (ParseException)e); + printErr(options.Code, (ParseException) e); } else { @@ -107,8 +107,8 @@ private static void printErr(string str, ParseException e) _x = _x.Take(line - 1); var column = idx - (string.Join("\n", _x).Length + (_x.Any() ? 1 : 0)) + 1; Console.WriteLine("Error at position " + idx + ", line " + line - + " column " + column + ": " + - e.Message); + + " column " + column + ": " + + e.Message); Console.WriteLine(" " + trimd); Console.WriteLine(new string(' ', 2 + (column - (res.Length - trimd.Length))) + '^'); } @@ -116,8 +116,8 @@ private static void printErr(string str, ParseException e) public static string GetVersion() { return Assembly.GetExecutingAssembly().GetName().Version.Major + "." + - Assembly.GetExecutingAssembly().GetName().Version.Minor + "." + - Assembly.GetExecutingAssembly().GetName().Version.Build; + Assembly.GetExecutingAssembly().GetName().Version.Minor + "." + + Assembly.GetExecutingAssembly().GetName().Version.Build; } private static void preformSetUp(IList args) @@ -228,5 +228,4 @@ private static void enterInteractive() } } } -} - +} \ No newline at end of file diff --git a/src/Hassium/Properties/AssemblyInfo.cs b/src/Hassium/Properties/AssemblyInfo.cs index a74fdbf..c1cfb31 100644 --- a/src/Hassium/Properties/AssemblyInfo.cs +++ b/src/Hassium/Properties/AssemblyInfo.cs @@ -2,6 +2,7 @@ // Information about this assembly is defined by the following attributes. // Change them to the values specific to your project. + [assembly: AssemblyTitle("Hassium")] [assembly: AssemblyDescription("Hassium is a programming language written for the heck of it.")] [assembly: AssemblyConfiguration("")] @@ -13,9 +14,9 @@ // The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". // The form "{Major}.{Minor}.*" will automatically update the build and revision, // and "{Major}.{Minor}.{Build}.*" will update just the revision. + [assembly: AssemblyVersion("0.9.3")] // The following attributes are used to specify the signing key for the assembly, // if desired. See the Mono documentation for more information about signing. //[assembly: AssemblyDelaySign(false)] -//[assembly: AssemblyKeyFile("")] - +//[assembly: AssemblyKeyFile("")] \ No newline at end of file diff --git a/src/Hassium/Semantics/LocalScope.cs b/src/Hassium/Semantics/LocalScope.cs index b598841..a5c35ab 100644 --- a/src/Hassium/Semantics/LocalScope.cs +++ b/src/Hassium/Semantics/LocalScope.cs @@ -11,5 +11,4 @@ public LocalScope() Symbols = new List(); } } -} - +} \ No newline at end of file diff --git a/src/Hassium/Semantics/SemanticAnalyser.cs b/src/Hassium/Semantics/SemanticAnalyser.cs index 9e732d9..4fa34ae 100644 --- a/src/Hassium/Semantics/SemanticAnalyser.cs +++ b/src/Hassium/Semantics/SemanticAnalyser.cs @@ -4,102 +4,101 @@ namespace Hassium.Semantics { - public class SemanticAnalyser - { - public AstNode Code { get; private set; } + public class SemanticAnalyser + { + public AstNode Code { get; private set; } - private SymbolTable result = new SymbolTable(); - private LocalScope currentLocalScope; + private SymbolTable result = new SymbolTable(); + private LocalScope currentLocalScope; - public SemanticAnalyser(AstNode code) - { - Code = code; - } + public SemanticAnalyser(AstNode code) + { + Code = code; + } - public SymbolTable Analyse() - { - checkout(Code); + public SymbolTable Analyse() + { + checkout(Code); - return result; - } + return result; + } - private void checkout(AstNode theNode) - { - if (theNode == null || theNode.Children == null) return; + private void checkout(AstNode theNode) + { + if (theNode == null || theNode.Children == null) return; - foreach (AstNode node in theNode.Children) - { - if (node is BinOpNode) - { - BinOpNode bnode = ((BinOpNode) node); - if (((BinOpNode) node).BinOp == BinaryOperation.Assignment) - { - if (!result.Symbols.Contains(bnode.Left.ToString())) - { - result.Symbols.Add(bnode.Left.ToString()); - } - checkout(node); - } - } - else - checkout(node); - } + foreach (AstNode node in theNode.Children) + { + if (node is BinOpNode) + { + BinOpNode bnode = ((BinOpNode) node); + if (((BinOpNode) node).BinOp == BinaryOperation.Assignment) + { + if (!result.Symbols.Contains(bnode.Left.ToString())) + { + result.Symbols.Add(bnode.Left.ToString()); + } + checkout(node); + } + } + else + checkout(node); + } - foreach (AstNode node in theNode.Children) - { - if (node is FuncNode) - { - FuncNode fnode = ((FuncNode)node); - currentLocalScope = new LocalScope(); - result.ChildScopes[fnode.Name + "`" + fnode.Parameters.Count] = currentLocalScope; - currentLocalScope.Symbols.AddRange(fnode.Parameters); - analyseLocalCode(fnode.Body); - } - else if (node is ClassNode) - { - var cnode = ((ClassNode)node); + foreach (AstNode node in theNode.Children) + { + if (node is FuncNode) + { + FuncNode fnode = ((FuncNode) node); + currentLocalScope = new LocalScope(); + result.ChildScopes[fnode.Name + "`" + fnode.Parameters.Count] = currentLocalScope; + currentLocalScope.Symbols.AddRange(fnode.Parameters); + analyseLocalCode(fnode.Body); + } + else if (node is ClassNode) + { + var cnode = ((ClassNode) node); - foreach (var fnode in cnode.Children[0].Children.OfType().Select(pnode => pnode)) - { - currentLocalScope = new LocalScope(); - result.ChildScopes[cnode.Name + "." + fnode.Name] = currentLocalScope; - currentLocalScope.Symbols.AddRange(fnode.Parameters); - analyseLocalCode(fnode.Body); - } - } - else if (node is LambdaFuncNode) - { - LambdaFuncNode fnode = ((LambdaFuncNode)node); - currentLocalScope = new LocalScope(); - result.ChildScopes["lambda_" + fnode.GetHashCode()] = currentLocalScope; - currentLocalScope.Symbols.AddRange(fnode.Parameters); - analyseLocalCode(fnode.Body); - } + foreach (var fnode in cnode.Children[0].Children.OfType().Select(pnode => pnode)) + { + currentLocalScope = new LocalScope(); + result.ChildScopes[cnode.Name + "." + fnode.Name] = currentLocalScope; + currentLocalScope.Symbols.AddRange(fnode.Parameters); + analyseLocalCode(fnode.Body); + } + } + else if (node is LambdaFuncNode) + { + LambdaFuncNode fnode = ((LambdaFuncNode) node); + currentLocalScope = new LocalScope(); + result.ChildScopes["lambda_" + fnode.GetHashCode()] = currentLocalScope; + currentLocalScope.Symbols.AddRange(fnode.Parameters); + analyseLocalCode(fnode.Body); + } + } - } - - if(theNode.Children.Count > 0) foreach(AstNode x in theNode.Children) checkout(x); - } - - private void analyseLocalCode(AstNode theNode) - { - foreach(AstNode node in theNode.Children) - { - if (node is BinOpNode) - { - BinOpNode bnode = ((BinOpNode)node); - if (bnode.BinOp == BinaryOperation.Assignment) - { - if (!result.Symbols.Contains(bnode.Left.ToString()) && !currentLocalScope.Symbols.Contains(bnode.Left.ToString())) - { - currentLocalScope.Symbols.Add(bnode.Left.ToString()); - } - } - } - else - analyseLocalCode(node); - } - } - } -} + if (theNode.Children.Count > 0) foreach (AstNode x in theNode.Children) checkout(x); + } + private void analyseLocalCode(AstNode theNode) + { + foreach (AstNode node in theNode.Children) + { + if (node is BinOpNode) + { + BinOpNode bnode = ((BinOpNode) node); + if (bnode.BinOp == BinaryOperation.Assignment) + { + if (!result.Symbols.Contains(bnode.Left.ToString()) && + !currentLocalScope.Symbols.Contains(bnode.Left.ToString())) + { + currentLocalScope.Symbols.Add(bnode.Left.ToString()); + } + } + } + else + analyseLocalCode(node); + } + } + } +} \ No newline at end of file diff --git a/src/Hassium/Semantics/SymbolTable.cs b/src/Hassium/Semantics/SymbolTable.cs index e3c87c8..438095d 100644 --- a/src/Hassium/Semantics/SymbolTable.cs +++ b/src/Hassium/Semantics/SymbolTable.cs @@ -14,5 +14,4 @@ public SymbolTable() ChildScopes = new Dictionary(); } } -} - +} \ No newline at end of file