Interpreter Example
import operator
def execute(cmd_seq, stack):
cmd = cmd_seq[0]
if isinstance(cmd, int):
stack.insert(0, cmd)
cmd_seq.pop(0)
elif cmd in ['add', 'sub']:
if not len(stack) >= 2:
raise Irreducible
n1 = stack.pop(0)
if not isinstance(n1, int):
raise Irreducible
n2 = stack.pop(0)
ans = int(operator.__dict__[cmd](n2, n1))
- Consider input:
[5, 2, 'add']