typestar

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

  1. Plain form lists instructions; each local.get pushes, i32.add pops two.
  2. Folded form nests operands inside their operator like Lisp.
  3. Both spell the identical bytecode — folding is purely for the reader.

Keywords and builtins used here

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.

Type this snippet

Step 3 of 3 in Modules, step 3 of 28 in Language basics.

← Previous Next →