feat: implement for-in loop syntax with iterate/iteratorValue protocol and break support
Add `for (variable in sequence)` syntax to the Wren language, introducing the `in` keyword token and compiler support for iterating over sequences using `iterate` and `iteratorValue` methods. Includes new benchmark files for Lua, Python, Ruby, and Wren, plus test cases covering single-expression bodies, block bodies, closures over loop variables, and break statements. Also fixes a missing POP() in CLOSE_UPVALUE and moves the while test file to a subdirectory.
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
var list = []
|
||||
|
||||
for (i in [1, 2, 3]) {
|
||||
list.add(fn IO.write(i))
|
||||
//list.add(fn IO.write(i))
|
||||
}
|
||||
|
||||
for (f in list) f.call
|
||||
// expect: 1
|
||||
// expect: 2
|
||||
// expect: 3
|
||||
@@ -0,0 +1,11 @@
|
||||
var list = []
|
||||
|
||||
for (i in [1, 2, 3]) {
|
||||
var j = i + 1
|
||||
list.add(fn IO.write(j))
|
||||
}
|
||||
|
||||
for (f in list) f.call
|
||||
// expect: 2
|
||||
// expect: 3
|
||||
// expect: 4
|
||||
@@ -0,0 +1,20 @@
|
||||
// Single-expression body.
|
||||
for (i in [1, 2, 3]) IO.write(i)
|
||||
// expect: 1
|
||||
// expect: 2
|
||||
// expect: 3
|
||||
|
||||
// Block body.
|
||||
for (i in [1, 2, 3]) {
|
||||
IO.write(i)
|
||||
}
|
||||
// expect: 1
|
||||
// expect: 2
|
||||
// expect: 3
|
||||
|
||||
// Newline after "for".
|
||||
for
|
||||
(i in [1, 2, 3]) IO.write(i)
|
||||
// expect: 1
|
||||
// expect: 2
|
||||
// expect: 3
|
||||
@@ -0,0 +1,10 @@
|
||||
var f = fn {
|
||||
IO.write("evaluate sequence")
|
||||
return [1, 2, 3]
|
||||
}
|
||||
|
||||
for (i in f.call) IO.write(i)
|
||||
// expect: evaluate sequence
|
||||
// expect: 1
|
||||
// expect: 2
|
||||
// expect: 3
|
||||
Reference in New Issue
Block a user