| 1 |
(module $examples |
| 2 |
(type $func (;0;) (func)) |
| 3 |
(type $cont (;1;) (cont $func)) |
| 4 |
(type (;2;) (func (param i32))) |
| 5 |
(type (;3;) (func (param i32 (ref $cont)))) |
| 6 |
(type (;4;) (func (result i32 (ref $cont)))) |
| 7 |
(type (;5;) (func (param i32 (ref $cont)) (result i32))) |
| 8 |
(import "generator" "yield" (tag $yield (;0;) (type 2) (param i32))) |
| 9 |
(import "spectest" "print_i32" (func $log (;0;) (type 2))) |
| 10 |
(export "naturals" (func $naturals)) |
| 11 |
(export "one-two-three" (func $one-two-three)) |
| 12 |
(export "fibonacci" (func $fibonacci)) |
| 13 |
(export "print-first" (func $print-first)) |
| 14 |
(export "sum-first" (func $sum-first)) |
| 15 |
(func $naturals (;1;) (type $func) |
| 16 |
(local $n i32) |
| 17 |
loop $l |
| 18 |
local.get $n |
| 19 |
suspend $yield |
| 20 |
local.get $n |
| 21 |
i32.const 1 |
| 22 |
i32.add |
| 23 |
local.set $n |
| 24 |
br $l |
| 25 |
end |
| 26 |
) |
| 27 |
(func $one-two-three (;2;) (type $func) |
| 28 |
i32.const 1 |
| 29 |
suspend $yield |
| 30 |
i32.const 2 |
| 31 |
suspend $yield |
| 32 |
i32.const 3 |
| 33 |
suspend $yield |
| 34 |
) |
| 35 |
(func $fibonacci (;3;) (type $func) |
| 36 |
(local $a i32) (local $b i32) (local $t i32) |
| 37 |
i32.const 1 |
| 38 |
local.set $b |
| 39 |
loop $l |
| 40 |
local.get $a |
| 41 |
suspend $yield |
| 42 |
local.get $a |
| 43 |
local.set $t |
| 44 |
local.get $b |
| 45 |
local.set $a |
| 46 |
local.get $t |
| 47 |
local.get $a |
| 48 |
i32.add |
| 49 |
local.set $b |
| 50 |
br $l |
| 51 |
end |
| 52 |
) |
| 53 |
(func $print-first (;4;) (type 3) (param $n i32) (param $k (ref $cont)) |
| 54 |
loop $l |
| 55 |
block $on_yield (type 4) (result i32 (ref $cont)) |
| 56 |
local.get $n |
| 57 |
if ;; label = @3 |
| 58 |
local.get $k |
| 59 |
resume $cont (on $yield $on_yield) |
| 60 |
end |
| 61 |
return |
| 62 |
end |
| 63 |
local.set $k |
| 64 |
call $log |
| 65 |
local.get $n |
| 66 |
i32.const -1 |
| 67 |
i32.add |
| 68 |
local.set $n |
| 69 |
br $l |
| 70 |
end |
| 71 |
unreachable |
| 72 |
) |
| 73 |
(func $sum-first (;5;) (type 5) (param $n i32) (param $k (ref $cont)) (result i32) |
| 74 |
(local $sum i32) |
| 75 |
loop $l |
| 76 |
block $on_yield (type 4) (result i32 (ref $cont)) |
| 77 |
local.get $n |
| 78 |
if ;; label = @3 |
| 79 |
local.get $k |
| 80 |
resume $cont (on $yield $on_yield) |
| 81 |
end |
| 82 |
local.get $sum |
| 83 |
return |
| 84 |
end |
| 85 |
local.set $k |
| 86 |
local.get $sum |
| 87 |
i32.add |
| 88 |
local.set $sum |
| 89 |
local.get $n |
| 90 |
i32.const -1 |
| 91 |
i32.add |
| 92 |
local.set $n |
| 93 |
br $l |
| 94 |
end |
| 95 |
unreachable |
| 96 |
) |
| 97 |
) |