Login Register


The Programming Language Challenge filter_list
Author
Message
RE: The Programming Language Challenge #4
Well, I've had this lying around for awhile, but I was too lazy to post until now. xD
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))

Reply





Messages In This Thread
The Programming Language Challenge - by m0dem - 11-26-2015, 09:49 PM
RE: The Programming Language Challenge - by Rick - 11-30-2015, 09:27 AM
RE: The Programming Language Challenge - by m0dem - 11-30-2015, 04:07 PM
RE: The Programming Language Challenge - by m0dem - 12-15-2015, 02:28 AM



Users browsing this thread: 1 Guest(s)