Skip to content

Instantly share code, notes, and snippets.

@Sebb767
Created April 21, 2016 16:52
Show Gist options
  • Save Sebb767/7b4b748c54aa4403e0540fe403770782 to your computer and use it in GitHub Desktop.
Save Sebb767/7b4b748c54aa4403e0540fe403770782 to your computer and use it in GitHub Desktop.
Interpreter for Brain Death (GC15X1H)
#!/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()
#print
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