def preprocess_var_decls(tree): """Eliminate global variable declarations of the form S = Set() M = Map() and return a list of pairs of variables names and type names (i.e., 'Set' or 'Map'). """ assert isinstance(tree, P.Module) pat = P.Assign([P.Name(P.PatVar('_VAR'), P.Wildcard())], P.Call(P.Name(P.PatVar('_KIND'), P.Load()), [], [], None, None)) decls = OrderedSet() body = [] changed = False for stmt in tree.body: match = P.match(pat, stmt) if match is not None: var, kind = match['_VAR'], match['_KIND'] if kind not in ['Set', 'Map']: raise L.ProgramError( 'Unknown declaration initializer {}'.format(kind)) decls.add((var, kind)) changed = True else: body.append(stmt) if changed: tree = tree._replace(body=body) return tree, list(decls)
def visit_Assign(self, node): node = self.generic_visit(node) # Translate multiple assignments as a single statement # (e.g. "a = b = c") into sequential single assignments. if len(node.targets) > 1: stmts = [] values = list(node.targets) + [node.value] for lhs, rhs in reversed(list(pairs(values))): rhs_load = P.ContextSetter.run(rhs, P.Load) stmts.append(P.Assign([lhs], rhs_load)) node = stmts return node