Refactor front end

This commit is contained in:
Rerumu 2021-12-14 06:33:39 -05:00
parent 84e688877b
commit d55441e5fb
2 changed files with 59 additions and 17 deletions

View File

@ -1,3 +1,9 @@
This is a WIP (read: absolutely not ready for serious work) tool for translating WebAssembly into Lua. Support is specifically for LuaJIT, with the secondary objective being Lua 5.4 and Roblox's Luau. This is a WebAssembly to Lua translation tool. The generated code is designed to run without any dependencies past the bundled runtime.
The instructions are minimal; run the program as `program <edition> <file...>` to generate the translations into stdout. Editions currently supported are `LuaJIT` and `Luau`. You can run the tool via `program help` to list command line instructions.
| | | |
|----------|-----------------|-----------------------|
| LuaJIT | :green_circle: | Minimum version 2.1.0 |
| Luau | :yellow_circle: | |
| Lua 5.4 | :red_circle: | |

View File

@ -5,13 +5,8 @@ mod analyzer;
mod ast; mod ast;
mod writer; mod writer;
fn lang_from_string<'a>(name: &str, wasm: &'a Module) -> Box<dyn Transpiler<'a> + 'a> { static LUAJIT_RUNTIME: &str = include_str!("../runtime/luajit.lua");
match name.to_lowercase().as_str() { static LUAU_RUNTIME: &str = include_str!("../runtime/luau.lua");
"luau" => Box::new(Luau::new(wasm)),
"luajit" => Box::new(LuaJIT::new(wasm)),
_ => panic!("Bad option: {}", name),
}
}
fn parse_module(name: &str) -> Module { fn parse_module(name: &str) -> Module {
let wasm = deserialize_file(name).expect("Failed to parse Wasm file"); let wasm = deserialize_file(name).expect("Failed to parse Wasm file");
@ -22,16 +17,57 @@ fn parse_module(name: &str) -> Module {
} }
} }
fn main() { fn run_translator<'a, T: Transpiler<'a>>(wasm: &'a Module) {
let mut args = std::env::args().skip(1); let module = T::new(wasm);
let name = args.next().expect("No language specified");
let output = std::io::stdout(); let output = std::io::stdout();
for v in args { module
let wasm = parse_module(&v); .transpile(&mut output.lock())
let module = lang_from_string(&name, &wasm); .expect("Failed to transpile");
}
module.transpile(&mut output.lock()).unwrap(); fn do_translate(name: &str, file: &str) {
let wasm = &parse_module(file);
match name.to_lowercase().as_str() {
"luau" => run_translator::<Luau>(wasm),
"luajit" => run_translator::<LuaJIT>(wasm),
_ => panic!("Bad language: {}", name),
}
}
fn do_runtime(name: &str) {
match name.to_lowercase().as_str() {
"luajit" => println!("{}", LUAJIT_RUNTIME),
"luau" => println!("{}", LUAU_RUNTIME),
_ => panic!("Bad runtime: {}", name),
}
}
fn do_help() {
println!("usage: program translate <lang> <file>");
println!(" or: program runtime <lang>");
println!(" or: program help");
}
fn main() {
let mut args = std::env::args().skip(1);
match args.next().as_deref().unwrap_or("help") {
"help" => do_help(),
"runtime" => {
let lang = args.next().expect("No runtime specified");
do_runtime(&lang);
}
"translate" => {
let lang = args.next().expect("No language specified");
let file = args.next().expect("No file specified");
do_translate(&lang, &file);
}
bad => {
eprintln!("Bad action `{}`; try `help`", bad);
}
} }
} }