From 47c95042a3c72d953b4c5059f5de2cc0347defd5 Mon Sep 17 00:00:00 2001 From: Peter Zeller Date: Wed, 12 Feb 2020 23:21:26 +0100 Subject: [PATCH 1/7] adapt StdLib to new typeclass features --- wurst/Wurst.wurst | 3 +- wurst/_handles/primitives/Boolean.wurst | 5 + wurst/_handles/primitives/Integer.wurst | 5 + wurst/_handles/primitives/Real.wurst | 5 + wurst/_handles/primitives/String.wurst | 6 + wurst/_wurst/BasicTypeClasses.wurst | 43 +++++++ wurst/_wurst/TypeCasting.wurst | 162 ++++++++++++++++++++++++ wurst/data/BitSet.wurst | 7 + wurst/data/HashList.wurst | 41 +++--- wurst/data/HashMap.wurst | 15 ++- wurst/data/HashSet.wurst | 10 +- wurst/data/LinkedList.wurst | 40 ++++-- wurst/math/Matrices.wurst | 13 ++ wurst/math/Quaternion.wurst | 5 + wurst/math/Vectors.wurst | 9 ++ wurst/util/Colors.wurst | 12 ++ wurst/util/StringUtils.wurst | 11 ++ wurst/util/TerrainUtils.wurst | 7 + 18 files changed, 357 insertions(+), 42 deletions(-) create mode 100644 wurst/_wurst/BasicTypeClasses.wurst 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/primitives/Boolean.wurst b/wurst/_handles/primitives/Boolean.wurst index cccfe965..77c32571 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 diff --git a/wurst/_handles/primitives/Integer.wurst b/wurst/_handles/primitives/Integer.wurst index 21519ddc..f2c69cf0 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 diff --git a/wurst/_handles/primitives/Real.wurst b/wurst/_handles/primitives/Real.wurst index 77236daf..f5e6fc73 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) diff --git a/wurst/_handles/primitives/String.wurst b/wurst/_handles/primitives/String.wurst index c97da4bd..f013270e 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,8 @@ 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 \ No newline at end of file diff --git a/wurst/_wurst/BasicTypeClasses.wurst b/wurst/_wurst/BasicTypeClasses.wurst new file mode 100644 index 00000000..fb2dc4f2 --- /dev/null +++ b/wurst/_wurst/BasicTypeClasses.wurst @@ -0,0 +1,43 @@ +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 + override function toIndex(T x) returns int + override function fromIndex(int index) returns T + +/** + 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 + override function toIndex(T x) returns int + override function fromIndex(int index) returns T + override function defaultValue() returns T + +/** + 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..8247bf64 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,164 @@ 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