Website Summary Files Commits Branches @commit @diff @stats
path: root / assembler / src / main.rs
diff options
context:
space:
mode:
Commitacbd7c4b6cb7917ab0033d2cb18df182d614a85a (patch)
Author
 Syndamia <kamen@syndamia.com>
2025-02-14 18:15:12 +0200
Committer
 Syndamia <kamen@syndamia.com>
2025-02-14 18:15:12 +0200
Files8b6e351f066119574c7f0782187ae6ba06b7a424 /assembler/src/main.rs
tonkobali-acbd7c4b6cb7917ab0033d2cb18df182d614a85a.tar
tonkobali-acbd7c4b6cb7917ab0033d2cb18df182d614a85a.tar.gz
tonkobali-acbd7c4b6cb7917ab0033d2cb18df182d614a85a.zip
Parent741c317d0ab0481117033b6826cc69856807506e (diff)
feat(assembler): Implement proper cli interface and improve debug output
1 files changed, 47 insertions, 14 deletions
-rw-r--r--assembler/src/main.rs61
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();
}