1
0
forked from Mirror/wren
Files
wren/corelib.wren
Bob Nystrom 507e89d3b3 Range operators!
Added ".." and "..." infix operators. Num implements them to
return Range objects. Those in turn implement the iterator
protocol, so now numeric for loops are easy:

    for (i in 1..10) { ... }

I also cleaned up the Wren benchmarks to use this.

In the process, I discovered the method_call benchmark really
just showed loop peformance, so I unrolled the loops in all of
the languages to stress method calls.
2013-12-25 15:01:45 -08:00

49 lines
873 B
Plaintext

// Note: This is converted to a C string literal and inserted into
// src/wren_core.c using make_corelib.
class IO {
static write(obj) {
IO.write__native__(obj.toString)
return obj
}
}
class List {
toString {
var result = "["
var i = 0
// TODO: Use for loop.
while (i < this.count) {
if (i > 0) result = result + ", "
result = result + this[i].toString
i = i + 1
}
result = result + "]"
return result
}
}
class Range {
new(min, max) {
_min = min
_max = max
}
min { return _min }
max { return _max }
iterate(previous) {
if (previous == null) return _min
if (previous == _max) return false
return previous + 1
}
iteratorValue(iterator) {
return iterator
}
}
class Num {
.. other { return new Range(this, other) }
... other { return new Range(this, other - 1) }
}