diff --git a/README b/README.org similarity index 82% rename from README rename to README.org index 61ffdac..b7d74e6 100644 --- a/README +++ b/README.org @@ -2,6 +2,6 @@ tricc (pronounced "trick-c" or "trixie" like pixie) is a WIP toy compiler I am writing to understand compilers better, that's it. Will probably use LLVM as the backend. -Notes: -- Does not work -- Is WIP \ No newline at end of file +** Notes: ++ Does not work ++ Is WIP diff --git a/src/args.rs b/src/args.rs index 48b9f5a..e1dab96 100644 --- a/src/args.rs +++ b/src/args.rs @@ -4,47 +4,57 @@ use std::process::exit; const VERSION: &str = env!("CARGO_PKG_VERSION"); const CRATE: &str = env!("CARGO_CRATE_NAME"); +// naive argument handling #[derive(Default)] -struct Options { +pub struct Args { version: bool, - file: String, + file: Option, } -// naive argument handling -pub fn handle() -> String { - let args: Vec = env::args().collect(); - - if args.len() < 2 { - println!("Usage: {} [-v] ", CRATE); - exit(0); +impl Args { + pub fn new() -> Args { + Args { + version: false, + file: None, + } } - let mut options: Options = Default::default(); + pub fn handle(&mut self) { + let args: Vec = env::args().collect(); - for arg in &args[1..] { - match arg.as_str() { - "-v" | "--version" => options.version = true, - flag if flag.starts_with('-') => panic!("option {} not implemented!", flag), - file => { - if !options.file.is_empty() { - panic!("please specify only a single source file!"); + if args.len() < 2 { + println!("Usage: {} [-v] ", CRATE); + exit(0); + } + + for arg in &args[1..] { + match arg.as_str() { + "-v" | "--version" => self.version = true, + flag if flag.starts_with('-') => panic!("option {} not implemented!", flag), + file => { + if self.file.is_some() { + panic!("please specify only a single source file!"); + } + self.file = Some(file.to_owned()); } - options.file = file.to_string(); + } + } + + if self.version { + println!("{} version: {}", CRATE, VERSION); + } + + if self.file.is_none() { + if self.version { + exit(0); + } else { + panic!("no file supplied!"); } } } - if options.version { - println!("{} version: {}", CRATE, VERSION); + #[inline] + pub fn get_file(self) -> String { + self.file.expect("no file supplied!") } - - if options.file.is_empty() { - if options.version { - exit(0); - } else { - panic!("no file supplied!"); - } - } - - options.file } diff --git a/src/main.rs b/src/main.rs index e64bc57..6326e56 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,28 @@ -use tricc::args; +use std::fs; +use std::panic; + +use tricc::args::Args; fn main() { - let file: String = args::handle(); - println!("{}", file); + panic::set_hook(Box::new(|panic_info| { + if let Some(msg) = panic_info.payload().downcast_ref::<&str>() { + eprintln!("{}", msg); + } else if let Some(msg) = panic_info.payload().downcast_ref::() { + eprintln!("{}", msg); + } else if let Some(location) = panic_info.location() { + eprintln!( + "panic occurred in file '{}' at line {}", + location.file(), + location.line(), + ); + } else { + eprintln!("panic occurred"); + } + })); + + let mut args = Args::new(); + args.handle(); + + let file = args.get_file(); + let contents = fs::read_to_string(&file).expect("Couldn't read the file"); }