[an error occurred while processing this directive]

cs150: Notes 32



class CPrimitiveType(CType): 
   def __init__(self, s): 
       self._name = s 
   def __str__(self): 
       return self._name 
   def isPrimitiveType(self): 
       return True 
   def matches(self, other): 
      return other.isPrimitiveType() \
             and ________________________
class CProcedureType(CType): 
   def __init__(self, args, rettype): 
      self._args = args 
      self._rettype = rettype 
   def __str__(self): 
      return "(" + str(self._args) + " -> " \
                 + str(self._rettype) + ")" 
   def isProcedureType(self): return True 
   def getReturnType(self): return self._rettype 
   def getParameters(self): return self._args 
   def matches(self, other): 

      return ________________________________

         and ___________________________________________

         and ___________________________________________
def typecheck(expr, env): 
   if isPrimitive(expr): 
      return typePrimitive(expr) 
   elif isConditional(expr): 
      return typeConditional(expr, env) 
   elif isLambda(expr): 
      return typeLambda(expr, env) 
   elif isDefinition(expr): 
      typeDefinition(expr, env) 
   elif isName(expr): 
      return typeName(expr, env) 
   elif isApplication(expr): 
      return typeApplication(expr, env) 
   else: evalError ("Unknown expression: " + str(expr)) 
class Environment:
    # Store a [type, value] pair for each variable.
    def addVariable(self, name, typ, value):
        self._frame[name] = (typ, value)
    def lookupPlace(self, name):
        if self._frame.has_key(name): return self._frame[name]
        elif (self._parent): return self._parent.lookupPlace(name)
        else: return None
    def lookupVariableType(self, name):
        place = _____________________

        if place: return ______________
        else: return CErrorType("Name not found")
    def lookupVariable(self, name):
        return self.lookupPlace(name)[1]
def typeDefinition(expr, env):
    assert isDefinition(expr)
    if len(expr) != 5:
        evalError ("Bad definition: %s" % str(expr))
    name = expr[1]
    if isinstance(name, str):
        if expr[2] != ':':
            evalError ("Definition missing type: %s" % str(expr))        
        typ = CType.fromParsed(expr[3])

        etyp = __________________________
        if not typ.matches(etyp):
            evalError("Mistyped definition: ..." % (name, typ, etyp))
    elif isinstance(name, list):
        evalError ("Procedure definition syntax not implemented")
    else: evalError ("Bad definition: %s" % str(expr))
def typeLambda(expr, env): # error checking code removed
    newenv = Environment(env)
    params = expr[1]
    paramnames = []
    paramtypes = []
    for i in range(0, len(params) / 3):
        name = params[i*3]
        typ = CType.fromParsed(params[(i*3)+2])


    resulttype = ___________(expr[2], newenv)
    return CProcedureType(CProductType(paramtypes), resulttype)

cs1120: Computer Science
University of Virginia
Using these Materials