Advertisement
am1x

pikto2pyb

Nov 8th, 2023 (edited)
772
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.10 KB | Fixit | 0 0
  1. import sys
  2.  
  3.  
  4. commands = {
  5.     # empty space
  6.     "_" : (0, None),
  7.  
  8.     # actions
  9.     "." : (1, "pass"),
  10.  
  11.     "pit0" : (1, "pitcher = 0"),
  12.     "pit+1" : (1, "pitcher += 1"),
  13.     "pit-1" : (1, "pitcher = max(pitcher - 1, 0)"),
  14.     "mem0" : (1, "memory = 0"),
  15.     "mem+p" : (1, "memory += pitcher"),
  16.     "mem-p" : (1, "memory = max(memory - pitcher, 0)"),
  17.     "fl0+" : (1, "fl0 = True"),
  18.     "fl0-" : (1, "fl0 = False"),
  19.     "fl1+" : (1, "fl1 = True"),
  20.     "fl1-" : (1, "fl1 = False"),
  21.  
  22.     "rfwd" : (1, "mr.step_forward()"),
  23.     "rleft" : (1, "mr.turn_left()"),
  24.     "rright" : (1, "mr.turn_right()"),
  25.     "rfix" : (1, "mr.fix_cell()"),
  26.     "rtow" : (1, "mr.tow()"), # for Tower
  27.  
  28.     # subroutines
  29.     "@" : (3, "proc_main()"),
  30.     "A" : (3, "proc_A()"),
  31.     "B" : (3, "proc_B()"),
  32.     "C" : (3, "proc_C()"),
  33.     "D" : (3, "proc_D()"),
  34.  
  35.     # fixed repeaters
  36.     "(0)" : (2, 0),
  37.     "(1)" : (2, 1),
  38.     "(2)" : (2, 2),
  39.     "(3)" : (2, 3),
  40.     "(4)" : (2, 4),
  41.     "(5)" : (2, 5),
  42.     "(6)" : (2, 6),
  43.  
  44.     # dynamic repeaters, vars only
  45.     "(pit)" : (4, "pitcher"),
  46.     "(mem)" : (4, "memory"),
  47.  
  48.     # if conditionals
  49.     "<pit>" : (5, "(pitcher > 0)"),
  50.     "<!pit>" : (5, "(pitcher <= 0)"),
  51.     "<mem>" : (5, "(memory > 0)"),
  52.     "<!mem>" : (5, "(memory <= 0)"),
  53.     "<memeq>" : (5, "(memory == pitcher)"),
  54.     "<memne>" : (5, "(memory != pitcher)"),
  55.     "<memlt>" : (5, "(memory < pitcher)"),
  56.     "<memle>" : (5, "(memory <= pitcher)"),
  57.     "<memgt>" : (5, "(memory > pitcher)"),
  58.     "<memge>" : (5, "(memory >= pitcher)"),
  59.     "<fl0>" : (5, "fl0"),
  60.     "<!fl0>" : (5, "not fl0"),
  61.     "<fl1>" : (5, "fl1"),
  62.     "<!fl1>" : (5, "not fl1"),
  63.  
  64.     "<rclear>" : (5, "mr.path_clear()"),
  65.     "<!rclear>" : (5, " not mr.path_clear()"),
  66.     "<rfwd>" : (5, "mr.can_step_forward()"), #for Mover
  67.     "<!rfwd>" : (5, "not mr.can_step_forward()"), #for Mover
  68.  
  69.     "<rcnor>" : (5, "mr.cell_normal()"),
  70.     "<!rcnor>" : (5, "not mr.cell_normal()"),
  71.     "<rcbro>" : (5, "mr.cell_broken()"),
  72.     "<!rcbro>" : (5, "not mr.cell_broken()"),
  73.     "<rcfix>" : (5, "mr.cell_fixed()"),
  74.     "<!rcfix>" : (5, "not mr.cell_fixed()"),
  75.  
  76.     # while conditionals
  77.     "[pit]" : (6, "(pitcher > 0)"),
  78.     "[!pit]" : (6, "(pitcher <= 0)"),
  79.     "[mem]" : (6, "(memory > 0)"),
  80.     "[!mem]" : (6, "(memory <= 0)"),
  81.     "[memeq]" : (6, "(memory == pitcher)"),
  82.     "[memne]" : (6, "(memory != pitcher)"),
  83.     "[memlt]" : (6, "(memory < pitcher)"),
  84.     "[memle]" : (6, "(memory <= pitcher)"),
  85.     "[memgt]" : (6, "(memory > pitcher)"),
  86.     "[memge]" : (6, "(memory >= pitcher)"),
  87.     "[fl0]" : (6, "fl0"),
  88.     "[!fl0]" : (6, "not fl0"),
  89.     "[fl1]" : (6, "fl1"),
  90.     "[!fl1]" : (6, "not fl1"),
  91.  
  92.     "[rclear]" : (6, "mr.path_clear()"),
  93.     "[!rclear]" : (6, "not mr.path_clear()"),
  94.     "[rfwd]" : (6, "mr.can_step_forward()"), #for Mover
  95.     "[!rfwd]" : (6, "not mr.can_step_forward()"), #for Mover
  96.  
  97.     "[rcnor]" : (6, "mr.cell_normal()"),
  98.     "[!rcnor]" : (6, "not mr.cell_normal()"),
  99.     "[rcbro]" : (6, "mr.cell_broken()"),
  100.     "[!rcbro]" : (6, "not mr.cell_broken()"),
  101.     "[rcfix]" : (6, "mr.cell_fixed()"),
  102.     "[!rcfix]" : (6, "not mr.cell_fixed()"),
  103. }
  104.  
  105. tab_string = "    "
  106.  
  107. def compile_section(lines):
  108.     stack = []
  109.     stack.append((-1, 1))
  110.  
  111.     for l in lines:
  112.         qb = False
  113.         lw = l.rstrip().split()
  114.         ls = len(l.rstrip())
  115.         for i in range(ls):
  116.             ci = l[i]
  117.             if not ci in commands:
  118.                 print("Unknown symbol", i, ord(ci), file=sys.stderr)
  119.                 ci = "."
  120.             cci = commands[ci]
  121.             if cci[0] == 0:
  122.                 continue
  123.             if not qb:
  124.                 while stack[-1][0] >= i:
  125.                     stack.pop()
  126.                     assert stack
  127.                 qb = True
  128.  
  129.             print(tab_string * stack[-1][1], end='');
  130.  
  131.             if cci[0] == 1 or cci[0] == 3:
  132.                 print(cci[1])
  133.  
  134.             elif cci[0] == 2: # fixed repeaters
  135.                 print("for _ in range(" + str(cci[1]) + "):")
  136.                 stack.append((i, stack[-1][1] + 1))
  137.  
  138.             elif cci[0] == 4: # dynamic repeaters (pitcher and memory)
  139.                 print("for _ in range(" + cci[1] + "):")
  140.                 stack.append((i, stack[-1][1] + 1))
  141.  
  142.             elif cci[0] == 5: # if conditionals
  143.                 print("if "+ cci[1] + ":")
  144.                 stack.append((i, stack[-1][1] + 1))
  145.  
  146.             elif cci[0] == 6: # while conditionals
  147.                 print("while "+ cci[1] + ":")
  148.  
  149.         pass # for i in range(ls)
  150.     pass # for l in lines
  151.  
  152.  
  153. def precompile_program():
  154.     res = [None] * 5
  155.     for i in range(5):
  156.         res[i] = list()
  157.  
  158.     main_section = 0
  159.     cur_section = 0
  160.     cur_skip = True
  161.  
  162.     for l in sys.stdin:
  163.         lw = l.rstrip().split()
  164.         while lw and (lw[-1] == "_"):
  165.             lw.pop()
  166.         ls = len(lw)
  167.  
  168.         if ls == 0:
  169.             cur_skip = True
  170.             continue
  171.  
  172.         if cur_skip and (ls == 1) and (lw[0] in commands) and (commands[lw[0]][0] == 3):
  173.             # selecting another section
  174.             cur_section = ord(lw[0][0]) - ord('@')
  175.             print("Section " + str(i) + " selected.", file=sys.stderr);
  176.             if main_section == 0:
  177.                 main_section = cur_section
  178.             continue
  179.         cur_skip = False
  180.         res[cur_section].append(lw)
  181.     if (not res[0]) and (main_section > 0):
  182.         res[0].append([chr(ord('@') + main_section)])
  183.     return res
  184.  
  185.  
  186. def main():
  187.     codes = precompile_program()
  188.     cn = len(codes)
  189.     print("import MoscowRobots as mr")
  190.     print()
  191.     print("pitcher = 0")
  192.     print("memory = 0")
  193.     print("fl0 = False")
  194.     print("fl1 = False")
  195.     print()
  196.  
  197.     for i in range(cn):
  198.         sname = commands[chr(ord('@') + i)][1]
  199.         print("def " + sname + ":")
  200.         compile_section(codes[i])
  201.         print("    pass\n")
  202.     print("proc_main()")
  203.  
  204.  
  205. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement