mirror of
https://github.com/azur1s/bobbylisp.git
synced 2024-10-16 02:37:40 -05:00
optional log, redirect output file
This commit is contained in:
parent
9f4cf84f34
commit
7f28b2f8df
|
@ -23,5 +23,11 @@ pub enum Options {
|
||||||
/// Print parsed AST and exit (for debugging).
|
/// Print parsed AST and exit (for debugging).
|
||||||
#[clap(short, long)]
|
#[clap(short, long)]
|
||||||
ast: bool,
|
ast: bool,
|
||||||
|
/// Log process.
|
||||||
|
#[clap(short, long)]
|
||||||
|
log: bool,
|
||||||
|
/// Output file path.
|
||||||
|
#[clap(short, long, parse(from_os_str))]
|
||||||
|
output: Option<PathBuf>,
|
||||||
},
|
},
|
||||||
}
|
}
|
|
@ -20,11 +20,19 @@ fn main() {
|
||||||
Options::Compile {
|
Options::Compile {
|
||||||
input: file_name,
|
input: file_name,
|
||||||
ast: _print_ast,
|
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
|
// Start timer
|
||||||
let start = std::time::Instant::now();
|
let start = std::time::Instant::now();
|
||||||
|
|
||||||
// Get file contents
|
// Get file contents
|
||||||
|
logif!(0, format!("Reading {}", &file_name.display()));
|
||||||
let src = fs::read_to_string(&file_name).expect("Failed to read file");
|
let src = fs::read_to_string(&file_name).expect("Failed to read file");
|
||||||
|
|
||||||
// Lex the file
|
// Lex the file
|
||||||
|
@ -101,31 +109,35 @@ fn main() {
|
||||||
report.finish().print(Source::from(&src)).unwrap();
|
report.finish().print(Source::from(&src)).unwrap();
|
||||||
}
|
}
|
||||||
); // End errors reporting
|
); // 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 {
|
match ast {
|
||||||
Some(ast) => {
|
Some(ast) => {
|
||||||
// Convert the AST to HIR
|
// Convert the AST to HIR
|
||||||
let ir = ast_to_ir(ast);
|
let ir = ast_to_ir(ast);
|
||||||
log(0, "Generated HIR.");
|
logif!(0, "Generated HIR.");
|
||||||
|
|
||||||
// Generate code
|
// Generate code
|
||||||
let mut codegen = cpp::Codegen::new();
|
let mut codegen = cpp::Codegen::new();
|
||||||
codegen.gen(ir);
|
codegen.gen(ir);
|
||||||
log(0, "Successfully generated code.");
|
logif!(0, "Successfully generated code.");
|
||||||
|
|
||||||
// Write code to file
|
// 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");
|
file.write_all(codegen.emitted.as_bytes()).expect("Failed to write to file");
|
||||||
|
|
||||||
// End timer
|
// End timer
|
||||||
let duration = start.elapsed().as_millis();
|
let duration = start.elapsed().as_millis();
|
||||||
|
|
||||||
log(0, format!("Compilation took {}ms", duration));
|
logif!(0, format!("Compilation took {}ms", duration));
|
||||||
log(0, format!("Wrote output to `out.cpp`. All done."));
|
logif!(0, format!("Wrote output to `{}`. All done.", output_path.display()));
|
||||||
},
|
},
|
||||||
None => {
|
None => {
|
||||||
log(2, "Failed to parse.");
|
logif!(2, "Failed to parse.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue