Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Parsser(object):
- A_COMMAND = 0
- C_COMMAND = 1
- L_COMMAND = 2
- def __init__(self, inputFile):
- currentFile = open(inputFile, "r")
- self.lines = currentFile.readlines() # now I have list of lines.
- self.currentCommand = ""
- self.lineNum = 0
- self.totalNumOfline = len(currentFile.readlines())
- def hasMoreCommands(self):
- if (self.lineNum + 1 == self.totalNumOfline):
- return False
- return True
- def advance(self):
- if (self.hasMoreCommands()):
- line = (self.lines[self.lineNum]).strip()
- self.lineNum += 1
- if (line == '' or line.startswith("\\\\")):
- self.advance()
- else:
- line = line.replace(' ', '') # avoiding D =M+1 (what about tabs?)
- index = line.find("\\\\")
- if (index != -1):
- line = line[index:]
- self.currentCommand = line
- def commandType(self):
- if (self.currentCommand.startswith('@')):
- return self.A_COMMAND
- else:
- return self.C_COMMAND
- def symbol(self):
- if(self.commandType() == self.A_COMMAND):
- return self.currentCommand[1:]
- elif(self.commandType() == self.C_COMMAND):
- return self.currentCommand
- elif(self.commandType() == self.L_COMMAND):
- return self.currentCommand[1:len(self.currentCommand)] #trying to get the inside of the parenthesis.
- def dest(self):
- assert self.currentCommand == self.C_COMMAND
- index = self.currentCommand.find("=")
- assert index != -1
- return self.currentCommand[:index]
- def comp(self):
- assert self.currentCommand == self.C_COMMAND
- firstIndex = self.currentCommand.find("=")
- assert firstIndex != -1
- secondInex = self.currentCommand.find(";")
- return self.currentCommand[firstIndex+1:secondInex]
- def jump(self):
- assert self.currentCommand == self.C_COMMAND
- index = self.currentCommand.find(";");
- assert index != -1
- return self.currentCommand[index+1:]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement