Guest User

Untitled

a guest
Apr 26th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. import inspect
  2.  
  3. # Note that this is just a proxy to make the tests pass
  4. class Command(list):
  5. def matches(self, expression):
  6. pass
  7.  
  8. class Interpreter:
  9.  
  10. def __init__(self, command=None):
  11. self.command = command if command else Command()
  12. self.vars = {}
  13.  
  14. def interpret(self, expression):
  15. try:
  16. return eval(expression, {}, self.vars)
  17. except NameError:
  18. if self.command.matches(expression):
  19. parameter_name = inspect.getargspec(self.command.execute)[0][0]
  20.  
  21. if parameter_name == 'variables':
  22. return self.command.execute(self.vars)
  23. else:
  24. return self.command.execute([self.command, ])
  25. else:
  26. return 'null'
  27. except SyntaxError:
  28. l_value, r_value = expression.split('=')
  29.  
  30. try:
  31. self.vars[l_value] = int(r_value)
  32. except ValueError:
  33. self.vars[l_value] = self.interpret(r_value)
Add Comment
Please, Sign In to add comment