Skip to content

Commit 7de1895

Browse files
committed
Basic arg parsing
1 parent ab040b5 commit 7de1895

5 files changed

Lines changed: 69 additions & 7 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[package]
2-
name = "learning-compilers"
2+
name = "notcc"
3+
description = "A compiler for a subset of C written in Rust. Based on the book \"Writing a C Compiler\" by Nora Sandler."
34
version = "0.1.0"
45
edition = "2024"
56

rust_src/driver.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
}

rust_src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1+
mod driver;
12
pub mod error;
23

3-
pub fn driver() {
4-
println!("you did it!")
5-
}
4+
pub use driver::{Cli, driver_main};

rust_src/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
//mod driver;
22
use notcc;
3-
3+
use std::env;
44
fn main() -> Result<(), notcc::error::Error> {
5-
notcc::driver();
5+
// TODO why mut?
6+
notcc::driver_main(&mut env::args())?;
67
return notcc::error::fail("Bad news bro!");
78
}

0 commit comments

Comments
 (0)