1 # Example program showing 'return-continuation-until-mark' return other values
 2 # alongside continuations.
 3 #
 4 # Print out a given list of numbers.
 5 #
 6 # To run:
 7 #   $ git clone https://github.com/akkartik/mu
 8 #   $ cd mu
 9 #   $ ./mu continuation4.mu
10 #
11 # Expected output:
12 #   1
13 #   2
14 #   3
15 
16 def main [
17   local-scope
18   l:&:list:num <- copy 0
19   l <- push 3, l
20   l <- push 2, l
21   l <- push 1, l
22   k:continuation, x:num, done?:bool <- call-with-continuation-mark 100/mark, create-yielder, l
23   {
24     break-if done?
25     $print x 10/newline
26     k, x:num, done?:bool <- call k
27     loop
28   }
29 ]
30 
31 def create-yielder l:&:list:num -> n:num, done?:bool [
32   local-scope
33   load-inputs
34   {
35     done? <- equal l, 0
36     break-if done?
37     n <- first l
38     l <- rest l
39     return-continuation-until-mark 100/mark, n, done?
40     loop
41   }
42   # A function that returns continuations shouldn't get the opportunity to
43   # return. Calling functions should stop calling its continuation after this
44   # point.
45   return-continuation-until-mark 100/mark, -1, done?
46   assert 0/false, [called too many times, ran out of continuations to return]
47 ]