def _merge_lists(self, xs: List[ListEntry]) -> Union[ast.BinOp, ListEntry]:
        """Merge lists by summing them."""
        if len(xs) == 1:
            return xs[0]

        result = ast.BinOp(left=xs[0], right=xs[1], op=ast.Add())
        for x in xs[2:]:
            result = ast.BinOp(left=result, right=x, op=ast.Add())
        return result
示例#2
0
    def dispatch_var_type(self, tree):
        code = horast.unparse(tree)
        stripped_code = code.strip()
        if stripped_code in PYTHON_FORTRAN_TYPE_PAIRS:
            type_name, precision = PYTHON_FORTRAN_TYPE_PAIRS[stripped_code]
            self.write(type_name)
            if precision is not None:
                self.write('*')
                self.write(str(precision))
        elif _match_array(tree):
            sli = tree.slice
            assert isinstance(sli,
                              typed_ast3.Index), typed_astunparse.dump(tree)
            assert isinstance(sli.value, typed_ast3.Tuple)
            assert len(sli.value.elts) in (2, 3), sli.value.elts
            elts = sli.value.elts
            self.dispatch_var_type(elts[1])
            self.write(', ')
            self.write('dimension(')

            if len(sli.value.elts) == 2:
                self.write(':')
            else:
                if not self._context_input_args:
                    self.dispatch(elts[2])
                else:
                    assert isinstance(elts[2], typed_ast3.Tuple)
                    _LOG.warning('coercing indices of %i dims to 0-based',
                                 len(elts[2].elts))
                    # _LOG.warning('coercing indices of %s in %s to 0-based', arg.arg, t.name)
                    tup_elts = []
                    for elt in elts[2].elts:
                        if isinstance(elt, typed_ast3.Num):
                            assert isinstance(elt.n, int)
                            upper = typed_ast3.Num(n=elt.n - 1)
                        else:
                            assert isinstance(elt, typed_ast3.Name)
                            upper = typed_ast3.BinOp(left=elt,
                                                     op=typed_ast3.Sub(),
                                                     right=typed_ast3.Num(n=1))
                        tup_elts.append(
                            typed_ast3.Slice(lower=typed_ast3.Num(n=0),
                                             upper=upper,
                                             step=None))
                    tup = typed_ast3.Tuple(elts=tup_elts,
                                           ctx=typed_ast3.Load())
                    self.dispatch(tup)

            self.write(')')
        elif _match_io(tree):
            self.write('integer')
        elif isinstance(tree, typed_ast3.Call) and isinstance(tree.func, typed_ast3.Name) \
                and tree.func.id == 'type':
            self.dispatch(tree)
        else:
            raise NotImplementedError('not yet implemented: {}'.format(
                typed_astunparse.dump(tree)))
示例#3
0
    def visit_AugAssign(self, node: ast3.AugAssign) -> VisitorOutput:
        """Converts `A (op)= Statement` into `A = A (op) (Statement)`
        """
        left_value = copy_ast3(node.target)
        left_value.ctx = ast3.Load()  # type: ignore

        new_target = self.visit(node.target)
        new_value = self.visit_BinOp(
            ast3.BinOp(left=left_value,
                       op=node.op,
                       right=node.value,
                       lineno=node.lineno,
                       col_offset=node.col_offset))
        return ast3.Assign(
            targets=[new_target], value=new_value,
            type_comment=None)  # TODO(helq): don't ignore the type comment!
示例#4
0
 def dispatch_for_iter(self, tree):
     if not isinstance(tree, typed_ast3.Call) \
             or not isinstance(tree.func, typed_ast3.Name) or tree.func.id != 'range' \
             or len(tree.args) not in (1, 2, 3):
         self._unsupported_syntax(tree)
     if len(tree.args) == 1:
         lower = typed_ast3.Num(n=0)
         upper = tree.args[0]
         step = None
     else:
         lower, upper, step, *_ = tree.args + [None, None]
     self.dispatch(lower)
     self.write(', ')
     if isinstance(upper, typed_ast3.BinOp) and isinstance(upper.op, typed_ast3.Add) \
             and isinstance(upper.right, typed_ast3.Num) and upper.right.n == 1:
         self.dispatch(upper.left)
     else:
         self.dispatch(typed_ast3.BinOp(left=upper, op=typed_ast3.Sub(),
                                        right=typed_ast3.Num(n=1)))
     if step is not None:
         self.write(', ')
         self.dispatch(step)