def substituted_expression(e, env): """ Return an expression with all freely occurring identifiers in E replaced by their corresponding value in ENV. """ from pltools import Environment subst = Environment(env) rewriter = VariableSubstitute(subst) return rewriter.rewrite(e)
def __init__(self, globals=None, tvsupply=None): # Record the global Python namespace (if any) in which code is # defined. This maps identifiers to values; by convention # Copperhead objects have a 'cu_type' slot that we will # use for typing information. # # This dictionary should never be modified. self.globals = globals or dict() # Supply of unique type variable names: #tv0, #tv1, #tv2, ... # They're known to be unique because they are illegal identifiers. self._tvsupply = tvsupply or name_supply(['#tv'], drop_zero=False) # The typings environment maps local identifiers to their # corresponding types. self.typings = Environment() # Type variables associated with formal parameters, both in # lambdas and procedures, are required to be monomorphic. This # set records all introduced type variables that are known to be # monomorphic. Since we make all internal variables unique, we # only need a single set for this, rather than a hierarchical # environment structure as with self.typings. self.monomorphs = set() # During inference, we accumulate a set of freely occurring # identifiers. This set contains AST nodes, rather than names. # Thus, multiple occurrences of a given name (e.g., 'x') may be # found in this set if they occurred at separate source # locations. For example, the expression 'x+x' will introduce # two free occurrences, not one. self.free_occurrences = set() # The inference system builds up a set of assumptions about the # types of identifiers occurring outside the current compilation # unit and which have no known 'cu_type' attribute. # This table provides a mapping from type variables created for # free occurrences to the AST node at which they occur. self.assumptions = dict()
def closure_conversion(ast, M): 'Perform closure conversion' env = Environment(M.globals, __builtin__.__dict__) return Front.closure_conversion(ast, env)
def free_variables(e, env={}): 'Generates all freely occurring identifiers in E not bound in ENV.' from pltools import Environment return FreeVariables(Environment(env)).visit(e)