def _translate_compare(self, left, ops, comparators, location):
if isinstance(ops[0], ast.In) or isinstance(ops[0], ast.NotIn):
if len(comparators) != 1:
raise translation_error('only <element> [not] in <sequence> supported',
location, self.lines[location[0]],
suggestion='2 in [2] in [[2]] is cute, but it\'s not supported')
else:
in_node = self._translate_in(left, comparators[0], location)
if isinstance(ops[0], ast.In):
return in_node
else:
return {'type': 'unary_op', 'op': 'not', 'value': in_node, 'pseudo_type': 'Boolean'}
op = PSEUDO_OPS[type(ops[0])]
right_node = self._translate_node(comparators[0])
left_node = self._translate_node(left)
self._confirm_comparable(op, left_node['pseudo_type'], right_node['pseudo_type'], location)
result = {
'type': 'comparison',
'op': op,
'left': left_node,
'right': right_node,
'pseudo_type': 'Boolean'
}
if len(comparators) == 1:
return result
else:
for r in comparators[1:]:
left_node, right_node = right_node, self._translate_node(r)
self._confirm_comparable(op, left_node['pseudo_type'], right_node['pseudo_type'], location)
result = {
'type': 'binary_op',
'op': 'and',
'left': result,
'right': {
'type': 'comparison',
'op': op,
'left': left_node,
'right': right_node,
'pseudo_type': 'Boolean'
},
'pseudo_type': 'Boolean'
}
return result
评论列表
文章目录