Skip to content

Commit

Permalink
Merge pull request #2 from kumavale/refactor/clap
Browse files Browse the repository at this point in the history
Upgrade clap
  • Loading branch information
kumavale authored Feb 17, 2024
2 parents 4938d16 + 7ec9c06 commit 5a92f4b
Show file tree
Hide file tree
Showing 3 changed files with 17 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
24 changes: 12 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
#[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 {
#[arg(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 5a92f4b

Please sign in to comment.