-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d0b4af7
commit cdfb7e2
Showing
5 changed files
with
82 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use std::{env::Args, process::ExitCode}; | ||
|
||
pub const COMMANDS: &[Command] = &[ | ||
Command { | ||
name: "help", | ||
descripition: "Print this message", | ||
run: |program, _| { | ||
usage(program); | ||
ExitCode::SUCCESS | ||
}, | ||
}, | ||
Command { | ||
name: "compile", | ||
descripition: "Compile a program: chs compile <file.chs>", | ||
run: |program, args| { | ||
if let Some(file_path) = args.next() { | ||
ExitCode::SUCCESS | ||
} else { | ||
eprintln!("Expect file path."); | ||
ExitCode::FAILURE | ||
} | ||
}, | ||
}, | ||
Command { | ||
name: "parse", | ||
descripition: "Parse a program and it's AST: chs parse <file.chs>", | ||
run: |program, args| { | ||
if let Some(file_path) = args.next() { | ||
match chs_ast::parse_file(file_path) { | ||
Ok(ast) => { | ||
println!("{ast}"); | ||
ExitCode::SUCCESS | ||
} | ||
Err(err) => { | ||
eprintln!("{err}"); | ||
ExitCode::FAILURE | ||
} | ||
} | ||
} else { | ||
eprintln!("Expect file path."); | ||
ExitCode::FAILURE | ||
} | ||
}, | ||
}, | ||
]; | ||
|
||
pub struct Command { | ||
pub name: &'static str, | ||
pub descripition: &'static str, | ||
pub run: fn(&str, &mut Args) -> ExitCode, | ||
} | ||
|
||
pub fn usage(program: &str) { | ||
println!("USAGE: {program} <COMMAND> [OPTIONS]"); | ||
println!("COMMANDS:"); | ||
|
||
for ele in COMMANDS.iter() { | ||
println!( | ||
" {name} {descripition}", | ||
name = ele.name, | ||
descripition = ele.descripition, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,24 @@ | ||
#![allow(unused)] | ||
|
||
use std::{env, process::exit}; | ||
use std::{env, process::{exit, ExitCode}}; | ||
|
||
fn main() { | ||
let mut argv = env::args(); | ||
let program = argv.next().expect("Program always provided."); | ||
let cmd = argv.next().unwrap_or_else(|| usage_err(&program, "Expect Command")); | ||
match cmd.as_str() { | ||
"help" => usage(&program), | ||
"com"|"compile" => {} | ||
"parse" => { | ||
let file_path = argv.next().unwrap_or_else(|| msg_err("ERROR: File not provided.")); | ||
let ast = chs_ast::parse_file(file_path).unwrap_or_else(|err| msg_err(err)); | ||
println!("{ast}"); | ||
use cli::{usage, COMMANDS}; | ||
mod cli; | ||
|
||
fn main() -> ExitCode { | ||
let mut args = env::args(); | ||
let program = args.next().expect("Program always provided."); | ||
if let Some(cmd) = args.next() { | ||
if let Some(cmd) = COMMANDS.iter().find(|c| c.name == cmd) { | ||
(cmd.run)(&program, &mut args) | ||
} else { | ||
println!("Invalid command."); | ||
usage(&program); | ||
ExitCode::FAILURE | ||
} | ||
c => usage_err(&program, format!("Invalid Command \"{c}\"")) | ||
} else { | ||
println!("Expect command."); | ||
usage(&program); | ||
ExitCode::FAILURE | ||
} | ||
} | ||
|
||
fn usage(program: &str) { | ||
println!("USAGE: {program} <COMMAND> [OPTIONS]"); | ||
println!("COMMANDS:"); | ||
println!(" help - Show this message."); | ||
println!(" com|compile - Compile a program: chs com <file.chs>"); | ||
println!(" parse - Parse a program and print its AST: chs parse <file.chs>"); | ||
} | ||
|
||
fn usage_err(program: &str, err: impl ToString) -> ! { | ||
eprintln!("ERROR: {}", err.to_string()); | ||
usage(&program); | ||
exit(1); | ||
} | ||
|
||
fn msg_err(err: impl ToString) -> ! { | ||
eprintln!("{}", err.to_string()); | ||
exit(1); | ||
} |