Category talk:Wren-trait: Difference between revisions

m
→‎Source code: Now uses Wren S/H lexer.
m (→‎Source code: Changed a program comment.)
m (→‎Source code: Now uses Wren S/H lexer.)
 
(8 intermediate revisions by the same user not shown)
Line 1:
===Source code===
<langsyntaxhighlight ecmascriptlang="wren">/* Module "trait.wren" */
 
/* Cloneable is an abstract class which enables child classes to automatically be
Line 13:
*/
class CloneableSeq is Sequence {
clone() { this } /* to be overridden by child class */
}
 
Line 29:
 
< (other) { compare(other) < 0 }
> (other) { compare(other) > 0 }
<=(other) { compare(other) <= 0 }
>=(other) { compare(other) >= 0 }
==(other) { compare(other) == 0 }
!=(other) { compare(other) != 0 }
}
 
/* Stepped wraps a Sequence so it can be iterated by steps other than 1. */
class Stepped is Sequence {
// Constructs a new stepped sequence.
construct new(seq, step) {
if (!(seq is Sequence)) Fiber.abort("First argument must be a sequence.")
_seq = seq
_step = (step < 1) ? 1 : step // minimum step of 1
}
 
// Ensures a range is ascending before passing it to the constructor.
// It it isn't, returns an empty range. Useful when bounds are variable.
static ascend(range, step) {
if (!(range is Range)) Fiber.abort("First argument must be a range.")
return (range.from <= range.to) ? new(range, step) : 0...0
}
 
// Ensures a range is descending before passing it to the constructor.
// It it isn't, returns an empty range. Useful when bounds are variable.
static descend(range, step) {
if (!(range is Range)) Fiber.abort("First argument must be a range.")
return (range.from >= range.to) ? new(range, step) : 0...0
}
 
// Convenience versions of the above methods which call them with a step of 1.
static ascend(range) { ascend(range, 1) }
static descend(range) { descend(range, 1) }
 
// Iterator protocol methods.
iterate(iterator) {
if (!iterator) {
return _seq.iterate(iterator)
} else {
var count = _step
while (count > 0 && iterator) {
iterator = _seq.iterate(iterator)
count = count - 1
}
return iterator
}
}
 
iteratorValue(iterator) { _seq.iteratorValue(iterator) }
}
 
/*
Reversed wraps a Sequence (other than a range) so it can be iterated in reverse
and by steps other than 1.
*/
class Reversed is Sequence {
// Constructs a new reversed sequence.
construct new(seq, step) {
if (!(seq is Sequence) || seq is Range) {
Fiber.abort("First argument must be a sequence other than a range.")
}
_seq = seq
_step = (step < 1) ? 1 : step // minimum step of 1
}
 
// Convenience method which calls the constructor with a step of 1.
static new(seq) { Reversed.new(seq, 1) }
 
// Iterator protocol methods.
iterate(iterator) {
var it = _seq.iterate(iterator)
if (it == null || it == 0) {
it = _seq.count - 1
} else if (it == false) {
it = _seq.count - 1 - _step
} else {
it = it - 1 - _step
}
return (it >= 0) ? it : false
}
 
iteratorValue(iterator) { _seq.iteratorValue(iterator) }
}
 
/* SeqEntry represents an (index, value) pair for use with the Indexed class. */
class SeqEntry {
// Constructs a new SeqEntry object.
construct new(index, value) {
_index = index
_value = value
}
 
// Properties.
index { _index }
value { _value }
 
// Returns the current instance's string representation.
toString { "%(_index):%(_value)" }
}
 
/*
Indexed wraps a Sequence (other than a range) so its elements can be iterated over
together with their zero-based indices.
*/
class Indexed is Sequence {
// Constructs a new indexed sequence with a step of 'step' and optionally reversed.
construct new(seq, step, reversed) {
if (seq is Range) Fiber.abort("First argument cannot be a range.")
if (!(reversed is Bool)) Fiber.abort("Third argument must be true or false.")
_seq = !reversed ? Stepped.new(seq, step) : Reversed.new(seq, step)
}
 
// Constructs a new indexed sequence with a step of 'step' and 'reversed' set to false.
static new(seq, step) { new(seq, step, false) }
 
// Constructs a new indexed sequence with a step of 1.
construct new(seq) {
if (!(seq is Sequence) || seq is Range) {
Fiber.abort("Argument must be a sequence other than a range.")
}
_seq = seq
}
 
// Iterator protocol methods.
iterate(iterator) { _seq.iterate(iterator) }
 
iteratorValue(iterator) {
return SeqEntry.new(iterator, _seq.iteratorValue(iterator))
}
}
 
