From a0986b3f7ed244d956261c365d82fec38bff3a98 Mon Sep 17 00:00:00 2001 From: Jacob Misirian Date: Thu, 25 Aug 2016 01:43:44 -0500 Subject: [PATCH] Fixed format and cleaned up examples. Added JacHash example. --- examples/JacHash.has | 54 +++++++++++++++++++ examples/binaryString.has | 16 ------ examples/{dict.has => dictionary.has} | 0 examples/extend.has | 25 --------- examples/{fib.has => fibonacci.has} | 0 examples/irc.has | 32 ----------- src/Hassium/Runtime/GlobalFunctions.cs | 11 +++- src/Hassium/Runtime/Objects/HassiumObject.cs | 3 +- .../Objects/Text/HassiumStringBuilder.cs | 2 +- 9 files changed, 66 insertions(+), 77 deletions(-) create mode 100644 examples/JacHash.has delete mode 100644 examples/binaryString.has rename examples/{dict.has => dictionary.has} (100%) delete mode 100644 examples/extend.has rename examples/{fib.has => fibonacci.has} (100%) delete mode 100644 examples/irc.has diff --git a/examples/JacHash.has b/examples/JacHash.has new file mode 100644 index 0000000..2f99080 --- /dev/null +++ b/examples/JacHash.has @@ -0,0 +1,54 @@ +use Text; + +func main () { + JacHash hash = new JacHash (); + println (hash.computeHash ("apple".toList ())); +} + +MAX_LENGTH = 16; +FILLER_BYTE = 0xF; + +class JacHash { + func new () {} + func computeHash (bytes : list) { + this.init (); + bytes = this.pad (bytes); + result = []; + foreach (i in range (0, bytes.length)) + this.x += bytes [i]; + StringBuilder sb = new StringBuilder (); + for (int i = 0; i < bytes.length; i++) + bytes [i % MAX_LENGTH] = this.next (bytes [i].toInt ()); + for (int i = 0; i < MAX_LENGTH; i++) + sb.appendFormat ("{0:x2}", bytes [i].toInt ()); + + return sb.toString (); + } + + func next (bl : int) : int { + this.a = this.shiftLeft(bl, this.x); + this.b = (this.b ^ bl) - this.x; + this.c = (this.a + this.b) & this.x; + this.d ^= this.x - this.b; + this.x ^= this.d; + return (this.a * this.c + this.b - this.x * this.d ^ bl) & 0xFF; + } + + func shiftLeft (b : int, bits : int) : int { + return b << bits | b >> 32 - bits; + } + + func pad (bytes : list) : list { + for (int i = bytes.length; i < MAX_LENGTH; i++) + bytes.add (FILLER_BYTE); + return bytes; + } + + func init () { + this.a = 0x6B87 & 0xFF; + this.b = 0x7F43 & 0xFF; + this.c = 0xA4Ad & 0xFF; + this.d = 0xDC3F & 0xFF; + this.x = 0; + } +} diff --git a/examples/binaryString.has b/examples/binaryString.has deleted file mode 100644 index 2e0298c..0000000 --- a/examples/binaryString.has +++ /dev/null @@ -1,16 +0,0 @@ -func main () { - println (computeBinary ("00000110")); -} - -func computeBinary (b : string) : int { - int total = 0; - int addAmount = 1; - - foreach (c in b.reverse ()) { - if (c == '1') { - total += addAmount; - } - addAmount *= 2; - } - return total; -} diff --git a/examples/dict.has b/examples/dictionary.has similarity index 100% rename from examples/dict.has rename to examples/dictionary.has diff --git a/examples/extend.has b/examples/extend.has deleted file mode 100644 index 5513105..0000000 --- a/examples/extend.has +++ /dev/null @@ -1,25 +0,0 @@ -use Text; - -func main () { - MyClass.one (); - MyClass.two (); - StringBuilder.thisDidNotExistBefore (); -} - -class MyClass { - func one () { - println ("Hello from one"); - } -} - -extend MyClass { - func two () { - println ("Hello from two"); - } -} - -extend StringBuilder { - func thisDidNotExistBefore () { - println ("Why hey! I am an addition to StringBuilder!"); - } -} diff --git a/examples/fib.has b/examples/fibonacci.has similarity index 100% rename from examples/fib.has rename to examples/fibonacci.has diff --git a/examples/irc.has b/examples/irc.has deleted file mode 100644 index d96e705..0000000 --- a/examples/irc.has +++ /dev/null @@ -1,32 +0,0 @@ -use Net; -use IO; - -func main () { - conn = new Connection(Dns.resolveAddress("hassiumlang.com"), 1337); - conn.listen(); - - while (true) - conn.send(input()); -} - -class Connection { - func new (ip, port) { - this.conn = new NetConnection(ip, port); - this.reader = new FileReader(this.conn.getStream()); - this.writer = new FileWriter(this.conn.getStream()); - println("Established"); - } - - func listen () { - Thread.executeMethod(this.listen_()); - } - func listen_ () { - while (true) { - println(this.reader.readLine()); - } - } - - func send(msg) { - this.writer.writeLine(msg); - } -} diff --git a/src/Hassium/Runtime/GlobalFunctions.cs b/src/Hassium/Runtime/GlobalFunctions.cs index 20cb7c9..0e1145f 100644 --- a/src/Hassium/Runtime/GlobalFunctions.cs +++ b/src/Hassium/Runtime/GlobalFunctions.cs @@ -31,9 +31,16 @@ public class GlobalFunctions public static HassiumString format(VirtualMachine vm, params HassiumObject[] args) { - string[] elements = new string[args.Length - 1]; + object[] elements = new object[args.Length - 1]; for (int i = 0; i < elements.Length; i++) - elements[i] = args[i + 1].ToString(vm).String; + { + if (args[i + 1] is HassiumInt) + elements[i] = args[i + 1].ToInt(vm).Int; + else if (args[i + 1] is HassiumFloat) + elements[i] = args[i + 1].ToFloat(vm).Float; + else + elements[i] = args[i + 1].ToString(vm).String; + } return new HassiumString(string.Format(args[0].ToString(vm).String, elements)); } public static HassiumObject getAttribute(VirtualMachine vm, params HassiumObject[] args) diff --git a/src/Hassium/Runtime/Objects/HassiumObject.cs b/src/Hassium/Runtime/Objects/HassiumObject.cs index 573a0b8..c6d0290 100644 --- a/src/Hassium/Runtime/Objects/HassiumObject.cs +++ b/src/Hassium/Runtime/Objects/HassiumObject.cs @@ -286,7 +286,8 @@ public virtual HassiumString ToString(VirtualMachine vm, params HassiumObject[] { if (Attributes.ContainsKey(TOSTRING)) return Attributes[TOSTRING].Invoke(vm, args).ToString(vm, args); - return Type().ToString(vm); + throw new InternalException(vm, InternalException.ATTRIBUTE_NOT_FOUND, TOSTRING, Type()); + //return Type().ToString(vm); } public virtual HassiumTuple ToTuple(VirtualMachine vm, params HassiumObject[] args) { diff --git a/src/Hassium/Runtime/Objects/Text/HassiumStringBuilder.cs b/src/Hassium/Runtime/Objects/Text/HassiumStringBuilder.cs index 22fe48d..6742cf3 100644 --- a/src/Hassium/Runtime/Objects/Text/HassiumStringBuilder.cs +++ b/src/Hassium/Runtime/Objects/Text/HassiumStringBuilder.cs @@ -40,7 +40,7 @@ public HassiumStringBuilder append(VirtualMachine vm, params HassiumObject[] arg } public HassiumStringBuilder appendFormat(VirtualMachine vm, params HassiumObject[] args) { - StringBuilder.Append(GlobalFunctions.format(vm, args)); + StringBuilder.Append(GlobalFunctions.format(vm, args).ToString(vm).String); return this; } public HassiumStringBuilder appendLine(VirtualMachine vm, params HassiumObject[] args)