celestial_hub_compass/cli/
emit.rs1use clap::Args;
2
3use crate::{ast::Statement, lexer::Lexer, parser::Parser};
4
5#[derive(Args)]
6pub struct EmitASTOptions {
7 #[arg(short = 'f')]
9 pub filepath: String,
10
11 #[arg(short, long, action = clap::ArgAction::Count)]
13 debug: u8,
14}
15
16pub fn ast(
17 EmitASTOptions { filepath, debug }: &EmitASTOptions,
18) -> Result<Vec<Statement>, Box<dyn std::error::Error>> {
19 let source_code = std::fs::read_to_string(filepath)?;
20 let lexer = Lexer::new(&source_code[..], filepath).map_err(|e| e.to_string())?;
21
22 if *debug > 0 {
23 println!("{}", lexer);
24 }
25
26 let ast = Parser::new().parse(lexer)?;
27
28 if *debug > 0 {
29 println!("{:#?}", ast);
30 }
31
32 Ok(ast)
33}