fork 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. The difference between this and the last version is that this version removed all the
  6. unnecessary `global` statements.
  7. '''
  8. import re
  9. import sys
  10.  
  11. def push(match):
  12. stack.append(len(match.group(1)))
  13. def eat():
  14. stack.pop()
  15. def combine_pancakes():
  16. a, b = stack.pop(), stack.pop()
  17. stack.append(a + b)
  18. def give_pancake():
  19. stack.append(int(input()))
  20. def give_hotcake():
  21. stack.append(ord(sys.stdin.read(1)))
  22. def show_pancake():
  23. print(chr(stack[-1]), end='')
  24. def take_from_pancakes():
  25. a, b = stack.pop(), stack.pop()
  26. stack.append(a - b)
  27. def flip_pancakes():
  28. stack[-1], stack[-2] = stack[-2], stack[-1]
  29. def put_another_pancake():
  30. stack.append(stack[-1])
  31. def label(match):
  32. label = match.group(1)
  33. if label in labels:
  34. return
  35. labels[label] = stack[-1] - 2
  36. def not_tasty(match):
  37. if not stack[-1]:
  38. line = labels[match.group(1)]
  39. def is_tasty():
  40. if stack[-1]:
  41. line = labels[match.group(1)]
  42. def put_syrup():
  43. for i in range(len(stack)): stack[i] += 1
  44. def put_butter():
  45. stack[-1] += 1
  46. def remove_syrup():
  47. for i in range(len(stack)): stack[i] -= 1
  48. def remove_butter():
  49. stack[-1] -= 1
  50. def eat_all():
  51. sys.exit(0)
  52.  
  53. program = []
  54. line = input()
  55. while '~' not in line:
  56. program.append(line)
  57. line = input()
  58.  
  59. commands = {
  60. r'Put this ([^ ]*?) pancake on top!': push,
  61. r'Eat the pancake on top!': eat,
  62. r'Put the top pancakes together!': combine_pancakes,
  63. r'Give me a pancake!': give_pancake,
  64. r'How about a hotcake?': give_hotcake,
  65. r'Show me a pancake!': show_pancake,
  66. r'Take from the top pancakes!': take_from_pancakes,
  67. r'Flip the pancakes on top!': flip_pancakes,
  68. r'Put another pancake on top!': put_another_pancake,
  69. r'\[(.*)\]': label,
  70. r'If the pancake isn\'t tasty, go over to "(.*)".': not_tasty,
  71. r'If the pancake is tasty, go over to "(.*)".': is_tasty,
  72. r'Put syrup on the pancakes!': put_syrup,
  73. r'Put butter on the pancakes': put_butter,
  74. r'Take off the syrup!': remove_syrup,
  75. r'Take off the butter!': remove_butter,
  76. r'Eat all of the pancakes!': eat_all
  77. }
  78. labels = dict()
  79. line = 0
  80. stack = []
  81. while program[line] != 'Eat all of the pancakes!':
  82. #print("%i:%s:%s"%(line,program[line],stack)) #for debug purposes
  83. for command in commands.keys():
  84. match = re.match(command, program[line])
  85. if match:
  86. try: commands[command](match)
  87. except TypeError: commands[command]()
  88. break
  89. else:
  90. print('Line %i is not a valid command'%line, file=sys.stderr)
  91. sys.exit(1)
  92. line += 1
Runtime error #stdin #stdout #stderr 0.04s 9488KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Traceback (most recent call last):
  File "./prog.py", line 54, in <module>
EOFError: EOF when reading a line