From 7f28b2f8df4a8b0b80e928056e9408884e78de2e Mon Sep 17 00:00:00 2001 From: Natapat Samutpong Date: Mon, 7 Mar 2022 16:48:36 +0700 Subject: [PATCH] optional log, redirect output file --- crates/main/src/args.rs | 6 ++++++ crates/main/src/main.rs | 26 +++++++++++++++++++------- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/crates/main/src/args.rs b/crates/main/src/args.rs index 6049da0..41d55a3 100644 --- a/crates/main/src/args.rs +++ b/crates/main/src/args.rs @@ -23,5 +23,11 @@ pub enum Options { /// Print parsed AST and exit (for debugging). #[clap(short, long)] ast: bool, + /// Log process. + #[clap(short, long)] + log: bool, + /// Output file path. + #[clap(short, long, parse(from_os_str))] + output: Option, }, } \ No newline at end of file diff --git a/crates/main/src/main.rs b/crates/main/src/main.rs index 73b5077..c23efe9 100644 --- a/crates/main/src/main.rs +++ b/crates/main/src/main.rs @@ -20,11 +20,19 @@ fn main() { Options::Compile { input: file_name, ast: _print_ast, + log: should_log, + output, } => { + // Macro to only log if `should_log` is true + macro_rules! logif { + ($level:expr, $msg:expr) => { if should_log { log($level, $msg); } }; + } + // Start timer let start = std::time::Instant::now(); // Get file contents + logif!(0, format!("Reading {}", &file_name.display())); let src = fs::read_to_string(&file_name).expect("Failed to read file"); // Lex the file @@ -101,31 +109,35 @@ fn main() { report.finish().print(Source::from(&src)).unwrap(); } ); // End errors reporting - log(0, format!("Parsing took {}ms", start.elapsed().as_millis())); + logif!(0, format!("Parsing took {}ms", start.elapsed().as_millis())); match ast { Some(ast) => { // Convert the AST to HIR let ir = ast_to_ir(ast); - log(0, "Generated HIR."); + logif!(0, "Generated HIR."); // Generate code let mut codegen = cpp::Codegen::new(); codegen.gen(ir); - log(0, "Successfully generated code."); + logif!(0, "Successfully generated code."); // Write code to file - let mut file = fs::File::create("out.cpp").expect("Failed to create file"); + let output_path = match output { + Some(output) => output, + None => file_name.with_extension("cpp").file_name().unwrap().to_os_string().into(), + }; + let mut file = fs::File::create(&output_path).expect("Failed to create file"); file.write_all(codegen.emitted.as_bytes()).expect("Failed to write to file"); // End timer let duration = start.elapsed().as_millis(); - log(0, format!("Compilation took {}ms", duration)); - log(0, format!("Wrote output to `out.cpp`. All done.")); + logif!(0, format!("Compilation took {}ms", duration)); + logif!(0, format!("Wrote output to `{}`. All done.", output_path.display())); }, None => { - log(2, "Failed to parse."); + logif!(2, "Failed to parse."); } } }