prompt.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import sys
  2. import readline
  3. import termcolor
  4. from sccd.action_lang.dynamic.memory import *
  5. from sccd.action_lang.parser.text import *
  6. from lark.exceptions import *
  7. if __name__ == "__main__":
  8. scope = Scope("interactive", parent=None)
  9. memory = Memory()
  10. memory.push_frame(scope)
  11. readline.set_history_length(1000)
  12. while True:
  13. try:
  14. line = input("> ")
  15. stmt = parse_block(line)
  16. stmt.init_stmt(scope)
  17. print_debug(termcolor.colored(str(stmt), 'yellow'))
  18. # Grow current stack frame if necessary
  19. diff = scope.size() - len(memory.current_frame().storage)
  20. if diff > 0:
  21. memory.current_frame().storage.extend([None]*diff)
  22. if isinstance(stmt, ExpressionStatement):
  23. expr_type = stmt.expr.init_expr(scope) # expr already initialized but init_expr should be idempotent
  24. val = stmt.expr.eval(memory)
  25. print("%s: %s" % (str(val), str(expr_type)))
  26. else:
  27. stmt.exec(memory)
  28. except (UnexpectedToken, UnexpectedCharacters) as e:
  29. print(" " + " "*e.column + "^")
  30. print(e)
  31. except (LarkError, ModelError, SCCDRuntimeException) as e:
  32. print(e)
  33. except KeyboardInterrupt:
  34. print()
  35. exit()