From 2117f916c940d40afdc79d95f0ee9b302eb88317 Mon Sep 17 00:00:00 2001 From: Hazem Krimi Date: Fri, 8 Mar 2024 14:40:23 +0100 Subject: [PATCH] Initial commit --- .gitignore | 1 + Add.asm | 13 +++++++++++ Cargo.lock | 54 +++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 7 ++++++ src/main.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/parser.rs | 14 ++++++++++++ src/types.rs | 10 +++++++++ 7 files changed, 160 insertions(+) create mode 100644 .gitignore create mode 100755 Add.asm create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/main.rs create mode 100644 src/parser.rs create mode 100644 src/types.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Add.asm b/Add.asm new file mode 100755 index 0000000..56414fe --- /dev/null +++ b/Add.asm @@ -0,0 +1,13 @@ +// This file is part of www.nand2tetris.org +// and the book "The Elements of Computing Systems" +// by Nisan and Schocken, MIT Press. +// File name: projects/06/add/Add.asm + +// Computes R0 = 2 + 3 (R0 refers to RAM[0]) + +@2 +D=A // hi +@3 +D=D+A +@0 +M=D diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..193c363 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,54 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" +dependencies = [ + "memchr", +] + +[[package]] +name = "hack-assembler" +version = "0.1.0" +dependencies = [ + "regex", +] + +[[package]] +name = "memchr" +version = "2.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" + +[[package]] +name = "regex" +version = "1.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b62dbe01f0b06f9d8dc7d49e05a0785f153b00b2c227856282f671e0318c9b15" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86b83b8b9847f9bf95ef68afb0b8e6cdb80f498442f5179a29fad448fcc1eaea" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..9b110f5 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "hack-assembler" +version = "0.1.0" +edition = "2021" + +[dependencies] +regex = "1.10.3" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..432c3b5 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,61 @@ +use std::env; +use std::ffi; +use std::fs; +use std::io::Write; +use std::path; +use std::process; + +use regex::Regex; +use types::Instruction; + +mod parser; +mod types; + +fn process(instruction: String) -> String { + // let parsed = parser::parse(instruction); + + // match parsed { + // Instruction::AInstruction { .. } => { + // let a_instruction: Instruction::AInstruction = parsed; + + // println!("{}", a_instruction.decimal) + // }, + // _ => () + // } + + instruction +} + +fn main() { + let arg = env::args() + .nth(1) + .expect("You must provide the filepath of the program to assemble!"); + let path = path::PathBuf::from(arg); + + match path + .extension() + .unwrap_or_else(|| ffi::OsStr::new("")) + .to_str() + .unwrap_or("") + { + "asm" => { + let filename = path.file_stem().unwrap().to_str().unwrap(); + let mut file = fs::File::create(format!("{}.hack", filename)).unwrap(); + let content = fs::read_to_string(&path).expect("You must provide a correct filepath!"); + let re = Regex::new(r"\s*\/\/.*").unwrap(); + let without_whitespace: String = content + .lines() + .filter(|line| !line.starts_with("//") && !line.is_empty()) + .map(|line| re.replace_all(line, "")) + .map(|line| process(line.to_string())) + .map(|line| format!("{}\n", line)) + .collect(); + + file.write_all(without_whitespace.as_bytes()).unwrap(); + } + _ => { + println!("The file extension must be asm!"); + process::exit(1) + } + } +} diff --git a/src/parser.rs b/src/parser.rs new file mode 100644 index 0000000..ff856e7 --- /dev/null +++ b/src/parser.rs @@ -0,0 +1,14 @@ +use crate::types::Instruction; + +pub fn parse(mut instruction: String) -> Instruction { + if instruction.chars().nth(0).unwrap() == '@' { + instruction.remove(0); + return Instruction::AInstruction { decimal: instruction }; + } + + Instruction::CInstruction { + dest: "1".to_owned(), + comp: "2".to_owned(), + jump: "3".to_owned(), + } +} diff --git a/src/types.rs b/src/types.rs new file mode 100644 index 0000000..8320589 --- /dev/null +++ b/src/types.rs @@ -0,0 +1,10 @@ +pub enum Instruction { + AInstruction { + decimal: String + }, + CInstruction { + dest: String, + comp: String, + jump: String + } +} \ No newline at end of file