Exemple #1
0
    def visit_If(self, ast_if: If):
        if_node = IfNode()
        # coord = coordinate(ast_if.col_offset, ast_if.lineno)
        # self.set_coordinate(if_node, coord)
        # condition
        if_node.condition = self.visit(ast_if.test)

        # convert pyc true statement to list
        if type(ast_if.body) is not list:
            ast_if.body = [ast_if.body]

        self.parent = if_node.true_stmt
        for child in ast_if.body or []:
            child_node = self.visit(child)
            if child_node:
                if type(child_node) is list:
                    if_node.true_stmt.extend(child_node)
                else:
                    if_node.true_stmt.append(child_node)

        # convert pyc false statement to list
        if ast_if.orelse:
            if type(ast_if.orelse) is not list:
                ast_if.orelse = [ast_if.orelse]

            self.parent = if_node.false_stmt
            for child in ast_if.orelse or []:
                child_node = self.visit(child)
                if child_node:
                    if type(child_node) is list:
                        if_node.false_stmt.extend(child_node)
                    else:
                        if_node.false_stmt.append(child_node)
        return if_node
    def compile_ifnode(self, srcnode, parent):
        comparison = get_comparison('{}'.format(srcnode.test))
        if_ = If(test=comparison, body=[], orelse=[])
        parent.body.append(self.annotate_runtime_errors(if_, srcnode))
        for item in srcnode.children:
            self._compile(item, if_)

        if srcnode.else_:
            m = Module(body=[])
            for item in srcnode.else_.children:
                self._compile(item, m)
            if_.orelse = m.body

        if if_.body == []:
            if_.body = [Pass()]
    def compile_choosenode(self, srcnode, parent):
        if srcnode.test is None or srcnode.test.strip() == '':
            srcnode.test = 'True'

        choosevar = self.unique_id('choose')
        chooseflag = self.unique_id('chosen')

        parent.body.append(self.annotate_runtime_errors(
            parse_and_strip(u'{} = False\n'
                            u'{} = {}\n'.format(chooseflag,
                                                choosevar,
                                                srcnode.test)),
            srcnode))

        for item in srcnode.children:
            if isinstance(item, im.WhenNode):
                comparison = get_comparison(u'{} is False and {} == ({})'
                                            .format(chooseflag,
                                                    choosevar,
                                                    item.test))
                if_ = If(test=comparison, body=[], orelse=[])
                if_.body.extend(
                    parse_and_strip('{} = True'.format(chooseflag)))
                set_pos(if_, item)
                parent.body.append(self.annotate_runtime_errors(if_, srcnode))
                for sub in item.children:
                    self._compile(sub, if_)

            elif isinstance(item, im.OtherwiseNode):
                comparison = get_comparison('{} is False'.format(chooseflag))
                if_ = If(test=comparison, body=[], orelse=[])
                set_pos(if_, item)
                parent.body.append(if_)
                for sub in item.children:
                    self._compile(sub, if_)
                if if_.body == []:
                    if_.body = [Pass()]
            else:
                self._compile(item, parent)
Exemple #4
0
 def visit_If(self, node: ast.If):
     node.body = self._process_stmts(node.body)
     if node.orelse:
         node.orelse = self._process_stmts(node.orelse)
     return node