Advertisement
am1x

pikto2py.py

Oct 17th, 2023
880
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.15 KB | Pets | 0 0
  1. import sys
  2.  
  3.  
  4. commands = {
  5.     # empty space
  6.     " " : (0, None),
  7.     # actions
  8.     "." : (1, "pass"),
  9.     "f" : (1, "mr.forward()"),
  10.     "l" : (1, "mr.turn_left()"),
  11.     "r" : (1, "mr.turn_right()"),
  12.     # subroutines
  13.     "A" : (3, "proc_A()"),
  14.     "B" : (3, "proc_B()"),
  15.     "C" : (3, "proc_C()"),
  16.     "D" : (3, "proc_D()"),
  17.     # fixed repeaters
  18.     "0" : (2, 0),
  19.     "1" : (2, 1),
  20.     "2" : (2, 2),
  21.     "3" : (2, 3),
  22.     "4" : (2, 4),
  23.     "5" : (2, 5),
  24.     "6" : (2, 6),
  25. };
  26.  
  27. def main():
  28.     print("def proc_" + "A" + ":");
  29.     compile_section_A()
  30.     print("    pass\n")
  31.  
  32.     for c in "BCD":
  33.         print("def proc_" + c + ":");
  34.         print("    pass\n")
  35.  
  36.  
  37. def compile_section_A():
  38.     stack = []
  39.     stack.append((-1, 1))
  40.  
  41.     for l in sys.stdin:
  42.         qb = False
  43.         ls = len(l.rstrip())
  44.         for i in range(ls):
  45.             ci = l[i]
  46.             if not ci in commands:
  47.                 print("Unknown symbol", i, ord(ci))
  48.                 ci = '.'
  49.             cci = commands[ci]
  50.             if cci[0] == 0:
  51.                 continue
  52.             if not qb:
  53.                 while stack[-1][0] >= i:
  54.                     stack.pop()
  55.                     assert stack
  56.                 qb = True
  57.             print("    " * stack[-1][1], end='');
  58.             if cci[0] == 1 or cci[0] == 3:
  59.                 print(cci[1])
  60.             elif cci[0] == 2:
  61.                 print("for _ in range(" + str(cci[1]) + "):")
  62.                 stack.append((i, stack[-1][1] + 1))
  63.     pass
  64.  
  65. main()
  66.  
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement