1
1
Fork 0
mirror of https://github.com/azur1s/bobbylisp.git synced 2024-10-16 02:37:40 -05:00

write to file

This commit is contained in:
azur 2023-03-01 04:47:56 +07:00
parent 2e1258f09a
commit 813d0dfd9e
2 changed files with 17 additions and 26 deletions

2
b.hlm
View file

@ -1,5 +1,5 @@
println((\x: num -> x + 35)(34)); println((\x: num -> x + 35)(34));
16---1*3/-16+8%-2; println(16---1*3/-16+8%-2);
let a: num = 1 in let a: num = 1 in
println(a); println(a);

View file

@ -2,6 +2,8 @@
pub mod read; pub mod read;
pub mod trans; pub mod trans;
use std::io::Write;
use read::parse::{lex, parse}; use read::parse::{lex, parse};
use trans::low::{translate_expr, translate_js}; use trans::low::{translate_expr, translate_js};
@ -15,34 +17,23 @@ fn main() {
let (ast, parse_errs) = parse(tokens, src.len()); let (ast, parse_errs) = parse(tokens, src.len());
if let Some(ast) = ast { if let Some(ast) = ast {
println!();
println!("\x1b[90m───SOURCE─────────────────────────────────────────\x1b[0m");
println!("{src}");
println!("\x1b[90m───PARSE TREE─────────────────────────────────────\x1b[0m");
for (e, _) in &ast {
println!("{}", {
let e = format!("{:?}", e);
if e.len() > 50 {
format!("{}...", &e[..47])
} else {
e
}
});
}
println!("\x1b[90m───INTERNAL AST───────────────────────────────────\x1b[0m");
let nexprs = ast.into_iter().map(|(e, _)| translate_expr(e)).collect::<Vec<_>>(); let nexprs = ast.into_iter().map(|(e, _)| translate_expr(e)).collect::<Vec<_>>();
for expr in &nexprs {
println!("{}", expr);
}
println!("\x1b[90m───JS OUTPUT──────────────────────────────────────\x1b[0m");
let jsexprs = nexprs.into_iter().map(translate_js).collect::<Vec<_>>(); let jsexprs = nexprs.into_iter().map(translate_js).collect::<Vec<_>>();
for expr in &jsexprs { let mut file = std::fs::File::create("out.js").expect("Failed to create file");
let s = format!("{}", expr); let s = jsexprs
println!("{}{}", s, if s.ends_with(';') { "" } else { ";" }); .into_iter()
} .map(|e| {
println!(); 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 parse_errs