|
| 1 | +use crate::error::Error; |
| 2 | +use clap::Parser; |
| 3 | +use std::env; |
| 4 | +#[derive(Parser, Debug)] |
| 5 | +#[command(version, about = "Compiles input-file.c creating an executable /path/to/input-file using the system gcc", long_about = None)] // Read from `Cargo.toml` |
| 6 | +pub struct Cli { |
| 7 | + /// Input C file name |
| 8 | + input_file: String, |
| 9 | + /// Run the lexer but stop before parsing |
| 10 | + #[arg(long, default_value_t = false)] |
| 11 | + lex: bool, |
| 12 | + /// Print out the tokens found by the lexer |
| 13 | + #[arg(long = "print-tokens", default_value_t = false)] |
| 14 | + print_tokens: bool, |
| 15 | + /// Run the lexer and parser but stop before codegen |
| 16 | + #[arg(long, default_value_t = false)] |
| 17 | + parse: bool, |
| 18 | + /// Pretty print the output of the parser |
| 19 | + #[arg(long = "pretty-print", default_value_t = false)] |
| 20 | + pretty_print: bool, |
| 21 | + /// Run up to the parser and semantic analysis |
| 22 | + #[arg(long, default_value_t = false)] |
| 23 | + validate: bool, |
| 24 | + /// Run the lexer, parser, and TACKY IR generation stopping before assembly generation |
| 25 | + #[arg(long, default_value_t = false)] |
| 26 | + tacky: bool, |
| 27 | + /// Pretty print TACKY IR after generation |
| 28 | + #[arg(long = "pretty-print-tacky", default_value_t = false)] |
| 29 | + pretty_print_tacky: bool, |
| 30 | + /// Run the lexer, parser, codegen but stop before assembly generation |
| 31 | + #[arg(long, default_value_t = false)] |
| 32 | + codegen: bool, |
| 33 | + /// Emit assembly in input-file.s rather than linking an executable |
| 34 | + #[arg(long = "asm-only", short = 'S', default_value_t = false)] |
| 35 | + asm_only: bool, |
| 36 | +} |
| 37 | + |
| 38 | +type ArgVec = Vec<String>; |
| 39 | +fn parse_arguments(args: ArgVec) -> Cli { |
| 40 | + Cli::parse_from(args) |
| 41 | + // TODO exclusivity checking for --lex, --codegen, etc. |
| 42 | +} |
| 43 | + |
| 44 | +pub fn driver_main(args: &mut env::Args) -> Result<(), Error> { |
| 45 | + let args: ArgVec = args.collect(); |
| 46 | + let cli = parse_arguments(args); |
| 47 | + dbg!(cli); |
| 48 | + Ok(()) |
| 49 | +} |
| 50 | + |
| 51 | +#[cfg(test)] |
| 52 | +mod tests { |
| 53 | + use crate::driver::parse_arguments; |
| 54 | + |
| 55 | + #[test] |
| 56 | + fn arg_parser() { |
| 57 | + // TODO test extra arg parsing logic |
| 58 | + let cli = parse_arguments(vec!["".to_string(), "file".to_string()]); |
| 59 | + assert_eq!(cli.input_file, "file"); |
| 60 | + } |
| 61 | +} |
0 commit comments