Read source from file instead of hardcoding it

This commit is contained in:
Garen Tyler 2020-12-10 20:02:00 -07:00
parent 1c3d7c6bd1
commit d3518ef510
4 changed files with 20 additions and 19 deletions

View File

@ -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<T: Into<String>>(src: T) -> Vec<u8> {
wat::parse_str(compile_wat(src)).unwrap()
}
pub fn compile_wat<T: Into<String>>(src: T) -> String {
parse::parse(src).emit()
}

View File

@ -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(())
}

View File

@ -14,9 +14,6 @@ pub fn parse<T: Into<String>>(src: T) -> AstNode {
.repeat_range(0..usize::MAX)
.map(|matched| {
let data = from_str::<Vec<String>>(&matched)?;
for d in &data {
println!("{}", d);
}
let mut statements = vec![];
for d in data {
statements.push(from_str::<AstNode>(&d)?);

6
test.pvt Normal file
View File

@ -0,0 +1,6 @@
function main(num) {
return num + a(2);
}
function a(num) {
return num;
}