C instruction wip

This commit is contained in:
Hazem Krimi
2024-03-12 00:29:41 +01:00
parent 90b2a5f5e7
commit ac8a70f33d
5 changed files with 108 additions and 22 deletions
+25
View File
@@ -0,0 +1,25 @@
// 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/max/MaxL.asm
// Symbol-less version of the Max.asm program.
@0
D=M
@1
D=D-M
@12
D;JGT
@1
D=M
@2
M=D
@16
0;JMP
@0
D=M
@2
M=D
@16
0;JMP
+68 -5
View File
@@ -1,9 +1,72 @@
pub fn decimal_to_binary(decimal: &i32) -> String { pub fn decimal_to_fifteen_bits_binary(decimal: &i32) -> String {
String::from(format!("{decimal:015b}")) String::from(format!("{decimal:015b}"))
} }
// fn translate_dest(dest: &String) -> String { pub fn translate_dest(dest: &String) -> String {
// let cloned = dest.clone(); let cloned = dest.clone();
// cloned match cloned.as_str() {
// } "M" => "001",
"D" => "010",
"MD" => "011",
"A" => "100",
"AM" => "101",
"AD" => "110",
"AMD" => "111",
_ => "000",
}
.to_string()
}
pub fn translate_jump(jump: &String) -> String {
let cloned = jump.clone();
match cloned.as_str() {
"JGT" => "001",
"JEQ" => "010",
"JGE" => "011",
"JLT" => "100",
"JNE" => "101",
"JLE" => "110",
"JMP" => "111",
_ => "000",
}
.to_string()
}
pub fn translate_comp(comp: &String) -> String {
let cloned = comp.clone();
match cloned.as_str() {
"0" => "0101010",
"1" => "0111111",
"-1" => "0111010",
"D" => "0001100",
"A" => "0110000",
"!D" => "0001101",
"!A" => "0110001",
"-D" => "0001111",
"-A" => "0110011",
"D+1" => "0011111",
"A+1" => "0110111",
"D-1" => "0001110",
"A-1" => "0110010",
"D+A" => "0000010",
"D-A" => "0010011",
"A-D" => "0000111",
"D&A" => "0000000",
"D|A" => "0010101",
"M" => "1110000",
"!M" => "1110001",
"-M" => "1110011",
"M+1" => "1110111",
"M-1" => "1110010",
"D+M" => "1000010",
"D-M" => "1010011",
"M-D" => "1000111",
"D&M" => "1000000",
"D|M" => "1010101",
_ => panic!("Unexpected error converting the code!"),
}
.to_string()
}
+13 -15
View File
@@ -8,33 +8,31 @@ use std::process;
use regex::Regex; use regex::Regex;
use types::Instruction; use types::Instruction;
mod types;
mod parser;
mod code; mod code;
mod parser;
mod types;
fn process(instruction: String) -> String { fn process(instruction: String) -> String {
match parser::parse(&instruction) { match parser::parse(&instruction) {
Some(parsed) => match parsed { Some(parsed) => match parsed {
Instruction::AInstruction(parsed_instruction) => { Instruction::AInstruction(parsed_instruction) => {
let translated = code::decimal_to_binary(&parsed_instruction.decimal.parse::<i32>().unwrap()); let translated = code::decimal_to_fifteen_bits_binary(
&parsed_instruction.decimal.parse::<i32>().unwrap(),
);
String::from(format!("0{}", translated)) String::from(format!("0{}", translated))
} }
Instruction::CInstruction(parsed_instruction) => { Instruction::CInstruction(parsed_instruction) => {
println!( String::from("111")
"C: {}, {}, {}", + code::translate_comp(&parsed_instruction.comp).as_str()
parsed_instruction.dest, + code::translate_dest(&parsed_instruction.dest).as_str()
parsed_instruction.comp, + code::translate_jump(
parsed_instruction.jump.unwrap_or_else(|| "".to_string()) &parsed_instruction.jump.unwrap_or_else(|| "".to_string()),
); )
.as_str()
instruction
} }
}, },
None => { None => panic!("Unexpected error!"),
println!("Unexpected error!");
process::exit(1)
}
} }
} }
+1 -1
View File
@@ -15,7 +15,7 @@ pub fn parse(instruction: &String) -> Option<Instruction> {
match slice.as_slice() { match slice.as_slice() {
[dest, comp_and_jump] => match comp_and_jump.find(";") { [dest, comp_and_jump] => match comp_and_jump.find(";") {
Some(with_jump) => { Some(_) => {
let second_slice: Vec<&str> = comp_and_jump.split(";").collect(); let second_slice: Vec<&str> = comp_and_jump.split(";").collect();
match second_slice.as_slice() { match second_slice.as_slice() {