diff --git a/wurst/Wurst.wurst b/wurst/Wurst.wurst index dbb85a22..a9c82e6b 100644 --- a/wurst/Wurst.wurst +++ b/wurst/Wurst.wurst @@ -8,4 +8,5 @@ import public Basics import public Wurstunit import public TypeCasting import public Colors -import public Annotations \ No newline at end of file +import public Annotations +import public BasicTypeClasses \ No newline at end of file diff --git a/wurst/_handles/_Handles.wurst b/wurst/_handles/_Handles.wurst index 181f47fa..9643dc8b 100755 --- a/wurst/_handles/_Handles.wurst +++ b/wurst/_handles/_Handles.wurst @@ -29,3 +29,8 @@ import public Trigger import public Unit import public Weather import public Widget +import BasicTypeClasses + +public implements Default + override function defaultValue() returns handle + return null \ No newline at end of file diff --git a/wurst/_handles/primitives/Boolean.wurst b/wurst/_handles/primitives/Boolean.wurst index cccfe965..6320af98 100644 --- a/wurst/_handles/primitives/Boolean.wurst +++ b/wurst/_handles/primitives/Boolean.wurst @@ -1,10 +1,15 @@ package Boolean import NoWurst +import BasicTypeClasses /** Converts this boolean into a string */ public function boolean.toString() returns string return this ? "true" : "false" +public implements Show + override function toString(bool b) returns string + return b.toString() + /** Converts this string into a boolean */ public function string.toBool() returns boolean return this == "1" or this == "true" ? true : false @@ -16,3 +21,7 @@ public function boolean.toInt() returns int /** Converts this int into a boolean */ public function int.toBool() returns boolean return this != 0 + +public implements Default + override function defaultValue() returns bool + return false \ No newline at end of file diff --git a/wurst/_handles/primitives/Integer.wurst b/wurst/_handles/primitives/Integer.wurst index 21519ddc..61f5a261 100644 --- a/wurst/_handles/primitives/Integer.wurst +++ b/wurst/_handles/primitives/Integer.wurst @@ -1,6 +1,7 @@ package Integer import NoWurst import Real +import BasicTypeClasses public constant INT_MAX = 2147483647 public constant INT_MIN = -2147483648 @@ -29,6 +30,10 @@ public function int.toReal() returns real public function int.toString() returns string return I2S(this) +public implements Show + override function toString(int b) returns string + return b.toString() + /** Returns this int to the power of the argument int */ public function int.pow(int x) returns int int result = 1 @@ -55,3 +60,7 @@ public function int.bitOr(int other) returns int /** Returns the result of a bitwise exclusive OR operation performed on this int and the argument int. */ public function int.bitXor(int other) returns int return BlzBitXor(this, other) + +public implements Default + override function defaultValue() returns int + return 0 \ No newline at end of file diff --git a/wurst/_handles/primitives/Real.wurst b/wurst/_handles/primitives/Real.wurst index 77236daf..79e7e093 100644 --- a/wurst/_handles/primitives/Real.wurst +++ b/wurst/_handles/primitives/Real.wurst @@ -1,5 +1,6 @@ package Real import NoWurst +import BasicTypeClasses public constant REAL_MAX = 340282366920938000000000000000000000000. public constant REAL_MIN = -340282366920938000000000000000000000000. @@ -52,6 +53,10 @@ public function real.toInt() returns int public function real.toString() returns string return R2S(this) +public implements Show + override function toString(real b) returns string + return b.toString() + /** Returns the string representation of this real with the given amount if digits precision */ public function real.toString(int precision) returns string return R2SW(this, precision, precision) @@ -97,3 +102,7 @@ public function real.lerp(real target, real alpha) returns real /** Checks if this real is between low and high value */ public function real.isBetween(real low, real high) returns bool return this >= low and this <= high + +public implements Default + override function defaultValue() returns real + return 0. \ No newline at end of file diff --git a/wurst/_handles/primitives/String.wurst b/wurst/_handles/primitives/String.wurst index c97da4bd..ed9a1841 100644 --- a/wurst/_handles/primitives/String.wurst +++ b/wurst/_handles/primitives/String.wurst @@ -1,6 +1,7 @@ package String import NoWurst import Integer +import BasicTypeClasses constant charset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" constant numberset = "0123456789" @@ -271,3 +272,12 @@ public class StringLines /** Returns a StringLines Object of the given string for iteration */ public function string.toLines() returns StringLines return new StringLines(this, 0, this.countOccurences("\n") + 1) + + +public implements Show + override function toString(string s) returns string + return s + +public implements Default + override function defaultValue() returns string + return "" diff --git a/wurst/_wurst/BasicTypeClasses.wurst b/wurst/_wurst/BasicTypeClasses.wurst new file mode 100644 index 00000000..1fcb88b6 --- /dev/null +++ b/wurst/_wurst/BasicTypeClasses.wurst @@ -0,0 +1,38 @@ +package BasicTypeClasses +import NoWurst + +/** + T is a type that can be converted to an integer. + The toIndex function must be injective. + */ +public interface ToIndex + function toIndex(T x) returns int + +/** + T is a type that can be created from an integer T. + */ +public interface FromIndex + function fromIndex(int index) returns T + +public interface ConvertIndex extends ToIndex, FromIndex + +/** + T is a type with a default value. + */ +public interface Default + function defaultValue() returns T + +/** + AnyRef is the type class for all reference types (interfaces and classes). + */ +public interface AnyRef extends ConvertIndex, Default + +/** + T is a type that can be converted to a string. + */ +public interface Show + function toString(T t) returns string + +/** returns a string representation */ +public function T.toString() returns string + return T.toString(this) diff --git a/wurst/_wurst/TypeCasting.wurst b/wurst/_wurst/TypeCasting.wurst index a11b28b2..a7240550 100644 --- a/wurst/_wurst/TypeCasting.wurst +++ b/wurst/_wurst/TypeCasting.wurst @@ -3,6 +3,7 @@ import NoWurst import _Handles import Table import Annotations +import BasicTypeClasses constant typecastdata = new Table() /** How many decimals to preserve for reals */ @@ -244,3 +245,169 @@ public function booleanToIndex(boolean u) returns int public function booleanFromIndex(int index) returns boolean return index == 1 +// instances +public implements ConvertIndex + override function toIndex(real x) returns int + return realToIndex(x) + override function fromIndex(int i) returns real + return realFromIndex(i) +public implements ConvertIndex + override function toIndex(string x) returns int + return stringToIndex(x) + override function fromIndex(int i) returns string + return stringFromIndex(i) +public implements ConvertIndex + override function toIndex(player x) returns int + return playerToIndex(x) + override function fromIndex(int i) returns player + return playerFromIndex(i) +public implements ConvertIndex + override function toIndex(widget x) returns int + return widgetToIndex(x) + override function fromIndex(int i) returns widget + return widgetFromIndex(i) +public implements ConvertIndex + override function toIndex(unit x) returns int + return unitToIndex(x) + override function fromIndex(int i) returns unit + return unitFromIndex(i) +public implements ConvertIndex + override function toIndex(destructable x) returns int + return destructableToIndex(x) + override function fromIndex(int i) returns destructable + return destructableFromIndex(i) +public implements ConvertIndex + override function toIndex(item x) returns int + return itemToIndex(x) + override function fromIndex(int i) returns item + return itemFromIndex(i) +public implements ConvertIndex + override function toIndex(ability x) returns int + return abilityToIndex(x) + override function fromIndex(int i) returns ability + return abilityFromIndex(i) +public implements ConvertIndex + override function toIndex(force x) returns int + return forceToIndex(x) + override function fromIndex(int i) returns force + return forceFromIndex(i) +public implements ConvertIndex + override function toIndex(group x) returns int + return groupToIndex(x) + override function fromIndex(int i) returns group + return groupFromIndex(i) +public implements ConvertIndex + override function toIndex(trigger x) returns int + return triggerToIndex(x) + override function fromIndex(int i) returns trigger + return triggerFromIndex(i) +public implements ConvertIndex + override function toIndex(triggeraction x) returns int + return triggeractionToIndex(x) + override function fromIndex(int i) returns triggeraction + return triggeractionFromIndex(i) +public implements ConvertIndex + override function toIndex(triggercondition x) returns int + return triggerconditionToIndex(x) + override function fromIndex(int i) returns triggercondition + return triggerconditionFromIndex(i) +public implements ConvertIndex + override function toIndex(timer x) returns int + return timerToIndex(x) + override function fromIndex(int i) returns timer + return timerFromIndex(i) +public implements ConvertIndex + override function toIndex(location x) returns int + return locationToIndex(x) + override function fromIndex(int i) returns location + return locationFromIndex(i) +public implements ConvertIndex + override function toIndex(region x) returns int + return regionToIndex(x) + override function fromIndex(int i) returns region + return regionFromIndex(i) +public implements ConvertIndex + override function toIndex(rect x) returns int + return rectToIndex(x) + override function fromIndex(int i) returns rect + return rectFromIndex(i) +public implements ConvertIndex + override function toIndex(sound x) returns int + return soundToIndex(x) + override function fromIndex(int i) returns sound + return soundFromIndex(i) +public implements ConvertIndex + override function toIndex(effect x) returns int + return effectToIndex(x) + override function fromIndex(int i) returns effect + return effectFromIndex(i) +public implements ConvertIndex + override function toIndex(dialog x) returns int + return dialogToIndex(x) + override function fromIndex(int i) returns dialog + return dialogFromIndex(i) +public implements ConvertIndex