Created
April 21, 2016 16:52
-
-
Save Sebb767/7b4b748c54aa4403e0540fe403770782 to your computer and use it in GitHub Desktop.
Interpreter for Brain Death (GC15X1H)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# | |
# WARNING: This program serves as a reference! Please write your own interpreter to justify your find! | |
# WARNUNG: Dieses Programm soll als Referenzimplementierung gelten! Bitte schreibe deine eigene Lösung um dir den Find zu verdienen! | |
# | |
# (c) Sebb767, 2016 | |
# | |
import sys | |
if(len(sys.argv) < 2): | |
print("Usage: %s file" % sys.argv[0]) | |
exit(1) | |
mem = [ 0, 0, 0, 0, 0, 0, 0 ] # 7 register | |
ptr = 0 # reg pointer | |
ctr = 0 # command counter | |
loop = [] # loop index | |
commands = [ '+', '-', 'i', 'd', '=', '(', ')' ] | |
code = "" # code | |
result = "" | |
# read whole file and extract commands | |
with open(sys.argv[1]) as f: | |
content = f.readlines() | |
for line in content: | |
for c in line: | |
if c in commands: | |
code += c | |
while (ctr < len(code)): | |
cmd = code[ctr] | |
# incr/decr | |
if (cmd == 'i'): | |
mem[ptr] += 1 | |
if (cmd == 'd'): | |
mem[ptr] -= 1 | |
# reg | |
if (cmd == '+'): | |
ptr += 1 | |
if (cmd == '-'): | |
ptr -= 1 | |
#loop | |
if (cmd == '('): | |
loop.append(ctr) | |
if (cmd == ')'): | |
if(mem[ptr]!=0): | |
ctr = loop[-1] | |
else: | |
loop.pop() | |
if (cmd == '='): | |
print("%s %s" % (hex(mem[ptr]), chr(mem[ptr]))) | |
result += chr(mem[ptr]) | |
ctr += 1 | |
print("\nExited, result: %s" % result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment