ast_translator.py 文件源码

python
阅读 29 收藏 0 点赞 0 评论 0

项目:pseudo-python 作者: alehander42 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号