Skip to content

Commit 0e8b4c0

Browse files
videlecjdemeyer
authored andcommitted
autogeneration of paridecl.pxd
1 parent 9ea328a commit 0e8b4c0

File tree

3 files changed

+69
-38
lines changed

3 files changed

+69
-38
lines changed

.gitignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ cypari2/auto_gen.pxi.tmp
44
cypari2/auto_gen.pxi
55
cypari2/auto_instance.pxi.tmp
66
cypari2/auto_instance.pxi
7-
cypari2/auto_paridecl.pxd.tmp
8-
cypari2/auto_paridecl.pxd
7+
cypari2/paridecl.pxd.tmp
8+
cypari2/paridecl.pxd
99
cypari2/closure.c
1010
cypari2/convert.c
1111
cypari2/gen.c

autogen/generator.py

+56-26
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,6 @@
5050
"""
5151
'''
5252

53-
decl_banner = autogen_top + '''
54-
from .types cimport *
55-
56-
cdef extern from *:
57-
'''
58-
5953

6054
function_re = re.compile(r"^[A-Za-z][A-Za-z0-9_]*$")
6155
function_blacklist = {"O", # O(p^e) needs special parser support
@@ -82,7 +76,27 @@ class PariFunctionGenerator(object):
8276
def __init__(self):
8377
self.gen_filename = os.path.join('cypari2', 'auto_gen.pxi')
8478
self.instance_filename = os.path.join('cypari2', 'auto_instance.pxi')
85-
self.decl_filename = os.path.join('cypari2', 'auto_paridecl.pxd')
79+
self.decl_filename = os.path.join('cypari2', 'paridecl.pxd')
80+
81+
def write_paridecl_no_desc(self, D):
82+
r"""
83+
Write the template ``template_paridecl.pxd`` into the declaration file
84+
after removing all functions from ``D`` (that are obtained from
85+
``pari.desc``).
86+
"""
87+
fnc = re.compile(" (int|long|void|GEN) +([A-Za-z][0-9A-Za-z_]*)\(.*\)")
88+
89+
functions = set(f.get('cname') for f in D)
90+
91+
with open('autogen/template_paridecl.pxd') as template:
92+
for line in template.read().split('\n'):
93+
match = fnc.match(line)
94+
if match:
95+
out_type, fname = match.groups()
96+
if fname in functions:
97+
continue
98+
self.decl_file.write(line)
99+
self.decl_file.write("\n")
86100

87101
def can_handle_function(self, function, cname="", **kwds):
88102
"""
@@ -117,9 +131,10 @@ def can_handle_function(self, function, cname="", **kwds):
117131
if sec == "programming/control":
118132
# Skip if, return, break, ...
119133
return False
134+
120135
return True
121136

122-
def handle_pari_function(self, function, cname, prototype="", help="", obsolete=None, **kwds):
137+
def handle_pari_function(self, function, cname, prototype, help, args=None, ret=None, obsolete=None, **kwds):
123138
r"""
124139
Handle one PARI function: decide whether or not to add the
125140
function, in which file (as method of :class:`Gen` or
@@ -128,8 +143,8 @@ def handle_pari_function(self, function, cname, prototype="", help="", obsolete=
128143
129144
EXAMPLES::
130145
131-
>>> from autogen.parser import read_pari_desc
132146
>>> from autogen.generator import PariFunctionGenerator
147+
>>> import sys
133148
>>> G = PariFunctionGenerator()
134149
>>> G.gen_file = sys.stdout
135150
>>> G.instance_file = sys.stdout
@@ -226,13 +241,14 @@ def polredord(self, x):
226241
return new_gen(_ret)
227242
<BLANKLINE>
228243
"""
229-
try:
230-
args, ret = parse_prototype(prototype, help)
231-
except NotImplementedError:
232-
return # Skip unsupported prototype codes
233-
244+
if args is None or ret is None:
245+
aargs, rret = parse_prototype(prototype, help)
246+
if args is None:
247+
args = aargs
248+
if ret is None:
249+
ret = rret
250+
sys.stdout.flush()
234251
doc = get_rest_doc(function)
235-
236252
self.write_declaration(cname, args, ret, self.decl_file)
237253

