Symbol Table Module Class Implemented

This commit is contained in:
2021-03-29 11:25:10 +03:00
parent d88fc422ee
commit 9e54efe74a
2 changed files with 37 additions and 4 deletions
+35 -2
View File
@@ -1,6 +1,4 @@
class ParserModule: class ParserModule:
def __init__(self):
pass
def parse_file(self,file_name): def parse_file(self,file_name):
assembly_file=open(file_name,"r") assembly_file=open(file_name,"r")
segmented_file=[] segmented_file=[]
@@ -33,4 +31,39 @@ class ParserModule:
assembly_file.close() assembly_file.close()
return segmented_file return segmented_file
class SymbolTableModule:
def __init__(self):
self.c_commands=[{None:"000","M":"001","D":"010","MD":"011","A":"100","AM":"101","AD":"110","AMD":"111"},{"0":"0101010","1":"0111111","-1":"0001101","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"},{None:"000","JGT":"001","JEG":"010","JGE":"011","JLT":"100","JNE":"101","JLE":"110","JMP":"111"}]
self.a_commands={"SP":"0000000000000000","LCL":"0000000000000001","ARG":"0000000000000010","THIS":"0000000000000011","THAT":"0000000000000100","R0":"0000000000000000","R1":"0000000000000001","R2":"0000000000000010","R3":"0000000000000011","R4":"0000000000000100","R5":"0000000000000101","R6":"0000000000000110","R7":"0000000000000111","R8":"0000000000001000","R9":"0000000000001001","R10":"0000000000001010","R11":"0000000000001011","R12":"0000000000001100","R13":"0000000000001101","R14":"0000000000001110","R15":"0000000000001111","SCREEN":"0100000000000000","KBD":"0110000000000000"}
self.counter=1024
def translate_l_commands(self,assembly_array):
line_idx=1
for command in assembly_array:
if not command[0] == "L":
line_idx+=1
if command[0] == "L" and not self.a_commands.get(command[1]):
self.a_commands[command[1]]=self.decimal_to_binary(line_idx)
def translate_a_commands(self,assembly_array):
for command in assembly_array:
if command[0] == "A" and not self.a_commands.get(command[1]) and not command[1].isdecimal():
self.a_commands[command[1]]=self.decimal_to_binary(self.counter)
self.counter+=1
def check_command(self,command_array):
command_type,command=command_array
if command_type == "A":
return self.a_commands.get(command)
if command_type == "C":
return "111"+self.c_commands[1].get(command[1])+self.c_commands[0].get(command[0])+self.c_commands[2].get(command[2])
return ""
def decimal_to_binary(self,value):
binary_power=16
binary_array=["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0"]
while value > 0:
if(value>=2**binary_power):
value-=2**binary_power
binary_array[15-binary_power]="1"
binary_power-=1
return "".join(binary_array)
+2 -2
View File
@@ -5,13 +5,13 @@ Build a fully fuctional Assembler that takes the Assembly writen code and transf
To Do: To Do:
1.Build the Parser Module - Incomplete 1.Build the Parser Module - Complete - no comments added
Description: Will break each assembly command into is components to be later process by the other modules. Description: Will break each assembly command into is components to be later process by the other modules.
2.Build the Code Module - Incomplete 2.Build the Code Module - Incomplete
Description: Will take the data from the parser module and translate it into machine code. Description: Will take the data from the parser module and translate it into machine code.
3.Build the Symbol Table Module - Incomplete 3.Build the Symbol Table Module - Complete - no comments added
Description: Will take data from the parser module and transform symbols into acutal addresses which will be store into a python dictionary. Description: Will take data from the parser module and transform symbols into acutal addresses which will be store into a python dictionary.
4.Connect the 3 modules together - Incomplete 4.Connect the 3 modules together - Incomplete