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()
} }
+16 -16
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) => { match parsed_instruction.decimal.parse::<i32>() {
let translated = code::decimal_to_fifteen_bits_binary( Ok(decimal) => {
&parsed_instruction.decimal.parse::<i32>().unwrap(), 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) => { }
String::from("111") Instruction::CInstruction(parsed_instruction) => {
+ code::translate_comp(&parsed_instruction.comp).as_str() String::from("111")
+ code::translate_dest(&parsed_instruction.dest).as_str() + code::translate_comp(&parsed_instruction.comp.unwrap_or_else(|| "".to_string()))
+ code::translate_jump(
&parsed_instruction.jump.unwrap_or_else(|| "".to_string()),
)
.as_str() .as_str()
} + code::translate_dest(&parsed_instruction.dest.unwrap_or_else(|| "".to_string()))
}, .as_str()
None => panic!("Unexpected error!"), + code::translate_jump(&parsed_instruction.jump.unwrap_or_else(|| "".to_string()))
.as_str()
}
} }
} }
+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 slice: Vec<&str> = cloned.split("=").collect(); let dest_and_comp_exist = re_dest_and_comp.is_match(&cloned);
let comp_and_jump_exist = re_comp_and_jump.is_match(&cloned);
match slice.as_slice() { let dest = match dest_and_comp_exist {
[dest, comp_and_jump] => match comp_and_jump.find(";") { true => {
Some(_) => { let slice: Vec<&str> = cloned.split("=").collect();
let second_slice: Vec<&str> = comp_and_jump.split(";").collect();
match second_slice.as_slice() { Some(slice.as_slice()[0].to_string())
[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 { false => None,
dest: dest.to_string(), };
comp: comp_and_jump.to_string(),
jump: None, let comp = match comp_and_jump_exist {
})), true => {
}, let slice: Vec<&str> = cloned.split("=").collect();
_ => None,
// if jump_exist {
// let another_slice: Vec<&str> = slice.as_slice()[1].split(";").collect();
// Some(another_slice.as_slice()[0].to_string());
// }
Some(slice.as_slice()[1].to_string())
}
false => 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>,
} }