feat: add range operators and iterator protocol for numeric for loops

Implement `..` and `...` infix operators on Num returning Range objects that support the iterator protocol, enabling concise numeric for loops like `for (i in 1..10)`. Add Range class with min/max properties and iterate/iteratorValue methods. Introduce TOKEN_DOTDOT and TOKEN_DOTDOTDOT lexer tokens with PREC_RANGE precedence in the compiler. Unroll method_call benchmark loops across all languages to stress dynamic dispatch instead of loop overhead, reducing iteration count from 1M to 100K with 10x unrolling. Update Wren benchmarks (binary_trees, fib, for) to use new range syntax. Add benchmark/README.md documenting each benchmark's purpose.
This commit is contained in:
Bob Nystrom
2013-12-25 23:01:45 +00:00
parent b72a42eedd
commit 6c61df3ed7
12 changed files with 209 additions and 44 deletions
+21 -7
View File
@@ -29,14 +29,21 @@ class NthToggle is Toggle {
}
var start = OS.clock
var n = 1000000
var i = 0
var n = 100000
var val = true
var toggle = new Toggle(val)
while (i < n) {
for (i in 0...n) {
val = toggle.activate.value
val = toggle.activate.value
val = toggle.activate.value
val = toggle.activate.value
val = toggle.activate.value
val = toggle.activate.value
val = toggle.activate.value
val = toggle.activate.value
val = toggle.activate.value
val = toggle.activate.value
i = i + 1
}
IO.write(toggle.value)
@@ -44,10 +51,17 @@ IO.write(toggle.value)
val = true
var ntoggle = new NthToggle(val, 3)
i = 0
while (i < n) {
for (i in 0...n) {
val = ntoggle.activate.value
val = ntoggle.activate.value
val = ntoggle.activate.value
val = ntoggle.activate.value
val = ntoggle.activate.value
val = ntoggle.activate.value
val = ntoggle.activate.value
val = ntoggle.activate.value
val = ntoggle.activate.value
val = ntoggle.activate.value
i = i + 1
}
IO.write(ntoggle.value)