Translate A instructions without symbols

This commit is contained in:
Hazem Krimi
2024-03-08 20:11:44 +01:00
parent 2117f916c9
commit 90b2a5f5e7
4 changed files with 80 additions and 31 deletions
+9
View File
@@ -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
// }
+24 -12
View File
@@ -8,23 +8,35 @@ use std::process;
use regex::Regex; use regex::Regex;
use types::Instruction; use types::Instruction;
mod parser;
mod types; mod types;
mod parser;
mod code;
fn process(instruction: String) -> String { 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 { String::from(format!("0{}", translated))
// Instruction::AInstruction { .. } => { }
// let a_instruction: Instruction::AInstruction = parsed; Instruction::CInstruction(parsed_instruction) => {
println!(
// println!("{}", a_instruction.decimal) "C: {}, {}, {}",
// }, parsed_instruction.dest,
// _ => () parsed_instruction.comp,
// } parsed_instruction.jump.unwrap_or_else(|| "".to_string())
);
instruction instruction
} }
},
None => {
println!("Unexpected error!");
process::exit(1)
}
}
}
fn main() { fn main() {
let arg = env::args() let arg = env::args()
@@ -43,7 +55,7 @@ fn main() {
let mut file = fs::File::create(format!("{}.hack", filename)).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 content = fs::read_to_string(&path).expect("You must provide a correct filepath!");
let re = Regex::new(r"\s*\/\/.*").unwrap(); let re = Regex::new(r"\s*\/\/.*").unwrap();
let without_whitespace: String = content let processed: String = content
.lines() .lines()
.filter(|line| !line.starts_with("//") && !line.is_empty()) .filter(|line| !line.starts_with("//") && !line.is_empty())
.map(|line| re.replace_all(line, "")) .map(|line| re.replace_all(line, ""))
@@ -51,7 +63,7 @@ fn main() {
.map(|line| format!("{}\n", line)) .map(|line| format!("{}\n", line))
.collect(); .collect();
file.write_all(without_whitespace.as_bytes()).unwrap(); file.write_all(processed.as_bytes()).unwrap();
} }
_ => { _ => {
println!("The file extension must be asm!"); println!("The file extension must be asm!");
+33 -9
View File
@@ -1,14 +1,38 @@
use crate::types::Instruction; use crate::types::{AInstruction, CInstruction, Instruction};
pub fn parse(mut instruction: String) -> Instruction { pub fn parse(instruction: &String) -> Option<Instruction> {
if instruction.chars().nth(0).unwrap() == '@' { let mut cloned = instruction.clone();
instruction.remove(0);
return Instruction::AInstruction { decimal: instruction }; if cloned.chars().nth(0).unwrap() == '@' {
cloned.remove(0);
return Some(Instruction::AInstruction(AInstruction {
decimal: cloned.to_string(),
}));
} }
Instruction::CInstruction { let slice: Vec<&str> = cloned.split("=").collect();
dest: "1".to_owned(),
comp: "2".to_owned(), match slice.as_slice() {
jump: "3".to_owned(), [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,
} }
} }
+12 -8
View File
@@ -1,10 +1,14 @@
pub struct AInstruction {
pub decimal: String,
}
pub struct CInstruction {
pub dest: String,
pub comp: String,
pub jump: Option<String>,
}
pub enum Instruction { pub enum Instruction {
AInstruction { AInstruction(AInstruction),
decimal: String CInstruction(CInstruction),
},
CInstruction {
dest: String,
comp: String,
jump: String
}
} }