Skip to content

Commit f54ac9c

Browse files
authored
Add pprint versions of codegen macros for readability. (#1437)
* Add pretty-printing macros for codegen macros. * Define UFix type to print nicer in the output of codegen. * `UFix` -> `ufixnum`. * Add the macro names instead of generating for allowing to grep. * `print` -> `pprint`.
1 parent 7479591 commit f54ac9c

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

Diff for: library/primitive-types.lisp

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,6 @@
7676
(define-type IFix
7777
"Non-allocating tagged integer; range is platform-dependent. Does not error on overflow. Uses `fixnum`.")
7878

79-
(repr :native (cl:and cl:fixnum cl:unsigned-byte))
80-
(define-type UFix
79+
(repr :native coalton-impl/util:ufixnum)
80+
(define-type UFix
8181
"Non-allocating tagged non-negative integer; range is platform-dependent. Uses `(and fixnum unsigned-byte)`."))

Diff for: src/package.lisp

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
#:coalton-codegen
2020
#:coalton-codegen-ast
2121
#:coalton-codegen-types
22+
#:pprint-coalton-codegen
23+
#:pprint-coalton-codegen-ast
24+
#:pprint-coalton-codegen-types
2225
#:coalton
2326
#:declare
2427
#:define

Diff for: src/reader.lisp

+23
Original file line numberDiff line numberDiff line change
@@ -184,5 +184,28 @@ It ensures the presence of source metadata for STREAM and then calls MAYBE-READ-
184184
"Dump the AST for toplevel definitions occurring in FORMS to *standard-out* and return NIL."
185185
(compile-forms 'coalton:coalton-codegen-ast forms))
186186

187+
(defmacro define-pprint-codegen-macro (name codegen)
188+
"Generate a pretty-printing macro for codegen macros."
189+
`(defmacro ,name (&body forms)
190+
,(format nil "Pretty print code generated by ~S." codegen)
191+
(let ((package (gensym "PACKAGE")))
192+
`(let ((,package (make-package '#:pprint-codegen-package
193+
:use '(#:cl))))
194+
(unwind-protect (progn
195+
;; Import all symbols in the current package
196+
;; not to print its package prefix.
197+
(do-symbols (symb *package*)
198+
(unless (nth-value 1
199+
(find-symbol (symbol-name symb) ,package))
200+
(import symb ,package)))
201+
(let ((*package* ,package))
202+
(pprint (,',codegen ,@forms))
203+
(values)))
204+
(delete-package ,package))))))
205+
206+
(define-pprint-codegen-macro coalton:pprint-coalton-codegen coalton:coalton-codegen)
207+
(define-pprint-codegen-macro coalton:pprint-coalton-codegen-types coalton:coalton-codegen-types)
208+
(define-pprint-codegen-macro coalton:pprint-coalton-codegen-ast coalton:coalton-codegen-ast)
209+
187210
(defmacro coalton:coalton (&rest forms)
188211
(compile-forms 'coalton:coalton forms))

Diff for: src/utilities.lisp

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#:project-indices ; FUNCTION
3333
#:project-elements ; FUNCTION
3434
#:maybe-read-form ; FUNCTION
35+
#:ufixnum ; TYPE
3536
))
3637

3738
(in-package #:coalton-impl/util)
@@ -216,3 +217,6 @@ Examples:
216217
(mapcar (lambda (idx)
217218
(nth (position idx list) data))
218219
elements))
220+
221+
(deftype ufixnum ()
222+
'(and fixnum unsigned-byte))

0 commit comments

Comments
 (0)