PYTHON   11

run.py

Guest on 25th July 2021 04:53:40 PM

  1.  
  2. """run.py  runs the entire system, calling the scanner, parser, and
  3.   interpreter
  4. """
  5. import scanner
  6. import parse
  7. import interpreter
  8. import sys, traceback  # libraries of system routines (for exception handling)
  9.  
  10. # collect text here
  11. print "Type program here, terminated by  !  as the first char on the line:"
  12. textlines = ""
  13. finished = False
  14. while not finished :
  15.     # invariant:  textlines holds all input text entered so far
  16.     text = raw_input("")
  17.     if len(text) > 0 and text[0] == "!" :
  18.         finished = True
  19.     else :
  20.         textlines = textlines + " " + text
  21.  
  22. try:
  23.     words = scanner.scan(textlines)
  24.     print "Input words:", words
  25.     print
  26.  
  27.     tree = parse.parse(words)
  28.     print "Operator tree:", tree
  29.     print
  30.  
  31.     interpreter.interpretProgram(tree)
  32.  
  33.     print "Interpretation complete.  Contents of namespace:"
  34.     interpreter.printNamespace()
  35. except:
  36.     print
  37.     print "The interpreter has crashed due to a programming error."
  38.     print "Here is the execution trace, which lists the calls that led to the crash:"
  39.     print
  40.     traceback.print_exc(file=sys.stdout)
  41.  
  42. print
  43. raw_input("Press Enter key to terminate the session")

Raw Paste


Login or Register to edit or fork this paste. It's free.