import sys,os
from math import *
stack = []
pop = stack.pop
push = stack.append
length = stack.__len__
def output(n):
print n
def isnum(n):
for x in n:
if not x in "0123456789.-":
return 0
return 1
def replace(a, b):
if length() < max([a,b]):
"Stack Over Range!"
else:
stack[-a], stack[-b] = stack[-b], stack[-a]
def dup():
a = pop()
push(a)
push(a)
def sum():
r = 0
for x in stack:
r += x
while stack:
n = pop()
push(r)
def iftrue():
if pop():
return
else:
n = pop()
def ifelse(): # True, False, Test
if pop():
n = pop()
else:
replace(1,2)
n = pop()
syntax={
"Q":sys.exit,
".":(lambda :output(pop())),#
"P":(lambda :output(stack[-1])),#Print TOS
"L":(lambda :output(stack)),#List
"S":(lambda :replace(1,2)), #Swap
"D":dup,
"SUM":sum,
"?":iftrue,
"??":ifelse
}
opr=[
"+","-","*","/",
"<",">","<=",">=",
"^","**","%"
]
tokenList = []
udic = {}
def analyze():
global tokenList
while tokenList:
t = tokenList[0]
tokenList = tokenList[1:]
if syntax.has_key(t):
syntax[t]()
elif t in opr:
if length() < 2:
print "Stack Over Range!"
else:
b, a = pop(), pop()
push(eval(str(a) + t + str(b)))
elif t in ["=","=="]:
b, a = pop(), pop()
push(eval(a == b))
elif t[-1] == "=":
udic[t[:-1]]=pop()
elif udic.has_key(t):
push(udic[t])
elif t == "!":
tokenList = (pop().split()) + tokenList
elif isnum(t):
push(float(t))
elif t[0] == "@":
os.system(t[1:])
elif t[0] == "_":
f = t[1:]
push(eval(f+"("+str(pop())+")"))
elif t[0] == "$":
r = t.split("$")
replace(int(r[1]),int(r[2]))
else:
push(t)
# Main Loop
while 1:
if not tokenList:
instr = raw_input("%03.3d>>>" % length())
if instr[0] == '"':
push(instr[1:] + " ")
continue
else:
tokenList += instr.split()
analyze()