def visit_BinOp(self, node): left = self.visit(node.left) right = self.visit(node.right) if isstr(node.left): left = "pythonic::types::str({})".format(left) elif isstr(node.right): right = "pythonic::types::str({})".format(right) return operator_to_lambda[type(node.op)](left, right)
def visit_BinOp(self, node): left = self.visit(node.left) right = self.visit(node.right) # special case pow for positive integral exponent if ispowi(node): right = 'std::integral_constant<long, {}>{{}}'.format( node.right.value) if isstr(node.left): left = "pythonic::types::str({})".format(left) elif isstr(node.right): right = "pythonic::types::str({})".format(right) return operator_to_lambda[type(node.op)](left, right)
def visit_documented_node(self, key, node): if node.body: first_stmt = node.body[0] if isinstance(first_stmt, ast.Expr): if isstr(first_stmt.value): self.update = True docstring = first_stmt.value.value self.docstrings[key] = docstring node.body.pop(0) return self.generic_visit(node)
def visit_Module(self, node): err = ("Top level statements can only be assignments, strings," "functions, comments, or imports") WhiteList = ast.FunctionDef, ast.Import, ast.ImportFrom, ast.Assign for n in node.body: if isinstance(n, ast.Expr) and isstr(n.value): continue if isinstance(n, WhiteList): continue raise PythranSyntaxError(err, n) self.generic_visit(node)
def visit_BinOp(self, node): # replace "str" % (...) by builtins.str.__mod__(...) # the reason why we do this is that % formatting is handled by # a third party library that's relatively costly to load, so using a # function name instead of an operator overload makes it possible to # load it only when needed. The drawback is that % formatting is no # longer supported when lhs is not a literal self.generic_visit(node) if isinstance(node.op, ast.Mod) and isstr(node.left): self.update = True return ast.Call( ast.Attribute( ast.Attribute(ast.Name('builtins', ast.Load(), None, None), 'str', ast.Load()), '__mod__', ast.Load()), [node.left, node.right], []) return node
def visit_Module(self, node): err = ("Top level statements can only be assignments, strings," "functions, comments, or imports") WhiteList = ast.FunctionDef, ast.Import, ast.ImportFrom, ast.Assign for n in node.body: if isinstance(n, ast.Expr) and isstr(n.value): continue if isinstance(n, WhiteList): continue raise PythranSyntaxError(err, n) ancestors = beniget.Ancestors() ancestors.visit(node) duc = ExtendedDefUseChains(ancestors) duc.visit(node) for k, v in duc.unbounds.items(): raise PythranSyntaxError("Unbound identifier {}".format(k), v[0]) self.generic_visit(node)
def visit_Subscript(self, node): value = self.visit(node.value) # we cannot overload the [] operator in that case if isstr(node.value): value = 'pythonic::types::str({})'.format(value) # positive static index case if (isnum(node.slice) and (node.slice.value >= 0) and isinstance(node.slice.value, int)): return "std::get<{0}>({1})".format(node.slice.value, value) # positive indexing case elif self.all_positive(node.slice): slice_ = self.visit(node.slice) return "{1}.fast({0})".format(slice_, value) # extended slice case elif isextslice(node.slice): slices = [self.visit(elt) for elt in node.slice.elts] return "{1}({0})".format(','.join(slices), value) # standard case else: slice_ = self.visit(node.slice) return "{1}[{0}]".format(slice_, value)
def isompdirective(self, node): return isstr(node) and node.value.startswith("omp ")
def visit_Expr(self, node): 'Remove other top-level strings' if isstr(node.value): return None return node