| 1 |
;; Scheme includes pre-processor. |
| 2 |
;; |
| 3 |
;; All include/resolve statements are replaced with scheme data contained in the referenced file. |
| 4 |
;; File content is placed within a (begin) block. |
| 5 |
;; |
| 6 |
;; TODO Remove headers print HACK in replace-source. Maybe via a (values) return? |
| 7 |
;; |
| 8 |
;; Written by Akce 2020. |
| 9 |
;; SPDX-License-Identifier: Unlicense |
| 10 |
|
| 11 |
(library (private install sipp) |
| 12 |
(export |
| 13 |
directory-separator-string |
| 14 |
join-string |
| 15 |
replace-source) |
| 16 |
(import |
| 17 |
(rnrs) |
| 18 |
(only (chezscheme) directory-separator)) |
| 19 |
|
| 20 |
;; [proc] replace-source: opens a scheme file, replacing all instances of (include/resolve) with contents of file. |
| 21 |
;; [return] scheme list object with forms embedded. |
| 22 |
;; HACK ALERT: this also prints the header lines to (current-output-port) assuming that callers will print the |
| 23 |
;; HACK ALERT: returned object to this same port. It's an easy way to get all headers followed by code/data. |
| 24 |
(define replace-source |
| 25 |
(case-lambda |
| 26 |
[(path) |
| 27 |
(replace-source path #f)] |
| 28 |
[(path print-sipp-header) |
| 29 |
(with-input-from-file path |
| 30 |
(lambda () |
| 31 |
(when print-sipp-header |
| 32 |
(display ";; DO NOT EDIT THIS FILE!!")(newline) |
| 33 |
(display ";; This inlined chez-srfi library code is autogenerated using command:")(newline) |
| 34 |
(display ";; $ ")(display (apply join-string " " (command-line)))(newline) |
| 35 |
(display ";; Source origin: https://github.com/arcfide/chez-srfi")(newline) |
| 36 |
(display ";; Please refer to project site for full credits and original code.")(newline)) |
| 37 |
;; Print initial header block. Hopefully that's a language tag and copyright info. |
| 38 |
;; ie, print lines till we hit the first scheme statement or empty line. |
| 39 |
;; NOTE: multiline comments are *not* handled. |
| 40 |
(display ";;;;;; File header: ")(display path)(newline) |
| 41 |
(let loop () |
| 42 |
(case (peek-char) |
| 43 |
[(#\# #\;) |
| 44 |
(display (get-line (current-input-port))) |
| 45 |
(newline) |
| 46 |
(loop)])) |
| 47 |
(let loop ([obj (read)] [acc '()]) |
| 48 |
(cond |
| 49 |
[(eof-object? obj) |
| 50 |
(reverse acc)] |
| 51 |
[else |
| 52 |
(loop (read) (cons (replace-object obj) acc))]))))])) |
| 53 |
|
| 54 |
;; [proc] replace-object: recurses through a scheme list object, replacing all (include/resolve) calls with the |
| 55 |
;; contents of the referred to file. |
| 56 |
(define replace-object |
| 57 |
(lambda (obj) |
| 58 |
(cond |
| 59 |
[(pair? obj) |
| 60 |
(case (car obj) |
| 61 |
[(include/resolve) |
| 62 |
`(begin |
| 63 |
,@(include/resolve (cdr obj))) |
| 64 |
] |
| 65 |
[else |
| 66 |
(imap replace-object obj)])] |
| 67 |
[else |
| 68 |
obj]))) |
| 69 |
|
| 70 |
(define directory-separator-string (list->string `(,(directory-separator)))) |
| 71 |
|
| 72 |
;; (include/resolve ((?dir ?dirn ...) ?filename)) |
| 73 |
(define include/resolve |
| 74 |
(lambda (args) |
| 75 |
(let ([dir-args (car args)] |
| 76 |
[filename (cadr args)]) |
| 77 |
;; construct the path and let replace-source earn its keep. |
| 78 |
(replace-source (apply join-string directory-separator-string (append (cdr dir-args) (list filename))))))) |
| 79 |
|
| 80 |
;; [proc] imap: simple map that handles improper lists. |
| 81 |
(define imap |
| 82 |
(lambda (proc ilist) |
| 83 |
(let loop ([i ilist]) |
| 84 |
(cond |
| 85 |
[(null? i) |
| 86 |
i] |
| 87 |
[else #;(pair? i) |
| 88 |
(cons* (proc (car i)) |
| 89 |
(cond |
| 90 |
[(list? (cdr i)) |
| 91 |
(loop (cdr i))] |
| 92 |
[else |
| 93 |
(proc (cdr i))]))])))) |
| 94 |
|
| 95 |
;; [proc] string-join: join all string parts together using separator. |
| 96 |
;; |
| 97 |
;; Note that the signature to this version of join-string differs to string-join in SRFI-13. |
| 98 |
;; The separator is the first arg and therefore always explicit which allows for the string |
| 99 |
;; parts as regular arguments, rather than a list of strings. |
| 100 |
;; |
| 101 |
;; Naive implementation that uses (potentially) multiple calls to string-append. |
| 102 |
(define join-string |
| 103 |
(lambda (sep . str-parts) |
| 104 |
(cond |
| 105 |
[(null? str-parts) |
| 106 |
""] |
| 107 |
[else |
| 108 |
(let loop ([acc (car str-parts)] [rest (cdr str-parts)]) |
| 109 |
(cond |
| 110 |
[(null? rest) |
| 111 |
acc] |
| 112 |
[else |
| 113 |
(loop (string-append acc sep (car rest)) (cdr rest))]))]))) |
| 114 |
) |