Advertisement
3snoW

Number Factory Interpreter

Feb 9th, 2018
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.58 KB | None | 0 0
  1. # This is a patched version of the original code submitted.
  2. #   It had a few bugs, mainly because (X,Y) in the factory layout is
  3. #   represented as (Y,X) in the code, due to the way python accesses
  4. #   arrays, so often the coordinates of rooms were reversed.
  5. #
  6. # It works as far as i've tested. Rooms that I didn't test:
  7. #   S, P, Q, &, =, /, X, !
  8. # I also didn't test loops within loops. An exception will be raised
  9. #   if the robot goes out of the factory, but this may be an intended
  10. #   behavior.
  11. #
  12. # 3snoW
  13. log = []
  14. charlog = []
  15. depth = 0
  16. next = ""
  17. code = ""
  18. line = 1
  19. char = 0
  20. tempcmd = ""
  21. cmd = ""
  22. loc = [3,2]
  23. hand = None
  24. factoryStorage = [
  25. [0,1,2,10,None],
  26. [None,None,None,None,None],
  27. [None,None,None,None,None],
  28. [None,None,1,None,None],]
  29. factorymap = [
  30. ["0","1","2","T","%"],
  31. ["-","S","P","Q","*"],
  32. ["+","#","@","C","&"],
  33. ["=","/","X","!","~"],]
  34. S=[]
  35. P=[]
  36. from queue import *
  37. Q=Queue()
  38. def Supply(number):
  39.     global hand
  40.     if hand==None:
  41.         hand=number
  42.     else:
  43.         hand=None
  44. def PickDrop(X,Y):
  45.     global hand
  46.     if hand==None:
  47.         hand=factoryStorage[X][Y]
  48.         factoryStorage[X][Y]=None
  49.     else:
  50.         factoryStorage[X][Y]=hand
  51.         hand=None
  52. def Math(X,Y,mod=1):
  53.     global hand
  54.     global factoryStorage
  55.     if hand==None:
  56.         hand=factoryStorage[X][Y]
  57.         factoryStorage[X][Y]=None
  58.     elif factoryStorage[X][Y]==None:
  59.         factoryStorage[X][Y]=hand
  60.         hand=None
  61.     else:
  62.         factoryStorage[X][Y]+=(hand*mod)
  63.         hand=None
  64. def StackRoom(stack):
  65.     global hand
  66.     if hand==None:
  67.        if len(stack)!=0:
  68.            hand=stack.pop
  69.     else:
  70.         stack.append(hand)
  71.         hand=None
  72. def QueueRoom(queue):
  73.     global hand
  74.     if hand==None:
  75.        if not queue.empty:
  76.            hand=queue.get
  77.     else:
  78.         queue.put(hand)
  79.         hand=None
  80. def OutputInt():
  81.     global hand
  82.     if hand!=None:
  83.        print(hand,end=" ",flush=True)
  84.        hand=None
  85. def OutputLetter():
  86.     global hand
  87.     if hand!=None:
  88.        if hand in Letters:
  89.             print(Letters[hand],end="",flush=True)
  90.        hand=None
  91. def Burn():
  92.     global hand
  93.     hand=None
  94. def Compare(X,Y,invert=False):
  95.     global hand
  96.     global factoryStorage
  97.     result=0
  98.     if hand==None:
  99.         hand=factoryStorage[X][Y]
  100.         factoryStorage[X][Y]=None
  101.     elif factoryStorage[X][Y]==None:
  102.         factoryStorage[X][Y]=hand
  103.         hand=None
  104.     elif factoryStorage[X][Y]==hand:
  105.             hand=None
  106.             factoryStorage[X][Y]=None
  107.             result=1
  108.     else:
  109.             hand=None
  110.             factoryStorage[X][Y]=None
  111.             result=0
  112.     if invert==True and result==1:
  113.         result=0
  114.     elif invert==True and result==0:
  115.         result=1
  116.     factoryStorage[3][2]=result
  117. def Invert(X,Y):
  118.     global hand
  119.     if hand==None:
  120.         hand=factoryStorage[X][Y]
  121.         factoryStorage[X][Y]=None
  122.     elif hand==0:
  123.         factoryStorage[X][Y]=1
  124.         hand=None
  125.     else:
  126.         factoryStorage[X][Y]=0
  127.         hand=None
  128. def Copy(X,Y,CX,CY):
  129.     global hand
  130.     if hand==None:
  131.         hand=factoryStorage[X][Y]
  132.         factoryStorage[X][Y]=None
  133.     else:
  134.         factoryStorage[X][Y]=hand
  135.         factoryStorage[CX][CY]=hand
  136.         hand=None
  137. def Input(X,Y):
  138.     hold=input("\n> ")
  139.     hold=int(hold)
  140.     factoryStorage[X][Y]=hold
  141. Letters = {
  142. 0: " ",
  143. 1: "A",
  144. 2: "B",
  145. 3: "C",
  146. 4: "D",
  147. 5: "E",
  148. 6: "F",
  149. 7: "G",
  150. 8: "H",
  151. 9: "I",
  152. 10: "J",
  153. 11: "K",
  154. 12: "L",
  155. 13: "M",
  156. 14: "N",
  157. 15: "O",
  158. 16: "P",
  159. 17: "Q",
  160. 18: "R",
  161. 19: "S",
  162. 20: "T",
  163. 21: "U",
  164. 22: "V",
  165. 23: "W",
  166. 24: "X",
  167. 25: "Y",
  168. 26: "Z",
  169. 27: "\n"}
  170.  
  171. import linecache
  172. import os
  173. A = os.getcwd()
  174. print('Choose a Number Factory program file in: ' + A)
  175. code = input("File name: ")
  176. code = A+"/"+code
  177. print(code)
  178. while 1 == 1:
  179.     tempcmd = linecache.getline(code, line)
  180.     if len(tempcmd)!=0:
  181.         cmd = tempcmd[char]
  182.     #uncomment for debug. X and Y represent the locations in the map, not in the array.
  183.     #print('X:'+str(loc[1])+' Y:'+str(loc[0])+' hand:'+str(hand)+' cmd: '+cmd+ ' pos: '+str(factorymap[loc[0]][loc[1]]))
  184.     if depth == 0:
  185.         if cmd == '>':
  186.             loc[1]+=1
  187.             if loc==[0,4]:
  188.                 Input(0,4)
  189.         if cmd == '<':
  190.             loc[1]-=1
  191.             if loc==[0,4]:
  192.                 Input(0,4)
  193.         if cmd == '^':
  194.             loc[0]-=1
  195.             if loc==[0,4]:
  196.                 Input(0,4)
  197.         if cmd == 'V':
  198.             loc[0]+=1
  199.             if loc==[0,4]:
  200.                 Input(0,4)
  201.         if cmd == '%':
  202.             room=factorymap[loc[0]][loc[1]]
  203.             if room=="0":
  204.                 Supply(0)
  205.             elif room=="1":
  206.                 Supply(1)
  207.             elif room=="2":
  208.                 Supply(2)
  209.             elif room=="T":
  210.                 Supply(10)
  211.             elif room=="%":
  212.                 PickDrop(0,4)
  213.             elif room=="-":
  214.                 Math(1,0,-1)
  215.             elif room=="S":
  216.                 StackRoom(S)
  217.             elif room=="P":
  218.                 StackRoom(P)
  219.             elif room=="Q":
  220.                 QueueRoom(Q)
  221.             elif room=="*":
  222.                 OutputInt()
  223.             elif room=="+":
  224.                 Math(2,0,1)
  225.             elif room=="#":
  226.                 Copy(2,1,2,2)
  227.             elif room=="@":
  228.                 PickDrop(2,2)
  229.             elif room=="C":
  230.                 PickDrop(2,3)
  231.             elif room=="&":
  232.                 Burn()
  233.             elif room=="=":
  234.                 Compare(3,0,False)
  235.             elif room=="/":
  236.                 Compare(3,1,True)
  237.             elif room=="X":
  238.                 PickDrop(3,2)
  239.             elif room=="!":
  240.                 Invert(3,3)
  241.             elif room=="~":
  242.                 OutputLetter()
  243.         if cmd == '':
  244.             break
  245.         if cmd == '(':
  246.             if factoryStorage[3][2] == 0:
  247.                 depth=depth+1
  248.             else:
  249.                 log.append(line)
  250.                 charlog.append(char)
  251.         if cmd == ')':
  252.             if factoryStorage[3][2] != 0:
  253.                 line = log[-1]
  254.                 char = charlog[-1]
  255.             else:
  256.                 log.pop
  257.                 charlog.pop
  258.     else:
  259.         if cmd == '(':
  260.             depth+=1
  261.         if cmd == ')':
  262.             depth-=1
  263.         if cmd == '':
  264.             depth=0
  265.             break
  266.     if len(tempcmd)>char+1:
  267.         next = tempcmd[char+1]
  268.         if next == ' ' or next == '\n':
  269.             char=0
  270.             line = line + 1
  271.         else:
  272.             char=char+1
  273.     else:
  274.         break
  275. print('End of program.')
  276. linecache.clearcache()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement