2017-11-06 09:28:53 +00:00
|
|
|
# Example program showing 'return-continuation-until-mark' return other values
|
|
|
|
# alongside continuations.
|
|
|
|
#
|
|
|
|
# Print out a given list of numbers.
|
|
|
|
#
|
2017-11-25 18:17:23 +00:00
|
|
|
# To run:
|
|
|
|
# $ git clone https://github.com/akkartik/mu
|
|
|
|
# $ cd mu
|
|
|
|
# $ ./mu continuation4.mu
|
|
|
|
#
|
2017-11-06 09:28:53 +00:00
|
|
|
# Expected output:
|
|
|
|
# 1
|
|
|
|
# 2
|
|
|
|
# 3
|
2017-11-05 09:47:03 +00:00
|
|
|
|
|
|
|
def main [
|
|
|
|
local-scope
|
2018-06-17 18:20:53 +00:00
|
|
|
l:&:list:num <- copy null
|
2017-11-05 09:47:03 +00:00
|
|
|
l <- push 3, l
|
|
|
|
l <- push 2, l
|
|
|
|
l <- push 1, l
|
2017-12-15 08:15:47 +00:00
|
|
|
k:continuation, x:num, done?:bool <- call-with-continuation-mark 100/mark, create-yielder, l
|
2017-11-05 09:47:03 +00:00
|
|
|
{
|
|
|
|
break-if done?
|
|
|
|
$print x 10/newline
|
|
|
|
k, x:num, done?:bool <- call k
|
|
|
|
loop
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
def create-yielder l:&:list:num -> n:num, done?:bool [
|
|
|
|
local-scope
|
2017-12-04 07:25:40 +00:00
|
|
|
load-inputs
|
2017-11-05 09:47:03 +00:00
|
|
|
{
|
2018-06-17 18:20:53 +00:00
|
|
|
done? <- equal l, null
|
2017-11-05 18:22:25 +00:00
|
|
|
break-if done?
|
2017-11-05 09:47:03 +00:00
|
|
|
n <- first l
|
|
|
|
l <- rest l
|
2017-12-15 08:15:47 +00:00
|
|
|
return-continuation-until-mark 100/mark, n, done?
|
2017-11-05 09:47:03 +00:00
|
|
|
loop
|
|
|
|
}
|
2017-11-05 18:22:25 +00:00
|
|
|
# A function that returns continuations shouldn't get the opportunity to
|
|
|
|
# return. Calling functions should stop calling its continuation after this
|
|
|
|
# point.
|
2017-12-15 08:15:47 +00:00
|
|
|
return-continuation-until-mark 100/mark, -1, done?
|
2018-06-17 07:05:38 +00:00
|
|
|
assert false, [called too many times, ran out of continuations to return]
|
2017-11-05 09:47:03 +00:00
|
|
|
]
|