mirror of https://github.com/azur1s/bobbylisp.git
52 lines
1.5 KiB
Rust
52 lines
1.5 KiB
Rust
#![feature(trait_alias)]
|
|
pub mod read;
|
|
pub mod trans;
|
|
|
|
use std::io::Write;
|
|
|
|
use read::parse::{lex, parse};
|
|
use trans::low::{translate_expr, translate_js};
|
|
|
|
fn main() {
|
|
let path = std::env::args().nth(1).expect("No file path provided");
|
|
let src = std::fs::read_to_string(path).expect("Failed to read file");
|
|
|
|
let (tokens, lex_errs) = lex(src.to_owned());
|
|
|
|
let parse_errs = if let Some(tokens) = tokens {
|
|
let (ast, parse_errs) = parse(tokens, src.len());
|
|
|
|
if let Some(ast) = ast {
|
|
let nexprs = ast.into_iter().map(|(e, _)| translate_expr(e)).collect::<Vec<_>>();
|
|
let jsexprs = nexprs.into_iter().map(translate_js).collect::<Vec<_>>();
|
|
|
|
let mut file = std::fs::File::create("out.js").expect("Failed to create file");
|
|
let s = jsexprs
|
|
.into_iter()
|
|
.map(|e| {
|
|
let s = format!("{}", e);
|
|
if s.ends_with(';') {
|
|
s
|
|
} else {
|
|
format!("{};", s)
|
|
}
|
|
})
|
|
.collect::<Vec<_>>()
|
|
.join("\n");
|
|
file.write_all(s.as_bytes()).expect("Failed to write to file");
|
|
}
|
|
|
|
parse_errs
|
|
} else {
|
|
Vec::new()
|
|
};
|
|
|
|
if !lex_errs.is_empty() || !parse_errs.is_empty() {
|
|
lex_errs
|
|
.into_iter()
|
|
.map(|e| e.map(|c| c.to_string()))
|
|
.chain(parse_errs.into_iter().map(|e| e.map(|t| t.to_string())))
|
|
.for_each(|e| println!("{}", e));
|
|
}
|
|
}
|