import sys
commands = {
# empty space
" " : (0, None),
# actions
"." : (1, "pass"),
"f" : (1, "mr.forward()"),
"l" : (1, "mr.turn_left()"),
"r" : (1, "mr.turn_right()"),
# subroutines
"A" : (3, "proc_A()"),
"B" : (3, "proc_B()"),
"C" : (3, "proc_C()"),
"D" : (3, "proc_D()"),
# fixed repeaters
"0" : (2, 0),
"1" : (2, 1),
"2" : (2, 2),
"3" : (2, 3),
"4" : (2, 4),
"5" : (2, 5),
"6" : (2, 6),
};
def main():
print("def proc_" + "A" + ":");
compile_section_A()
print(" pass\n")
for c in "BCD":
print("def proc_" + c + ":");
print(" pass\n")
def compile_section_A():
stack = []
stack.append((-1, 1))
for l in sys.stdin:
qb = False
ls = len(l.rstrip())
for i in range(ls):
ci = l[i]
if not ci in commands:
print("Unknown symbol", i, ord(ci))
ci = '.'
cci = commands[ci]
if cci[0] == 0:
continue
if not qb:
while stack[-1][0] >= i:
stack.pop()
assert stack
qb = True
print(" " * stack[-1][1], end='');
if cci[0] == 1 or cci[0] == 3:
print(cci[1])
elif cci[0] == 2:
print("for _ in range(" + str(cci[1]) + "):")
stack.append((i, stack[-1][1] + 1))
pass
main()
Comments