Skip to content

Instantly share code, notes, and snippets.

@josch
Forked from anonymous/file_1
Created February 23, 2010 05:24
Show Gist options
  • Save josch/311896 to your computer and use it in GitHub Desktop.
Save josch/311896 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import sys
class VM:
max_space = 32
def __init__(self, program, initial_data):
self.program_store = list()
self.data_store = self.max_space*[0]
self.pc = 0
self.in1 = 0
self.in2 = 0
self.acc = 0
f = open(sys.argv[1])
for line in f:
self.program_store.append(line.strip())
f.close
for i, data in enumerate(sys.argv[2:]):
if i > (self.max_space - 1):
raise ValueError, "maximum of %d items"%self.max_space
self.data_store[i] = int(data)
def run(self):
while self.parse(self.program_store[self.pc]):
self.print_status()
def parse(self, input):
print input
inst, arg = input.upper().rsplit(" ", 1)
if inst == "LOAD":
self.acc = self.data_store[int(arg)]
self.pc += 1
elif inst == "STORE":
self.data_store[int(arg)] = self.acc
self.pc += 1
elif inst == "ADD":
self.acc += self.data_store[int(arg)]
self.pc += 1
elif inst == "SUB":
self.acc -= self.data_store[int(arg)]
self.pc += 1
elif inst == "LOADI":
self.acc = int(arg)
self.pc += 1
elif inst == "ADDI":
self.acc += int(arg)
self.pc += 1
elif inst == "SUBI":
self.acc -= int(arg)
self.pc += 1
elif inst == "LOADIN 2":
self.acc = self.data_store[self.in2 + int(arg)]
self.pc += 1
elif inst == "STOREIN 2":
self.data_store[self.in2 + int(arg)] = self.acc
self.pc += 1
elif inst == "LOADIN 1":
self.acc = self.data_store[self.in1 + int(arg)]
self.pc += 1
elif inst == "STOREIN 1":
self.data_store[self.in1 + int(arg)] = self.acc
self.pc += 1
elif inst == "MOVE ACC":
if arg == "IN1":
self.in1 = self.acc
elif arg == "IN2":
self.in2 = self.acc
else:
raise NotImplementedError
self.pc += 1
elif inst == "MOVE IN1":
if arg == "ACC":
self.acc = self.in1
elif arg == "IN2":
self.in2 = self.in1
else:
raise NotImplementedError
self.pc += 1
elif inst == "MOVE IN2":
if arg == "ACC":
self.acc = self.in2
elif arg == "IN1":
self.in1 = self.in2
else:
raise NotImplementedError
self.pc += 1
elif inst == "JUMP":
self.pc += int(arg)
elif inst == "JUMP(=)":
if self.acc == 0:
self.pc += int(arg)
else:
self.pc += 1
elif inst == "JUMP(<)":
if self.acc < 0:
self.pc += int(arg)
else:
self.pc += 1
elif inst == "JUMP(>)":
if self.acc > 0:
self.pc += int(arg)
else:
self.pc += 1
elif inst == "JUMP(!=)":
if self.acc != 0:
self.pc += int(arg)
else:
self.pc += 1
elif inst == "JUMP(<=)":
if self.acc <= 0:
self.pc += int(arg)
else:
self.pc += 1
elif inst == "JUMP(>=)":
if self.acc >= 0:
self.pc += int(arg)
else:
self.pc += 1
elif inst == "STOP":
return False
elif inst == "NOP":
pass
else:
raise NotImplementedError
return True
def print_status(self):
print "ACC:", vm.acc, "PC:", vm.pc, "IN1:", vm.in1, "IN2:", vm.in2, "data_store", vm.data_store
print
if __name__ == "__main__":
vm = VM(sys.argv[1], sys.argv[2:])
vm.print_status()
vm.run()
vm.print_status()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment