Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Code formatter for Scheme using Guile's pretty-print method. This reads from stdin and writes to stdout.

View scmfmt.scm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
(use-modules (ice-9 pretty-print))
 
;; Helper methods for maintaining comments and whitespace.
(define (copy-line-comment)
(let ((char (read-char)))
(if (not (eof-object? char))
(if (eq? char #\newline)
(newline)
(begin (write-char char) (copy-line-comment))))))
(define (maintain-empty-lines)
(let ((char1 (read-char)) (char2 (peek-char)))
(if (and (eq? char1 #\newline) (eq? char2 #\newline))
(write-char (read-char)))))
 
;; The main method. This reads from and writes to stdin/stdout.
(define (scmfmt)
(let ((char (peek-char)))
(if (not (eof-object? char))
(begin
(cond ((eq? char #\;) (copy-line-comment))
((eq? char #\newline) (maintain-empty-lines))
((char-whitespace? char) (read-char))
(#t (pretty-print (read))))
(scmfmt)))))
(scmfmt)
Owner

Note that this throws away both block comments and non-top-level comments.

This code was formatted by itself, so it should provide a good idea of what sample output looks like.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.