Read source from file instead of hardcoding it
This commit is contained in:
parent
1c3d7c6bd1
commit
d3518ef510
@ -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()
|
||||
}
|
||||
|
21
src/main.rs
21
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(())
|
||||
}
|
||||
|
@ -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)?);
|
||||
|
Loading…
x
Reference in New Issue
Block a user