Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt StdLib to new typeclass features #343

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion wurst/Wurst.wurst
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ import public Basics
import public Wurstunit
import public TypeCasting
import public Colors
import public Annotations
import public Annotations
import public BasicTypeClasses
5 changes: 5 additions & 0 deletions wurst/_handles/_Handles.wurst
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ import public Trigger
import public Unit
import public Weather
import public Widget
import BasicTypeClasses

public implements Default<handle>
override function defaultValue() returns handle
return null
9 changes: 9 additions & 0 deletions wurst/_handles/primitives/Boolean.wurst
Original file line number Diff line number Diff line change
@@ -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<bool>
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
Expand All @@ -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<bool>
override function defaultValue() returns bool
return false
9 changes: 9 additions & 0 deletions wurst/_handles/primitives/Integer.wurst
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package Integer
import NoWurst
import Real
import BasicTypeClasses

public constant INT_MAX = 2147483647
public constant INT_MIN = -2147483648
Expand Down Expand Up @@ -29,6 +30,10 @@ public function int.toReal() returns real
public function int.toString() returns string
return I2S(this)

public implements Show<int>
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
Expand All @@ -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<int>
override function defaultValue() returns int
return 0
9 changes: 9 additions & 0 deletions wurst/_handles/primitives/Real.wurst
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package Real
import NoWurst
import BasicTypeClasses

public constant REAL_MAX = 340282366920938000000000000000000000000.
public constant REAL_MIN = -340282366920938000000000000000000000000.
Expand Down Expand Up @@ -52,6 +53,10 @@ public function real.toInt() returns int
public function real.toString() returns string
return R2S(this)

public implements Show<real>
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)
Expand Down Expand Up @@ -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<real>
override function defaultValue() returns real
return 0.
10 changes: 10 additions & 0 deletions wurst/_handles/primitives/String.wurst
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package String
import NoWurst
import Integer
import BasicTypeClasses

constant charset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
constant numberset = "0123456789"
Expand Down Expand Up @@ -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<string>
override function toString(string s) returns string
return s

public implements Default<string>
override function defaultValue() returns string
return ""
38 changes: 38 additions & 0 deletions wurst/_wurst/BasicTypeClasses.wurst
Original file line number Diff line number Diff line change
@@ -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<T:>
function toIndex(T x) returns int

/**
T is a type that can be created from an integer T.
*/
public interface FromIndex<T:>
function fromIndex(int index) returns T

public interface ConvertIndex<T:> extends ToIndex<T>, FromIndex<T>

/**
T is a type with a default value.
*/
public interface Default<T:>
function defaultValue() returns T

/**
AnyRef is the type class for all reference types (interfaces and classes).
*/
public interface AnyRef<T:> extends ConvertIndex<T>, Default<T>

/**
T is a type that can be converted to a string.
*/
public interface Show<T:>
function toString(T t) returns string

