Cargo features to control debugging output

This commit is contained in:
Emmanuel BENOîT 2022-12-31 15:26:34 +01:00
parent 14aef73d93
commit c31a97eb76
2 changed files with 9 additions and 3 deletions

View file

@ -6,3 +6,8 @@ edition = "2021"
[dependencies] [dependencies]
lazy_static = "1.4.0" lazy_static = "1.4.0"
[features]
debug_output = ["dump_tokens", "dump_ast"]
dump_tokens = []
dump_ast = []

View file

@ -11,6 +11,7 @@ use std::{
process::ExitCode, process::ExitCode,
}; };
#[cfg(feature = "dump_ast")]
use ast::AstDumper; use ast::AstDumper;
use errors::{ErrorHandler, ErrorType}; use errors::{ErrorHandler, ErrorType};
use interpreter::evaluate; use interpreter::evaluate;
@ -24,6 +25,7 @@ fn run(source: String) -> ErrorHandler {
let scanner = Scanner::new(source); let scanner = Scanner::new(source);
let tokens = scanner.scan_tokens(&mut error_handler); let tokens = scanner.scan_tokens(&mut error_handler);
#[cfg(feature = "dump_tokens")]
for token in tokens.iter() { for token in tokens.iter() {
println!("{:#?}", token); println!("{:#?}", token);
} }
@ -32,10 +34,9 @@ fn run(source: String) -> ErrorHandler {
match parser.parse(&mut error_handler) { match parser.parse(&mut error_handler) {
None => (), None => (),
Some(ast) => { Some(ast) => {
#[cfg(feature = "dump_ast")]
println!("AST generated ! {}", ast.dump()); println!("AST generated ! {}", ast.dump());
if let Some(v) = evaluate(&mut error_handler, &ast) { evaluate(&mut error_handler, &ast);
println!("Final evaluated value: {:?}", v)
}
} }
} }