Skip to content

Commit 4aaaf31

Browse files
author
Carlos Rojas
committed
add factorial example
1 parent b823b8b commit 4aaaf31

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

examples/factorial/fac.wast

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
(module
2+
(import "env" "print_int" (func $print (type $i2v)))
3+
4+
(; Type declarations ;)
5+
(type $i2v (func (param i32) (result)))
6+
(type $i32toi32 (func (param i32) (result i32)))
7+
(type $v2v (func (param) (result)))
8+
9+
(; Define one function ;)
10+
(export "main" (func $main))
11+
(memory 2)
12+
(table funcref (elem $fac $dummy))
13+
14+
(global $g1 i32 (i32.const 0))
15+
(global $g2 (mut i32) (i32.const 0))
16+
17+
(func $dummy (type $i2v))
18+
(func $fac (type $i32toi32)
19+
(i32.gt_s
20+
(local.get 0)
21+
(i32.const 1))
22+
(if (result i32)
23+
(then
24+
(i32.sub
25+
(local.get 0)
26+
(i32.const 1))
27+
(call $fac)
28+
(local.get 0)
29+
i32.mul)
30+
(else
31+
(i32.const 1))))
32+
33+
(func $main (type $v2v)
34+
(local $arg i32)
35+
(local.set $arg (i32.const 5))
36+
(loop
37+
(local.get $arg)
38+
(call $fac)
39+
(call $print)
40+
(br 0)))
41+
)
42+

0 commit comments

Comments
 (0)