forked from alex-eg/sex
space toplevel forms the way the source does
Every toplevel form was followed by a blank line whether or not one was written, so the prelude's run of typedefs arrived at twice the height. A function had no location of its own to measure from either, having lost it where its header and body are stitched together.
This commit is contained in:
@@ -123,7 +123,8 @@ Hello, Alex!
|
||||
* Features
|
||||
** Full C interoperability
|
||||
Just ~(include "Your/Favourite/Library.h")~ and use it as you would
|
||||
have is C.
|
||||
have is C. One ~include~ takes as many headers as you care to name:
|
||||
~(include stdio.h stdlib.h string.h)~.
|
||||
|
||||
*** Auto kebabification
|
||||
For hardcore fans of traditional Lisp naming convention,
|
||||
|
||||
@@ -36,7 +36,7 @@
|
||||
(define (line-directive src)
|
||||
;; `%line' is fmt-c's #line directive. cpp-line concatenates its
|
||||
;; second argument verbatim, so the file name arrives already quoted.
|
||||
`(%line ,(cdr src) ,(fmt #f #\" (car src) #\")))
|
||||
`(%line ,(cadr src) ,(fmt #f #\" (car src) #\")))
|
||||
|
||||
(define (walk-body stmts)
|
||||
"Walk a statement list, re-anchoring each statement that has a known
|
||||
@@ -498,14 +498,32 @@ forms, and what remains."
|
||||
(('define name . rest) `(%define ,(atom-to-fmt-c name) ,@rest))
|
||||
(else (walk-expr form))))
|
||||
|
||||
;;; Two toplevel forms are separated by a blank line only if they were
|
||||
;;; written that way: the prelude's typedefs come out packed, and so do
|
||||
;;; the three includes at the top of a program that wrote them as three
|
||||
;;; adjacent lines. Forms from different files -- the prelude, then an
|
||||
;;; imported module, then the unit itself -- always get one.
|
||||
(define (blank-line-between? prev next)
|
||||
(if (equal? (form-file prev) (form-file next))
|
||||
(let ((end (form-end-line prev))
|
||||
(start (form-line next)))
|
||||
(and end start (> (- start end) 1)))
|
||||
#t))
|
||||
|
||||
(define (emit-c sex-forms)
|
||||
(for-each (lambda (form)
|
||||
;; Forms the reader did not produce -- the prelude, and
|
||||
;; anything a macro built that we could not attribute --
|
||||
;; have no location and get no directive.
|
||||
(let ((src (and (not (eq? (sex-line-directives) 'none))
|
||||
(form-source form))))
|
||||
(when src
|
||||
(fmt #t (c-expr (line-directive src)))))
|
||||
(fmt #t (c-expr (process-toplevel-form form)) nl))
|
||||
(pack-comments sex-forms)))
|
||||
(let emit ((forms (pack-comments sex-forms)) (prev #f))
|
||||
(unless (null? forms)
|
||||
(let ((form (car forms)))
|
||||
(when (and prev (blank-line-between? prev form))
|
||||
(fmt #t nl))
|
||||
;; Forms the reader did not produce -- the prelude, and
|
||||
;; anything a macro built that we could not attribute --
|
||||
;; have no location and get no directive.
|
||||
(let ((src (and (not (eq? (sex-line-directives) 'none))
|
||||
(form-source form))))
|
||||
(when src
|
||||
(fmt #t (c-expr (line-directive src)))))
|
||||
;; `fl', not `nl': a comment leaves the cursor at the end of
|
||||
;; its line while everything else has already broken it
|
||||
(fmt #t (c-expr (process-toplevel-form form)) fl)
|
||||
(emit (cdr forms) form)))))
|
||||
|
||||
@@ -38,11 +38,13 @@
|
||||
(define (peek port)
|
||||
(peek-char port))
|
||||
|
||||
;;; Record where a form started. Called with the line of the opening
|
||||
;;; delimiter, sampled before it is consumed
|
||||
;;; Record where a form started and where it ended. Called with the
|
||||
;;; line of the opening delimiter, sampled before it is consumed, once
|
||||
;;; the form itself has been read -- so the current line is the one its
|
||||
;;; closing delimiter sits on
|
||||
(define (stamp form line)
|
||||
(when (pair? form)
|
||||
(set-form-source! form (current-source-file) line))
|
||||
(set-form-source! form (current-source-file) line (current-line)))
|
||||
form)
|
||||
|
||||
(define (delimiter? c)
|
||||
|
||||
@@ -254,7 +254,7 @@
|
||||
;; emits the comment immediately before the declaration
|
||||
(cons form
|
||||
(if doc
|
||||
(cons (list 'comment doc) acc)
|
||||
(cons (copy-form-source! form (list 'comment doc)) acc)
|
||||
acc)))
|
||||
|
||||
(define (strip-fn-header-comments fn-form)
|
||||
@@ -277,7 +277,11 @@
|
||||
;; the parameters are the body's outermost scope
|
||||
(header (map resolve-closure-types (take expanded boundary)))
|
||||
(body (walk-body (drop expanded boundary) env))
|
||||
(processed (append header body)))
|
||||
;; A function is header and body stitched together, so the
|
||||
;; result is fresh structure: without this the definition
|
||||
;; has no location, and nothing downstream can say where
|
||||
;; the function was written
|
||||
(processed (copy-form-source! sex-fn-raw (append header body))))
|
||||
(with-docstring doc processed
|
||||
(append (hash-table-ref env :lambda-aux-code) acc))))))
|
||||
|
||||
|
||||
@@ -633,4 +633,18 @@ compiles."
|
||||
(test-assert "nor is an aggregate tag"
|
||||
(emits? "(struct t ((z int))) (struct s ((a (¤ struct t))))" "struct t a[]"))
|
||||
(test-assert "nor one behind a pointer"
|
||||
(emits? "(struct t ((z int))) (struct s ((a (¤ * struct t))))" "struct t* a[]"))))
|
||||
(emits? "(struct t ((z int))) (struct s ((a (¤ * struct t))))" "struct t* a[]")))
|
||||
|
||||
;; Toplevel forms used to be separated by a blank line unconditionally,
|
||||
;; which spread a block of adjacent typedefs over twice the lines its
|
||||
;; author wrote
|
||||
(test-group "vertical spacing"
|
||||
(test-assert "adjacent forms stay adjacent"
|
||||
(emits? "(include a.h)\n(include b.h)" "#include <a.h>\n#include <b.h>"))
|
||||
(test-assert "a blank line is kept"
|
||||
(emits? "(include a.h)\n\n(include b.h)" "#include <a.h>\n\n#include <b.h>"))
|
||||
;; the gap is measured from where the form ended, not where it began
|
||||
(test-assert "a multi-line form does not invent one"
|
||||
(emits? "(struct s\n ((x int)))\n(include b.h)" "};\n#include <b.h>"))
|
||||
(test-assert "nor swallows one"
|
||||
(emits? "(struct s\n ((x int)))\n\n(include b.h)" "};\n\n#include <b.h>"))))
|
||||
|
||||
@@ -97,7 +97,7 @@
|
||||
(define (compile-to-c source)
|
||||
"Run reader -> semen -> writer on SOURCE, returning the generated C.
|
||||
parse-all is called directly rather than through read-raw-forms so the
|
||||
fixture can be given a file name: a location is (file . line), and the
|
||||
fixture can be given a file name: a location carries one, and the
|
||||
file is what makes an imported module report itself rather than the unit
|
||||
that imported it."
|
||||
(let ((forms (with-input-from-string source
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
form-source
|
||||
form-file
|
||||
form-line
|
||||
form-end-line
|
||||
copy-form-source!
|
||||
stamp-form-source!
|
||||
form-location
|
||||
|
||||
19
utils.scm
19
utils.scm
@@ -93,9 +93,12 @@
|
||||
;;; read-with-source-info / get-line-number, which only works when forms
|
||||
;;; are produced by the built-in `read'.
|
||||
;;;
|
||||
;;; A location is (file . line). The file matters because imported
|
||||
;;; A location is (file line end-line): where the form starts and the
|
||||
;;; line its closing delimiter is on. The file matters because imported
|
||||
;;; modules paste their public forms into the current unit: those forms
|
||||
;;; originate in another file and must be reported as such
|
||||
;;; originate in another file and must be reported as such. The end line
|
||||
;;; tells the C writer whether two toplevel forms were written with a
|
||||
;;; blank line between them
|
||||
(define +form-sources+ (make-hash-table eq?))
|
||||
|
||||
;;; The file `parse-all' is currently reading. Bound by the reader
|
||||
@@ -114,8 +117,8 @@
|
||||
(define (form-type form)
|
||||
(hash-table-ref/default +form-types+ form #f))
|
||||
|
||||
(define (set-form-source! form file line)
|
||||
(hash-table-set! +form-sources+ form (cons file line)))
|
||||
(define (set-form-source! form file line #!optional (end-line line))
|
||||
(hash-table-set! +form-sources+ form (list file line end-line)))
|
||||
|
||||
(define (form-source form)
|
||||
(hash-table-ref/default +form-sources+ form #f))
|
||||
@@ -126,7 +129,11 @@
|
||||
|
||||
(define (form-line form)
|
||||
(let ((src (form-source form)))
|
||||
(and src (cdr src))))
|
||||
(and src (cadr src))))
|
||||
|
||||
(define (form-end-line form)
|
||||
(let ((src (form-source form)))
|
||||
(and src (caddr src))))
|
||||
|
||||
(define (copy-form-source! from to)
|
||||
"Give TO the location of FROM, if FROM has one. Returns TO, so it can
|
||||
@@ -144,7 +151,7 @@ wrap a form-building expression."
|
||||
"\"file:line: \" for FORM, or \"\" when it has none"
|
||||
(let ((src (form-source form)))
|
||||
(if src
|
||||
(string-append (car src) ":" (number->string (cdr src)) ": ")
|
||||
(string-append (car src) ":" (number->string (cadr src)) ": ")
|
||||
"")))
|
||||
|
||||
(define (sex-error form message . args)
|
||||
|
||||
Reference in New Issue
Block a user