buzz
buzz copied to clipboard
(Co)routines
Implement coroutines
import "lib/std";
fun main() > void {
| Async call, creates a new routine (new VM instance), count is not yet executed
rti<str, num> counter = async count(10);
| A routine is over when a OP_RETURN as been executed
while (!counter.over()) {
print("counter is {resume counter}");
}
assert(counter.over(), message: "Routine should be over now");
| await either runs the routine until completion or get the return value of a terminated routine
| if the function yields but is never resumed when we reach the await, yields value are dismissed (?)
print(await counter);
}
| returns str, yields num
fun count(num n) > str > num {
for (num i = 0; i < n; i = i + 1) {
| error or yield is ignored if not called with a async call?
yield i;
}
return "Counting is done!";
}