making the CLI interface much nicer with clap

This commit is contained in:
Henrik Böving
2021-03-05 20:51:54 +01:00
parent 11635c8495
commit 5d51f1af5b
4 changed files with 142 additions and 34 deletions

106
Cargo.lock generated
View File

@@ -1,5 +1,31 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
[[package]]
name = "ansi_term"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
dependencies = [
"winapi",
]
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]]
name = "bitflags"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]] [[package]]
name = "block-buffer" name = "block-buffer"
version = "0.7.3" version = "0.7.3"
@@ -45,6 +71,21 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "2.33.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002"
dependencies = [
"ansi_term",
"atty",
"bitflags",
"strsim",
"textwrap",
"unicode-width",
"vec_map",
]
[[package]] [[package]]
name = "digest" name = "digest"
version = "0.8.1" version = "0.8.1"
@@ -69,10 +110,20 @@ dependencies = [
"typenum", "typenum",
] ]
[[package]]
name = "hermit-abi"
version = "0.1.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "hm-asm-cli" name = "hm-asm-cli"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"clap",
"hm-asm-simulator", "hm-asm-simulator",
"pest", "pest",
] ]
@@ -107,6 +158,12 @@ version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.88"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03b07a082330a35e43f63177cc01689da34fbffa0105e1246cf0311472cac73a"
[[package]] [[package]]
name = "log" name = "log"
version = "0.4.14" version = "0.4.14"
@@ -238,6 +295,12 @@ dependencies = [
"opaque-debug", "opaque-debug",
] ]
[[package]]
name = "strsim"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.61" version = "1.0.61"
@@ -249,6 +312,15 @@ dependencies = [
"unicode-xid", "unicode-xid",
] ]
[[package]]
name = "textwrap"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
dependencies = [
"unicode-width",
]
[[package]] [[package]]
name = "typenum" name = "typenum"
version = "1.12.0" version = "1.12.0"
@@ -261,12 +333,24 @@ version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56dee185309b50d1f11bfedef0fe6d036842e3fb77413abef29f8f8d1c5d4c1c" checksum = "56dee185309b50d1f11bfedef0fe6d036842e3fb77413abef29f8f8d1c5d4c1c"
[[package]]
name = "unicode-width"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
[[package]] [[package]]
name = "unicode-xid" name = "unicode-xid"
version = "0.2.1" version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
[[package]]
name = "vec_map"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]] [[package]]
name = "wasm-bindgen" name = "wasm-bindgen"
version = "0.2.71" version = "0.2.71"
@@ -322,3 +406,25 @@ name = "wasm-bindgen-shared"
version = "0.2.71" version = "0.2.71"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d6f8ec44822dd71f5f221a5847fb34acd9060535c1211b70a05844c0f6383b1" checksum = "7d6f8ec44822dd71f5f221a5847fb34acd9060535c1211b70a05844c0f6383b1"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

View File

@@ -14,3 +14,4 @@ keywords = ["asm", "cli", "hm-asm", "simulator", "assembler"]
[dependencies] [dependencies]
hm-asm-simulator = { path = "../hm-asm-simulator", version = "0.1.0" } hm-asm-simulator = { path = "../hm-asm-simulator", version = "0.1.0" }
pest = "2.0" pest = "2.0"
clap = "2.33"

View File

@@ -5,7 +5,7 @@ A CLI frontend for `hm-asm-simulate`, it provides two commands:
## Generate ## Generate
You can generate the data and program memory for a program like this 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: Data Memory:
0 1 1 0 0 1 1 0
0 0 0 0 0 0 0 0
@@ -22,7 +22,7 @@ And that's your program!
## Simulate ## Simulate
Alternatively you can simulate an asm program for n clock cycles like this: 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 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. mahara as an in browser lab book -> we just autogenerate the tables.

View File

@@ -1,4 +1,5 @@
use std::env; extern crate clap;
use std::fs; use std::fs;
use hm_asm_simulator::{ use hm_asm_simulator::{
@@ -8,43 +9,43 @@ use hm_asm_simulator::{
}; };
use pest::Parser; use pest::Parser;
use clap::{Arg, App, SubCommand};
mod html; mod html;
use html::html_state_table; use html::html_state_table;
fn main() { fn main() {
let sub_cmd = env::args().nth(1); let matches = App::new("hm-asm-cli")
let file_name = env::args().nth(2); .version("0.1.0")
let steps = env::args().nth(3); .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 { if let Some(matches) = matches.subcommand_matches("compile") {
let file_content = match file_name { let file_content = fs::read_to_string(matches.value_of("COMP_FILE").unwrap()).expect("Could not read the provided asm file");
Some(file_name) => { let instructions = parse_asm(AsmParser::parse(Rule::program, &file_content).unwrap_or_else(|e| panic!("{}", e)));
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); let binary = generate_binary(instructions);
println!("{}", binary); println!("{}", binary);
} } else if let Some(matches) = matches.subcommand_matches("simulate") {
else if sub_cmd == "simulate" { let file_content = fs::read_to_string(matches.value_of("SIM_FILE").unwrap()).expect("Could not read the provided asm file");
if let Some(steps) = steps { let instructions = parse_asm(AsmParser::parse(Rule::program, &file_content).unwrap_or_else(|e| panic!("{}", e)));
let states = simulate(instructions, steps.parse::<usize>().unwrap()); let states = simulate(instructions, matches.value_of("cycles").unwrap().parse::<usize>().unwrap());
//println!("{:#?}", states); //println!("{:#?}", states);
println!("{}", html_state_table(states)); println!("{}", html_state_table(states));
} }
} }
} else {
println!("No argument was passed, exiting");
}
}