diff options
| Commit | acbd7c4b6cb7917ab0033d2cb18df182d614a85a (patch) | |
|---|---|---|
| Author | 2025-02-14 18:15:12 +0200 | |
| Committer | 2025-02-14 18:15:12 +0200 | |
| Files | 8b6e351f066119574c7f0782187ae6ba06b7a424 /assembler/src | |
| tonkobali-acbd7c4b6cb7917ab0033d2cb18df182d614a85a.tar tonkobali-acbd7c4b6cb7917ab0033d2cb18df182d614a85a.tar.gz tonkobali-acbd7c4b6cb7917ab0033d2cb18df182d614a85a.zip | ||
| Parent | 741c317d0ab0481117033b6826cc69856807506e (diff) | |
feat(assembler): Implement proper cli interface and improve debug output
2 files changed, 57 insertions, 25 deletions
| -rw-r--r-- | assembler/src/main.rs | 61 | ||||
| -rw-r--r-- | assembler/src/parser/mod.rs | 21 |
diff --git a/assembler/src/main.rs b/assembler/src/main.rs index 3a6dd4a..f2dde97 100644 --- a/assembler/src/main.rs +++ b/assembler/src/main.rs @@ -3,18 +3,33 @@ mod parser; use std::fs::File; use std::io::{Write, BufRead, BufReader}; +use clap::Parser; use parser::Program; pub(crate) type Word = i64; -static PROGRAM_START: usize = 10; static PROGRAM_COUNTER: usize = 3; +#[derive(Parser, Debug)] +#[command(about, long_about = None)] +struct Args { + source: String, + + #[arg(default_value = "program.bin")] + destination: String, + + #[arg(short, long, default_value_t = 10)] + start_index: usize, + + #[arg(long, default_value_t = false)] + debug: bool, +} + fn main() { - let file_path = "./example.s"; + let args = Args::parse(); /* Read */ - let file = File::open(file_path).unwrap(); + let file = File::open(args.source).unwrap(); let lines = BufReader::new(file).lines(); /* Lex */ @@ -23,29 +38,47 @@ fn main() { tokens.extend(lexer::split(line)); } - println!("{:#?}", tokens); + if args.debug { + println!("\n### Lexed tokens ###\n"); + println!("{:#?}", tokens); + } /* Parse */ let mut prog = Program::new(tokens); - println!("{}", prog); - println!(""); + + if args.debug { + println!("\n### Parsed tokens ###\n"); + prog.print(0); + } /* Semantic analysis */ prog.remove_definition_addresses(); - println!("{}", prog); - println!(""); + + if args.debug { + println!("\n### After remove_definition_addresses ###\n"); + prog.print(0); + } + prog.analyze(); /* Intermediate code gen */ prog.collapse_instructions(); - println!("{}", prog); - println!(""); + + if args.debug { + println!("\n### After collapse_instructions ###\n"); + prog.print(0); + } /* Machine code gen */ - prog.instructions_to_constants(PROGRAM_START); - println!("{}", prog); - let binary = prog.to_bytes(PROGRAM_START, PROGRAM_COUNTER); + prog.instructions_to_constants(args.start_index); + + if args.debug { + println!("\n### After instructions_to_constants ###\n"); + prog.print(args.start_index); + } + + let binary = prog.to_bytes(args.start_index, PROGRAM_COUNTER); - let mut out = File::create("program.bin").unwrap(); + let mut out = File::create(args.destination).unwrap(); out.write_all(&binary).unwrap(); } diff --git a/assembler/src/parser/mod.rs b/assembler/src/parser/mod.rs index d43b9c5..f93875b 100644 --- a/assembler/src/parser/mod.rs +++ b/assembler/src/parser/mod.rs @@ -16,25 +16,24 @@ pub(crate) struct Program { instructions: Vec<Instruction>, } -impl Display for Program { - fn fmt(&self, f: &mut Formatter) -> Result { - writeln!(f, "-- Definitions --")?; +impl Program { + pub(crate) fn print(&self, start_index: usize) { + println!("-- Definitions --"); for (name, loc) in self.definitions.iter() { - writeln!(f, "{} := {:?}", name, loc)?; + println!("{} := {:?}", name, loc); } - writeln!(f, "-- Constants --")?; + println!("-- Constants --"); for (i, cnst) in self.constants.iter().enumerate() { - writeln!(f, "{: >8}: {} ", i, cnst)?; + println!("{: >8}: {} ", i + start_index, cnst); } - writeln!(f, "-- Instructions --")?; + println!("-- Instructions --"); for (i, instr) in self.instructions.iter().enumerate() { - write!(f, "{: >8}: {} ", i, instr.name)?; + print!("{: >8}: {} ", i + start_index, instr.name); for arg in &instr.args { - write!(f, "{:?}, ", arg)?; + print!("{:?}, ", arg); } - writeln!(f, "")?; + println!(""); } - Ok(()) } } |
