index a unit past its first bad form

Record plain declarations, with location and docstring, and keep going
when one toplevel form is wrong. sexc --check and --symbols print
that. Specs, realizations, and closure helpers stay out of the index
for future rework (@alex-eg).
This commit is contained in:
2026-11-11 07:38:33 +03:00
parent 5e842c4df6
commit 7bbb8e19ac
10 changed files with 876 additions and 50 deletions

View File

@@ -17,6 +17,7 @@
getopt-long
sex-macros
sex-modules
sex-query
reader
semen
srfi-1 ; list routines
@@ -80,6 +81,19 @@
(pad padding) "land on the right source line; `none' is for reading -C" nl
(pad padding) "output by eye")
(required #f)
(value #t))
(check ,(fmt #f "Print semantic diagnostics as an s-expression and" nl
(pad padding) "do not compile. Exit status is non-zero only" nl
(pad padding) "when the file cannot be read")
(required #f)
(value #f))
(symbols "Print the symbol table as an s-expression and do not compile"
(required #f)
(value #f))
(expand-lines
,(fmt #f "Print the C for Sex lines START:END. START with no end," nl
(pad padding) "or `:`, is the whole buffer. Does not compile")
(required #f)
(value #t)))))
(define (print-help)
@@ -169,7 +183,7 @@
;;; executable or an object goes to cc's -o, where `-' is a file name
(define (text-output? args)
(any (lambda (option) (get-arg args option #f))
'(emit-c macro-expand public-interface)))
'(emit-c macro-expand public-interface check symbols expand-lines)))
(define (write-to-file-or-stdout output what)
(if (stdout-output? output)
@@ -251,23 +265,43 @@ status, which is ours to pass on."
(mark-imported! (to-absolute-pathname input-source)))
(let ((process (lambda ()
(check-public-interface unit-forms)
(semen-process (append prelude unit-forms)))))
(semen-process (append (compiler-prelude) unit-forms)))))
(if (eq? input-source 'stdin)
(process)
(with-directory input-source (process)))))
(define prelude
'((include inttypes.h)
(include stdbool.h)
(include stddef.h)
(typedef u8 uint8-t)
(typedef i8 int8-t)
(typedef u16 uint16-t)
(typedef i16 int16-t)
(typedef u32 uint32-t)
(typedef i32 int32-t)
(typedef u64 uint64-t)
(typedef i64 int64-t)))
;;; Query output. C from --expand-lines owns stdout; diagnostics and the
;;; symbol table go beside it on stderr so a slice stays valid text.
;;; On their own they are the stdout of the command.
(define (report-query analysis args output)
(let* ((expanding? (get-arg args 'expand-lines #f))
(want-check? (get-arg args 'check #f))
(want-symbols? (get-arg args 'symbols #f))
(range (and expanding? (parse-expand-lines expanding?))))
;; A reader failure is the one non-zero status. Say what it was,
;; on stdout for --check and on stderr when stdout is C.
(unless (analysis-read? analysis)
(let ((where (if (or expanding? (not want-check?))
(current-error-port)
#f)))
(if where
(write-diagnostics analysis where)
(write-to-file-or-stdout
output
(lambda () (write-diagnostics analysis (current-output-port))))))
(exit 70))
(when (or want-check? want-symbols?)
(let ((port (if expanding? (current-error-port) #f)))
(define (write-reports)
(when want-check? (write-diagnostics analysis (current-output-port)))
(when want-symbols? (write-symbols analysis (current-output-port))))
(if port
(with-output-to-port port write-reports)
(write-to-file-or-stdout output write-reports))))
(when expanding?
(let ((text (analysis-expand analysis (car range) (cdr range))))
(unless text (exit 70))
(write-to-file-or-stdout output (lambda () (display text)))))))
;;; An error is told by its message, file:line first where it has one.
;;; CHICKEN's call history after it is for working on sexc itself, and
@@ -307,7 +341,7 @@ status, which is ours to pass on."
(print-help)
(return #f))
(when (and (equal? output "-") (not (text-output? args)))
(error "-o - is stdout, which takes only -C, -m or --public-interface"))
(error "-o - is stdout, which takes only -C, -m, --check, --symbols, --expand-lines or --public-interface"))
(when (same-file? input output)
(error "-o names the input, and would overwrite its source" input))
(when (and (string? input) (not (file-exists? input)))
@@ -324,6 +358,15 @@ status, which is ours to pass on."
;; what types they export
(load-persistent-module-paths)
(when (or (get-arg args 'check #f)
(get-arg args 'symbols #f)
(get-arg args 'expand-lines #f))
(report-query (if (eq? input 'stdin)
(analyze-port (current-input-port) "stdin")
(analyze-file input))
args output)
(return #f))
(when (get-arg args 'public-interface #f)
(assert (not (eq? input 'stdin)) "Error: --public-interface requires file argument")