Пример #1
0
    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)
Пример #2
0
    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)
Пример #3
0
 def ConvertTypename(self, tree):
     #TODO(spranesh): Handle quals.
     t = self.ConvertTree(tree.type)
     return N.DefineVariable(
         None,  # No coords for Typename
         name=tree.name,
         type=t)
Пример #4
0
 def ConvertDecl(self, tree):
     # We have to check if the child is a
     #   a. a function declaration
     #   b. a structure declaration
     # Otherwise it is a normal declaration
     t = self.ConvertTree(tree.type)
     if isinstance(t, N.DeclareFunction):
         t.name = tree.name
         t.storage = tree.storage
         t.quals = tree.quals
         return t
     if isinstance(t, N.Structure):
         t.storage = tree.storage
         t.quals = tree.quals
         return t
     if isinstance(t, N.Enumerator):
         t.storage, t.quals = None, None
         return t
     if isinstance(t, N.Union):
         t.storage, t.quals = None, None
         return t
     else:
         return N.DefineVariable(GetCoords(tree),
                                 name=tree.name,
                                 init=self.ConvertTree(tree.init),
                                 type=t,
                                 storage=tree.storage,
                                 quals=tree.quals)
Пример #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)))
Пример #6
0
 def _ConvertArg(self, arg, position):
     return N.DefineVariable(position, name=arg.arg)
Пример #7
0
 def ConvertName(self, tree):
     if isinstance(tree.ctx, ast.Param):
         return N.DefineVariable(self.gc.GC(tree), name=tree.id)
     return N.ReferVariable(self.gc.GC(tree), name=tree.id)
Пример #8
0
                  condition=N.Expression(position=None,
                                         operator='EQ',
                                         children=[
                                             N.ReferVariable(position=None,
                                                             name='n'),
                                             N.Constant(position=None,
                                                        value='0')
                                         ]),
                  if_true=if_true,
                  if_false=if_false)

factorial_tree = N.DefineFunction(
    position=None,
    name='Factorial',
    arguments=N.FunctionArguments(
        position=None, arguments=[N.DefineVariable(position=None, name='n')]),
    body=[ifelse])

module_tree = N.Module(position=None,
                       filename='sample_last.py',
                       children=[factorial_tree])

T.TreeConverter().AttachParents(module_tree)

tree = module_tree.GetChildren()[0]

__all__ = ['tree', 'module_tree']

if __name__ == '__main__':
    print(module_tree.ToStr())