Skip to content

Commit 55a4cf9

Browse files
authored
refactor: restructure CLI with optimization flags and debug modes (#242)
Refactor command-line interface to support optimization levels and comprehensive debug output modes with improved error handling. Changes: - Add library target configuration to Cargo.toml: - Expose wavec as both binary and library - Enable path specification for lib.rs - Refactor CLI argument parsing: - Extract commands module with handle_run/handle_build functions - Add DebugFlags struct for modular debug output control - Support optimization flags (-O0, -O1, -O2, -O3, -Oz, -Ofast) - Remove verbose flag in favor of debug modes - Implement debug output system: - --debug-wave=tokens: Display lexer token stream - --debug-wave=ast: Show abstract syntax tree - --debug-wave=ir: Print LLVM intermediate representation - --debug-wave=mc: Display machine code path - --debug-wave=hex: Dump binary in hexadecimal format - --debug-wave=all: Enable all debug outputs - Update compilation pipeline: - Pass optimization flag to clang invocation - Thread debug flags through runner and backend - Remove standalone CompilerConfig system - Simplify runner.rs execution flow: - Direct output instead of buffering - Remove unused import resolution code - Pass opt_flag and debug flags to backend - Add dedicated errors module: - Define CliError enum (NotEnoughArgs, UnknownCommand, MissingArgument) - Implement Display trait for formatted error messages - Streamline help output: - Group commands, optimization, and debug options - Add practical usage examples - Remove verbose mode information Examples: wavec run test.wave wavec run -O3 test.wave wavec run --debug-wave=ir test.wave wavec run -Ofast --debug-wave=all test.wave The refactored CLI provides better developer experience with granular control over optimization and debugging output.
1 parent 3760907 commit 55a4cf9

File tree

8 files changed

+221
-440
lines changed

8 files changed

+221
-440
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ name = "wavec"
33
version = "0.1.5-pre-beta"
44
edition = "2021"
55

6+
[lib]
7+
name = "wavec"
8+
path = "src/lib.rs"
9+
610
# [target.x86_64-apple-darwin]
711
# linker = "clang"
812
# [target.aarch64-apple-darwin]

llvm_temporary/src/llvm_temporary/llvm_backend.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,14 @@ use std::process::Command;
22
use std::fs;
33
use std::path::Path;
44

5-
pub fn compile_ir_to_machine_code(ir: &str, file_stem: &str) -> String {
6-
let target_dir = Path::new("target");
7-
if !target_dir.exists() {
8-
fs::create_dir_all(target_dir).expect("Unable to create target directory");
9-
}
10-
5+
pub fn compile_ir_to_machine_code(ir: &str, file_stem: &str, opt_flag: &str) -> String {
116
let ir_path = "target/temp.ll";
12-
fs::write(ir_path, ir).expect("Unable to write IR to file");
7+
fs::write(ir_path, ir).unwrap();
138

149
let machine_code_path = format!("target/{}", file_stem);
1510

1611
let output = Command::new("clang")
17-
.arg("-O2")
12+
.arg(opt_flag)
1813
.arg("-o")
1914
.arg(&machine_code_path)
2015
.arg(ir_path)

src/commands.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
use std::path::Path;
2+
use crate::{compile_and_img, compile_and_run};
3+
use crate::errors::CliError;
4+
5+
#[derive(Default)]
6+
pub struct DebugFlags {
7+
pub ast: bool,
8+
pub tokens: bool,
9+
pub ir: bool,
10+
pub mc: bool,
11+
pub hex: bool,
12+
}
13+
14+
impl DebugFlags {
15+
pub fn apply(&mut self, mode: &str) {
16+
match mode {
17+
"tokens" => self.tokens = true,
18+
"ast" => self.ast = true,
19+
"ir" => self.ir = true,
20+
"mc" => self.mc = true,
21+
"hex" => self.hex = true,
22+
"all" => {
23+
self.ast = true;
24+
self.ir = true;
25+
self.mc = true;
26+
self.hex = true;
27+
}
28+
_ => {}
29+
}
30+
}
31+
}
32+
33+
pub fn handle_run(
34+
file_path: &Path,
35+
opt_flag: &str,
36+
debug: &DebugFlags,
37+
) -> Result<(), CliError> {
38+
unsafe {
39+
compile_and_run(file_path, opt_flag, debug);
40+
}
41+
Ok(())
42+
}
43+
44+
pub fn handle_build(
45+
file_path: &Path,
46+
opt_flag: &str,
47+
debug: &DebugFlags,
48+
) -> Result<(), CliError> {
49+
println!("Building with {}...", opt_flag);
50+
51+
unsafe {
52+
compile_and_img(file_path);
53+
}
54+
55+
if debug.mc {
56+
println!("Machine code built successfully.");
57+
}
58+
59+
Ok(())
60+
}
61+
62+
pub fn img_run(file_path: &Path) -> Result<(), CliError> {
63+
unsafe {
64+
compile_and_img(file_path);
65+
}
66+
Ok(())
67+
}

src/compiler_config.rs

Lines changed: 0 additions & 183 deletions
This file was deleted.

src/errors.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use std::fmt;
2+
3+
#[derive(Debug)]
4+
pub enum CliError {
5+
NotEnoughArgs,
6+
UnknownCommand(String),
7+
MissingArgument {
8+
command: &'static str,
9+
expected: &'static str,
10+
},
11+
}
12+
13+
impl fmt::Display for CliError {
14+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
15+
match self {
16+
CliError::NotEnoughArgs => write!(f, "Error: Not enough arguments"),
17+
CliError::UnknownCommand(cmd) => write!(f, "Error: Unknown command '{}'", cmd),
18+
CliError::MissingArgument { command, expected } => write!(
19+
f,
20+
"Error: Missing argument for '{}'. Expected: {}",
21+
command, expected
22+
),
23+
}
24+
}
25+
}

src/lib.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
pub mod runner;
22
pub mod version;
3+
pub mod commands;
4+
pub mod errors;
35

46
use std::path::Path;
57
use colorex::Colorize;
68
use crate::version::get_os_pretty_name;
79

8-
pub unsafe fn compile_and_run(path: &Path) {
9-
runner::run_wave_file(path);
10+
use commands::DebugFlags;
11+
12+
pub unsafe fn compile_and_run(path: &Path, opt_flag: &str, debug: &DebugFlags) {
13+
runner::run_wave_file(path, opt_flag, debug);
1014
}
1115

1216
pub unsafe fn compile_and_img(path: &Path) {
@@ -16,8 +20,10 @@ pub unsafe fn compile_and_img(path: &Path) {
1620
pub fn version_wave() {
1721
let os = format!("({})", get_os_pretty_name()).color("117,117,117");
1822

19-
println!("{} {} {}",
20-
"wavec".color("2,161,47"),
21-
version::version().color("2,161,47"),
22-
os);
23+
println!(
24+
"{} {} {}",
25+
"wavec".color("2,161,47"),
26+
version::version().color("2,161,47"),
27+
os
28+
);
2329
}

0 commit comments

Comments
 (0)