From b15f7f1971e33e1e5ad58fdfdefe7d5dddb934ca Mon Sep 17 00:00:00 2001 From: Natapat Samutpong Date: Sat, 5 Mar 2022 20:53:28 +0700 Subject: [PATCH] write to file, use `console.log` --- src/back/js.rs | 2 +- src/main.rs | 38 ++++++++++++++++++++++++-------------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/back/js.rs b/src/back/js.rs index faee345..237212c 100644 --- a/src/back/js.rs +++ b/src/back/js.rs @@ -56,7 +56,7 @@ fn gen_ir(ir: &IR) -> String { match name.as_str() { "print" => { let args = gen_ir(&args[0]); - format!("process.stdout.write({});", args.trim_end_matches(";")) + format!("console.log({});", args.trim_end_matches(";")) }, _ => { let args = args diff --git a/src/main.rs b/src/main.rs index cddb76c..78e8c6a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,8 @@ -use std::{fs, io::Write, time}; +use std::{ + fs, + io::{self, Write}, + time, +}; use chumsky::{Parser, Stream}; use clap::Parser as ArgParser; @@ -27,21 +31,25 @@ use crate::util::log; fn main() { let args = Args::parse(); match args.options { - Options::Compile { input: file_name, ast: _print_ast } => { + Options::Compile { + input: file_name, + ast: _print_ast, + } => { // Get file contents. let src = fs::read_to_string(&file_name).expect("Failed to read file"); - + // Lex the file. let (tokens, lex_error) = lexer().parse_recovery(src.as_str()); let len = src.chars().count(); - + // Parse the file. - let (ast, parse_error) = parser().parse_recovery(Stream::from_iter(len..len + 1, tokens.clone().unwrap().into_iter())); - + let (ast, parse_error) = parser().parse_recovery(Stream::from_iter( + len..len + 1, + tokens.clone().unwrap().into_iter(), + )); + if lex_error.is_empty() { - if parse_error.is_empty() { - match ast { // If there is some AST then generate code. Some(ast) => { @@ -50,22 +58,24 @@ fn main() { let ir = ir::ast_to_ir(ast); let out = back::js::gen(ir); - println!("{}", out); + + let file = fs::File::create("out.js").expect("Failed to create file"); + let mut file = io::BufWriter::new(file); + file.write_all(out.as_bytes()) + .expect("Failed to write file"); let all_elapsed = start.elapsed(); log(0, format!("Done in {}s", all_elapsed.as_secs_f64())); - }, + } // If there is no AST, then notify the user. None => println!("no ast :("), }; - } else { eprintln!("{:#?}\n(Parser error)", parse_error); } - } else { eprintln!("{:#?}\n(Lexer error)", lex_error); } - }, + } } -} \ No newline at end of file +}