# TokigunStudio Whitespace Implementation

def N():
 p=[0]
 while p[-1]<2:p+=[r()]
 return((p[1]-1)%3-1)*reduce(lambda x,y:x*2+y,p[2:-1],0)
def L():
 p=(0,)
 while p[-1]<2:p=p+(r(),)
 return p[1:-1]
def M(l):
 v=reduce(lambda a,b:a*2+b,l,0L)
 if 7&len(l)or v==0:return'label%d_%s'%(len(l)&3,hex(v)[2:-1])
 else:return j([`chr(v>>k&255)`[1:-1]for k in range(len(l)-8,-1,-8)])
from sys import*;from StringIO import*;X='push,dup,copy,swap,pop,slide,add,sub,mul,div,mod,store,retrieve,call,jmp,jz,jn,ret,halt,putchar,putint,getchar,getint'.split(',');Y='00,020,010,021,022,012,1000,1001,1002,1010,1011,110,111,201,202,210,211,212,222,1200,1201,1210,1211'.split(',');Z='10100000000002222000000';v=argv[1:];n,=len(v)and[v[0][0]=='-'and{'-c':1,'-d':2,'--':0}.get(v.pop(0),3)or 0]or[3];j=''.join;x=exit
if n>2 or[1,2,2][n]!=len(v):print'TokigunStudio Whitespace 0.3 Implementation\nby Kang Seonghoon <tokigun@gmail.com>\n\nUsage: python %s <file>\n   or: python %s [-c|-d] <infile> <outfile>\n\nno arg\texecute whitespace code.\n-c\tcompile whitespace assembly code.\n-d\tdecompile whitespace assembly code.\n'%(argv[0],argv[0]);x(0)
try:s=file(v[0],'r').read();f=n and file(v[1],'w');v=tuple(v)
except:print'cannot open file.';x(1)
if n==1:
 print'compile ' '%s to %s...'%v,;N=lambda n:n and('\t '[n>0]+N(abs(n)/2)[1:-1]+' ''\t'[n%2]+'\n')or'\n';L=lambda l:j([j([' \t'[ord(v)>>k&1]for k in range(7,-1,-1)])for v in l])+'\n'
 for l in s.splitlines():
  l=(';'in l and l[:l.find(';')]or l).split();len(l)and':'==l[0][-1]and f.write('\n  '+L(l.pop(0)[:-1]))
  if len(l):p=X.index(l[0].lower());p+1 and len(l)>(Z[p]>'0')and f.write(j([' \t\n'[int(k)]for k in Y[p]])+(Z[p]>'0'and(0,lambda a:N(int(a)),L)[int(Z[p])](l[1])or''))
 f.close();print'done.';x(0)
elif n:print'decompile %s to %s...'%v,
s=StringIO(j([{32:'0',9:'1',10:'2'}.get(ord(c),'')for c in s]));r=lambda:int(s.read(1));W=lambda h:h==s.read(len(h))or s.seek(i,0);c=[];p={}
while[None]!=c[-1:]:
 i=s.tell()
 if W('200'):l=L();p[l]=len(c);n and f.write(M(l)+':\n')
 else:v=reduce(lambda a,b:a or W(Y[b])and(b,(int,N,L)[int(Z[b])]()),range(23),0);c+=[v];n and v and f.write('  '+X[v[0]]+(Z[v[0]]>'0'and(' '+(0,str,M)[int(Z[v[0]])](v[1]))or'')+'\n')
if n:f.close();print'done.';x(0)
s=[];h={};q=[];u=s.append;U=lambda:len(s)and s.pop();i=k=0;c.pop()
while i<len(c)and k-18:
 k,v=c[i];i+=1
 if k==0:u(v)
 elif k==1:n=U();u(n);u(n)
 elif(k==2)&(v>0):u(s[-v])
 elif k==3:n=U();m=U();u(n);u(m)
 elif k==4:U()
 elif k==5:s[-v-1:-1]=[]
 elif k==6:u(U()+U())
 elif k==7:u(-U()+U())
 elif k==8:u(U()*U())
 elif k==9:n=U();u(U()//n)
 elif k==10:n=U();u(U()%n)
 elif k==11:n=U();h[U()]=n
 elif k==12:u(h.get(U(),0))
 elif k==13:q+=[i];i=p[v]
 elif k==14:i=p[v]
 elif k==15 and U()==0:i=p[v]
 elif k==16 and U()<0:i=p[v]
 elif k==17 and len(q):i=q.pop()
 elif k==19:stdout.write(chr(U()))
 elif k==20:stdout.write(str(U()))
 elif k==21:h[U()]=ord(stdin.read(1))
 elif k==22: 
  v=''
  while('0'+v[v[0:1]=='-':]).isdigit():v+=stdin.read(1)
  h[U()]=int(v[:-1]+'0')/10
