import re, string, sys, pickle
# Functions not included herein should be copied from the previous listings.

NULL = 0
Inputbuf = []

NCODE = 100
NSTACK = 100
Code = [] 
Stack = [0] * NSTACK
Pc = 0
Stackp = 0

class Tree:
	pass
	
class CodeItem:
	pass
	
def initcode():
	global Code
	for i in range(0, NCODE):
		t = CodeItem()
		Code.append(t)

	
def pushop():
	global Stack, Stackp, Code, Pc
	
	Stack[Stackp] = Code[Pc].number
	Stackp = Stackp + 1
	Pc = Pc + 1
	
	
def addop():
	global Stack, Stackp, Code, Pc
	
	Stackp = Stackp - 1
	right = Stack[Stackp]
	Stackp = Stackp - 1
	left = Stack[Stackp]
	Stack[Stackp] = left + right
	Stackp = Stackp + 1

# define subop, mulop and divop here.
	
	
def generate(codep, ptree):
	try:
		# if the field 'number' is not present, the
	        # following line generates an exception.

		n = ptree.number 
		Code[codep].op = pushop
		codep = codep + 1
		Code[codep].number = n
		codep = codep + 1
		return codep
	except:
		if (ptree.op == '+'):
			codep = generate(codep, ptree.left)
			codep = generate(codep, ptree.right)
			Code[codep].op = addop
			codep = codep + 1
			return codep
			
		# elif (ptree.op == '-'): We will write the code
                # generation actions for '-', '*', '/' here.
               
			
def eval(ptree): # Generate the instructions, then execute them
	global Pc, Stackp, Code, Stack
	Pc = generate(0, ptree)
	Code[Pc].op = NULL
	
	Stackp = 0
	Pc = 0
	while Code[Pc].op != NULL:
		tmp = Pc
		Pc = Pc + 1
		Code[tmp].op()
	return Stack[0]
	
	
def eval2():    # Directly execute the loaded code       
	global Pc, Stackp, Code, Stack
	
	Stackp = 0
	Pc = 0
	while Code[Pc].op != NULL:
		tmp = Pc
		Pc = Pc + 1
		Code[tmp].op()
	return Stack[0]
	
	
def main():
	global Inputbuf, Code
	
	try:
		f = open(sys.argv[1])
		Code = pickle.load(f)
		f.close()
		result = eval2()
		print 'result is:', result
		return result
	except:
		print 'Not opening code file, reading from k/b'
		initcode()
		Inputbuf = raw_input()
		ptree = expression()
		result = eval(ptree)
		f = open('code.out', 'w')
		pickle.dump(Code, f)
		print 'Code dumped in a file called dat'
		print 'result is:', result
		return result
		

if __name__=='__main__':
	result = main()