From 065bc93bc6eefad26e86428692e9bdab4c989758 Mon Sep 17 00:00:00 2001 From: Hydrostic Date: Sat, 9 May 2026 23:37:59 +0800 Subject: [PATCH] feat(main): add args parse and exec process --- Cargo.lock | 145 +++++++++++++++++++++++++++++++++++++---- Cargo.toml | 3 +- src/frontend/parser.rs | 4 +- src/ir/generator.rs | 4 +- src/ir/mod.rs | 2 +- src/main.rs | 83 ++++++++++++++++++++++- 6 files changed, 222 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 037fc18..2e2a33c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,12 +11,102 @@ dependencies = [ "memchr", ] +[[package]] +name = "anstream" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000" + +[[package]] +name = "anstyle-parse" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys", +] + [[package]] name = "autocfg" version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" +[[package]] +name = "clap" +version = "4.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ddb117e43bbf7dacf0a4190fef4d345b9bad68dfc649cb349e7d17d28428e51" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "714a53001bf66416adb0e2ef5ac857140e7dc3a0c48fb28b2f10762fc4b5069f" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2ce8604710f6733aa641a2b3731eaa1e8b3d9973d5e3565da11800813f997a9" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8d4a3bb8b1e0c1050499d1815f5ab16d04f0959b233085fb31653fbfc9d98f9" + [[package]] name = "codespan-reporting" version = "0.13.1" @@ -28,6 +118,25 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "colorchoice" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570" + +[[package]] +name = "compiler" +version = "0.1.0" +dependencies = [ + "clap", + "codespan-reporting", + "num", + "petgraph", + "regex", + "strum", + "thiserror", +] + [[package]] name = "equivalent" version = "1.0.2" @@ -77,6 +186,12 @@ dependencies = [ "hashbrown 0.17.0", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + [[package]] name = "memchr" version = "2.8.0" @@ -156,6 +271,12 @@ dependencies = [ "autocfg", ] +[[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + [[package]] name = "petgraph" version = "0.8.3" @@ -215,18 +336,6 @@ version = "0.8.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dc897dd8d9e8bd1ed8cdad82b5966c3e0ecae09fb1907d58efaa013543185d0a" -[[package]] -name = "rusty-minic" -version = "0.1.0" -dependencies = [ - "codespan-reporting", - "num", - "petgraph", - "regex", - "strum", - "thiserror", -] - [[package]] name = "serde" version = "1.0.228" @@ -257,6 +366,12 @@ dependencies = [ "syn", ] +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + [[package]] name = "strum" version = "0.28.0" @@ -330,6 +445,12 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4ac048d71ede7ee76d585517add45da530660ef4390e49b098733c6e897f254" +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + [[package]] name = "winapi-util" version = "0.1.11" diff --git a/Cargo.toml b/Cargo.toml index 71ecf51..484171d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,10 @@ [package] -name = "rusty-minic" +name = "compiler" version = "0.1.0" edition = "2024" [dependencies] +clap = { version = "4.6.1", features = ["derive"] } codespan-reporting = "0.13.1" num = "0.4.3" petgraph = "0.8.3" diff --git a/src/frontend/parser.rs b/src/frontend/parser.rs index d20d164..915cad2 100644 --- a/src/frontend/parser.rs +++ b/src/frontend/parser.rs @@ -255,7 +255,7 @@ impl Parser { } self.advance(1); let mut statements = vec![]; - println!("parse block stmt"); + // println!("parse block stmt"); loop { if self.peek().is_none() { let span = self.next().unwrap().span; @@ -282,7 +282,7 @@ impl Parser { // parse statement here statements.push(self.parse_stmt()?); } - println!("finish parse block stmt"); + // println!("finish parse block stmt"); let end_span = statements.last().map(|s| s.span()).unwrap_or(start_span); Some(BlockStmt { statements, diff --git a/src/ir/generator.rs b/src/ir/generator.rs index f51eb25..f931826 100644 --- a/src/ir/generator.rs +++ b/src/ir/generator.rs @@ -28,7 +28,9 @@ impl Generator { pub fn emit(&mut self, compile_unit: CompileUnit) -> Vec { self.generate_compile_unit(compile_unit) } - + pub fn get_diagnostics(&self) -> &Diagnositics { + &self.diagnostic + } fn generate_compile_unit(&mut self, compile_unit: CompileUnit) -> Vec { let mut instrs = vec![]; use GlobalDeclStmt::*; diff --git a/src/ir/mod.rs b/src/ir/mod.rs index 9ad5206..a7c4177 100644 --- a/src/ir/mod.rs +++ b/src/ir/mod.rs @@ -1,3 +1,3 @@ -mod generator; +pub mod generator; pub mod types; pub mod err; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index dbd654b..071fac6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,85 @@ mod ir; mod utils; mod diagnostic; mod err; -fn main() { - println!("Hello, world!"); + +use std::{fs::File, io::BufRead}; + +use clap::Parser as ArgParser; + +use crate::{frontend::{lexer::Lexer, parser::Parser}, ir::generator::Generator}; + +/// Simple minic compiler built by Rust +#[derive(ArgParser, Debug)] +#[command(version, about, long_about = None)] +struct Args { + /// Output the generated IR code + #[arg(short = 'I', long = "ir")] + output_ir: bool, + /// Use recursive descent parsing + #[arg(short = 'D', long = "recursive-descent")] + recursive_descent: bool, + /// Useless paramter + #[arg(short = 'S', long = "symbol")] + _useless: bool, + /// Output file for the generated code (default: stdout) + #[arg(short = 'o', long = "output")] + output: Option, + /// Source file to compile + source: String, +} + +fn main() { + let args = Args::parse(); + if !args.output_ir { + eprintln!("Currently only IR generation is supported. Use -I to enable it."); + return; + } + if !args.recursive_descent { + eprintln!("Currently only recursive descent parsing is supported. Use -D to enable it."); + return; + } + let source_path = std::path::Path::new(&args.source); + let file = match File::open(&args.source) { + Ok(f) => f, + Err(e) => { + eprintln!("Failed to open source file {}: {}", args.source, e); + return; + } + }; + let mut buf_reader = std::io::BufReader::new(file); + let mut lexer = Lexer::new(); + let mut full_text = String::new(); + loop { + let mut line = String::new(); + let bytes_read = buf_reader.read_line(&mut line).unwrap(); + if bytes_read == 0 { + break; + } + full_text.push_str(&line); + lexer.parse_next_str(&line); + } + let (tokens, diagnostics) = lexer.finish(); + if !diagnostics.is_empty() { + diagnostics.print(&format!("{}", source_path.display()), &full_text); + } + let mut parser = Parser::new(tokens, diagnostics); + let compile_unit = parser.parse(); + if !parser.diagnostics.is_empty() { + parser.diagnostics.print(&format!("{}", source_path.display()), &full_text); + } + let mut generator = Generator::new(); + let ir = generator.emit(compile_unit); + if !generator.get_diagnostics().is_empty() { + generator.get_diagnostics().print(&format!("{}", source_path.display()), &full_text); + } + if let Some(output_path) = args.output { + match std::fs::write(&output_path, ir.iter().map(|instr| instr.to_string()).collect::>().join("\n")) { + Ok(_) => println!("IR code written to {}", output_path), + Err(e) => eprintln!("Failed to write IR code to {}: {}", output_path, e), + } + } else { + for instr in ir { + println!("{}", instr); + } + } }