Contructed the Memory Access Hash Map, fixed the Arithmetic Hash Map and modified the Parser Module

This commit is contained in:
2021-04-14 21:02:32 +03:00
parent f63031d889
commit 50226cdcb2
+24 -21
View File
@@ -10,17 +10,17 @@ class VirtualMachineCompiler:
self.files.append(file_path) self.files.append(file_path)
self.assembly_commands={ self.assembly_commands={
None:"", None:"",
"add":"@SP\nA=M\nD=M\n@SP\nM=M-1\nA=M\nM=D+M\n", "add":"@SP\nA=M-1\nD=M\n@SP\nM=M-1\nA=M-1\nM=D+M\n",
"sub":"@SP\nA=M\nD=M\n@SP\nM=M-1\nA=M\nM=M-D\n", "sub":"@SP\nA=M-1\nD=M\n@SP\nM=M-1\nA=M-1\nM=M-D\n",
"neg":"@SP\nA=M\nM=-M\n", "neg":"@SP\nA=M-1\nM=-M\n",
"eq":"@SP\nA=M\nD=M\n@SP\nAM=M-1\nD=M-D\nM=-1\n@LABEL{number}\nD;JEQ\n@SP\nA=M\nM=0\n(LABLE{number})\n", "eq":"@SP\nA=M-1\nD=M\n@SP\nM=M-1\nA=M-1\nD=M-D\nM=-1\n@LABEL{number}\nD;JEQ\n@SP\nA=M\nM=0\n(LABLE{number})\n",
"gt":"@SP\nA=M\nD=M\n@SP\nAM=M-1\nD=M-D\nM=-1\n@LABEL{number}\nD;JGT\n@SP\nA=M\nM=0\n(LABLE{number})\n", "gt":"@SP\nA=M-1\nD=M\n@SP\nM=M-1\nA=M-1\nD=M-D\nM=-1\n@LABEL{number}\nD;JGT\n@SP\nA=M\nM=0\n(LABLE{number})\n",
"lt":"@SP\nA=M\nD=M\n@SP\nAM=M-1\nD=M-D\nM=-1\n@LABEL{number}\nD;JLT\n@SP\nA=M\nM=0\n(LABLE{number})\n", "lt":"@SP\nA=M-1\nD=M\n@SP\nM=M-1\nA=M-1\nD=M-D\nM=-1\n@LABEL{number}\nD;JLT\n@SP\nA=M\nM=0\n(LABLE{number})\n",
"and":"@SP\nA=M\nD=M\n@SP\nM=M-1\nA=M\nM=D&M\n", "and":"@SP\nA=M-1\nD=M\n@SP\nM=M-1\nA=M-1\nM=D&M\n",
"or":"@SP\nA=M\nD=M\n@SP\nM=M-1\nA=M\nM=D|M\n", "or":"@SP\nA=M-1\nD=M\n@SP\nM=M-1\nA=M-1\nM=D|M\n",
"not":"@SP\nA=M\nM=!M\n", "not":"@SP\nA=M-1\nM=!M\n",
"push":"", "push":"{m_arg}D=M\n@SP\nM=M+1\nA=M-1\nM=D\n",
"pop":"", "pop":"{m_arg}D=A\n@R13\nM=D\n@SP\nAM=M-1\nD=M\n@R13\nA=M\nM=D\n",
"label":"", "label":"",
"goto":"", "goto":"",
"if-goto":"", "if-goto":"",
@@ -30,14 +30,14 @@ class VirtualMachineCompiler:
} }
self.arguments={ self.arguments={
None:"", None:"",
"argument":"ARG", "argument":"@ARG\nD=M\n@{arg2}\nA=D+M\n",
"local":"LCL", "local":"@LCL\nD=M\n@{arg2}\nA=D+M\n",
"static":"", "static":"@{static_arg}\n",
"constant":"", "constant":"@{arg2}\n",
"this":"", "this":"@THIS\nD=M\n@{arg2}\nA=D+M\n",
"that":"", "that":"@THAT\nD=M\n@{arg2}\nA=D+M\n",
"pointer":"", "pointer":"@R3\nD=M\n@{arg2}\nA=D+M\n",
"temp":"" "temp":"@R5\nD=M\n@{arg2}\nA=D+M\n"
} }
self.label_number=0 self.label_number=0
def folder_search(self): def folder_search(self):
@@ -49,6 +49,7 @@ class VirtualMachineCompiler:
self.folder_search() self.folder_search()
for file in self.files: for file in self.files:
file_content=open(file) file_content=open(file)
file_array=[]
for line in file_content: for line in file_content:
command=[None,None,None] command=[None,None,None]
line=line[:line.find("//")] line=line[:line.find("//")]
@@ -58,13 +59,15 @@ class VirtualMachineCompiler:
line_array=line.split() line_array=line.split()
for index in range(len(line_array)): for index in range(len(line_array)):
command[index]=line_array[index] command[index]=line_array[index]
self.files_content.append(command) file_array.append(command)
self.files_content.append(file_array)
file_content.close() file_content.close()
def code_writer_module(self): def code_writer_module(self):
file_path=self.file_path file_path=self.file_path
if not self.is_folder: if not self.is_folder:
file_path=file_path[:file_path.find(".")] file_path=file_path[:file_path.find(".")]
assembly_file=open(file_path,"w") assembly_file=open(file_path,"w")
for line in self.files_content: for file in range(len(self.files_content)):
for line in self.files_content[file]:
continue continue