diff --git a/src/lib.rs b/src/lib.rs index 08a0ac2..c8450c1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,8 +3,15 @@ #![allow(dead_code)] extern crate regex; +extern crate wat; pub mod ast; pub mod parse; -pub use parse::parse; +pub fn compile>(src: T) -> Vec { + wat::parse_str(compile_wat(src)).unwrap() +} + +pub fn compile_wat>(src: T) -> String { + parse::parse(src).emit() +} diff --git a/src/main.rs b/src/main.rs index cc55b7c..4a85abb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,22 +1,13 @@ -extern crate wat; - use std::fs::File; use std::io::prelude::*; fn main() -> std::io::Result<()> { - let src = r#" - function a(num) { - return num; - } - function main(num) { - var amt = a(2); - return num + amt; - }"#; - let ast = pivot::parse(src); - println!("{}", ast); - let code = ast.emit(); - println!("{}", code); - let binary = wat::parse_str(code).unwrap(); + // Read the source from a file. + let mut src = String::new(); + File::open("test.pvt")?.read_to_string(&mut src)?; + // Compile it + let binary = pivot::compile(src); + // Write it to a file. File::create("out.wasm")?.write_all(&binary)?; Ok(()) } diff --git a/src/parse/mod.rs b/src/parse/mod.rs index 567866a..fd7b88d 100644 --- a/src/parse/mod.rs +++ b/src/parse/mod.rs @@ -14,9 +14,6 @@ pub fn parse>(src: T) -> AstNode { .repeat_range(0..usize::MAX) .map(|matched| { let data = from_str::>(&matched)?; - for d in &data { - println!("{}", d); - } let mut statements = vec![]; for d in data { statements.push(from_str::(&d)?); diff --git a/test.pvt b/test.pvt new file mode 100644 index 0000000..bd8cd23 --- /dev/null +++ b/test.pvt @@ -0,0 +1,6 @@ +function main(num) { + return num + a(2); +} +function a(num) { + return num; +}