PYTHON   19

interpreter.py

Guest on 25th July 2021 04:51:49 PM

  1. # interpreter.py
  2.  
  3. """Interpreter for a mini-assignment language with variables and loops.
  4.   The main storage structure is the namespace,  ns :
  5. """
  6. ns = {}  # a global variable that holds the program's variable names
  7.          # It is a Python dictionary.  For example, if
  8.          #   ns = {'x': 2, 'y': [0,0,0]},
  9.          # then variable  x  has integer value 2
  10.          # and variable  y  names an array of length 3 that holds 3 zeros.
  11.  
  12. def printNamespace() :
  13.     """prints the current value of the global namespace"""
  14.     print ns
  15.  
  16.  
  17. ####
  18. """The interpreter interprets the meaning of this collection of operator trees:
  19.    CLIST ::=  [ CTREE+ ]
  20.               where  CTREE+  means  one or more CTREEs
  21.    CTREE ::=  ["=", VAR, ETREE]  |  ["print", VAR]  |  ["while", ETREE, CLIST]
  22.               |  ["if", ETREE, CLIST, CLIST]  
  23.    ETREE ::=  ["num", NUMERAL]  |  VTREE   |  ["newarray", ETREE]
  24.               |  [OP, ETREE1, ETREE2]
  25.               where OP is either "+" or "-" or "*"
  26.    VTREE ::=  ["var", VAR]  |  ["index", VAR, ETREE]
  27. """
  28.  
  29.  
  30. def interpretCLIST(p) :
  31.     """pre: p  is a program represented as a  CLIST
  32.       post:  ns  holds all the updates commanded by program  p
  33.    """
  34.     for command in p :
  35.         interpretCTREE(command)
  36.  
  37.  
  38. def interpretCTREE(c) :
  39.     """pre: c  is a command represented as a CTREE
  40.       post:  ns  holds all the updates commanded by  c
  41.    """
  42.     operator = c[0]
  43.     if operator == "=" :   # assignment command
  44.         vtree = c[1]
  45.         exprval = interpretETREE(c[2])
  46.         if vtree[0] == "var" :
  47.             ns[vtree[1]] = exprval   # do the assignment
  48.         else :
  49.             pass  # FIXME
  50.     elif operator == "print" :   # print command
  51.         exprval = interpretETREE(c[1])
  52.         print exprval
  53.     elif operator == "while" :   # while command
  54.         expr = c[1]
  55.         body = c[2]
  56.         while (interpretETREE(expr) != 0) :
  57.             interpretCLIST(body)
  58.     else :   # error
  59.         crash("invalid command")
  60.  
  61. def interpretETREE(e) :
  62.     """pre: e  is an expression represented as an ETREE:
  63.       post:  ans  holds the numerical value of  e
  64.    """
  65.     operation = e[0]
  66.     if e[0] == "num" :  # a numeral
  67.         ans = int(e[1])
  68.     elif e[0] == "var" :
  69.         varname = e[1]
  70.         if varname in ns :   # see if varname is defined
  71.             ans = ns[varname]  # look up its value
  72.         else :
  73.             crash("variable name undefined")
  74.     else :   #  [op, e1, e2]
  75.         op = e[0]
  76.         ans1 = interpretETREE(e[1])
  77.         ans2 = interpretETREE(e[2])
  78.         if op == "+" :
  79.             ans = ans1 + ans2
  80.         elif op == "-" :
  81.             ans = ans1 - ans2
  82.         else :
  83.             crash("illegal arithmetic operator")
  84.     return ans
  85.  
  86.  
  87. def crash(message) :
  88.     """pre: message is a string
  89.       post: message is printed and interpreter stopped
  90.    """
  91.     print message + "! crash! core dump:"
  92.     printNamespace()
  93.     raise Exception   # stops the interpreter
  94.  
  95.  
  96. def interpretProgram(tree) :
  97.     """interprets a complete program tree
  98.       pre: tree is a  CLIST
  99.       post: final values are deposited in global var,  ns
  100.    """
  101.     global ns
  102.     ns = {}
  103.     interpretCLIST(tree)

Raw Paste


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