mirror of
https://github.com/hazemKrimi/hack-assembler.git
synced 2026-05-01 18:20:28 +00:00
Initial commit
This commit is contained in:
@@ -0,0 +1 @@
|
||||
/target
|
||||
@@ -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
|
||||
Generated
+54
@@ -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"
|
||||
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "hack-assembler"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
regex = "1.10.3"
|
||||
+61
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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(),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
pub enum Instruction {
|
||||
AInstruction {
|
||||
decimal: String
|
||||
},
|
||||
CInstruction {
|
||||
dest: String,
|
||||
comp: String,
|
||||
jump: String
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user