fork(14) download
  1. '''
  2. This is an interpreter for the Pancake Stack esolang. To use it, paste your program into the
  3. stdin, followed by any number of ~s on one line. Regular stdin is after the ~s.
  4. '''
  5. import re
  6. import sys
  7.  
  8. def push(match):
  9. global stack
  10. stack.append(len(match.group(1)))
  11. def eat():
  12. global stack
  13. stack.pop()
  14. def combine_pancakes():
  15. global stack
  16. a, b = stack.pop(), stack.pop()
  17. stack.append(a + b)
  18. def give_pancake():
  19. global stack
  20. stack.append(int(input()))
  21. def give_hotcake():
  22. global stack
  23. stack.append(ord(sys.stdin.read(1)))
  24. def show_pancake():
  25. global stack
  26. print(chr(stack[-1]), end='')
  27. def take_from_pancakes():
  28. global stack
  29. a, b = stack.pop(), stack.pop()
  30. stack.append(a - b)
  31. def flip_pancakes():
  32. global stack
  33. stack[-1], stack[-2] = stack[-2], stack[-1]
  34. def put_another_pancake():
  35. global stack
  36. stack.append(stack[-1])
  37. def label(match):
  38. global labels
  39. global stack
  40. label = match.group(1)
  41. if label in labels:
  42. return
  43. labels[label] = stack[-1] - 1
  44. def not_tasty(match):
  45. global labels
  46. global stack
  47. global line
  48. if not stack[-1]:
  49. line = labels[match.group(1)]
  50. def is_tasty():
  51. global labels
  52. global stack
  53. global line
  54. if stack[-1]:
  55. line = labels[match.group(1)]
  56. def put_syrup():
  57. global stack
  58. for i in range(len(stack)): stack[i] += 1
  59. def put_butter():
  60. global stack
  61. stack[-1] += 1
  62. def remove_syrup():
  63. global stack
  64. for i in range(len(stack)): stack[i] -= 1
  65. def remove_butter():
  66. global stack
  67. stack[-1] -= 1
  68. def eat_all():
  69. global stack
  70. sys.exit(0)
  71.  
  72. program = []
  73. line = input()
  74. while '~' not in line:
  75. program.append(line)
  76. line = input()
  77.  
  78. commands = {
  79. r'Put this ([^ ]*?) pancake on top!': push,
  80. r'Eat the pancake on top!': eat,
  81. r'Put the top pancakes together!': combine_pancakes,
  82. r'Give me a pancake!': give_pancake,
  83. r'How about a hotcake?': give_hotcake,
  84. r'Show me a pancake!': show_pancake,
  85. r'Take from the top pancakes!': take_from_pancakes,
  86. r'Flip the pancakes on top!': flip_pancakes,
  87. r'Put another pancake on top!': put_another_pancake,
  88. r'\[(.*)\]': label,
  89. r'If the pancake isn\'t tasty, go over to "(.*)".': not_tasty,
  90. r'If the pancake is tasty, go over to "(.*)".': is_tasty,
  91. r'Put syrup on the pancakes!': put_syrup,
  92. r'Put butter on the pancakes': put_butter,
  93. r'Take off the syrup!': remove_syrup,
  94. r'Take off the butter!': remove_butter,
  95. r'Eat all of the pancakes!': eat_all
  96. }
  97. labels = dict()
  98. line = 0
  99. stack = []
  100. while program[line] != 'Eat all of the pancakes!':
  101. #print("%i:%s:%s"%(line,program[line],stack)) #for debug purposes
  102. for command in commands.keys():
  103. match = re.match(command, program[line])
  104. if match:
  105. try: commands[command](match)
  106. except TypeError: commands[command]()
  107. break
  108. else:
  109. print('Line %i is not a valid command'%line, file=sys.stderr)
  110. sys.exit(1)
  111. line += 1
Runtime error #stdin #stdout #stderr 0.11s 10088KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 73, in <module>
EOFError: EOF when reading a line