Skip to content

Commit 7418ece

Browse files
committedAug 1, 2018
Add LICENSE, README and examples
1 parent 5e6dfca commit 7418ece

File tree

5 files changed

+452
-0
lines changed

5 files changed

+452
-0
lines changed
 

‎LICENSE

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Nick Craig-Wood
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

‎README.md

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# gpython
2+
3+
gpython is a part re-implementation / part port of the Python 3.4
4+
interpreter to the Go language, "batteries not included".
5+
6+
It includes:
7+
8+
* runtime - using compatible byte code to python3.4
9+
* lexer
10+
* parser
11+
* compiler
12+
* interactive mode (REPL)
13+
14+
It does not include very many python modules as many of the core
15+
modules are written in C not python. The converted modules are:
16+
17+
* builtins
18+
* marshal
19+
* math
20+
* time
21+
* sys
22+
23+
## Objectives
24+
25+
Gpython was written as a learning experiment to investigate how hard
26+
porting Python to Go might be. It turns out that all those C modules
27+
are a significant barrier to making a fully functional port.
28+
29+
## Status
30+
31+
The project works well enough to parse all the code in the python 3.4
32+
distribution and to compile and run python 3 programs which don't
33+
depend on a module gpython doesn't support.
34+
35+
See the examples directory for some python programs which run with
36+
gpython.
37+
38+
Speed hasn't been a goal of the conversions however it runs pystone at
39+
about 20% of the speed of cpython. The pi test runs quicker under
40+
gpython as I think the Go long integer primitives are faster than the
41+
Python ones.
42+
43+
There are many directions this project could go in. I think the most
44+
profitable would be to re-use the
45+
[grumpy](https://github.com/google/grumpy) runtime (which would mean
46+
changing the object model). This would give access to the C modules
47+
that need to be ported and would give grumpy access to a compiler and
48+
interpreter (gpython does support `eval` for instance).
49+
50+
I (@ncw) haven't had much time to work on gpython (I started it in
51+
2013 and have worked on it very sporadically) so someone who wants to
52+
take it in the next direction would be much appreciated.
53+
54+
## Limitations and Bugs
55+
56+
Lots!
57+
58+
## Similar projects
59+
60+
* [grumpy](https://github.com/google/grumpy) - a python to go transpiler
61+
62+
## License
63+
64+
This is licensed under the MIT licence, however it contains code which
65+
was ported fairly directly directly from the cpython source code under
66+
the (PSF LICENSE)[https://github.com/python/cpython/blob/master/LICENSE)

‎examples/pi_chudnovsky_bs.py

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
"""
2+
Python3 program to calculate Pi using python long integers, binary
3+
splitting and the Chudnovsky algorithm
4+
5+
See: http://www.craig-wood.com/nick/articles/ FIXME for explanation
6+
7+
Nick Craig-Wood <nick@craig-wood.com>
8+
"""
9+
10+
import math
11+
from time import time
12+
13+
def sqrt(n, one):
14+
"""
15+
Return the square root of n as a fixed point number with the one
16+
passed in. It uses a second order Newton-Raphson convgence. This
17+
doubles the number of significant figures on each iteration.
18+
"""
19+
# Use floating point arithmetic to make an initial guess
20+
floating_point_precision = 10**16
21+
n_float = float((n * floating_point_precision) // one) / floating_point_precision
22+
x = (int(floating_point_precision * math.sqrt(n_float)) * one) // floating_point_precision
23+
n_one = n * one
24+
while 1:
25+
x_old = x
26+
x = (x + n_one // x) // 2
27+
if x == x_old:
28+
break
29+
return x
30+
31+
32+
def pi_chudnovsky_bs(digits):
33+
"""
34+
Compute int(pi * 10**digits)
35+
36+
This is done using Chudnovsky's series with binary splitting
37+
"""
38+
C = 640320
39+
C3_OVER_24 = C**3 // 24
40+
def bs(a, b):
41+
"""
42+
Computes the terms for binary splitting the Chudnovsky infinite series
43+
44+
a(a) = +/- (13591409 + 545140134*a)
45+
p(a) = (6*a-5)*(2*a-1)*(6*a-1)
46+
b(a) = 1
47+
q(a) = a*a*a*C3_OVER_24
48+
49+
returns P(a,b), Q(a,b) and T(a,b)
50+
"""
51+
if b - a == 1:
52+
# Directly compute P(a,a+1), Q(a,a+1) and T(a,a+1)
53+
if a == 0:
54+
Pab = Qab = 1
55+
else:
56+
Pab = (6*a-5)*(2*a-1)*(6*a-1)
57+
Qab = a*a*a*C3_OVER_24
58+
Tab = Pab * (13591409 + 545140134*a) # a(a) * p(a)
59+
if a & 1:
60+
Tab = -Tab
61+
else:
62+
# Recursively compute P(a,b), Q(a,b) and T(a,b)
63+
# m is the midpoint of a and b
64+
m = (a + b) // 2
65+
# Recursively calculate P(a,m), Q(a,m) and T(a,m)
66+
Pam, Qam, Tam = bs(a, m)
67+
# Recursively calculate P(m,b), Q(m,b) and T(m,b)
68+
Pmb, Qmb, Tmb = bs(m, b)
69+
# Now combine
70+
Pab = Pam * Pmb
71+
Qab = Qam * Qmb
72+
Tab = Qmb * Tam + Pam * Tmb
73+
return Pab, Qab, Tab
74+
# how many terms to compute
75+
DIGITS_PER_TERM = math.log10(C3_OVER_24/6/2/6)
76+
N = int(digits/DIGITS_PER_TERM + 1)
77+
# Calclate P(0,N) and Q(0,N)
78+
P, Q, T = bs(0, N)
79+
one = 10**digits
80+
sqrtC = sqrt(10005*one, one)
81+
return (Q*426880*sqrtC) // T
82+
83+
# The last 5 digits or pi for various numbers of digits
84+
check_digits = (
85+
(100, 70679),
86+
(1000, 1989),
87+
(10000, 75678),
88+
(100000, 24646),
89+
(1000000, 58151),
90+
(10000000, 55897),
91+
)
92+
93+
if __name__ == "__main__":
94+
digits = 100
95+
pi = pi_chudnovsky_bs(digits)
96+
print(str(pi))
97+
for digits, check in check_digits:
98+
start =time()
99+
pi = pi_chudnovsky_bs(digits)
100+
print("chudnovsky_gmpy_bs: digits",digits,"time",time()-start)
101+
last_five_digits = pi % 100000
102+
if check == last_five_digits:
103+
print("Last 5 digits %05d OK" % last_five_digits)
104+
else:
105+
print("Last 5 digits %05d wrong should be %05d" % (last_five_digits, check))

‎examples/pystone.py

+271
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
#! /usr/bin/env python3
2+
3+
"""
4+
"PYSTONE" Benchmark Program
5+
6+
Version: Python/1.1 (corresponds to C/1.1 plus 2 Pystone fixes)
7+
8+
Author: Reinhold P. Weicker, CACM Vol 27, No 10, 10/84 pg. 1013.
9+
10+
Translated from ADA to C by Rick Richardson.
11+
Every method to preserve ADA-likeness has been used,
12+
at the expense of C-ness.
13+
14+
Translated from C to Python by Guido van Rossum.
15+
16+
Version History:
17+
18+
Version 1.1 corrects two bugs in version 1.0:
19+
20+
First, it leaked memory: in Proc1(), NextRecord ends
21+
up having a pointer to itself. I have corrected this
22+
by zapping NextRecord.PtrComp at the end of Proc1().
23+
24+
Second, Proc3() used the operator != to compare a
25+
record to None. This is rather inefficient and not
26+
true to the intention of the original benchmark (where
27+
a pointer comparison to None is intended; the !=
28+
operator attempts to find a method __cmp__ to do value
29+
comparison of the record). Version 1.1 runs 5-10
30+
percent faster than version 1.0, so benchmark figures
31+
of different versions can't be compared directly.
32+
33+
"""
34+
35+
LOOPS = 50000
36+
37+
from time import clock
38+
39+
__version__ = "1.1"
40+
41+
[Ident1, Ident2, Ident3, Ident4, Ident5] = range(1, 6)
42+
43+
class Record:
44+
45+
def __init__(self, PtrComp = None, Discr = 0, EnumComp = 0,
46+
IntComp = 0, StringComp = 0):
47+
self.PtrComp = PtrComp
48+
self.Discr = Discr
49+
self.EnumComp = EnumComp
50+
self.IntComp = IntComp
51+
self.StringComp = StringComp
52+
53+
def copy(self):
54+
return Record(self.PtrComp, self.Discr, self.EnumComp,
55+
self.IntComp, self.StringComp)
56+
57+
TRUE = 1
58+
FALSE = 0
59+
60+
def main(loops=LOOPS):
61+
benchtime, stones = pystones(loops)
62+
print("Pystone(%s) time for %d passes = %g" % \
63+
(__version__, loops, benchtime))
64+
print("This machine benchmarks at %g pystones/second" % stones)
65+
66+
67+
def pystones(loops=LOOPS):
68+
return Proc0(loops)
69+
70+
IntGlob = 0
71+
BoolGlob = FALSE
72+
Char1Glob = '\0'
73+
Char2Glob = '\0'
74+
Array1Glob = [0]*51
75+
Array2Glob = [x[:] for x in [Array1Glob]*51]
76+
PtrGlb = None
77+
PtrGlbNext = None
78+
79+
def Proc0(loops=LOOPS):
80+
global IntGlob
81+
global BoolGlob
82+
global Char1Glob
83+
global Char2Glob
84+
global Array1Glob
85+
global Array2Glob
86+
global PtrGlb
87+
global PtrGlbNext
88+
89+
starttime = clock()
90+
for i in range(loops):
91+
pass
92+
nulltime = clock() - starttime
93+
94+
PtrGlbNext = Record()
95+
PtrGlb = Record()
96+
PtrGlb.PtrComp = PtrGlbNext
97+
PtrGlb.Discr = Ident1
98+
PtrGlb.EnumComp = Ident3
99+
PtrGlb.IntComp = 40
100+
PtrGlb.StringComp = "DHRYSTONE PROGRAM, SOME STRING"
101+
String1Loc = "DHRYSTONE PROGRAM, 1'ST STRING"
102+
Array2Glob[8][7] = 10
103+
104+
starttime = clock()
105+
106+
for i in range(loops):
107+
Proc5()
108+
Proc4()
109+
IntLoc1 = 2
110+
IntLoc2 = 3
111+
String2Loc = "DHRYSTONE PROGRAM, 2'ND STRING"
112+
EnumLoc = Ident2
113+
BoolGlob = not Func2(String1Loc, String2Loc)
114+
while IntLoc1 < IntLoc2:
115+
IntLoc3 = 5 * IntLoc1 - IntLoc2
116+
IntLoc3 = Proc7(IntLoc1, IntLoc2)
117+
IntLoc1 = IntLoc1 + 1
118+
Proc8(Array1Glob, Array2Glob, IntLoc1, IntLoc3)
119+
PtrGlb = Proc1(PtrGlb)
120+
CharIndex = 'A'
121+
while CharIndex <= Char2Glob:
122+
if EnumLoc == Func1(CharIndex, 'C'):
123+
EnumLoc = Proc6(Ident1)
124+
CharIndex = chr(ord(CharIndex)+1)
125+
IntLoc3 = IntLoc2 * IntLoc1
126+
IntLoc2 = IntLoc3 / IntLoc1
127+
IntLoc2 = 7 * (IntLoc3 - IntLoc2) - IntLoc1
128+
IntLoc1 = Proc2(IntLoc1)
129+
130+
benchtime = clock() - starttime - nulltime
131+
if benchtime == 0.0:
132+
loopsPerBenchtime = 0.0
133+
else:
134+
loopsPerBenchtime = (loops / benchtime)
135+
return benchtime, loopsPerBenchtime
136+
137+
def Proc1(PtrParIn):
138+
PtrParIn.PtrComp = NextRecord = PtrGlb.copy()
139+
PtrParIn.IntComp = 5
140+
NextRecord.IntComp = PtrParIn.IntComp
141+
NextRecord.PtrComp = PtrParIn.PtrComp
142+
NextRecord.PtrComp = Proc3(NextRecord.PtrComp)
143+
if NextRecord.Discr == Ident1:
144+
NextRecord.IntComp = 6
145+
NextRecord.EnumComp = Proc6(PtrParIn.EnumComp)
146+
NextRecord.PtrComp = PtrGlb.PtrComp
147+
NextRecord.IntComp = Proc7(NextRecord.IntComp, 10)
148+
else:
149+
PtrParIn = NextRecord.copy()
150+
NextRecord.PtrComp = None
151+
return PtrParIn
152+
153+
def Proc2(IntParIO):
154+
IntLoc = IntParIO + 10
155+
while 1:
156+
if Char1Glob == 'A':
157+
IntLoc = IntLoc - 1
158+
IntParIO = IntLoc - IntGlob
159+
EnumLoc = Ident1
160+
if EnumLoc == Ident1:
161+
break
162+
return IntParIO
163+
164+
def Proc3(PtrParOut):
165+
global IntGlob
166+
167+
if PtrGlb is not None:
168+
PtrParOut = PtrGlb.PtrComp
169+
else:
170+
IntGlob = 100
171+
PtrGlb.IntComp = Proc7(10, IntGlob)
172+
return PtrParOut
173+
174+
def Proc4():
175+
global Char2Glob
176+
177+
BoolLoc = Char1Glob == 'A'
178+
BoolLoc = BoolLoc or BoolGlob
179+
Char2Glob = 'B'
180+
181+
def Proc5():
182+
global Char1Glob
183+
global BoolGlob
184+
185+
Char1Glob = 'A'
186+
BoolGlob = FALSE
187+
188+
def Proc6(EnumParIn):
189+
EnumParOut = EnumParIn
190+
if not Func3(EnumParIn):
191+
EnumParOut = Ident4
192+
if EnumParIn == Ident1:
193+
EnumParOut = Ident1
194+
elif EnumParIn == Ident2:
195+
if IntGlob > 100:
196+
EnumParOut = Ident1
197+
else:
198+
EnumParOut = Ident4
199+
elif EnumParIn == Ident3:
200+
EnumParOut = Ident2
201+
elif EnumParIn == Ident4:
202+
pass
203+
elif EnumParIn == Ident5:
204+
EnumParOut = Ident3
205+
return EnumParOut
206+
207+
def Proc7(IntParI1, IntParI2):
208+
IntLoc = IntParI1 + 2
209+
IntParOut = IntParI2 + IntLoc
210+
return IntParOut
211+
212+
def Proc8(Array1Par, Array2Par, IntParI1, IntParI2):
213+
global IntGlob
214+
215+
IntLoc = IntParI1 + 5
216+
Array1Par[IntLoc] = IntParI2
217+
Array1Par[IntLoc+1] = Array1Par[IntLoc]
218+
Array1Par[IntLoc+30] = IntLoc
219+
for IntIndex in range(IntLoc, IntLoc+2):
220+
Array2Par[IntLoc][IntIndex] = IntLoc
221+
Array2Par[IntLoc][IntLoc-1] = Array2Par[IntLoc][IntLoc-1] + 1
222+
Array2Par[IntLoc+20][IntLoc] = Array1Par[IntLoc]
223+
IntGlob = 5
224+
225+
def Func1(CharPar1, CharPar2):
226+
CharLoc1 = CharPar1
227+
CharLoc2 = CharLoc1
228+
if CharLoc2 != CharPar2:
229+
return Ident1
230+
else:
231+
return Ident2
232+
233+
def Func2(StrParI1, StrParI2):
234+
IntLoc = 1
235+
while IntLoc <= 1:
236+
if Func1(StrParI1[IntLoc], StrParI2[IntLoc+1]) == Ident1:
237+
CharLoc = 'A'
238+
IntLoc = IntLoc + 1
239+
if CharLoc >= 'W' and CharLoc <= 'Z':
240+
IntLoc = 7
241+
if CharLoc == 'X':
242+
return TRUE
243+
else:
244+
if StrParI1 > StrParI2:
245+
IntLoc = IntLoc + 7
246+
return TRUE
247+
else:
248+
return FALSE
249+
250+
def Func3(EnumParIn):
251+
EnumLoc = EnumParIn
252+
if EnumLoc == Ident3: return TRUE
253+
return FALSE
254+
255+
if __name__ == '__main__':
256+
import sys
257+
def error(msg):
258+
print(msg, end=' ', file=sys.stderr)
259+
print("usage: %s [number_of_loops]" % sys.argv[0], file=sys.stderr)
260+
sys.exit(100)
261+
print(sys.argv)
262+
nargs = len(sys.argv) - 1
263+
if nargs > 1:
264+
error("%d arguments are too many;" % nargs)
265+
elif nargs == 1:
266+
try: loops = int(sys.argv[1])
267+
except ValueError:
268+
error("Invalid argument %r;" % sys.argv[1])
269+
else:
270+
loops = LOOPS
271+
main(loops)

‎notes.txt

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Things to do before release
2121
* frame.Lasti is pointing past the instruction which puts tracebacks out
2222
* Subclass builtins
2323
* pygen
24+
* consider whether to re-use the grumpy runtime
2425

2526
FIXME recursive types for __repr__ in list, dict, tuple
2627
>>> L=[0]

0 commit comments

Comments
 (0)
Please sign in to comment.