From 1ad99fcfa50f9f07a996c21e1629534e0b9c3bfc Mon Sep 17 00:00:00 2001 From: azur Date: Wed, 1 Mar 2023 04:47:56 +0700 Subject: [PATCH] write to file --- b.hlm | 2 +- src/main.rs | 41 ++++++++++++++++------------------------- 2 files changed, 17 insertions(+), 26 deletions(-) diff --git a/b.hlm b/b.hlm index b3223e5..52465b2 100644 --- a/b.hlm +++ b/b.hlm @@ -1,5 +1,5 @@ println((\x: num -> x + 35)(34)); -16---1*3/-16+8%-2; +println(16---1*3/-16+8%-2); let a: num = 1 in println(a); diff --git a/src/main.rs b/src/main.rs index d5d71e7..3820dd0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,8 @@ pub mod read; pub mod trans; +use std::io::Write; + use read::parse::{lex, parse}; use trans::low::{translate_expr, translate_js}; @@ -15,34 +17,23 @@ fn main() { let (ast, parse_errs) = parse(tokens, src.len()); 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::>(); - - for expr in &nexprs { - println!("{}", expr); - } - println!("\x1b[90m───JS OUTPUT──────────────────────────────────────\x1b[0m"); let jsexprs = nexprs.into_iter().map(translate_js).collect::>(); - for expr in &jsexprs { - let s = format!("{}", expr); - println!("{}{}", s, if s.ends_with(';') { "" } else { ";" }); - } - println!(); + 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::>() + .join("\n"); + file.write_all(s.as_bytes()).expect("Failed to write to file"); } parse_errs