From 3e6a33899a0efddaf277877b2dcf8fa70c5c5210 Mon Sep 17 00:00:00 2001 From: Ross Andrews Date: Sat, 7 Jan 2023 00:53:55 -0600 Subject: [PATCH] Basic vasm cli wrapper --- Cargo.lock | 129 +++++++++++++++++++++++++++++ vasm/Cargo.toml | 2 + vasm/src/main.rs | 64 +++++++++++++- vasm_core/src/vasm_preprocessor.rs | 14 ++-- vweb/4th/4th.rom | Bin 3593 -> 3501 bytes 5 files changed, 201 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9feec9d..15b528c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,6 +13,15 @@ dependencies = [ "version_check", ] +[[package]] +name = "aho-corasick" +version = "0.7.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac" +dependencies = [ + "memchr", +] + [[package]] name = "arrayvec" version = "0.7.2" @@ -28,6 +37,17 @@ dependencies = [ "libloading", ] +[[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 = "autocfg" version = "1.1.0" @@ -149,6 +169,45 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e" +[[package]] +name = "clap" +version = "3.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e538f9ee5aa3b3963f09a997035f883677966ed50fce0292611927ce6f6d8c6" +dependencies = [ + "atty", + "bitflags", + "clap_derive", + "clap_lex", + "indexmap", + "lazy_static", + "strsim", + "termcolor", + "textwrap", +] + +[[package]] +name = "clap_derive" +version = "3.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7f98063cac4652f23ccda556b8d04347a7fc4b2cff1f7577cc8c6546e0d8078" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" +dependencies = [ + "os_str_bytes", +] + [[package]] name = "cocoa" version = "0.24.0" @@ -478,6 +537,21 @@ dependencies = [ "ahash", ] +[[package]] +name = "heck" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + [[package]] name = "hexf-parse" version = "0.2.1" @@ -876,6 +950,12 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c" +[[package]] +name = "os_str_bytes" +version = "6.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee" + [[package]] name = "parking_lot" version = "0.11.2" @@ -992,6 +1072,30 @@ dependencies = [ "toml", ] +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + [[package]] name = "proc-macro2" version = "1.0.37" @@ -1070,6 +1174,23 @@ dependencies = [ "bitflags", ] +[[package]] +name = "regex" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e076559ef8e241f2ae3479e36f97bd5741c0330689e217ad51ce2c76808b868a" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" + [[package]] name = "renderdoc-sys" version = "0.7.1" @@ -1191,6 +1312,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "textwrap" +version = "0.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7b3e525a49ec206798b40326a44121291b530c963cfb01018f63e135bac543d" + [[package]] name = "thiserror" version = "1.0.31" @@ -1257,6 +1384,8 @@ checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3" name = "vasm" version = "0.1.0" dependencies = [ + "clap", + "regex", "vasm_core", ] diff --git a/vasm/Cargo.toml b/vasm/Cargo.toml index f8328d0..0a2d5a6 100644 --- a/vasm/Cargo.toml +++ b/vasm/Cargo.toml @@ -7,3 +7,5 @@ edition = "2021" [dependencies] vasm_core = { path = "../vasm_core" } +clap = { version = "3.1.18", features = ["derive"] } +regex = "1.7.0" \ No newline at end of file diff --git a/vasm/src/main.rs b/vasm/src/main.rs index e7a11a9..8196935 100644 --- a/vasm/src/main.rs +++ b/vasm/src/main.rs @@ -1,3 +1,63 @@ -fn main() { - println!("Hello, world!"); +use std::fs::File; +use std::io::Write; +use clap::lazy_static::lazy_static; +use clap::Parser; +use regex::Regex; + +/// Vulcan Assembler +#[derive(Parser, Debug)] +#[clap(author, version, about, long_about = None)] +struct Args { + /// File to output to + #[clap(short, long)] + output: Option, + + /// Input file + #[clap()] + file: String, +} + +impl Args { + fn warning(&self) -> Option { + if !self.file.ends_with(".asm") { + return Some(format!("Warning: input file {} does not end in \".asm\"", self.file)) + } + if let Some(output) = self.output.as_ref() { + // Warn if it doesn't match /.rom$/ + if !output.ends_with(".rom") { + return Some(format!("Warning: output file {} does not end in \".rom\"", output)) + } + } + None + } + + fn deduce(&mut self) { + if self.output.is_none() { + let re = Regex::new(r"^(.*)\.asm$").unwrap(); + if let Some(cap) = re.captures(self.file.as_ref()) { + self.output = Some(format!("{}.rom", cap.get(1).unwrap().as_str())); + } else { + panic!("No output file specified, couldn't deduce one from input filename") + } + } + } +} + +fn main() { + let mut args = Args::parse(); + if let Some(warning) = args.warning() { + println!("{}", warning); + } + args.deduce(); + + match vasm_core::assemble_file(args.file.as_str()) { + Ok(bytes) => { + //println!("We got some bytes... {} of them", bytes.len()); + let mut outfile = File::create(args.output.unwrap()).expect("Failed to open output file"); + outfile.write(bytes.as_slice()).expect("Failed to write to output file"); + } + Err(error) => { + println!("{}", error) + } + } } diff --git a/vasm_core/src/vasm_preprocessor.rs b/vasm_core/src/vasm_preprocessor.rs index 2df5544..951c252 100644 --- a/vasm_core/src/vasm_preprocessor.rs +++ b/vasm_core/src/vasm_preprocessor.rs @@ -147,12 +147,14 @@ impl, F: Fn(String) -> Result> fn handle_macro(&mut self, mac: Macro) -> Option { match mac { Macro::Include(file) => { - let inc_result = (self.include)(file.clone()); - if let Ok(it) = inc_result { - self.filename_stack.push(file); - self.iter_stack.push(it.into_iter().enumerate()); - } else { - return inc_result.err(); + match (self.include)(file.clone()) { + Ok(it) => { + self.filename_stack.push(file); + self.iter_stack.push(it.into_iter().enumerate()); + } + Err(err) => { + return Some(err) + } } } diff --git a/vweb/4th/4th.rom b/vweb/4th/4th.rom index b2fd282f72dc9398d7b09b068ecf306bf2cfc946..9797f95fbfffe93e10095194914392a6f5b5167c 100644 GIT binary patch delta 7 OcmeB_Su4F^EiV8IvI5uu delta 11 ScmZ20-6^wSE$`&1JTU+p*92$)