RE: The Programming Language Challenge 12-15-2015, 02:28 AM
#4
Well, I've had this lying around for awhile, but I was too lazy to post until now. 

Code:
from __future__ import print_function
def tokenize(code):
return code.split()
def evaluate(code, stack, operators):
tokens = tokenize(code)
for token in tokens:
if token in operators:
if token in ["+", "-", "*", "/"]:
while len(stack) < 2:
stack.append(0)
ret = operators[token](stack)
if ret != None:
stack.append(ret)
else:
# push
stack.append(int(token))
stack = []
operators = {
"+": lambda stack: stack.pop() + stack.pop(),
"-": lambda stack: stack.pop() - stack.pop(),
"*": lambda stack: stack.pop() * stack.pop(),
"/": lambda stack: stack.pop() / stack.pop(),
"|": lambda stack: print(stack[-1]),
"=": lambda stack: int(stack.pop() == stack.pop()),
"!": lambda stack: int(stack.pop() != stack.pop()),
">": lambda stack: int(stack.pop() > stack.pop()),
"<": lambda stack: int(stack.pop() < stack.pop())
}
print("CTRL-D or CTRL-C to close REPL")
while True:
try:
code = raw_input(">>> ")
evaluate(code, stack, operators)
except KeyboardInterrupt, EOFError:
break
print("\nthe stack is: " + repr(stack))





![[+]](https://sinister.ly/images/modern/collapse_collapsed.png)