Dehardcode main function, impemented basic cli

This commit is contained in:
Gvidas Juknevičius 2024-12-21 23:08:29 +02:00
parent 857471c6a9
commit bf7f44e776
Signed by: MCorange
GPG Key ID: 12B1346D720B7FBB
2 changed files with 70 additions and 6 deletions

View File

@ -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();
}
}
}

View File

@ -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")?;
let prog = mclangc::parser::parse_program(tokens)?;
mclangc::validator::validate_code(&prog);
Ok(())
fn main() -> ExitCode {
let cli = mclangc::cli::CliArgs::parse();
cli.set_log_level();
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
}