/** returns a string representation */
public function T.toString<T: Show>() returns string
return T.toString(this)
167 changes: 167 additions & 0 deletions wurst/_wurst/TypeCasting.wurst
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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<real>
override function toIndex(real x) returns int
return realToIndex(x)
override function fromIndex(int i) returns real
return realFromIndex(i)
public implements ConvertIndex<string>
override function toIndex(string x) returns int
return stringToIndex(x)
override function fromIndex(int i) returns string
return stringFromIndex(i)
public implements ConvertIndex<player>
override function toIndex(player x) returns int
return playerToIndex(x)
override function fromIndex(int i) returns player
return playerFromIndex(i)
public implements ConvertIndex<widget>
override function toIndex(widget x) returns int
return widgetToIndex(x)
override function fromIndex(int i) returns widget
return widgetFromIndex(i)
public implements ConvertIndex<unit>
override function toIndex(unit x) returns int
return unitToIndex(x)
override function fromIndex(int i) returns unit
return unitFromIndex(i)
public implements ConvertIndex<destructable>
override function toIndex(destructable x) returns int
return destructableToIndex(x)
override function fromIndex(int i) returns destructable
return destructableFromIndex(i)
public implements ConvertIndex<item>
override function toIndex(item x) returns int
return itemToIndex(x)
override function fromIndex(int i) returns item
return itemFromIndex(i)
public implements ConvertIndex<ability>
override function toIndex(ability x) returns int
return abilityToIndex(x)
override function fromIndex(int i) returns ability
return abilityFromIndex(i)
public implements ConvertIndex<force>
override function toIndex(force x) returns int
return forceToIndex(x)
override function fromIndex(int i) returns force
return forceFromIndex(i)
public implements ConvertIndex<group>
override function toIndex(group x) returns int
return groupToIndex(x)
override function fromIndex(int i) returns group
return groupFromIndex(i)
public implements ConvertIndex<trigger>
override function toIndex(trigger x) returns int
return triggerToIndex(x)
override function fromIndex(int i) returns trigger
return triggerFromIndex(i)
public implements ConvertIndex<triggeraction>
override function toIndex(triggeraction x) returns int
return triggeractionToIndex(x)
override function fromIndex(int i) returns triggeraction
return triggeractionFromIndex(i)
public implements ConvertIndex<triggercondition>
override function toIndex(triggercondition x) returns int
return triggerconditionToIndex(x)
override function fromIndex(int i) returns triggercondition
return triggerconditionFromIndex(i)
public implements ConvertIndex<timer>
override function toIndex(timer x) returns int
return timerToIndex(x)
override function fromIndex(int i) returns timer
return timerFromIndex(i)
public implements ConvertIndex<location>
override function toIndex(location x) returns int
return locationToIndex(x)
override function fromIndex(int i) returns location
return locationFromIndex(i)
public implements ConvertIndex<region>
override function toIndex(region x) returns int
return regionToIndex(x)
override function fromIndex(int i) returns region
return regionFromIndex(i)
public implements ConvertIndex<rect>
override function toIndex(rect x) returns int
return rectToIndex(x)
override function fromIndex(int i) returns rect
return rectFromIndex(i)
public implements ConvertIndex<sound>
override function toIndex(sound x) returns int
return soundToIndex(x)
override function fromIndex(int i) returns sound
return soundFromIndex(i)
public implements ConvertIndex<effect>
override function toIndex(effect x) returns int
return effectToIndex(x)
override function fromIndex(int i) returns effect
return effectFromIndex(i)
public implements ConvertIndex<dialog>
override function toIndex(dialog x) returns int
return dialogToIndex(x)
override function fromIndex(int i) returns dialog
return dialogFromIndex(i)
public implements ConvertIndex<button>
override function toIndex(button x) returns int
return buttonToIndex(x)
override function fromIndex(int i) returns button
return buttonFromIndex(i)
public implements ConvertIndex<quest>
override function toIndex(quest x) returns int
return questToIndex(x)
override function fromIndex(int i) returns quest
return questFromIndex(i)
public implements ConvertIndex<questitem>
override function toIndex(questitem x) returns int
return questitemToIndex(x)
override function fromIndex(int i) returns questitem
return questitemFromIndex(i)
public implements ConvertIndex<leaderboard>
override function toIndex(leaderboard x) returns int
return leaderboardToIndex(x)
override function fromIndex(int i) returns leaderboard
return leaderboardFromIndex(i)
public implements ConvertIndex<multiboard>
override function toIndex(multiboard x) returns int
return multiboardToIndex(x)
override function fromIndex(int i) returns multiboard
return multiboardFromIndex(i)
public implements ConvertIndex<trackable>
override function toIndex(trackable x) returns int
return trackableToIndex(x)
override function fromIndex(int i) returns trackable
return trackableFromIndex(i)
public implements ConvertIndex<lightning>
override function toIndex(lightning x) returns int
return lightningToIndex(x)
override function fromIndex(int i) returns lightning
return lightningFromIndex(i)
public implements ConvertIndex<image>
override function toIndex(image x) returns int
return imageToIndex(x)
override function fromIndex(int i) returns image
return imageFromIndex(i)
public implements ConvertIndex<ubersplat>
override function toIndex(ubersplat x) returns int
return ubersplatToIndex(x)
override function fromIndex(int i) returns ubersplat
return ubersplatFromIndex(i)
public implements ConvertIndex<framehandle>
override function toIndex(framehandle x) returns int
return framehandleToIndex(x)
override function fromIndex(int i) returns framehandle
return framehandleFromIndex(i)
public implements ConvertIndex<oskeytype>
override function toIndex(oskeytype x) returns int
return oskeytypeToIndex(x)
override function fromIndex(int i) returns oskeytype
return oskeytypeFromIndex(i)
public implements ConvertIndex<boolean>
override function toIndex(boolean x) returns int
return booleanToIndex(x)
override function fromIndex(int i) returns boolean
return booleanFromIndex(i)
public implements ConvertIndex<int>
override function toIndex(int x) returns int
return x
override function fromIndex(int i) returns int
return i
8 changes: 8 additions & 0 deletions wurst/data/BitSet.wurst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import NoWurst
import Integer
import Wurstunit
import Annotations
import BasicTypeClasses

public constant BITSET_SIZE = 32

Expand Down Expand Up @@ -94,6 +95,13 @@ public function bitsetToIndex(bitset object) returns int
public function bitsetFromIndex(int index) returns bitset
return bitset(index)

public implements ConvertIndex<bitset>
override function toIndex(bitset x) returns int
return x.toInt()

override function fromIndex(int index) returns bitset
return bitset(index)

// Tests

@test function testGet()
Expand Down
Loading