Dehardcode main function, impemented basic cli
This commit is contained in:
parent
857471c6a9
commit
bf7f44e776
36
src/cli.rs
36
src/cli.rs
|
@ -1,3 +1,39 @@
|
||||||
|
use clap::{error::ErrorKind, CommandFactory};
|
||||||
|
|
||||||
|
use crate::logger::Level;
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Debug, clap::Parser)]
|
||||||
|
pub struct CliArgs {
|
||||||
|
/// Output more info, will get overwritten if -q|--quiet is specified
|
||||||
|
#[arg(long, short)]
|
||||||
|
verbose: bool,
|
||||||
|
/// Output nothing, except errors, will overwrite -v|--verbose
|
||||||
|
#[arg(long, short)]
|
||||||
|
quiet: bool,
|
||||||
|
/// Output file
|
||||||
|
#[arg(long, short, default_value="a.out")]
|
||||||
|
pub output: String,
|
||||||
|
/// All input files
|
||||||
|
#[clap(num_args = 1..)]
|
||||||
|
pub input: Vec<String>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CliArgs {
|
||||||
|
pub fn set_log_level(&self) {
|
||||||
|
if self.quiet {
|
||||||
|
unsafe {
|
||||||
|
crate::logger::LEVEL = Level::Error;
|
||||||
|
}
|
||||||
|
} else if self.verbose {
|
||||||
|
unsafe {
|
||||||
|
crate::logger::LEVEL = Level::Debug;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn validate(&self) {
|
||||||
|
if self.input.len() < 1 {
|
||||||
|
CliArgs::command().error(ErrorKind::TooFewValues, "at least one value is required for '<INPUT>' but none was supplied").exit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
40
src/main.rs
40
src/main.rs
|
@ -1,11 +1,39 @@
|
||||||
|
use std::{path::PathBuf, process::ExitCode};
|
||||||
|
|
||||||
|
use clap::Parser;
|
||||||
|
// Importing logger here too cause the logger macros dont work outside the mclanc lib
|
||||||
|
mod logger;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
fn main() -> anyhow::Result<()> {
|
|
||||||
let data = std::fs::read_to_string("test.mcl").unwrap();
|
|
||||||
|
|
||||||
let tokens = mclangc::tokeniser::tokenise(&data, "test.mcl")?;
|
fn main() -> ExitCode {
|
||||||
let prog = mclangc::parser::parse_program(tokens)?;
|
let cli = mclangc::cli::CliArgs::parse();
|
||||||
mclangc::validator::validate_code(&prog);
|
cli.set_log_level();
|
||||||
Ok(())
|
cli.validate();
|
||||||
|
for file in &cli.input {
|
||||||
|
let fp = PathBuf::from(file);
|
||||||
|
if !fp.exists() {
|
||||||
|
error!("File {fp:?} doesnt exits, exiting");
|
||||||
|
return ExitCode::FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
let data = std::fs::read_to_string(fp).unwrap();
|
||||||
|
info!("Tokenising {file}");
|
||||||
|
let Ok(tokens) = mclangc::tokeniser::tokenise(&data, &file) else {
|
||||||
|
error!("Failed to tokenise file, exiting");
|
||||||
|
return ExitCode::FAILURE;
|
||||||
|
};
|
||||||
|
info!("Parsing {file}");
|
||||||
|
let Ok(prog) = mclangc::parser::parse_program(tokens) else {
|
||||||
|
error!("Failed to parse file, exiting");
|
||||||
|
return ExitCode::FAILURE;
|
||||||
|
};
|
||||||
|
info!("Validating {file}");
|
||||||
|
let Ok(validated) = mclangc::validator::validate_code(&prog) else {
|
||||||
|
error!("Failed to validate file, exiting");
|
||||||
|
return ExitCode::FAILURE;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
ExitCode::SUCCESS
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user