Hey folks, I'm trying to write a transpiler in Rust for a custom C-like programming language that would either translate from the source language's text format to a .wat
or WASM bytecode format (.wat
would be easier for me to debug I think). Is there a recommended crate/library that provides high-level functions for such a task? I'm looking for something that provides utility functions like say:
let mut func = scope.define_func("add", vec![ParamType::ConstI32, ParamType::ConstI32], ResultType::ConstI32)
func.ops.push(Local::Get(0));
func.ops.push(Local::Get(1));
func.ops.push(I32::Add);
Which would generate:
(func (export "add") (param i32 i32) (result i32)
local.get 0
local.get 1
i32.add))
https://docs.rs/wasm-encoder/latest/wasm_encoder/ has an API which is very close to that.
@Dan Gohman thank you! This appears to be exactly what I need.
wasm-encoder
encodes to the binary format, but you can also use wasmprinter
from that same repro to print the text format from the output of wasm-encoder
Just to make sure I'm not reinventing the wheel, there isn't something higher-level that let's me translate imperative language constructs and have it do the hard work for me is there? I came across this whistle-lang compiler which actually looks pretty good: https://github.com/whistle-lang/whistle/tree/main/compiler
(I understand I can't get everything for free, but path of least resistance etc. :p )
I'm personally not familiar with any.
Last updated: Dec 23 2024 at 12:05 UTC