add better panic message and restructure src/args.rs
Signed-off-by: Amneesh Singh <natto@weirdnatto.in>
This commit is contained in:
@@ -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
|
writing to understand compilers better, that's it. Will probably use LLVM as
|
||||||
the backend.
|
the backend.
|
||||||
|
|
||||||
Notes:
|
** Notes:
|
||||||
- Does not work
|
+ Does not work
|
||||||
- Is WIP
|
+ Is WIP
|
70
src/args.rs
70
src/args.rs
@@ -4,47 +4,57 @@ use std::process::exit;
|
|||||||
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
const CRATE: &str = env!("CARGO_CRATE_NAME");
|
const CRATE: &str = env!("CARGO_CRATE_NAME");
|
||||||
|
|
||||||
|
// naive argument handling
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct Options {
|
pub struct Args {
|
||||||
version: bool,
|
version: bool,
|
||||||
file: String,
|
file: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
// naive argument handling
|
impl Args {
|
||||||
pub fn handle() -> String {
|
pub fn new() -> Args {
|
||||||
let args: Vec<String> = env::args().collect();
|
Args {
|
||||||
|
version: false,
|
||||||
if args.len() < 2 {
|
file: None,
|
||||||
println!("Usage: {} [-v] <file>", CRATE);
|
}
|
||||||
exit(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut options: Options = Default::default();
|
pub fn handle(&mut self) {
|
||||||
|
let args: Vec<String> = env::args().collect();
|
||||||
|
|
||||||
for arg in &args[1..] {
|
if args.len() < 2 {
|
||||||
match arg.as_str() {
|
println!("Usage: {} [-v] <file>", CRATE);
|
||||||
"-v" | "--version" => options.version = true,
|
exit(0);
|
||||||
flag if flag.starts_with('-') => panic!("option {} not implemented!", flag),
|
}
|
||||||
file => {
|
|
||||||
if !options.file.is_empty() {
|
for arg in &args[1..] {
|
||||||
panic!("please specify only a single source file!");
|
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 {
|
#[inline]
|
||||||
println!("{} version: {}", CRATE, VERSION);
|
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
|
|
||||||
}
|
}
|
||||||
|
28
src/main.rs
28
src/main.rs
@@ -1,6 +1,28 @@
|
|||||||
use tricc::args;
|
use std::fs;
|
||||||
|
use std::panic;
|
||||||
|
|
||||||
|
use tricc::args::Args;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let file: String = args::handle();
|
panic::set_hook(Box::new(|panic_info| {
|
||||||
println!("{}", file);
|
if let Some(msg) = panic_info.payload().downcast_ref::<&str>() {
|
||||||
|
eprintln!("{}", msg);
|
||||||
|
} else if let Some(msg) = panic_info.payload().downcast_ref::<String>() {
|
||||||
|
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");
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user