mirror of
https://github.com/hazemKrimi/hack-assembler.git
synced 2026-05-01 18:20:28 +00:00
Translate A instructions without symbols
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
pub fn decimal_to_binary(decimal: &i32) -> String {
|
||||
String::from(format!("{decimal:015b}"))
|
||||
}
|
||||
|
||||
// fn translate_dest(dest: &String) -> String {
|
||||
// let cloned = dest.clone();
|
||||
|
||||
// cloned
|
||||
// }
|
||||
+25
-13
@@ -8,22 +8,34 @@ use std::process;
|
||||
use regex::Regex;
|
||||
use types::Instruction;
|
||||
|
||||
mod parser;
|
||||
mod types;
|
||||
mod parser;
|
||||
mod code;
|
||||
|
||||
fn process(instruction: String) -> String {
|
||||
// let parsed = parser::parse(instruction);
|
||||
match parser::parse(&instruction) {
|
||||
Some(parsed) => match parsed {
|
||||
Instruction::AInstruction(parsed_instruction) => {
|
||||
let translated = code::decimal_to_binary(&parsed_instruction.decimal.parse::<i32>().unwrap());
|
||||
|
||||
// match parsed {
|
||||
// Instruction::AInstruction { .. } => {
|
||||
// let a_instruction: Instruction::AInstruction = parsed;
|
||||
String::from(format!("0{}", translated))
|
||||
}
|
||||
Instruction::CInstruction(parsed_instruction) => {
|
||||
println!(
|
||||
"C: {}, {}, {}",
|
||||
parsed_instruction.dest,
|
||||
parsed_instruction.comp,
|
||||
parsed_instruction.jump.unwrap_or_else(|| "".to_string())
|
||||
);
|
||||
|
||||
// println!("{}", a_instruction.decimal)
|
||||
// },
|
||||
// _ => ()
|
||||
// }
|
||||
|
||||
instruction
|
||||
instruction
|
||||
}
|
||||
},
|
||||
None => {
|
||||
println!("Unexpected error!");
|
||||
process::exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
@@ -43,7 +55,7 @@ fn main() {
|
||||
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
|
||||
let processed: String = content
|
||||
.lines()
|
||||
.filter(|line| !line.starts_with("//") && !line.is_empty())
|
||||
.map(|line| re.replace_all(line, ""))
|
||||
@@ -51,7 +63,7 @@ fn main() {
|
||||
.map(|line| format!("{}\n", line))
|
||||
.collect();
|
||||
|
||||
file.write_all(without_whitespace.as_bytes()).unwrap();
|
||||
file.write_all(processed.as_bytes()).unwrap();
|
||||
}
|
||||
_ => {
|
||||
println!("The file extension must be asm!");
|
||||
|
||||
+33
-9
@@ -1,14 +1,38 @@
|
||||
use crate::types::Instruction;
|
||||
use crate::types::{AInstruction, CInstruction, Instruction};
|
||||
|
||||
pub fn parse(mut instruction: String) -> Instruction {
|
||||
if instruction.chars().nth(0).unwrap() == '@' {
|
||||
instruction.remove(0);
|
||||
return Instruction::AInstruction { decimal: instruction };
|
||||
pub fn parse(instruction: &String) -> Option<Instruction> {
|
||||
let mut cloned = instruction.clone();
|
||||
|
||||
if cloned.chars().nth(0).unwrap() == '@' {
|
||||
cloned.remove(0);
|
||||
|
||||
return Some(Instruction::AInstruction(AInstruction {
|
||||
decimal: cloned.to_string(),
|
||||
}));
|
||||
}
|
||||
|
||||
Instruction::CInstruction {
|
||||
dest: "1".to_owned(),
|
||||
comp: "2".to_owned(),
|
||||
jump: "3".to_owned(),
|
||||
let slice: Vec<&str> = cloned.split("=").collect();
|
||||
|
||||
match slice.as_slice() {
|
||||
[dest, comp_and_jump] => match comp_and_jump.find(";") {
|
||||
Some(with_jump) => {
|
||||
let second_slice: Vec<&str> = comp_and_jump.split(";").collect();
|
||||
|
||||
match second_slice.as_slice() {
|
||||
[comp, jump] => Some(Instruction::CInstruction(CInstruction {
|
||||
dest: dest.to_string(),
|
||||
comp: comp.to_string(),
|
||||
jump: Some(jump.to_string()),
|
||||
})),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
None => Some(Instruction::CInstruction(CInstruction {
|
||||
dest: dest.to_string(),
|
||||
comp: comp_and_jump.to_string(),
|
||||
jump: None,
|
||||
})),
|
||||
},
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
+13
-9
@@ -1,10 +1,14 @@
|
||||
pub enum Instruction {
|
||||
AInstruction {
|
||||
decimal: String
|
||||
},
|
||||
CInstruction {
|
||||
dest: String,
|
||||
comp: String,
|
||||
jump: String
|
||||
}
|
||||
pub struct AInstruction {
|
||||
pub decimal: String,
|
||||
}
|
||||
|
||||
pub struct CInstruction {
|
||||
pub dest: String,
|
||||
pub comp: String,
|
||||
pub jump: Option<String>,
|
||||
}
|
||||
|
||||
pub enum Instruction {
|
||||
AInstruction(AInstruction),
|
||||
CInstruction(CInstruction),
|
||||
}
|
||||
Reference in New Issue
Block a user