Line 184 ⟶ 60:
static [key] { __map ? __map[key] : null }
 
// Retrieves a ByKey object's key from the object it wraps.
// If the same object has been wrapped more than once, returns the first key found which may
// not be the lowest. Returns 0 if the object is unwrapped.
Line 227 ⟶ 103:
}
 
/*
// Type aliases for classes in case of any name clashes with other modules.
Const represents a group of individually named read-only values which are
var Trait_Cloneable = Cloneable
stored internally in a map. Any attempt to change such a value is ignored
var Trait_CloneableSeq = CloneableSeq
though they can be removed from the map.
var Trait_Comparable = Comparable
*/
var Trait_Stepped = Stepped
class Const {
var Trait_Reversed = Reversed
// Returns the value of 'name' if it is present in the internal map
var Trait_SeqEntry = SeqEntry
// or null otherwise.
var Trait_Indexed = Indexed
static [name] { (__cmap && __cmap.containsKey(name)) ? __cmap[name] : null }
var Trait_ByRef = ByRef
 
var Trait_ByKey = ByKey</lang>
// Associates 'value' with 'name' in the internal map.
// If 'name' is already in the map, the change is ignored.
static [name]=(value) {
if (!__cmap) __cmap = {}
if (!__cmap.containsKey(name)) {
__cmap[name] = value
}
}
 
// Removes 'name' and its associated value from the internal map and returns
// that value. If 'name' was not present in the map, returns null.
static remove(name) { __cmap.remove(name) }
 
// Returns a list of the entries (name/value pairs) in the internal map.
static entries { __cmap.toList }
}
 
/*
Var represents a group of individually named read/write values which are
stored internally in a map. It can be used to simulate the creation of
variables at runtime. It can also be used to allow variable names which would
otherwise be illegal in Wren such as those which include non-ASCII characters.
*/
class Var {
// Returns the value of 'name' if it is present in the internal map
// or null otherwise.
static [name] { (__vmap && __vmap.containsKey(name)) ? __vmap[name] : null }
 
// Associates 'value' with 'name' in the internal map.
// Any existing value is replaced.
static [name]=(value) {
if (!__vmap) __vmap = {}
__vmap[name] = value
}
 
// Removes 'name' and its associated value from the internal map and returns
// that value. If 'name' was not present in the map, returns null.
static remove(name) { __vmap.remove(name) }
 
// Returns a list of the entries (name/value pairs) in the internal map.
static entries { __vmap.toList }
}
 
/* TypedVar is similar to Var except that simulated variables always retain the
same type as they had when they were originally created.
*/
class TypedVar {
// Returns the value of 'name' if it is present in the internal map
// or null otherwise.
static [name] { (__vmap && __vmap.containsKey(name)) ? __vmap[name] : null }
 
// Associates 'value' with 'name' in the internal map.
// Any existing value is replaced. However, it is an error to attempt to replace
// an existing value with a value of a different type.
static [name]=(value) {
if (!__vmap) __vmap = {}
if (!__vmap.containsKey(name)) {
__vmap[name] = value
} else {
var vtype = value.type
var mtype = __vmap[name].type
if (vtype != mtype) {
Fiber.abort("Expecting an item of type %(mtype), got %(vtype).")
} else {
__vmap[name] = value
}
}
}
 
// Removes 'name' and its associated value from the internal map and returns
// that value. If 'name' was not present in the map, returns null.
static remove(name) { __vmap.remove(name) }
 
// Returns a list of the entries (name/value pairs) in the internal map.
static entries { __vmap.toList }
}</syntaxhighlight>
9,476

edits