Skip to content

Commit

Permalink
Separated Convert.toNumber into Convert.toInt and Convert.toDouble
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobMisirian committed Sep 25, 2015
1 parent c58eab3 commit 651a180
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions src/Hassium/HassiumObjects/Conversion/HassiumConvert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,30 +34,41 @@ public class HassiumConvert : HassiumObject
{
public HassiumConvert()
{
Attributes.Add("toNumber", new InternalFunction(toNumber, 1));
Attributes.Add("toDouble", new InternalFunction(toDouble, 1));
Attributes.Add("toInt", new InternalFunction(toInt, 1));
Attributes.Add("toString", new InternalFunction(toString, 1));
Attributes.Add("toBool", new InternalFunction(toBool, 1));
}

public static HassiumObject toNumber(HassiumObject[] args)
public static HassiumObject toDouble(HassiumObject[] args)
{
if (args[0] is HassiumString)
{
var ret = Convert.ToDouble(((HassiumString) args[0]).Value);
return ret == System.Math.Truncate(ret) ? new HassiumInt((int) ret) : new HassiumDouble(ret);
var ret = Convert.ToDouble(((HassiumString)args[0]).Value);
return ret == System.Math.Truncate(ret) ? new HassiumInt((int)ret) : 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 args[0];
else if (args[0] is HassiumByte)
return new HassiumDouble(Convert.ToDouble(((HassiumByte)args[0]).Value));
else
{
throw new Exception("Unknown format for Convert.toNumber");
}
throw new Exception("Unknown format for Convert.toDouble()");
}

public static HassiumObject toInt(HassiumObject[] args)
{
if (args[0] is HassiumString)
return Convert.ToInt32(((HassiumString)args[0]).Value);
else if (args[0] is HassiumDouble)
return new HassiumInt(((HassiumDouble)args[0]).ValueInt);
else if (args[0] is HassiumInt)
return args[0];
else if (args[0] is HassiumByte)
return new HassiumInt(Convert.ToInt32(((HassiumByte)args[0]).Value));
else
throw new Exception("Unknown format for Convert.toInt()");
}

public static HassiumObject toString(HassiumObject[] args)
Expand Down

0 comments on commit 651a180

Please sign in to comment.