From e7e8690463bd4c2557b41c15a0bd38452a2259ef Mon Sep 17 00:00:00 2001 From: Alek Ratzloff Date: Thu, 7 May 2020 18:31:08 -0400 Subject: [PATCH] Add structopt and base argument parsing Signed-off-by: Alek Ratzloff --- Cargo.lock | 188 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 68 ++++++++++++++++++- 3 files changed, 254 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ec9f5b6..9971cec 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9,6 +9,47 @@ dependencies = [ "memchr", ] +[[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]] +name = "clap" +version = "2.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9" +dependencies = [ + "ansi_term", + "atty", + "bitflags", + "strsim", + "textwrap", + "unicode-width", + "vec_map", +] + [[package]] name = "derivative" version = "2.1.1" @@ -26,12 +67,36 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" +[[package]] +name = "heck" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205" +dependencies = [ + "unicode-segmentation", +] + +[[package]] +name = "hermit-abi" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61565ff7aaace3525556587bd2dc31d4a07071957be715e63ce7b1eccf51a8f4" +dependencies = [ + "libc", +] + [[package]] name = "lazy_static" version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +[[package]] +name = "libc" +version = "0.2.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99e85c08494b21a9054e7fe1374a732aeadaff3980b6990b94bfd3a70f690005" + [[package]] name = "memchr" version = "2.3.3" @@ -46,6 +111,33 @@ dependencies = [ "lazy_static", "regex", "snafu", + "structopt", +] + +[[package]] +name = "proc-macro-error" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98e9e4b82e0ef281812565ea4751049f1bdcdfccda7d3f459f2e138a40c08678" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f5444ead4e9935abd7f27dc51f7e852a0569ac888096d5ec2499470794e2e53" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "syn-mid", + "version_check", ] [[package]] @@ -105,6 +197,36 @@ dependencies = [ "syn", ] +[[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" + +[[package]] +name = "structopt" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "863246aaf5ddd0d6928dfeb1a9ca65f505599e4e1b399935ef7e75107516b4ef" +dependencies = [ + "clap", + "lazy_static", + "structopt-derive", +] + +[[package]] +name = "structopt-derive" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d239ca4b13aee7a2142e6795cbd69e457665ff8037aed33b3effdc430d2f927a" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "syn" version = "1.0.18" @@ -116,6 +238,26 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "syn-mid" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7be3539f6c128a931cf19dcee741c1af532c7fd387baa739c03dd2e96479338a" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +dependencies = [ + "unicode-width", +] + [[package]] name = "thread_local" version = "1.0.1" @@ -125,8 +267,54 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "unicode-segmentation" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e83e153d1053cbb5a118eeff7fd5be06ed99153f00dbcd8ae310c5fb2b22edc0" + +[[package]] +name = "unicode-width" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479" + [[package]] name = "unicode-xid" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" + +[[package]] +name = "vec_map" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" + +[[package]] +name = "version_check" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "078775d0255232fb988e6fccf26ddc9d1ac274299aaedcedce21c6f72cc533ce" + +[[package]] +name = "winapi" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6" +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" diff --git a/Cargo.toml b/Cargo.toml index 98fa9ee..7bf6b9b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,4 @@ snafu = "0.6.6" lazy_static = "1.4.0" regex = "1.3.7" derivative = "2.1.1" +structopt = "0.3" diff --git a/src/main.rs b/src/main.rs index 25e8af3..eb5de2e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,68 @@ mod syn; -//mod util; -fn main() { - println!("Hello, world!"); +use std::{ + convert::TryFrom, + fs, + io::{stdout, Write}, + path::{Path, PathBuf}, + process, +}; +use structopt::StructOpt; + +type Result = std::result::Result>; + +#[derive(StructOpt, Debug)] +struct Options { + /// The input file to work with. + /// + /// By default, the file will be executed. If -c is passed, it will not run and only compile. + /// Supplying - for the path will read from STDIN. + #[structopt(name = "FILE", parse(from_os_str))] + input: PathBuf, + + /* + /// Disassemble object that would be passed to VM and exit before running it. + #[structopt(short = "d", long)] + disassemble: bool, + */ +} + +fn main() -> Result<()> { + use syn::{ + parser::Parser, + }; + + let opt = Options::from_args(); + let text = fs::read_to_string(&opt.input)?; + let parser = Parser::try_from(text.as_str())?; + /* + let object = assemble::assemble_path(&opt.input)?; + + if opt.compile_only { + let outfile = opt.out.clone().unwrap_or_else(|| { + let mut outfile = opt.input.clone(); + assert!(outfile.set_extension("obj")); + outfile + }); + let bytes = object.to_bytes(); + let mut writer = get_writer(&outfile)?; + writer.write(&bytes)?; + Ok(()) + } else if opt.disassemble { + let outfile = opt + .out + .as_ref() + .map(|p| p.as_path()) + .unwrap_or_else(|| Path::new("-")); + let mut writer = get_writer(&outfile)?; + object.disasm(&mut writer)?; + Ok(()) + } else { + let mut state = State::new(); + state.load_object(object, opt.max_mem.unwrap_or(DEFAULT_MAX_MEM))?; + let status = state.exec()?; + process::exit((status & 0xffff_ffff) as i32); + } + */ + Ok(()) }