Stack style and folded style in WebAssembly
The same function twice: raw stack pushes, then nested s-expressions.
(module
;; Plain form: instructions push and pop an implicit stack.
(func (export "average_stack") (param $a i32) (param $b i32) (result i32)
local.get $a
local.get $b
i32.add
i32.const 2
i32.div_s)
;; Folded form: the same instructions, nested as s-expressions.
(func (export "average_folded") (param $a i32) (param $b i32) (result i32)
(i32.div_s
(i32.add (local.get $a) (local.get $b))
(i32.const 2))))
How it works
- Plain form lists instructions; each
local.getpushes,i32.addpops two. - Folded form nests operands inside their operator like Lisp.
- Both spell the identical bytecode — folding is purely for the reader.
Keywords and builtins used here
exportfunci32moduleparamresult
The run, in numbers
- Lines
- 14
- Characters to type
- 406
- Tokens
- 77
- Three-star pace
- 65 tpm
At the three-star pace of 65 tokens a minute, this run takes about 71 seconds.
Step 3 of 3 in Modules, step 3 of 28 in Language basics.