Parsing wip

This commit is contained in:
Hazem Krimi
2024-03-13 00:19:15 +01:00
parent ac8a70f33d
commit 615575655a
4 changed files with 61 additions and 44 deletions
+1 -1
View File
@@ -66,7 +66,7 @@ pub fn translate_comp(comp: &String) -> String {
"M-D" => "1000111", "M-D" => "1000111",
"D&M" => "1000000", "D&M" => "1000000",
"D|M" => "1010101", "D|M" => "1010101",
_ => panic!("Unexpected error converting the code!"), _ => "0000000",
} }
.to_string() .to_string()
} }
+11 -11
View File
@@ -14,25 +14,25 @@ mod types;
fn process(instruction: String) -> String { fn process(instruction: String) -> String {
match parser::parse(&instruction) { match parser::parse(&instruction) {
Some(parsed) => match parsed {
Instruction::AInstruction(parsed_instruction) => { Instruction::AInstruction(parsed_instruction) => {
let translated = code::decimal_to_fifteen_bits_binary( match parsed_instruction.decimal.parse::<i32>() {
&parsed_instruction.decimal.parse::<i32>().unwrap(), Ok(decimal) => {
); let translated = code::decimal_to_fifteen_bits_binary(&decimal);
String::from(format!("0{}", translated)) String::from(format!("0{}", translated))
} }
Err(_) => panic!("Failed to parse A instruction {}", instruction),
}
}
Instruction::CInstruction(parsed_instruction) => { Instruction::CInstruction(parsed_instruction) => {
String::from("111") String::from("111")
+ code::translate_comp(&parsed_instruction.comp).as_str() + code::translate_comp(&parsed_instruction.comp.unwrap_or_else(|| "".to_string()))
+ code::translate_dest(&parsed_instruction.dest).as_str() .as_str()
+ code::translate_jump( + code::translate_dest(&parsed_instruction.dest.unwrap_or_else(|| "".to_string()))
&parsed_instruction.jump.unwrap_or_else(|| "".to_string()), .as_str()
) + code::translate_jump(&parsed_instruction.jump.unwrap_or_else(|| "".to_string()))
.as_str() .as_str()
} }
},
None => panic!("Unexpected error!"),
} }
} }
+42 -25
View File
@@ -1,38 +1,55 @@
use crate::types::{AInstruction, CInstruction, Instruction}; use crate::types::{AInstruction, CInstruction, Instruction};
use regex::Regex;
pub fn parse(instruction: &String) -> Option<Instruction> { pub fn parse(instruction: &String) -> Instruction {
let mut cloned = instruction.clone(); let mut cloned = instruction.clone();
if cloned.chars().nth(0).unwrap() == '@' { if cloned.chars().nth(0).unwrap() == '@' {
cloned.remove(0); cloned.remove(0);
return Some(Instruction::AInstruction(AInstruction { return Instruction::AInstruction(AInstruction {
decimal: cloned.to_string(), decimal: cloned.trim().to_string(),
})); });
} } else {
let re_dest_and_comp = Regex::new(r"=").unwrap();
let re_comp_and_jump = Regex::new(r";").unwrap();
let dest_and_comp_exist = re_dest_and_comp.is_match(&cloned);
let comp_and_jump_exist = re_comp_and_jump.is_match(&cloned);
let dest = match dest_and_comp_exist {
true => {
let slice: Vec<&str> = cloned.split("=").collect(); let slice: Vec<&str> = cloned.split("=").collect();
match slice.as_slice() { Some(slice.as_slice()[0].to_string())
[dest, comp_and_jump] => match comp_and_jump.find(";") { }
Some(_) => { false => None,
let second_slice: Vec<&str> = comp_and_jump.split(";").collect(); };
match second_slice.as_slice() { let comp = match comp_and_jump_exist {
[comp, jump] => Some(Instruction::CInstruction(CInstruction { true => {
dest: dest.to_string(), let slice: Vec<&str> = cloned.split("=").collect();
comp: comp.to_string(),
jump: Some(jump.to_string()), // if jump_exist {
})), // let another_slice: Vec<&str> = slice.as_slice()[1].split(";").collect();
_ => None,
} // Some(another_slice.as_slice()[0].to_string());
} // }
None => Some(Instruction::CInstruction(CInstruction {
dest: dest.to_string(), Some(slice.as_slice()[1].to_string())
comp: comp_and_jump.to_string(), }
jump: None, false => None,
})), };
},
_ => None, let jump = match comp_and_jump_exist {
true => {
let slice: Vec<&str> = cloned.split(";").collect();
Some(slice.as_slice()[1].to_string())
}
false => None,
};
Instruction::CInstruction(CInstruction { dest, comp, jump })
} }
} }
+2 -2
View File
@@ -3,8 +3,8 @@ pub struct AInstruction {
} }
pub struct CInstruction { pub struct CInstruction {
pub dest: String, pub dest: Option<String>,
pub comp: String, pub comp: Option<String>,
pub jump: Option<String>, pub jump: Option<String>,
} }