238254
if len(args) > 0 and isinstance(args[0], PariArgumentGEN):
@@ -320,29 +336,43 @@ def __call__(self):
320336
"""
321337
D = read_pari_desc()
322338
D = sorted(D.values(), key=lambda d: d['function'])
323-
sys.stdout.write("Generating PARI functions:")
324339

325340
self.gen_file = io.open(self.gen_filename + '.tmp', 'w', encoding='utf-8')
326341
self.gen_file.write(gen_banner)
327342
self.instance_file = io.open(self.instance_filename + '.tmp', 'w', encoding='utf-8')
328343
self.instance_file.write(instance_banner)
329344
self.decl_file = io.open(self.decl_filename + '.tmp', 'w', encoding='utf-8')
330-
self.decl_file.write(decl_banner)
345+
346+
DD = []
347+
sys.stdout.write("Unhandled PARI function:\n")
348+
for v in D:
349+
func = v["function"]
350+
if not self.can_handle_function(**v):
351+
sys.stdout.write(" (%s)" % func)
352+
else:
353+
try:
354+
args, ret = parse_prototype(v["prototype"], v["help"])
355+
v["args"] = args
356+
v["ret"] = ret
357+
DD.append(v)
358+
except NotImplementedError:
359+
sys.stdout.write(" ((%s))" % func)
360+
sys.stdout.write("\n")
361+
362+
self.write_paridecl_no_desc(DD)
331363

332364
# Check for availability of hi-res SVG plotting. This requires
333365
# PARI-2.10 or later.
334366
have_plot_svg = False
335367

336-
for v in D:
368+
sys.stdout.write("Generating PARI functions:\n")
369+
for v in DD:
337370
func = v["function"]
338-
if self.can_handle_function(**v):
339-
sys.stdout.write(" %s" % func)
340-
sys.stdout.flush()
341-
self.handle_pari_function(**v)
342-
if func == "plothraw":
343-
have_plot_svg = True
344-
else:
345-
sys.stdout.write(" (%s)" % func)
371+
sys.stdout.write(" %s" % func)
372+
self.handle_pari_function(**v)
373+
sys.stdout.flush()
374+
if func == "plothraw":
375+
have_plot_svg = True
346376
sys.stdout.write("\n")
347377

348378
self.instance_file.write("DEF HAVE_PLOT_SVG = {}".format(have_plot_svg))

cypari2/paridecl.pxd renamed to autogen/template_paridecl.pxd

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
# distutils: libraries = gmp pari
22
"""
3-
Declarations for non-inline functions from PARI.
3+
Declarations of PARI functions.
44
55
This file contains all declarations from headers/paridecl.h from
6-
the PARI distribution, except the inline functions which are in
7-
declinl.pxi (that file is automatically included by this file).
8-
6+
the PARI distribution.
97
108
AUTHORS:
119
@@ -20,6 +18,9 @@ AUTHORS:
2018
2119
- Jeroen Demeyer (2014-09-19): upgrade to PARI 2.8 (:trac:`16997`)
2220
21+
- Vincent Delecroix (2017-2018): auto-generate part of the declarations
22+
from ``pari.desc``
23+
2324
"""
2425

2526
#*****************************************************************************
@@ -5172,12 +5173,6 @@ cdef inline int is_universal_constant(GEN x):
51725173
return _is_universal_constant(x) or (x is err_e_STACK)
51735174

51745175

5175-
# Auto-generated declarations. There are taken from the PARI version
5176-
# on the system, so they more up-to-date than the above. In case of
5177-
# conflicting declarations, auto_paridecl should have priority.
5178-
from .auto_paridecl cimport *
5179-
5180-
51815176
cdef inline int is_on_stack(GEN x) except -1:
51825177
"""
51835178
Is the GEN ``x`` stored on the PARI stack?
@@ -5188,3 +5183,9 @@ cdef inline int is_on_stack(GEN x) except -1:
51885183
if pari_mainstack.vbot <= s:
51895184
raise SystemError("PARI object in unused part of PARI stack")
51905185
return 0
5186+
5187+
5188+
#########################################################
5189+
# All functions below are auto-generated from pari.desc #
5190+
#########################################################
5191+
cdef extern from *:

0 commit comments

Comments
 (0)