Advertisement
computermaster1248

Number Factory Interpreter

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