def ConvertCall(self, tree):
        """ Convert a function call:

      Call(expr func, expr* args, keyword* keywords, expr? starargs, expr? kwargs)
      keyword = (identifier arg, expr value)

      into
      CallFunction: position, function, arguments """

        # Set up the arguments, using args, keywords, starargs, kwargs
        arguments = []
        for a in tree.args:

            arguments.append(self.ConvertTree(a))

        for k in tree.keywords:
            position_reference = tree.args[-1] if len(tree.args) > 0 else tree
            arguments.append(
                N.DefineVariable(position=self.gc.GC(position_reference),
                                 name=k.arg,
                                 init=self.ConvertTree(k.value)))

        argument_node = N.FunctionArguments(
            position=self.gc.GC(tree.args[0]) if len(tree.args) > 0 else None,
            arguments=arguments,
            var_arguments=self.ConvertTree(tree.starargs),
            kwd_arguments=self.ConvertTree(tree.kwargs))

        return N.CallFunction(position=self.gc.GC(tree),
                              function=self.ConvertTree(tree.func),
                              arguments=argument_node)
    def __ConvertArguments(self, args, position):
        """ Convert the Python argument node to the FunctionArguments L-AST node. The
    arguments need to be variable definitions, which do not exist elsewhere in
    Python (everything else is a ReferVariable).  We therefore handle it here
    itself. """

        if PY3:
            arguments = [self._ConvertArg(a, position) for a in args.args]
        else:
            # In Python 2, arguments are Name nodes with ctx=Param()
            arguments = [self.ConvertTree(a) for a in args.args]

        for (i, y) in enumerate(args.defaults):
            arguments[len(arguments) - len(args.defaults) +
                      i].init = self.ConvertTree(y)

        vararg, kwarg = None, None
        if args.vararg:
            vararg = N.DefineVariable(position=position, name=args.vararg)
        if args.kwarg:
            kwarg = N.DefineVariable(position=position, name=args.kwarg)

        return N.FunctionArguments(position=position,
                                   arguments=arguments,
                                   var_arguments=vararg,
                                   kwd_arguments=kwarg)
Example #3
0
    def ConvertFunccall(self, tree):
        arguments = []
        if tree.args:
            arguments = list(map(self.ConvertTree, tree.args.exprs))

        return N.CallFunction(position=GetCoords(tree),
                              function=self.ConvertTree(tree.name),
                              arguments=N.FunctionArguments(
                                  position=GetCoords(tree),
                                  arguments=arguments))
Example #4
0
    def ConvertFuncdecl(self, tree):
        """ Handle Function Declarations."""
        if tree.args:
            arguments = self.ConvertTree(tree.args)
        else:
            arguments = N.FunctionArguments(position=None, arguments=[])

        return N.DeclareFunction(position=GetCoords(tree),
                                 name=None,
                                 arguments=arguments,
                                 return_type=self.ConvertTree(tree.type))
Example #5
0
    def ConvertParamlist(self, tree):
        """ Handle Function Arguments. 
        In case the last is an ellipsis param, we store it as a var_arg
        of name va_list."""
        try:
            position = GetCoords(tree)
        except AssertionError:
            position = None

        #TODO(spranesh): Cheap Hack?
        if tree.params[-1].__class__.__name__ == 'EllipsisParam':
            try:
                va_list_position = GetCoords(tree.params[-1])
            except AssertionError:
                va_list_position = None

            return N.FunctionArguments(
                position=position,
                arguments=list(map(self.ConvertTree, tree.params[:-1])),
                var_arguments=[N.DefineVariable(va_list_position, 'va_list')])

        return N.FunctionArguments(position=position,
                                   arguments=list(
                                       map(self.ConvertTree, tree.params)))
Example #6
0
                ((- n 
                  (constant 1)))))))))))

"""

if_true = [
    N.Return(position=None,
             return_expression=N.Constant(position=None, value='1'))
]

fact_arguments = N.FunctionArguments(position=None,
                                     arguments=[
                                         N.Expression(
                                             position=None,
                                             operator='MINUS',
                                             children=[
                                                 N.ReferVariable(position=None,
                                                                 name='n'),
                                                 N.Constant(position=None,
                                                            value='1')
                                             ])
                                     ])

if_false = [
    N.Return(position=None,
             return_expression=N.Expression(
                 position=None,
                 operator='MULTIPLY',
                 children=[
                     N.ReferVariable(position=None, name='n'),
                     N.CallFunction(position=None,
                                    function=N.ReferVariable(position=None,