Every error ended in CHICKEN's call history, which is for working on sexc itself, and is now asked for by SEXC_DEBUG; a missing input file says so, rather than as an open-input-file error of CHICKEN's.
355 lines
14 KiB
Scheme
355 lines
14 KiB
Scheme
(import scheme
|
|
(scheme base) ; call/cc
|
|
brev-separate
|
|
(chicken base)
|
|
(chicken condition) ; handle-exceptions
|
|
(chicken file)
|
|
(chicken module)
|
|
(only (chicken pathname) normalize-pathname)
|
|
(chicken plist)
|
|
(chicken pretty-print)
|
|
(chicken process)
|
|
(chicken process-context)
|
|
(chicken port)
|
|
(chicken string) ; string-split
|
|
fmt
|
|
fmt-c-writer
|
|
getopt-long
|
|
sex-macros
|
|
sex-modules
|
|
reader
|
|
semen
|
|
srfi-1 ; list routines
|
|
utils)
|
|
|
|
(export
|
|
main)
|
|
|
|
;;; Main function facilities
|
|
|
|
(define opts-grammar
|
|
(let ((padding 26))
|
|
`((c-compiler ,(fmt #f "Select C compiler. Defaults to value of SEX_CC" nl
|
|
(pad padding) "environment variable, or if it is empty, to cc")
|
|
(required #f)
|
|
(value #t))
|
|
(compile-object "Compile object file instead of executable program"
|
|
(required #f)
|
|
(value #f)
|
|
(single-char #\c))
|
|
(features ,(fmt #f "Comma-separated feature names, added to the host's own" nl
|
|
(pad padding) "for #+ and #- feature expressions. May be given" nl
|
|
(pad padding) "more than once")
|
|
(required #f)
|
|
(value #t)
|
|
(single-char #\f))
|
|
(no-platform-features
|
|
,(fmt #f "Leave out the host's own features. With --features," nl
|
|
(pad padding) "this reads a file the way another platform would")
|
|
(required #f)
|
|
(value #f))
|
|
(emit-c "Emit C code"
|
|
(required #f)
|
|
(value #f)
|
|
(single-char #\C))
|
|
(public-interface "Get module's public interface"
|
|
(required #f)
|
|
(value #f))
|
|
(help "Show this help"
|
|
(required #f)
|
|
(value #f)
|
|
(single-char #\h))
|
|
(macro-expand "Emit macro-expanded semantically processed Sex code"
|
|
(required #f)
|
|
(value #f)
|
|
(single-char #\m))
|
|
(output ,(fmt #f "Write output to file. Default file name is a.out." nl
|
|
(pad padding) "With -C, -m or --public-interface, defaults to stdout," nl
|
|
(pad padding) "which `-' names too")
|
|
(required #f)
|
|
(value #t)
|
|
(single-char #\o))
|
|
(depfile
|
|
,(fmt #f "Write a make rule for the output to FILE: it depends on" nl
|
|
(pad padding) "the source and on every module read for it. Needs -o")
|
|
(required #f)
|
|
(value #t))
|
|
(line-directives
|
|
,(fmt #f "How much #line information to emit: statement (default)," nl
|
|
(pad padding) "toplevel, or none. `statement' is what makes a debugger" nl
|
|
(pad padding) "land on the right source line; `none' is for reading -C" nl
|
|
(pad padding) "output by eye")
|
|
(required #f)
|
|
(value #t)))))
|
|
|
|
(define (print-help)
|
|
(fmt #t "Usage: sexc [options] filename [-- options-for-c-compiler]\n")
|
|
(fmt #t "Options:\n")
|
|
(fmt #t (usage opts-grammar))
|
|
(fmt #t ""))
|
|
|
|
(define (help-arg? args)
|
|
(assoc 'help args))
|
|
|
|
(define (get-arg args arg-name default)
|
|
(let ((arg (assoc arg-name args)))
|
|
(if arg (cdr arg)
|
|
default)))
|
|
|
|
;;; Everything before the first `--' is ours to parse, everything after
|
|
;;; is handed to the C compiler verbatim
|
|
|
|
(define (separator? a)
|
|
(string=? a "--"))
|
|
|
|
(define (args-before-separator argv)
|
|
(take-while (complement separator?) argv))
|
|
|
|
(define (args-after-separator argv)
|
|
(let ((tail (drop-while (complement separator?) argv)))
|
|
(if (null? tail)
|
|
(list)
|
|
(cdr tail))))
|
|
|
|
;;; getopt-long reads a lone `-' as an option, so `-o -' died there as
|
|
;;; one missing its value. `--output=-' is the spelling it takes
|
|
(define (spell-stdout-output argv)
|
|
(let loop ((as argv) (acc (list)))
|
|
(cond ((null? as) (reverse acc))
|
|
((and (member (car as) '("-o" "--output"))
|
|
(pair? (cdr as))
|
|
(string=? (cadr as) "-"))
|
|
(loop (cddr as) (cons "--output=-" acc)))
|
|
(else (loop (cdr as) (cons (car as) acc))))))
|
|
|
|
;;; ...and a lone `-' left after that is the input, which is stdin: one
|
|
;;; getopt-long would read as an option it did not know, so it is taken
|
|
;;; out before
|
|
(define (stdin-dash? argv)
|
|
(and (member "-" argv) #t))
|
|
|
|
(define (get-rest-args args)
|
|
(cdr (assoc '@ args)))
|
|
|
|
(define (line-directives-arg args)
|
|
(let ((v (get-arg args 'line-directives "statement")))
|
|
(cond ((equal? v "statement") 'statement)
|
|
((equal? v "toplevel") 'toplevel)
|
|
((equal? v "none") 'none)
|
|
(else (error "--line-directives must be statement, toplevel or none, got" v)))))
|
|
|
|
|
|
|
|
;;; --features may be given more than once, and each may name several.
|
|
;;; Collect all of them
|
|
(define (cli-features args)
|
|
(append-map (lambda (entry)
|
|
(map string->symbol (string-split (cdr entry) ",")))
|
|
(filter (lambda (entry) (eq? (car entry) 'features)) args)))
|
|
|
|
(define (get-input-file args dash?)
|
|
(let ((inputs (append (if dash? (list "-") (list)) (get-rest-args args))))
|
|
(cond ((null? inputs) 'stdin)
|
|
;; any after the first were never read, and nothing said so
|
|
((pair? (cdr inputs))
|
|
(apply error "sexc compiles one input file at a time; given" inputs))
|
|
(dash? 'stdin)
|
|
(else (car inputs)))))
|
|
|
|
;;; Whether OUTPUT is the file INPUT, spelled however it is
|
|
(define (same-file? input output)
|
|
(and (string? input) (string? output) (not (stdout-output? output))
|
|
(equal? (normalize-pathname (to-absolute-pathname input))
|
|
(normalize-pathname (to-absolute-pathname output)))))
|
|
|
|
(define (stdout-output? output)
|
|
(or (eq? output 'default) (equal? output "-")))
|
|
|
|
;;; Whether ARGS ask for text, which `-o -' can send to stdout. An
|
|
;;; 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)))
|
|
|
|
(define (write-to-file-or-stdout output what)
|
|
(if (stdout-output? output)
|
|
(what)
|
|
;; a failure part way through leaves none of the file, which make
|
|
;; would take for done
|
|
(handle-exceptions exn
|
|
(begin (delete-file* output) (abort exn))
|
|
(with-output-to-file output
|
|
(fn (what))))))
|
|
|
|
(define (emit-c-or-sex sex-forms output args)
|
|
(write-to-file-or-stdout output
|
|
(lambda ()
|
|
(if (get-arg args 'macro-expand #f)
|
|
(map pp sex-forms)
|
|
(emit-c sex-forms)))))
|
|
|
|
;;; app.o: app.sex /m/greet.sex, and an empty rule per module, so that
|
|
;;; make takes a module deleted since as out of date rather than as a
|
|
;;; file it has no rule to make
|
|
(define (dependency-rule target source modules)
|
|
(let ((spell (lambda (path) (string-translate* path '((" " . "\\ "))))))
|
|
(apply string-append
|
|
(spell target) ":"
|
|
(append (map (lambda (path) (string-append " " (spell path)))
|
|
(if source (cons source modules) modules))
|
|
(list "\n")
|
|
(map (lambda (path) (string-append "\n" (spell path) ":\n"))
|
|
modules)))))
|
|
|
|
(define (write-dependencies depfile output input)
|
|
(when (stdout-output? output)
|
|
(error "--depfile needs -o, to name what depends on the modules"))
|
|
(with-output-to-file depfile
|
|
(lambda ()
|
|
(display (dependency-rule output
|
|
(and (not (eq? input 'stdin)) input)
|
|
(module-dependencies))))))
|
|
|
|
(define (compile-to-file sex-forms output args cc-args)
|
|
"Hand the generated C to the C compiler. Returns the compiler's exit
|
|
status, which is ours to pass on."
|
|
(let ((compiler (or (get-arg args 'c-compiler #f)
|
|
(get-env-var "SEX_CC")
|
|
"cc"))
|
|
(out-file (if (eq? output 'default)
|
|
"a.out"
|
|
output))
|
|
;; The generated C goes to a temporary .c file rather than the
|
|
;; compiler's stdin. It is removed however we leave -- emit-c
|
|
;; can throw, and used to leave the file behind when it did
|
|
(c-file (create-temporary-file "c")))
|
|
;; An unhandled error ends the process without unwinding, so the
|
|
;; cleanup cannot be left to dynamic-wind
|
|
(handle-exceptions exn
|
|
(begin (delete-file* c-file) (abort exn))
|
|
(with-output-to-file c-file
|
|
(lambda () (emit-c sex-forms)))
|
|
(let ((proc (process compiler (append (list "-o" out-file)
|
|
(if (get-arg args 'compile-object #f)
|
|
(list "-c")
|
|
(list))
|
|
(list c-file)
|
|
cc-args))))
|
|
(call-with-values (lambda () (process-wait proc))
|
|
(lambda (pid normal-exit? status)
|
|
(delete-file* c-file)
|
|
(if normal-exit? status 1)))))))
|
|
|
|
;;; The unit's own forms are checked for what its importers will need,
|
|
;;; the prelude's not: every unit has those
|
|
(define (semantic-process-forms unit-forms input-source)
|
|
(let ((process (lambda ()
|
|
(check-public-interface unit-forms)
|
|
(semen-process (append prelude unit-forms)))))
|
|
(if (eq? input-source 'stdin)
|
|
(process)
|
|
(with-directory input-source (process)))))
|
|
|
|
(define prelude
|
|
(append
|
|
'((include inttypes.h)
|
|
(include stdbool.h)
|
|
(include stddef.h) ; max_align_t, for closure environments
|
|
(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))
|
|
|
|
;; The closure environment is the part of the ABI, so include it in
|
|
;; every module
|
|
(list (closure-env-declaration))))
|
|
|
|
;;; 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
|
|
;;; SEXC_DEBUG asks for it
|
|
(define (main)
|
|
(if (get-environment-variable "SEXC_DEBUG")
|
|
(run)
|
|
(handle-exceptions exn
|
|
(let ((message ((condition-property-accessor 'exn 'message #f) exn))
|
|
(port (current-error-port)))
|
|
(unless message (abort exn))
|
|
(display "Error: " port)
|
|
(display message port)
|
|
(let ((arguments
|
|
((condition-property-accessor 'exn 'arguments (list)) exn)))
|
|
(unless (null? arguments)
|
|
(display ":" port)
|
|
(for-each (lambda (a) (display " " port) (write a port))
|
|
arguments)))
|
|
(newline port)
|
|
(exit 70))
|
|
(run))))
|
|
|
|
(define (run)
|
|
(let* ((argv (command-line-arguments))
|
|
(raw-args (spell-stdout-output (args-before-separator argv)))
|
|
(cc-args (args-after-separator argv))
|
|
(args (getopt-long (delete "-" raw-args) opts-grammar))
|
|
(output (get-arg args 'output 'default))
|
|
(help (help-arg? args))
|
|
|
|
(input (get-input-file args (stdin-dash? raw-args)))
|
|
(current-dir (current-directory)))
|
|
(call/cc
|
|
(lambda (return)
|
|
(when help
|
|
(print-help)
|
|
(return #f))
|
|
(when (and (equal? output "-") (not (text-output? args)))
|
|
(error "-o - is stdout, which takes only -C, -m 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)))
|
|
(error (string-append input ": no such file")))
|
|
;; Read time comes before everything, so the features have to be
|
|
;; in place before the first form is read
|
|
(current-features
|
|
(append (if (get-arg args 'no-platform-features #f)
|
|
(list)
|
|
(platform-features))
|
|
(cli-features args)))
|
|
|
|
;; before the interface too, which asks the modules it imports
|
|
;; what types they export
|
|
(load-persistent-module-paths)
|
|
|
|
(when (get-arg args 'public-interface #f)
|
|
(assert (not (eq? input 'stdin)) "Error: --public-interface requires file argument")
|
|
|
|
(write-to-file-or-stdout
|
|
output
|
|
(fn
|
|
(map pp (read-public-interface input))))
|
|
(return #f))
|
|
|
|
;; The file name in a #line directive now comes from the form's
|
|
;; own recorded location, so imported modules report themselves
|
|
;; rather than the unit that imported them
|
|
(sex-line-directives (line-directives-arg args))
|
|
(when (and (get-arg args 'emit-c #f)
|
|
(not (get-arg args 'line-directives #f)))
|
|
(sex-line-directives 'none))
|
|
|
|
(let ((sex-forms (semantic-process-forms (read-raw-forms input) input)))
|
|
(let ((depfile (get-arg args 'depfile #f)))
|
|
(when depfile
|
|
(write-dependencies depfile output input)))
|
|
(if (or (get-arg args 'macro-expand #f)
|
|
(get-arg args 'emit-c #f))
|
|
;; Emit processed and macro-expanded sex code, or emit C code
|
|
(emit-c-or-sex sex-forms output args)
|
|
;; Compile file! The C compiler's status is ours too
|
|
(let ((status (compile-to-file sex-forms output args cc-args)))
|
|
(unless (zero? status)
|
|
(exit status)))))))))
|