dev.fron.io rc / d901fe5
scripts: add scmfmt Tony Olagbaiye 10 days ago
1 changed file(s) with 29 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 #!/usr/bin/env -S guile --no-auto-compile
1 ;; Mode: -*- Scheme;-*-
2 !#
3
4 (use-modules (ice-9 pretty-print))
5
6 ;; Helper methods for maintaining comments and whitespace.
7 (define (copy-line-comment)
8 (let ((char (read-char)))
9 (if (not (eof-object? char))
10 (if (eq? char #\newline)
11 (newline)
12 (begin (write-char char) (copy-line-comment))))))
13 (define (maintain-empty-lines)
14 (let ((char1 (read-char)) (char2 (peek-char)))
15 (if (and (eq? char1 #\newline) (eq? char2 #\newline))
16 (write-char (read-char)))))
17
18 ;; The main method. This reads from and writes to stdin/stdout.
19 (define (scmfmt)
20 (let ((char (peek-char)))
21 (if (not (eof-object? char))
22 (begin
23 (cond ((eq? char #\;) (copy-line-comment))
24 ((eq? char #\newline) (maintain-empty-lines))
25 ((char-whitespace? char) (read-char))
26 (#t (pretty-print (read))))
27 (scmfmt)))))
28 (scmfmt)