The Wax language in MoonBit: parser, formatter, type checker, and wasm/wat emitters
#[export]
fn add(x: i32, y: i32) -> i32 {
x + y;
}let src =
#|#[export]
#|fn add(x: i32, y: i32) -> i32 { x + y; }
#|
let bytes = match @wax.compile_string(src) {
Ok(b) => b
Err(diagnostics) => ... // nothing compiled; these say why
}let text = @wax.compile_string_to_wat("fn one() -> i32 { 1; }")
// (func $one (result i32) (i32.const 1))@wax.format_string("fn f()->i32{1;}")
// Ok("fn f() -> i32 {\n 1;\n}")let i32_ : @wasm_types.ValType[@ast.Ident] = I32
let m : @ast.LocModule = [
@build.func(
"add",
params=[(Some("x"), i32_), (Some("y"), i32_)],
results=[i32_],
body=[
@ast.no_loc_instr(
BinOpI(
@basic.no_loc(Add),
@ast.no_loc_instr(Get(@build.ident("x"))),
@ast.no_loc_instr(Get(@build.ident("y"))),
),
),
],
attributes=[@build.exported()],
),
]
let session = @compile.Session::new()
let checked = session.check(m)
if @compile.rejected(session.reports()) {
// report and stop
} else {
let bytes = checked.to_bytes() // or checked.to_wat()
}let s = @build.Spans::new() // or Spans::new(fname="my.dsl")
let body = [
s.instr(Let([(Some(s.ident("a")), Some(i32_))], Some(s.instr(Int("1"))))),
s.instr(Get(s.ident("a"))),
]| package | |
|---|---|
| wax | the facade above: parse, format, compile, one call each |
| wax/compile | Session and Checked: the AST-first pipeline, no front end |
| wax/ast, wax/ast/build | the syntax tree, and constructors for it |
| wax/syntax/{lexer,tokens,parser,trivia} | the front end |
| wax/fmt | the formatter |
| wax/check, wax/check/{env,store,infer,members} | the type checker |
| wax/emit/wasm | Wax → the wasm module model |
| wax/wasm/{types,bin,wat,simd,atomics} | the wasm model, its encoder and its printer — no Wax anywhere in them |
| wax/{basic,diagnostic,message,printer,colors,warning,feature,cond,unicode} | spans, diagnostics and layout |
fn compile_string(src : String, fname? : String, features? : Set, policy? : Policy) -> Result[Bytes, Array[Diagnostic]] raise CompileErrorfn compile_string_to_wat(src : String, fname? : String, features? : Set, policy? : Policy) -> Result[String, Array[Diagnostic]] raise CompileErrorfn format_string(src : String, fname? : String, theme? : Theme) -> Result[String, Array[Diagnostic]]The Wax language in MoonBit: parser, formatter, type checker, and wasm/wat emitters