File: make_binops.py

package info (click to toggle)
jython 2.2.1-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 18,708 kB
  • ctags: 46,200
  • sloc: python: 150,937; java: 86,267; xml: 1,080; perl: 104; sh: 93; makefile: 81; ansic: 24
file content (170 lines) | stat: -rw-r--r-- 5,135 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
"""generates code for binops in PyObject and for all "simple" ops in PyInstance"""

binops = \
	[('add', '+'), ('sub', '-'), ('mul', '*'), ('div', '/'),
         ('floordiv', '//'), ('truediv', '/'),
	 ('mod', '%'), ('divmod', 'divmod'), ('pow', '**'), 
	 ('lshift', '<<'), ('rshift', '>>'), ('and', '&'), ('or', '|'), ('xor', '^')]

#
# this bit is superseded by src/templates/make_binops.py
#
## template = """\
##     /**
##      * Equivalent to the standard Python __%(name)s__ method
##      * @param     other the object to perform this binary operation with
##      *            (the right-hand operand).
##      * @return    the result of the %(name)s, or null if this operation
##      *            is not defined
##      **/
##     public PyObject __%(name)s__(PyObject other) { %(function)s }

##     /**
##      * Equivalent to the standard Python __r%(name)s__ method
##      * @param     other the object to perform this binary operation with
##      *            (the left-hand operand).
##      * @return    the result of the %(name)s, or null if this operation
##      *            is not defined.
##      **/
##     public PyObject __r%(name)s__(PyObject other) { %(rfunction)s }

##     /**
##      * Equivalent to the standard Python __i%(name)s__ method
##      * @param     other the object to perform this binary operation with
##      *            (the right-hand operand).
##      * @return    the result of the %(name)s, or null if this operation
##      *            is not defined
##      **/
##     public PyObject __i%(name)s__(PyObject other) { return _%(name)s(other); }

##     /**
##      * Implements the Python expression <code>this %(op)s other</code>
##      * @param     other the object to perform this binary operation with.
##      * @return    the result of the %(name)s.
##      * @exception PyTypeError if this operation can't be performed
##      *            with these operands.
##      **/
##     public final PyObject _%(name)s(PyObject o2) {%(divhook)s
##         PyObject x = __%(name)s__(o2);
##         if (x != null)
##             return x;
##         x = o2.__r%(name)s__(this);
##         if (x != null)
##             return x;
##         throw Py.TypeError(
##                  "__%(name)s__ nor __r%(name)s__ defined for these operands");
##     }

## """

## fp = open('binops.txt', 'w')

## fp.write('    // Generated by make_binops.py (Begin)\n\n')

## for name, op in binops:
## 	rfunction = function = 'return null;'
##         divhook = ""

## 	if name == 'pow':
## 		function = 'return __pow__(other, null);'
## 	if name == 'div':
## 		divhook = '''
##         if (Options.Qnew)
##             return _truediv(o2);'''
                    
## 	fp.write(template % {
##             'name':name,
##             'op':op,
##             'function':function,
##             'rfunction':rfunction,
##             'divhook':divhook
##         })

## fp.write('    // Generated by make_binops.py (End)\n\n')

## fp.close()



fp = open('binopsi.txt', 'w')

fp.write('    // Generated by make_binops.py\n\n')

comment = """\
    /**
     * Implements the __%(name)s__ method by looking it up
     * in the instance's dictionary and calling it if it is found.
     **/
"""

template1 = comment + """\
    public %(ret)s __%(name)s__() {
        PyObject ret = invoke("__%(name)s__");
        if (ret instanceof %(ret)s)
            return (%(ret)s)ret;
        throw Py.TypeError("__%(name)s__() should return a %(retname)s");
    }

"""
template2 = comment + """\
    public PyObject __%(name)s__() {
        return invoke("__%(name)s__");
    }

"""

string = 'PyString', 'string'
ops = [('hex', string), ('oct', string), 
		('int', ('PyInteger', 'int')), ('float', ('PyFloat', 'float')), 
		('long', ('PyLong', 'long')), ('complex', ('PyComplex', 'complex')),
		('pos', None), ('neg', None), ('abs', None), ('invert', None)]
	
fp.write('    // Unary ops\n\n')	
for name, ret in ops:
	if ret is None:
		fp.write(template2 % {'name':name})
	else:
		ret, retname = ret
		fp.write(template1 % {'name':name, 'ret':ret, 'retname':retname})



template = comment + """\
    public PyObject __%(name)s__(PyObject o) {
        Object ctmp = __coerce_ex__(o);
        if (ctmp == null || ctmp == Py.None)
            return invoke_ex("__%(name)s__", o);
        else {
            PyObject o1 = ((PyObject[])ctmp)[0];
            PyObject o2 = ((PyObject[])ctmp)[1];
            if (this == o1) // Prevent recusion if __coerce__ return self
                return invoke_ex("__%(name)s__", o2);
            else
                return %(function)s;
        }
    }

"""


template2 = comment + """\
    public PyObject __%(name)s__(PyObject o) {
        PyObject ret = invoke_ex("__%(name)s__", o);
        if (ret != null)
            return ret;
        return super.__%(name)s__(o);
    }

"""

fp.write('    // Binary ops\n\n')
for name, op in binops:	
	fp.write(template % {'name':name, 'function':'o1._%s(o2)' % name })
	fp.write(template % {'name':'r'+name, 'function':'o2._%s(o1)' % name })
        if name != 'divmod':
		fp.write(template2 % {'name':'i'+name
})


fp.close()