Read source from file instead of hardcoding it
This commit is contained in:
parent
1c3d7c6bd1
commit
d3518ef510
@ -3,8 +3,15 @@
|
|||||||
#![allow(dead_code)]
|
#![allow(dead_code)]
|
||||||
|
|
||||||
extern crate regex;
|
extern crate regex;
|
||||||
|
extern crate wat;
|
||||||
|
|
||||||
pub mod ast;
|
pub mod ast;
|
||||||
pub mod parse;
|
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::fs::File;
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
|
|
||||||
fn main() -> std::io::Result<()> {
|
fn main() -> std::io::Result<()> {
|
||||||
let src = r#"
|
// Read the source from a file.
|
||||||
function a(num) {
|
let mut src = String::new();
|
||||||
return num;
|
File::open("test.pvt")?.read_to_string(&mut src)?;
|
||||||
}
|
// Compile it
|
||||||
function main(num) {
|
let binary = pivot::compile(src);
|
||||||
var amt = a(2);
|
// Write it to a file.
|
||||||
return num + amt;
|
|
||||||
}"#;
|
|
||||||
let ast = pivot::parse(src);
|
|
||||||
println!("{}", ast);
|
|
||||||
let code = ast.emit();
|
|
||||||
println!("{}", code);
|
|
||||||
let binary = wat::parse_str(code).unwrap();
|
|
||||||
File::create("out.wasm")?.write_all(&binary)?;
|
File::create("out.wasm")?.write_all(&binary)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -14,9 +14,6 @@ pub fn parse<T: Into<String>>(src: T) -> AstNode {
|
|||||||
.repeat_range(0..usize::MAX)
|
.repeat_range(0..usize::MAX)
|
||||||
.map(|matched| {
|
.map(|matched| {
|
||||||
let data = from_str::<Vec<String>>(&matched)?;
|
let data = from_str::<Vec<String>>(&matched)?;
|
||||||
for d in &data {
|
|
||||||
println!("{}", d);
|
|
||||||
}
|
|
||||||
let mut statements = vec![];
|
let mut statements = vec![];
|
||||||
for d in data {
|
for d in data {
|
||||||
statements.push(from_str::<AstNode>(&d)?);
|
statements.push(from_str::<AstNode>(&d)?);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user