making the CLI interface much nicer with clap
This commit is contained in:
@@ -14,3 +14,4 @@ keywords = ["asm", "cli", "hm-asm", "simulator", "assembler"]
|
||||
[dependencies]
|
||||
hm-asm-simulator = { path = "../hm-asm-simulator", version = "0.1.0" }
|
||||
pest = "2.0"
|
||||
clap = "2.33"
|
||||
|
||||
@@ -5,7 +5,7 @@ A CLI frontend for `hm-asm-simulate`, it provides two commands:
|
||||
## Generate
|
||||
You can generate the data and program memory for a program like this
|
||||
```
|
||||
$ cargo run -- generate examples/add_endless.asm
|
||||
$ cargo run -- compile ../examples/add_endless.asm
|
||||
Data Memory:
|
||||
0 1 1 0
|
||||
0 0 0 0
|
||||
@@ -22,7 +22,7 @@ And that's your program!
|
||||
## Simulate
|
||||
Alternatively you can simulate an asm program for n clock cycles like this:
|
||||
```
|
||||
$ cargo run -- simulate examples/add_endless.asm 4
|
||||
$ cargo run -- simulate ../examples/add_endless.asm 4
|
||||
```
|
||||
It is going to proceed and print an HTML table of all states since the only purpose of this tool is to avoid using
|
||||
mahara as an in browser lab book -> we just autogenerate the tables.
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use std::env;
|
||||
extern crate clap;
|
||||
|
||||
use std::fs;
|
||||
|
||||
use hm_asm_simulator::{
|
||||
@@ -8,43 +9,43 @@ use hm_asm_simulator::{
|
||||
};
|
||||
|
||||
use pest::Parser;
|
||||
use clap::{Arg, App, SubCommand};
|
||||
|
||||
mod html;
|
||||
use html::html_state_table;
|
||||
|
||||
|
||||
fn main() {
|
||||
let sub_cmd = env::args().nth(1);
|
||||
let file_name = env::args().nth(2);
|
||||
let steps = env::args().nth(3);
|
||||
let matches = App::new("hm-asm-cli")
|
||||
.version("0.1.0")
|
||||
.author("Henrik Boeving <henrik@boeving-net.de")
|
||||
.about("A CLI frontend for the hm-asm-simulator")
|
||||
.subcommand(SubCommand::with_name("compile")
|
||||
.arg(Arg::with_name("COMP_FILE")
|
||||
.help("Sets the asm file to compile")
|
||||
.value_name("FILE")
|
||||
.takes_value(true)))
|
||||
.subcommand(SubCommand::with_name("simulate")
|
||||
.arg(Arg::with_name("SIM_FILE")
|
||||
.help("Sets the asm file to simulate")
|
||||
.value_name("FILE")
|
||||
.takes_value(true))
|
||||
.arg(Arg::with_name("cycles")
|
||||
.help("How many cycles to run the simulator for")
|
||||
.value_name("cycles")
|
||||
.takes_value(true)))
|
||||
.get_matches();
|
||||
|
||||
if let Some(sub_cmd) = sub_cmd {
|
||||
let file_content = match file_name {
|
||||
Some(file_name) => {
|
||||
fs::read_to_string(file_name).expect("Could not read the provided asm file")
|
||||
}
|
||||
None => {
|
||||
println!("No input file was provided");
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let instructions = parse_asm(
|
||||
AsmParser::parse(Rule::program, &file_content).unwrap_or_else(|e| panic!("{}", e)),
|
||||
);
|
||||
|
||||
if sub_cmd == "generate" {
|
||||
let binary = generate_binary(instructions);
|
||||
println!("{}", binary);
|
||||
}
|
||||
else if sub_cmd == "simulate" {
|
||||
if let Some(steps) = steps {
|
||||
let states = simulate(instructions, steps.parse::<usize>().unwrap());
|
||||
//println!("{:#?}", states);
|
||||
println!("{}", html_state_table(states));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
println!("No argument was passed, exiting");
|
||||
if let Some(matches) = matches.subcommand_matches("compile") {
|
||||
let file_content = fs::read_to_string(matches.value_of("COMP_FILE").unwrap()).expect("Could not read the provided asm file");
|
||||
let instructions = parse_asm(AsmParser::parse(Rule::program, &file_content).unwrap_or_else(|e| panic!("{}", e)));
|
||||
let binary = generate_binary(instructions);
|
||||
println!("{}", binary);
|
||||
} else if let Some(matches) = matches.subcommand_matches("simulate") {
|
||||
let file_content = fs::read_to_string(matches.value_of("SIM_FILE").unwrap()).expect("Could not read the provided asm file");
|
||||
let instructions = parse_asm(AsmParser::parse(Rule::program, &file_content).unwrap_or_else(|e| panic!("{}", e)));
|
||||
let states = simulate(instructions, matches.value_of("cycles").unwrap().parse::<usize>().unwrap());
|
||||
//println!("{:#?}", states);
|
||||
println!("{}", html_state_table(states));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user