Advertisement
Guest User

Untitled

a guest
Sep 29th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. class Parsser(object):
  2. A_COMMAND = 0
  3. C_COMMAND = 1
  4. L_COMMAND = 2
  5.  
  6. def __init__(self, inputFile):
  7. currentFile = open(inputFile, "r")
  8. self.lines = currentFile.readlines() # now I have list of lines.
  9. self.currentCommand = ""
  10. self.lineNum = 0
  11. self.totalNumOfline = len(currentFile.readlines())
  12.  
  13.  
  14. def hasMoreCommands(self):
  15. if (self.lineNum + 1 == self.totalNumOfline):
  16. return False
  17. return True
  18.  
  19.  
  20. def advance(self):
  21. if (self.hasMoreCommands()):
  22. line = (self.lines[self.lineNum]).strip()
  23. self.lineNum += 1
  24. if (line == '' or line.startswith("\\\\")):
  25. self.advance()
  26. else:
  27. line = line.replace(' ', '') # avoiding D =M+1 (what about tabs?)
  28. index = line.find("\\\\")
  29. if (index != -1):
  30. line = line[index:]
  31. self.currentCommand = line
  32.  
  33.  
  34. def commandType(self):
  35. if (self.currentCommand.startswith('@')):
  36. return self.A_COMMAND
  37. else:
  38. return self.C_COMMAND
  39.  
  40. def symbol(self):
  41. if(self.commandType() == self.A_COMMAND):
  42. return self.currentCommand[1:]
  43. elif(self.commandType() == self.C_COMMAND):
  44. return self.currentCommand
  45. elif(self.commandType() == self.L_COMMAND):
  46. return self.currentCommand[1:len(self.currentCommand)] #trying to get the inside of the parenthesis.
  47.  
  48. def dest(self):
  49. assert self.currentCommand == self.C_COMMAND
  50. index = self.currentCommand.find("=")
  51. assert index != -1
  52. return self.currentCommand[:index]
  53.  
  54. def comp(self):
  55. assert self.currentCommand == self.C_COMMAND
  56. firstIndex = self.currentCommand.find("=")
  57. assert firstIndex != -1
  58. secondInex = self.currentCommand.find(";")
  59. return self.currentCommand[firstIndex+1:secondInex]
  60.  
  61. def jump(self):
  62. assert self.currentCommand == self.C_COMMAND
  63. index = self.currentCommand.find(";");
  64. assert index != -1
  65. return self.currentCommand[index+1:]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement