Skip to content

Commit

Permalink
Improve CLI and remove rere.py
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcosAndradeV committed Jan 15, 2025
1 parent d0b4af7 commit cdfb7e2
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 219 deletions.
6 changes: 0 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ build:
release:
cargo build --release --bin chs

test: build
./rere.py replay test.list

record: build
./rere.py record test.list

chsc: release

help:
Expand Down
64 changes: 64 additions & 0 deletions chs/src/cli.rs
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,
);
}
}
50 changes: 18 additions & 32 deletions chs/src/main.rs
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);
}
173 changes: 0 additions & 173 deletions rere.py

This file was deleted.

8 changes: 0 additions & 8 deletions test.list

This file was deleted.

0 comments on commit cdfb7e2

Please sign in to comment.