Skip to content

ktye/w

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

webassembly compiler  (github.com/ktye/w)

write wasm by hand, but more compact than wat
(source file has same size as wasm binary)

add:I:II{x+y}        /exported function
sum.I:I {x/x+:i;x}   /private function
mac:    {a:3;b:5}    /flat macro
0:      {add;sum}    /function table
sin:F:F {}           /imported function (from module "ext")
256!{010203ff}       /data section

types are I(i32) F(f64)
no preference    apl style x*a+b, but lhs is evaluted first: x a b + *
arguments        x,y,z,x3,x4,.. are typed in the function declaration f:R:XYZ..
locals           are detected at evaluation order (type is derived) r:x+y
memory/heap      C x;I x;F x (get from addr x)  x::y (write y to addr x), also x::C?y
a;b;c            sequence. c(last) returns a typed value all others must return null
x?y              if x then y
$[a;b;c;d;e]     if a then b elseif c then d else e
x/y              n-loop execute-y x-times (x evaluates to I, y must be null)
                 creates implicit local i, and n if x is an expression. no nesting.
x?/y             while x do y (y must have no type)
C?x I?x F?x I?'x type conversions ?'(signed)
sum x ; x add y  named function call, dyad(infix), names can be defined later
8:{f;g;h}        add f,g,h to function table starting at offset 8
(I.x+1)(a;b)     call indirect: return type I, function index x+1, args a b


build
$ go build w.go
$ ./w j.w > j.wasm


examples
 file   size(w)  size(wasm)  what            try online
 j.w       2642        2576  stack language  ktye.github.io/j
 k3.w     20803       20963  k interpreter   ktye.github.io/zoo/#kw


# a complete wasm module (11 bytes input / 50 bytes output):
$ echo "f:I:II{x+y}" | w | xxd
0000000: 0061 736d 0100 0000 0107 0160 027f 7f01  .asm.......`....
0000010: 7f03 0201 0005 0301 0001 070b 0203 6d65  ..............me
0000020: 6d02 0001 6600 000a 0901 0700 2000 2001  m...f....... . .
0000030: 6a0b                          ^^^^ ^^^^  j.
         ^^add(i32)                    locX locY
	 
see also
 github.com/ktye/wg (uses go subset as source instead of custom language)
 much easier to debug because the program runs as go as it is.
 but not as compact.

About

webassembly compiler

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published