#!/usr/bin/env python3
# Amir Refai

import readline
from termcolor import colored

def calculate(arg):
	stack = []

	tokens = arg.split()

	for token in tokens:
		try:
			stack.append(int(token))
		except ValueError:
			val2 = stack.pop()
			val1 = stack.pop()
			if token == '+':
				result = val1 + val2
			elif token == '-':
				result = val1 - val2
			elif token == '^':
				result = val1**val2
			elif token == '%':
				result = val1 % val2
			stack.append(result)

	if len(stack) > 1:
		raise ValueError("Too many arguments on the stack")
	return stack[0]

def main():
	while True:
		try:
			text_in = input("rpn calc> ") 
			result = calculate(text_in)
			
			string = ""
			for token in text_in.split():
				if token == "+" or token == "-" or token == "^" or token=="%":
					string += colored(token, 'magenta') + " "
				else:
					string += colored(token, 'red') + " "

			print(string)
			print(result)
		except ValueError:
			pass

if __name__ == "__main__":
	main()
