def visitName(self, node, ctype=None): if isinstance(node.ctx, ast.Param): if node.id not in self.argtypes: raise CTypeError( node.id, 'function %s() requires argument %r' % (self.func_name, node.id)) ctype = self.argtypes[node.id] return cast.CName(node.id, ast.Param(), ctype, **n(node)) elif isinstance(node.ctx, ast.Load): try: ctype = self.scope(node.id) except NameError as err: raise cast.CError(node, NameError, err.args[0]) return cast.CName(node.id, ast.Load(), ctype, **n(node)) elif isinstance(node.ctx, ast.Store): assert type is not None if node.id in self.locls: ectype = self.locls[node.id] try: greatest_common_type(ctype, ectype) except: # Raise a custom exception if the types are not compatible raise ctype = ectype self.locls[node.id] = ctype return cast.CName(node.id, ast.Store(), ctype, **n(node)) else: assert False
def visitName(self, node, ctype=None): if isinstance(node.ctx, ast.Param): if node.id not in self.argtypes: raise CTypeError(node.id, 'function %s() requires argument %r' % (self.func_name, node.id)) ctype = self.argtypes[node.id] return cast.CName(node.id, ast.Param(), ctype, **n(node)) elif isinstance(node.ctx, ast.Load): try: ctype = self.scope(node.id) except NameError as err: raise cast.CError(node, NameError, err.args[0]) return cast.CName(node.id, ast.Load(), ctype, **n(node)) elif isinstance(node.ctx, ast.Store): assert type is not None if node.id in self.locls: ectype = self.locls[node.id] try: greatest_common_type(ctype, ectype) except: # Raise a custom exception if the types are not compatible raise ctype = ectype self.locls[node.id] = ctype return cast.CName(node.id, ast.Store(), ctype, **n(node)) else: assert False
def visitTuple(self, node): elts = list(self.visit_list(node.elts)) ltypes = [elt.ctype for elt in elts] ctype = greatest_common_type(ltypes) return cast.CList(elts, node.ctx, cList(ctype))
def visitIfExp(self, node, ctype=None): test = self.visit(node.test) body = self.visit(node.body) orelse = self.visit(node.orelse) ctype = greatest_common_type(body.ctype, orelse.ctype) return cast.CIfExp(test, body, orelse, ctype)
def visitFunctionDef(self, node): #'name', 'args', 'body', 'decorator_list' args = self.visit(node.args) body = list(self.visit_list(node.body)) return_types = returns(body) if len(return_types) == 0: return_type = None else: return_type = greatest_common_type(return_types) return cast.CFunctionDef(node.name, args, body, [], return_type)
def visitBinOp(self, node): left = self.visit(node.left) op = node.op right = self.visit(node.right) ctype = greatest_common_type(left.ctype, right.ctype) return cast.CBinOp(left, op, right, ctype)