Debian Codegraph @

external/scheme-builtins-generator.scm

raw
1 ;; Autogenerate a list of Scheme keywords (i.e., macros) and built-in
2 ;; functions. This is written for the Guile implementation. The
3 ;; principle of autogenerating this has the advantage of catching many
4 ;; builtins that would be tedious to maintain by hand, and the
5 ;; disadvantage that some builtins very specific to Guile and not
6 ;; relevant to other implementations are caught as well. However,
7 ;; since Scheme builtin function names tend to be rather specific,
8 ;; this should not be a significant problem.
9
10 (define port (open-output-file "../pygments/lexers/_scheme_builtins.py"))
11
12 (display
13 "\"\"\"
14 pygments.lexers._scheme_builtins
15 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16
17 Scheme builtins.
18
19 :copyright: Copyright 2006-2022 by the Pygments team, see AUTHORS.
20 :license: BSD, see LICENSE for details.
21 \"\"\"
22 "
23 port)
24
25 (format port
26 "\n# Autogenerated by external/scheme-builtins-generator.scm\n\
27 # using Guile ~a.\n\n"
28 (version))
29
30 (use-modules (srfi srfi-1)
31 (ice-9 match))
32
33 (define relevant-modules
34 ;; This is a nightmare. Scheme builtins are split in
35 ;; gazillions of standards, SRFIs and implementation
36 ;; extensions. With so many sources, it's hard to define
37 ;; what is really a Scheme builtin. This is a rather
38 ;; conservative list of Guile modules that might be used
39 ;; the most frequently (somewhat subjective, admittedly).
40 '(
41 ;; The real builtins.
42 (guile)
43 ;; Let's include the fundamental list library.
44 (srfi srfi-1)
45 ;; define-record-type
46 (srfi srfi-9)
47 ;; let-values, let*-values
48 (srfi srfi-11)
49 ;; case-lambda
50 (srfi srfi-16)
51 ;; Pattern matching
52 (ice-9 match)
53 ;; Included for compatibility with files written for R5RS
54 (rnrs r5rs)))
55
56 (define (get-all-bindings module)
57 ;; Need to recurse to find all public bindings. module-map
58 ;; only considers the module's own bindings.
59 (let* ((own (module-map cons module))
60 (uses (module-uses module)))
61 (append own (append-map get-all-bindings uses))))
62
63 (define all-bindings
64 (append-map
65 ;; Need to use module-public-interface to restrict to
66 ;; public bindings. Note that module-uses already
67 ;; returns public interfaces.
68 (lambda (mod-path)
69 (let* ((mod-object (resolve-module mod-path))
70 (iface (module-public-interface mod-object)))
71 (get-all-bindings iface)))
72 relevant-modules))
73
74 (define (filter-for pred)
75 (filter-map
76 (match-lambda
77 ((key . variable)
78 (and (variable-bound? variable)
79 (let ((value (variable-ref variable)))
80 (and (pred value)
81 key)))))
82 all-bindings))
83
84 (define (sort-and-uniq lst pred)
85 (let loop ((lst (sort lst pred))
86 (acc '()))
87 (match lst
88 (() (reverse! acc))
89 ((one . rest)
90 (loop (drop-while (lambda (elt)
91 (equal? elt one))
92 rest)
93 (cons one acc))))))
94
95 (define (dump-py-list lst)
96 (string-join
97 (map
98 (lambda (name)
99 (format #f " \"~a\"," name))
100 (sort-and-uniq
101 (map symbol->string lst)
102 string<?))
103 "\n"))
104
105 (define (dump-builtins name pred extra)
106 (format port
107 "~a = {\n~a\n}\n\n"
108 name
109 (dump-py-list (append extra (filter-for pred)))))
110
111 (define extra-procedures
112 ;; These are found in RnRS but not implemented by Guile.
113 '(load transcript-off transcript-on))
114
115 (dump-builtins 'scheme_keywords macro? '())
116 (dump-builtins 'scheme_builtins procedure? extra-procedures)