Skip to content

Commit

Permalink
Fixed format and cleaned up examples. Added JacHash example.
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobMisirian committed Aug 25, 2016
1 parent 6bb6eb9 commit a0986b3
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 77 deletions.
54 changes: 54 additions & 0 deletions examples/JacHash.has
Original file line number Diff line number Diff line change
@@ -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;
}
}
16 changes: 0 additions & 16 deletions examples/binaryString.has

This file was deleted.

File renamed without changes.
25 changes: 0 additions & 25 deletions examples/extend.has

This file was deleted.

File renamed without changes.
32 changes: 0 additions & 32 deletions examples/irc.has

This file was deleted.

11 changes: 9 additions & 2 deletions src/Hassium/Runtime/GlobalFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion src/Hassium/Runtime/Objects/HassiumObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Hassium/Runtime/Objects/Text/HassiumStringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a0986b3

Please sign in to comment.