+/TECS
git clone https://git.julia.blue/+/TECS
c8053ff
master
1# Handles input parsing and sanatization 2class CodeParser : 3currentLine = 0 4 5def __init__ (self ,fileName ): 6reader = open (fileName ) 7self .lines = filter (None , [i .split ('/' ,1 )[0 ].strip ()for i in reader ]) 8reader .close () 9 10def hasMoreCommands (self ): 11return self .currentLine < len (self .lines ) 12 13def advance (self ): 14self .currentLine += 1 15 16def commandType (self ): 17types = {"push" :"C_PUSH" ,"pop" :"C_POP" ,"label" :"C_LABEL" ,"goto" :"C_GOTO" , 18"if-goto" :"C_IF" ,"function" :"C_FUNCTION" ,"call" :"C_CALL" ,"return" :"C_RETURN" } 19temp = self .lines [self .currentLine ].split (' ' )[0 ] 20if temp in {"add" ,"sub" ,"neg" ,"eq" ,"gt" ,"lt" ,"and" ,"not" ,"or" }: 21return "C_ARITHMETIC" 22return types [temp ] 23 24def arg1 (self ): 25return self .lines [self .currentLine ].split (' ' )[0 if self .commandType ()== "C_ARITHMETIC" else 1 ] 26 27def arg2 (self ): 28return int (self .lines [self .currentLine ].split (' ' )[2 ])