A rudimentary interpreter for a stack-based language with Piet-like commands.
This file contains 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
#72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33 | |
nop | |
push 3 | |
dup | |
add | |
dup | |
mul | |
dup | |
dup | |
add | |
putc | |
push 3 | |
sub | |
dup | |
push 3 | |
mul | |
push 1 | |
add | |
dup | |
push 1 | |
add | |
putc | |
dup | |
push 4 | |
dup | |
add | |
add | |
dup | |
putc | |
dup | |
putc | |
dup | |
push 3 | |
add | |
dup | |
putc | |
dup | |
dup | |
push 5 | |
div | |
dup | |
add | |
dup | |
putc | |
dup | |
add | |
push 4 | |
dup | |
mul | |
dup | |
add | |
putc | |
push 1 | |
sub | |
putc | |
putc | |
push 3 | |
add | |
putc | |
putc | |
putc | |
putc |
This file contains 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
from sys import argv, stdout | |
with open(argv[1], 'r') as prog: | |
cmds = prog.readlines() | |
colors = ['red', 'ylw', 'grn', 'cyn', 'blu', 'mgn'] | |
hue = 4 | |
dark = 1 | |
stack = [] | |
codels = 0 | |
for cmd in cmds: | |
terms = cmd.split() | |
if len(terms) == 0: continue | |
if terms[0] == 'add': | |
if len(stack) > 1: | |
a, b = stack.pop(), stack.pop() | |
stack.append(b+a) | |
codels += 1 | |
hue = (hue+1)%6 | |
elif terms[0] == 'sub': | |
if len(stack) > 1: | |
a, b = stack.pop(), stack.pop() | |
stack.append(b-a) | |
codels += 1 | |
hue = (hue+1)%6 | |
dark = (dark+1)%3 | |
elif terms[0] == 'mul': | |
if len(stack) > 1: | |
a, b = stack.pop(), stack.pop() | |
stack.append(b*a) | |
codels += 1 | |
hue = (hue+1)%6 | |
dark = (dark+2)%3 | |
elif terms[0] == 'div': | |
if len(stack) > 1: | |
a, b = stack.pop(), stack.pop() | |
stack.append(b//a) | |
codels += 1 | |
hue = (hue+2)%6 | |
elif terms[0] == 'mod': | |
if len(stack) > 1: | |
a, b = stack.pop(), stack.pop() | |
stack.append(b%a) | |
codels += 1 | |
hue = (hue+2)%6 | |
dark = (dark+1)%3 | |
elif terms[0] == 'dup': | |
if stack: | |
stack.append(stack[-1]) | |
codels += 1 | |
hue = (hue+4)%6 | |
elif terms[0] == 'push': | |
stack.append(int(terms[1])) | |
codels += int(terms[1]) | |
dark = (dark+1)%3 | |
elif terms[0] == 'pop': | |
if stack: | |
stack.pop() | |
codels += 1 | |
dark = (dark+2)%3 | |
elif terms[0] == 'putc': | |
if stack: | |
a = stack.pop() | |
stdout.write(chr(a)) | |
codels += 1 | |
hue = (hue+5)%6 | |
dark = (dark+2)%3 | |
elif terms[0] == 'roll': | |
if len(stack) > 1: | |
a, b = stack.pop(), stack.pop() | |
while a: | |
t = stack.pop(-b) | |
stack += [t] | |
a -= 1 | |
codels += 1 | |
hue = (hue+4)%6 | |
dark = (dark+1)%3 | |
elif terms[0] == 'rot': | |
if stack: | |
stack.pop() | |
codels += 1 | |
hue = (hue+3)%6 | |
dark = (dark+1)%3 | |
elif terms[0] == 'swit': | |
if stack: | |
stack.pop() | |
codels += 1 | |
hue = (hue+3)%6 | |
dark = (dark+2)%3 | |
elif terms[0] == 'nop': | |
codels += 1 | |
elif terms[0] == 'end': | |
break | |
else: | |
continue | |
print("\t%3d %-6s\t%s%d %s"%(codels, cmd.strip(), colors[hue], dark, stack)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment