'''
This is an interpreter for the Pancake Stack esolang. To use it, paste your program into the
stdin, followed by any number of ~s on one line. Regular stdin is after the ~s.
'''
import re
import sys
def push(match):
global stack
stack.append(len(match.group(1)))
def eat():
global stack
stack.pop()
def combine_pancakes():
global stack
a, b = stack.pop(), stack.pop()
stack.append(a + b)
def give_pancake():
global stack
stack.append(int(input()))
def give_hotcake():
global stack
stack.append(ord(sys.stdin.read(1)))
def show_pancake():
global stack
print(chr(stack[-1]), end='')
def take_from_pancakes():
global stack
a, b = stack.pop(), stack.pop()
stack.append(a - b)
def flip_pancakes():
global stack
stack[-1], stack[-2] = stack[-2], stack[-1]
def put_another_pancake():
global stack
stack.append(stack[-1])
def label(match):
global labels
global stack
label = match.group(1)
if label in labels:
return
labels[label] = stack[-1] - 1
def not_tasty(match):
global labels
global stack
global line
if not stack[-1]:
line = labels[match.group(1)]
def is_tasty():
global labels
global stack
global line
if stack[-1]:
line = labels[match.group(1)]
def put_syrup():
global stack
for i in range(len(stack)): stack[i] += 1
def put_butter():
global stack
stack[-1] += 1
def remove_syrup():
global stack
for i in range(len(stack)): stack[i] -= 1
def remove_butter():
global stack
stack[-1] -= 1
def eat_all():
global stack
sys.exit(0)
program = []
line = input()
while '~' not in line:
program.append(line)
line = input()
commands = {
r'Put this ([^ ]*?) pancake on top!': push,
r'Eat the pancake on top!': eat,
r'Put the top pancakes together!': combine_pancakes,
r'Give me a pancake!': give_pancake,
r'How about a hotcake?': give_hotcake,
r'Show me a pancake!': show_pancake,
r'Take from the top pancakes!': take_from_pancakes,
r'Flip the pancakes on top!': flip_pancakes,
r'Put another pancake on top!': put_another_pancake,
r'\[(.*)\]': label,
r'If the pancake isn\'t tasty, go over to "(.*)".': not_tasty,
r'If the pancake is tasty, go over to "(.*)".': is_tasty,
r'Put syrup on the pancakes!': put_syrup,
r'Put butter on the pancakes': put_butter,
r'Take off the syrup!': remove_syrup,
r'Take off the butter!': remove_butter,
r'Eat all of the pancakes!': eat_all
}
labels = dict()
line = 0
stack = []
while program[line] != 'Eat all of the pancakes!':
#print("%i:%s:%s"%(line,program[line],stack)) #for debug purposes
for command in commands.keys():
match = re.match(command, program[line])
if match:
try: commands[command](match)
except TypeError: commands[command]()
break
else:
print('Line %i is not a valid command'%line, file=sys.stderr)
sys.exit(1)
line += 1
JycnClRoaXMgaXMgYW4gaW50ZXJwcmV0ZXIgZm9yIHRoZSBQYW5jYWtlIFN0YWNrIGVzb2xhbmcuIFRvIHVzZSBpdCwgcGFzdGUgeW91ciBwcm9ncmFtIGludG8gdGhlCnN0ZGluLCBmb2xsb3dlZCBieSBhbnkgbnVtYmVyIG9mIH5zIG9uIG9uZSBsaW5lLiBSZWd1bGFyIHN0ZGluIGlzIGFmdGVyIHRoZSB+cy4KJycnCmltcG9ydCByZQppbXBvcnQgc3lzCgpkZWYgcHVzaChtYXRjaCk6CiAgICBnbG9iYWwgc3RhY2sKICAgIHN0YWNrLmFwcGVuZChsZW4obWF0Y2guZ3JvdXAoMSkpKQpkZWYgZWF0KCk6CiAgICBnbG9iYWwgc3RhY2sKICAgIHN0YWNrLnBvcCgpCmRlZiBjb21iaW5lX3BhbmNha2VzKCk6CiAgICBnbG9iYWwgc3RhY2sKICAgIGEsIGIgPSBzdGFjay5wb3AoKSwgc3RhY2sucG9wKCkKICAgIHN0YWNrLmFwcGVuZChhICsgYikKZGVmIGdpdmVfcGFuY2FrZSgpOgogICAgZ2xvYmFsIHN0YWNrCiAgICBzdGFjay5hcHBlbmQoaW50KGlucHV0KCkpKQpkZWYgZ2l2ZV9ob3RjYWtlKCk6CiAgICBnbG9iYWwgc3RhY2sKICAgIHN0YWNrLmFwcGVuZChvcmQoc3lzLnN0ZGluLnJlYWQoMSkpKQpkZWYgc2hvd19wYW5jYWtlKCk6CiAgICBnbG9iYWwgc3RhY2sKICAgIHByaW50KGNocihzdGFja1stMV0pLCBlbmQ9JycpCmRlZiB0YWtlX2Zyb21fcGFuY2FrZXMoKToKICAgIGdsb2JhbCBzdGFjawogICAgYSwgYiA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