RAM machine interpreter


SUBMITTED BY: Guest

DATE: Dec. 31, 2013, 10:14 a.m.

FORMAT: Python

SIZE: 4.7 kB

HITS: 1298

  1. # This program interpretes code for Random Access Machine - RAM
  2. # RAM is hypothetical computational machine, like Turing's machine,
  3. # used for educational purposes and proving concepts
  4. # may be useful for debugging homework (my case)
  5. # feel free to modify to suit your needs
  6. # usage: takes one parameter - filename of file with RAM machine code
  7. import re
  8. import sys
  9. class Parser:
  10. program = []
  11. # instruction set, True - instruction HAS parameter
  12. instructionSet = [
  13. ("READ", False), # acc = stdin
  14. ("WRITE", False), # acc -> stdout
  15. ("LOAD", True), # acc = mem[param]
  16. ("STORE", True), # mem[param] = acc
  17. ("ALOAD", True), # acc = mem[mem[param]]
  18. ("ASTORE", True), # mem[mem[param]] = acc
  19. ("ADD", True), # acc += mem[param]
  20. ("SUB", True), # acc -= mem[param]
  21. ("MUL", True), # acc *= mem[param]
  22. ("DIV", True), # acc = acc / mem[param]
  23. ("MOD", True), # acc = acc % mem[param]
  24. ("SET", True), # acc = param
  25. ("INC", True), # acc += param
  26. ("DEC", True), # acc -= param
  27. ("JUMP", True), # instruction pointer = param
  28. ("JGTZ", True), # instruction pointer = param if acc > 0
  29. ("JZERO", True), # instruction pointer = param if acc == 0
  30. ("JNEG", True), # instruction pointer = param if acc < 0
  31. ("AJUMP", False), # instruction pointer = mem[param]
  32. ("NOP", False), # no operation
  33. ("HALT", False)] # end program
  34. def parseInput(self, filename):
  35. pattern = "^(\w+)((\s+(-?\d+?))?)\s*$"
  36. f = open(filename, "r")
  37. for line in f:
  38. line = line.split("#")[0]
  39. if(re.match(pattern, line)):
  40. parsedLine = re.search(pattern, line)
  41. instruction = parsedLine.group(1)
  42. parameter = parsedLine.group(4)
  43. hasParameter = False if parameter == None else True
  44. if((instruction, hasParameter) in self.instructionSet):
  45. if(hasParameter == True):
  46. self.program.append((instruction, parameter))
  47. else:
  48. self.program.append((instruction, None))
  49. else:
  50. print "Unknown instruction "+instruction
  51. sys.exit()
  52. else:
  53. print "Unknown instruction "+line
  54. sys.exit()
  55. if(self.program[-1][0] != "HALT"):
  56. self.program.append(("HALT", False))
  57. return self.program
  58. class Interpreter:
  59. program = []
  60. def __init__(self, inputProgram):
  61. self.program = inputProgram
  62. def runProgram(self):
  63. acc = 0 # accumulator
  64. ip = 0 # instruction pointer
  65. mem = dict() # memory
  66. instr = self.program[ip][0]
  67. param = self.program[ip][1]
  68. while(instr != "HALT"):
  69. ip += 1
  70. if(instr == "READ"):
  71. acc = int(sys.stdin.readline())
  72. elif(instr == "WRITE"):
  73. print acc
  74. elif(instr == "LOAD"):
  75. if(int(param) not in mem):
  76. mem[int(param)] = 0
  77. acc = mem[int(param)]
  78. elif(instr == "STORE"):
  79. mem[int(param)] = acc
  80. elif(instr == "ALOAD"):
  81. if(mem[int(param)] not in mem):
  82. mem[mem[int(param)]] = 0
  83. acc = mem[mem[int(param)]]
  84. elif(instr == "ASTORE"):
  85. mem[mem[int(param)]] = acc
  86. elif(instr == "ADD"):
  87. if(int(param) not in mem):
  88. mem[int(param)] = 0
  89. acc = acc + mem[int(param)]
  90. elif(instr == "SUB"):
  91. if(int(param) not in mem):
  92. mem[int(param)] = 0
  93. acc = acc - mem[int(param)]
  94. elif(instr == "MUL"):
  95. if(int(param) not in mem):
  96. mem[int(param)] = 0
  97. acc = acc * mem[int(param)]
  98. elif(instr == "DIV"):
  99. if(int(param) not in mem):
  100. mem[int(param)] = 0
  101. acc = acc / mem[int(param)]
  102. elif(instr == "MOD"):
  103. if(int(param) not in mem):
  104. mem[int(param)] = 0
  105. acc = acc % mem[int(param)]
  106. elif(instr == "SET"):
  107. acc = int(param)
  108. elif(instr == "INC"):
  109. acc = acc + int(param)
  110. elif(instr == "DEC"):
  111. acc = acc - int(param)
  112. elif(instr == "JUMP"):
  113. ip = int(param)
  114. elif(instr == "JGTZ"):
  115. if(acc > 0):
  116. ip = int(param)
  117. elif(instr == "JZERO"):
  118. if(acc == 0):
  119. ip = int(param)
  120. elif(instr == "JNEG"):
  121. if(acc < 0):
  122. ip = int(param)
  123. elif(instr == "AJUMP"):
  124. ip = acc
  125. instr = program[ip][0]
  126. param = program[ip][1]
  127. if(len(sys.argv) >= 2):
  128. filename = sys.argv[1]
  129. else:
  130. print "No input file"
  131. sys.exit()
  132. parser = Parser()
  133. program = parser.parseInput(filename)
  134. interpreter = Interpreter(program)
  135. interpreter.runProgram()

comments powered by Disqus