vowel_count.wat en WebAssembly
Recorre un pasaje en un segmento de datos byte a byte y suma vocales.
;; Count the vowels in a passage baked into linear memory.
(module
(memory 1)
(data (i32.const 0) "the rain in maine falls mainly on the plain")
(global $length i32 (i32.const 43))
;; ASCII codes: a 97, e 101, i 105, o 111, u 117.
(func $is_vowel (param $c i32) (result i32)
(i32.or
(i32.or
(i32.or
(i32.eq (local.get $c) (i32.const 97))
(i32.eq (local.get $c) (i32.const 101)))
(i32.or
(i32.eq (local.get $c) (i32.const 105))
(i32.eq (local.get $c) (i32.const 111))))
(i32.eq (local.get $c) (i32.const 117))))
;; Walk the passage byte by byte and tally.
(func $count (export "count_vowels") (result i32)
(local $i i32)
(local $total i32)
(block $done
(loop $scan
(br_if $done (i32.ge_s (local.get $i) (global.get $length)))
(local.set $total
(i32.add
(local.get $total)
(call $is_vowel (i32.load8_u (local.get $i)))))
(local.set $i (i32.add (local.get $i) (i32.const 1)))
(br $scan)))
(local.get $total))
(func (export "main") (result i32)
(call $count)))
Cómo funciona
- El segmento
(data ...)hornea 43 bytes de texto en la dirección 0. $is_vowelune con or cinco pruebasi32.eqcontra códigos ASCII.- El bucle suma la respuesta 0-o-1 directo al total: 14.
Palabras clave y builtins usados aquí
blockbrbr_ifcalldataexportfuncglobali32localloopmemorymoduleparamresult
El intento, en números
- Líneas
- 35
- Caracteres a escribir
- 959
- Tokens
- 218
- Ritmo de tres estrellas
- 60 tpm
Al ritmo de tres estrellas de 60 tokens por minuto, este intento toma unos 218 segundos.
Paso 3 de 3 en Bis; paso 28 de 28 en Fundamentos del lenguaje.