use ruchy::{Parser, WasmEmitter};
use std::fs;
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("🚀 Ruchy to WebAssembly Compilation Example\n");
compile_example("Simple Expression", "42 + 58", "simple.wasm")?;
compile_example(
"Function Definition",
r"
fun add(a, b) {
a + b
}
add(10, 20)
",
"function.wasm",
)?;
compile_example(
"Multiple Operations",
r"
fun multiply(x, y) {
x * y
}
multiply(8, 8)
",
"multiple.wasm",
)?;
compile_example(
"Mathematical Expression",
r"
let x = 10
let y = 20
x * y + 50
",
"math.wasm",
)?;
println!("\n✅ All examples compiled successfully!");
println!("📁 WASM files saved to current directory");
Ok(())
}
fn compile_example(
name: &str,
source: &str,
output_file: &str,
) -> Result<(), Box<dyn std::error::Error>> {
println!("📝 Compiling: {name}");
println!(" Source: {}", source.lines().next().unwrap_or("").trim());
let mut parser = Parser::new(source);
let ast = parser.parse().map_err(|e| format!("Parse error: {e}"))?;
let emitter = WasmEmitter::new();
let wasm_bytes = emitter
.emit(&ast)
.map_err(|e| format!("WASM generation error: {e}"))?;
wasmparser::validate(&wasm_bytes).map_err(|e| format!("WASM validation error: {e}"))?;
fs::write(output_file, &wasm_bytes)?;
println!(
" ✓ Generated {} ({} bytes)",
output_file,
wasm_bytes.len()
);
Ok(())
}