|
| 1 | +#lang racket |
| 2 | +(provide main) |
| 3 | + |
| 4 | +;; This is a utility for smashing together racket files into a single |
| 5 | +;; monolithic program. |
| 6 | +;; For example: |
| 7 | +;; racket -t combine.rkt -m compile-stdin.rkt > outlaw.rkt |
| 8 | +;; creates a file with all the source code needed for the Outlaw |
| 9 | +;; compiler. You still have to: |
| 10 | +;; a) comment out the standard library |
| 11 | +;; b) remove all of the requires and provides to make a valid Racket program |
| 12 | + |
| 13 | +;; String -> Void |
| 14 | +;; Combine all the files fn depends upon, print to stdout |
| 15 | +;; as one monolithic program |
| 16 | +(define (main fn) |
| 17 | + (printf "#lang racket\n") |
| 18 | + (let ((fs (all-files fn))) |
| 19 | + (for-each (lambda (f) |
| 20 | + (displayln (string-append ";; " f))) |
| 21 | + fs) |
| 22 | + (print-files fs))) |
| 23 | + |
| 24 | +;; Port -> [Listof S-Expr] |
| 25 | +;; read all s-expression until eof |
| 26 | +(define (read-all p) |
| 27 | + (let ((r (read p))) |
| 28 | + (if (eof-object? r) |
| 29 | + '() |
| 30 | + (cons r (read-all p))))) |
| 31 | + |
| 32 | +(define (print-files fs) |
| 33 | + (match fs |
| 34 | + ['() (void)] |
| 35 | + [(cons f fs) |
| 36 | + (displayln (make-string 72 #\;)) |
| 37 | + (displayln (string-append ";; " f "\n")) |
| 38 | + (print-file f) |
| 39 | + (print-files fs)])) |
| 40 | + |
| 41 | +(define (print-file f) |
| 42 | + (let ((p (open-input-file f))) |
| 43 | + (read-line p) ; ignore #lang |
| 44 | + (define (loop) |
| 45 | + (let ((l (read-line p))) |
| 46 | + (if (eof-object? l) |
| 47 | + (begin (newline) |
| 48 | + (close-input-port p)) |
| 49 | + (begin |
| 50 | + (displayln l) |
| 51 | + (loop))))) |
| 52 | + (loop))) |
| 53 | + |
| 54 | +(define (all-files fn) |
| 55 | + (remove-duplicates (all-files* fn '()))) |
| 56 | + |
| 57 | +(define (all-files* fn seen) |
| 58 | + (if (member fn seen) |
| 59 | + '() |
| 60 | + (let ((p (open-input-file fn))) |
| 61 | + (read-line p) ; ignore #lang |
| 62 | + (begin0 |
| 63 | + (let ((rs (get-requires (read-all p)))) |
| 64 | + (append (append-map (λ (f) (all-files* f (cons fn seen))) rs) |
| 65 | + (list fn))) |
| 66 | + (close-input-port p))))) |
| 67 | + |
| 68 | +(define (get-requires s) |
| 69 | + (match s |
| 70 | + ['() '()] |
| 71 | + [(cons (cons 'require rs) s) |
| 72 | + (append (filter string? rs) (get-requires s))] |
| 73 | + [(cons _ s) |
| 74 | + (get-requires s)])) |
| 75 | + |
| 76 | + |
0 commit comments