def visit_Compare(self, node): def opkind_map(node): if isinstance(node, py_ast.In): return ir_ast.BinOpKind.IN elif isinstance(node, py_ast.NotEq): return ir_ast.BinOpKind.NOT_EQUAL else: raise Exception('Unsupported BinOp kind', node.ops) if isinstance(node.ops, list): if len(node.ops) == 1: if isinstance(node.comparators, list) and len(node.comparators) == 1: return ir_ast.BinOp( self.visit(node.left), opkind_map(node.ops[0]), self.visit(node.comparators[0]), ) print('~~~~warn: Unsupported', node) print(node.comparators, node.left, node.ops) return self.Unsupported() else: op_kind = opkind_map(node.ops) return ir_ast.BinOp( self.visit(node.left), op_kind, self.visit(node.comparators), )
def visit_AugAssign(self, node): if isinstance(node.op, py_ast.Add): op_kind = ir_ast.BinOpKind.ADD else: raise TypeError("%s" % node.op) target = self.visit(node.target) return ir_ast.BinOp( target, ir_ast.BinOpKind.ASSIGN, ir_ast.BinOp( target, op_kind, self.visit(node.value), ), )
def visit_BinOp(self, node): node_kind = node.op.__class__.__name__ if node_kind == 'Pow': return ir_ast.Call( ir_ast.Name('pow'), [ ir_ast.Call.Arg(name=None, value=self.visit(node.left)), ir_ast.Call.Arg(name=None, value=self.visit(node.right)), ] ) def get_kind(kind): kind = kind.__class__.__name__ if kind == 'Add': return ir_ast.BinOpKind.ADD elif kind == 'Sub': return ir_ast.BinOpKind.SUB elif kind == 'Mult': return ir_ast.BinOpKind.MUL elif kind == 'Div': return ir_ast.BinOpKind.DIV elif kind == 'Mod': return ir_ast.BinOpKind.MOD elif kind == 'LShift': return ir_ast.BinOpKind.LEFT_SHIFT elif kind == 'RShift': return ir_ast.BinOpKind.RIGHT_SHIFT raise Exception('Unsupported BinOp kind %s' % kind) return ir_ast.BinOp(self.visit(node.left), get_kind(node.op), self.visit(node.right))
def visit_Attribute(self, node): return ir_ast.BinOp(self.visit(node.value), ir_ast.BinOpKind.DOT, ir_ast.Name(node.attr), )
def visit_Assign(self, node): if len(node.targets) != 1: raise Exception('Unsupported multiple Assign') return ir_ast.BinOp(self.visit(node.targets[0]), ir_ast.BinOpKind.ASSIGN, self.visit(node.value))
def _deserialize_object(self, object): if 'File' in object.keys(): obj = object['File'] return ast.File([self._deserialize_object(i) for i in obj['Body']], ) if 'FunctionDef' in object.keys(): obj = object['FunctionDef'] return ast.FunctionDef( obj['Name'], [self._deserialize_object(i) for i in obj['Args']], [self._deserialize_object(i) for i in obj['Body']], ) if 'ClassDef' in object.keys(): obj = object['ClassDef'] return ast.ClassDef( obj['Name'], obj['Bases'], [self._deserialize_object(i) for i in obj['Fields']], [self._deserialize_object(i) for i in obj['Body']], ) if 'Return' in object.keys(): obj = object['Return'] return ast.Return(self._deserialize_object(obj['Value']), ) if 'Assign' in object.keys(): obj = object['Assign'] return ast.Assign( self._deserialize_object(obj['Target']), self._deserialize_object(obj['Value']), ) if 'For' in object.keys(): obj = object['For'] return ast.For( self._deserialize_object(obj['Value']), self._deserialize_object(obj['Generator']), [self._deserialize_object(i) for i in obj['Body']], ) if 'Block' in object.keys(): obj = object['Block'] return ast.Block( [self._deserialize_object(i) for i in obj['Body']], ) if 'Try' in object.keys(): obj = object['Try'] return ast.Try([self._deserialize_object(i) for i in obj['Body']], ) if 'Raise' in object.keys(): obj = object['Raise'] return ast.Raise(self._deserialize_object(obj['Value']), ) if 'Catch' in object.keys(): obj = object['Catch'] return ast.Catch( [self._deserialize_object(i) for i in obj['Body']], ) if 'BoolOp' in object.keys(): obj = object['BoolOp'] return ast.BoolOp( obj['Kind'], self._deserialize_object(obj['Left']), self._deserialize_object(obj['Right']), ) if 'BinOp' in object.keys(): obj = object['BinOp'] return ast.BinOp( obj['Kind'], self._deserialize_object(obj['Left']), self._deserialize_object(obj['Right']), ) if 'UnaryOp' in object.keys(): obj = object['UnaryOp'] return ast.UnaryOp( obj['Kind'], self._deserialize_object(obj['Value']), ) if 'Constant' in object.keys(): obj = object['Constant'] return ast.Constant( obj['Kind'], obj['Value'], ) if 'Attribute' in object.keys(): obj = object['Attribute'] return ast.Attribute( self._deserialize_object(obj['Value']), obj['Attribute'], ) if 'Subscript' in object.keys(): obj = object['Subscript'] return ast.Subscript( self._deserialize_object(obj['Value']), self._deserialize_object(obj['Index']), ) if 'Name' in object.keys(): obj = object['Name'] return ast.Name( obj['Name'], obj['Kind'], ) if 'Array' in object.keys(): obj = object['Array'] return ast.Array( [self._deserialize_object(i) for i in obj['Values']], ) if 'Tuple' in object.keys(): obj = object['Tuple'] return ast.Tuple( [self._deserialize_object(i) for i in obj['Values']], ) if 'Call' in object.keys(): obj = object['Call'] return ast.Call( self._deserialize_object(obj['Value']), [self._deserialize_object(i) for i in obj['Args']], [self._deserialize_object(i) for i in obj['KwArgs']], ) if 'ArgumentDef' in object.keys(): obj = object['ArgumentDef'] return ast.ArgumentDef( obj['Key'], obj['Default'] if obj['Default'] is None else self._deserialize_object(obj['Default']), ) if 'KwArg' in object.keys(): obj = object['KwArg'] return ast.KwArg( obj['Key'], self._deserialize_object(obj['Value']), )