Пример #1
0
 def atom_number(self, nodelist):
     n = Transformer.atom_number(self, nodelist)
     number, lineno = nodelist[0][1:]
     if _is_integer(number):
         n = Const(long(number), lineno)
         return CallFunc(Name('Integer'), [n])
     if number.endswith('j'):
         n = Const(complex(number), lineno)
         return CallFunc(Name('sympify'), [n])
     n = Const(number, lineno)
     return CallFunc(Name('Real'), [n])
Пример #2
0
    def atom_name(self, nodelist):
        name, lineno = nodelist[0][1:]

        if self.local_dict.has_key(name):
            name_obj = self.local_dict[name]
            return Const(name_obj, lineno=lineno)
        elif self.global_dict.has_key(name):
            name_obj = self.global_dict[name]

            if isinstance(name_obj, (Basic, type)) or callable(name_obj):
                return Const(name_obj, lineno=lineno)
        elif name in ['True', 'False']:
            return Const(eval(name), lineno=lineno)

        symbol_obj = Symbol(name)
        self.local_dict[name] = symbol_obj

        return Const(symbol_obj, lineno=lineno)
Пример #3
0
 def __const(self):
     from compiler.ast import Discard, CallFunc, Name, Const
     try:
         n = Discard(
             CallFunc(Name('__write__'), [Const(self.__tokenText(strip=0))],
                      None, None))
         t = self.__tokenPop()
         _renumber(n, t[1] - 1)
         return n
     except:
         self.__error('bad constant')
Пример #4
0
 def convert(x):
     return CallFunc(Name('sympify'), [Const(x)])
Пример #5
0
def p_int_expression(t):
  ’expression : INT’
  t[0] = Const(t[1])
Пример #6
0
    def __get_ast(self):
        from compiler.ast import    Module, Stmt, Assign, AssName, Const, Function, For, Getattr,\
                                    TryFinally, TryExcept, If, Import, AssAttr, Name, CallFunc,\
                                    Class, Compare, Raise, And, Mod, Tuple, Pass, Not, Exec, List,\
                                    Discard, Keyword, Return, Dict, Break, AssTuple, Subscript,\
                                    Printnl, From, Lambda

        preppyNodes = self.__parse()
        if self._defSeen == 1:
            fixargs = self._fnc_argnames
            defaults = list(self._fnc_defaults)
            if self._fnc_kwargs:
                spargs = [fixargs[-1]]
                fixargs = fixargs[:-1]
            else:
                spargs = ['__kwds__']
            if self._fnc_varargs:
                spargs.insert(0, fixargs[-1])
                fixargs = fixargs[:-1]
            kwargs = fixargs[-len(defaults):]
            fixargs = fixargs[:-len(defaults)]
            flags = self._fnc_flags

            #construct the getOutput function
            nodes = [
                Assign([AssName('__lquoteFunc__', 'OP_ASSIGN')],
                       CallFunc(Getattr(Name(spargs[-1]), 'setdefault'),
                                [Const('__lquoteFunc__'),
                                 Name('str')], None, None)),
                Discard(
                    CallFunc(Getattr(Name(spargs[-1]), 'pop'),
                             [Const('__lquoteFunc__')], None, None)),
                Assign([AssName('__quoteFunc__', 'OP_ASSIGN')],
                       CallFunc(Getattr(Name(spargs[-1]), 'setdefault'),
                                [Const('__quoteFunc__'),
                                 Name('str')], None, None)),
                Discard(
                    CallFunc(Getattr(Name(spargs[-1]), 'pop'),
                             [Const('__quoteFunc__')], None, None))
            ]
            if not self._fnc_kwargs:
                nodes += [
                    If([(Name(spargs[-1]),
                         Stmt([
                             Raise(
                                 CallFunc(Name('TypeError'), [
                                     Const('get: unexpected keyword arguments')
                                 ], None, None), None, None)
                         ]))], None)
                ]
            nodes += [
                Assign([AssName('__append__', 'OP_ASSIGN')],
                       Getattr(List(()), 'append')),
                Assign([AssName('__write__', 'OP_ASSIGN')],
                       Lambda(['x'], [], 0,
                              CallFunc(Name('__append__'), [
                                  CallFunc(Name('__lquoteFunc__'), [Name('x')],
                                           None, None)
                              ], None, None))),
                Assign([AssName('__swrite__', 'OP_ASSIGN')],
                       Lambda(['x'], [], 0,
                              CallFunc(Name('__append__'), [
                                  CallFunc(Name('__quoteFunc__'), [Name('x')],
                                           None, None)
                              ], None, None)))
            ]
            for n in nodes:
                _denumber(n, self._fnc_lineno)
            preppyNodes = nodes + preppyNodes + [
                Return(
                    CallFunc(Getattr(Const(''), 'join'),
                             [Getattr(Name('__append__'), '__self__')], None,
                             None))
            ]
            argnames = list(fixargs) + list(kwargs) + list(spargs)
            FA = ('get', argnames, defaults, flags | 8, None,
                  Stmt(preppyNodes))
            global _newPreambleAst
            if not _newPreambleAst:
                _newPreambleAst = self._cparse(_newPreamble).node.nodes
                map(_denumber, _newPreambleAst)
            extraAst = _newPreambleAst
        else:
            global _preambleAst, _localizer
            if not _preambleAst:
                _preambleAst = self._cparse(_preamble).node.nodes
                map(_denumber, _preambleAst)
                _localizer = [
                    Assign([AssName('__d__', 'OP_ASSIGN')],
                           Name('dictionary')),
                    Discard(
                        CallFunc(
                            Getattr(CallFunc(Name('globals'), [], None, None),
                                    'update'), [Name('__d__')], None, None))
                ]
            preppyNodes = _localizer + preppyNodes
            FA = ('__code__', [
                'dictionary', 'outputfile', '__write__', '__swrite__',
                '__save_sys_stdout__'
            ], (), 0, None, Stmt(preppyNodes))
            extraAst = _preambleAst
        if sys.hexversion >= 0x2040000: FA = (None, ) + FA
        return Module(
            self.filename,
            Stmt([
                Assign([AssName('__checksum__', 'OP_ASSIGN')],
                       Const(getattr(self, 'sourcechecksum'))),
                Function(*FA),
            ] + extraAst))