You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
it would be interesting to add generators to Lox, with syntax similar to other languages (i.e., adding the yield keyword and using the resulting objects with an iterator protocol)
fun fib(limit) {
var a = 0
var b = 1
while (a <= limit) {
yield a
var previous_a = a
a = b
b = previous_a + b
}
}
var iter = fib(/* limit: */10)
while (!iter.at_end()) {
println(iter.value)
it.more()
}
for clox, the feature is not likely to require large runtime restructuring, as it is a matter of adding a new opcode and being able to save/restore a function's local environment (along with the program counter for properly resuming a function after the last yield)
for jlox, the feature unfortunately requires more extensive changes since the way code is currently executed (a recursive traversal of the AST) doesn't allow for easy resuming of a function's execution. one way to implement it would be by creating a family of "resumable" statement classes which store the current state of a statement's execution (e.g., this gist has a proof of concept of such an implementation).
The text was updated successfully, but these errors were encountered:
it would be interesting to add generators to Lox, with syntax similar to other languages (i.e., adding the
yield
keyword and using the resulting objects with an iterator protocol)for
clox
, the feature is not likely to require large runtime restructuring, as it is a matter of adding a new opcode and being able to save/restore a function's local environment (along with the program counter for properly resuming a function after the lastyield
)for
jlox
, the feature unfortunately requires more extensive changes since the way code is currently executed (a recursive traversal of the AST) doesn't allow for easy resuming of a function's execution. one way to implement it would be by creating a family of "resumable" statement classes which store the current state of a statement's execution (e.g., this gist has a proof of concept of such an implementation).The text was updated successfully, but these errors were encountered: