mirror of
https://github.com/exoticorn/curlywas.git
synced 2026-01-20 11:46:43 +01:00
make curlywas available as a library
This commit is contained in:
33
src/lib.rs
Normal file
33
src/lib.rs
Normal file
@@ -0,0 +1,33 @@
|
||||
use anyhow::{bail, Result};
|
||||
use std::io::prelude::*;
|
||||
use std::{fs::File, path::Path};
|
||||
|
||||
mod ast;
|
||||
mod constfold;
|
||||
mod emit;
|
||||
mod intrinsics;
|
||||
mod parser;
|
||||
mod typecheck;
|
||||
|
||||
type Span = std::ops::Range<usize>;
|
||||
|
||||
pub fn compile_file<P: AsRef<Path>>(path: P) -> Result<Vec<u8>> {
|
||||
let mut input = String::new();
|
||||
File::open(path)?.read_to_string(&mut input)?;
|
||||
|
||||
compile_str(&input)
|
||||
}
|
||||
|
||||
pub fn compile_str(input: &str) -> Result<Vec<u8>> {
|
||||
let mut script = match parser::parse(&input) {
|
||||
Ok(script) => script,
|
||||
Err(_) => bail!("Parse failed"),
|
||||
};
|
||||
|
||||
constfold::fold_script(&mut script);
|
||||
if let Err(_) = typecheck::tc_script(&mut script, &input) {
|
||||
bail!("Type check failed");
|
||||
}
|
||||
let wasm = emit::emit(&script);
|
||||
Ok(wasm)
|
||||
}
|
||||
24
src/main.rs
24
src/main.rs
@@ -1,15 +1,8 @@
|
||||
use anyhow::{anyhow, bail, Result};
|
||||
use anyhow::{anyhow, Result};
|
||||
use std::io::prelude::*;
|
||||
use std::{fs::File, path::PathBuf};
|
||||
|
||||
mod ast;
|
||||
mod constfold;
|
||||
mod emit;
|
||||
mod parser;
|
||||
mod typecheck;
|
||||
mod intrinsics;
|
||||
|
||||
type Span = std::ops::Range<usize>;
|
||||
use curlywas::compile_file;
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let mut filename = PathBuf::from(
|
||||
@@ -17,19 +10,8 @@ fn main() -> Result<()> {
|
||||
.nth(1)
|
||||
.ok_or_else(|| anyhow!("Path to .hw file missing"))?,
|
||||
);
|
||||
let mut input = String::new();
|
||||
File::open(&filename)?.read_to_string(&mut input)?;
|
||||
|
||||
let mut script = match parser::parse(&input) {
|
||||
Ok(script) => script,
|
||||
Err(_) => bail!("Parse failed")
|
||||
};
|
||||
|
||||
constfold::fold_script(&mut script);
|
||||
if let Err(_) = typecheck::tc_script(&mut script, &input) {
|
||||
bail!("Type check failed");
|
||||
}
|
||||
let wasm = emit::emit(&script);
|
||||
let wasm = compile_file(&filename)?;
|
||||
|
||||
wasmparser::validate(&wasm)?;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user