1(library (yuni util library-writer)
2         (export
3           library-spec
4           write-library)
5         (import (rnrs)
6                 (shorten)
7                 (srfi :8)
8                 (only (mosh pp) pp)
9                 (yuni core))
10
11(define* library-spec
12  (name export import comment code))
13
14(define* (write-library output (library-spec))
15  (define (prepare-port p)
16    (cond
17      ((port? p) p)
18      ((string? p)
19       (open-file-output-port p (file-options no-fail)
20                              'block (native-transcoder)))
21      (else
22        (assertion-violation 'prepare-port
23                             "invalid argument"
24                             p))))
25  (define (tabbed-out l)
26    ;; FIXME: tab-it..
27    (receive (port proc) (open-string-output-port)
28      (pp l port)
29      (let ((str (proc)))
30        str)))
31
32  (define (format-comment comment)
33    ;; FIXME: implement-it..
34    "")
35
36  (let-with library-spec (export import comment code name)
37    (define p (prepare-port output))
38    (display (format-comment (if (string? comment)
39                               (list comment)
40                               comment)) p)
41    (display "(library " p)
42    (write name p)
43    (newline p)
44    (display (tabbed-out (cons 'export export)) p)
45    (display (tabbed-out (cons 'import import)) p)
46    (for-each (^e (pp e p)) code)
47    (display ")" p)
48    (close-port p)))
49
50)
51
52