Skip to content

Commit

Permalink
refactor: clap version to 4 from 2
Browse files Browse the repository at this point in the history
  • Loading branch information
kumavale committed Feb 17, 2024
1 parent 4938d16 commit 3f74cb2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ description = "A MIPS-32 simulator"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "4.5.1", features = ["derive"] }
rand = "0.8.2"
clap = "2.33.3"
7 changes: 4 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
use std::io::{BufRead, BufReader};
use std::fs::File;
use std::path::PathBuf;

use super::token::Tokens;
use super::token::memory::Memory;
use super::lexer::tokenize;
use super::parser::parse;

pub fn run(files: &[&str]) {
pub fn run(files: &[PathBuf]) {
let mut tokens: Tokens = Tokens::new();
let mut memory = Memory::default();

// Join files => Everyone global
for (filename_idx, file) in files.iter().enumerate() {
let mut number_of_lines: u32 = 1;
let mut reader = BufReader::new(File::open(file).expect("Failed file open"));
tokens.add_file(file);
tokens.add_file(file.to_str().unwrap());

let mut buf = String::new();
while 0 < reader.read_line(&mut buf).unwrap() {
if let Err(e) = tokenize(number_of_lines, filename_idx, &buf, &mut tokens) {
eprintln!("{}:{}: {}", file, number_of_lines, e);
eprintln!("{}:{}: {}", file.display(), number_of_lines, e);
std::process::exit(1);
}
number_of_lines += 1;
Expand Down
25 changes: 13 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
#[macro_use]
extern crate clap;

mod cli;
mod lexer;
mod parser;
mod repl;
mod token;

use clap::{Arg, App};
use std::path::PathBuf;
use clap::Parser;

#[derive(Parser)]
#[command(version, about)]
struct Cli {
/// Optional name to operate on
#[arg(short, long, value_name = "FILE")]
files: Vec<PathBuf>,
}

fn main() {
let matches = App::new("mipsi")
.version(crate_version!())
.about(crate_description!())
.arg(Arg::with_name("file").multiple(true))
.get_matches();
let cli = Cli::parse();

// REPL
if matches.values_of("file").is_none() {
if cli.files.is_empty() {
repl::run();
return;
}

// CLI
let files: Vec<&str> = matches.values_of("file").unwrap().collect();
cli::run(&files);
cli::run(&cli.files);
}

0 comments on commit 3f74cb2

Please sign in to comment.