summaryrefslogtreecommitdiff
path: root/antiformatter/src/formatter.rkt
blob: 770251fca71492563319ab03474845f3f3984301 (plain) (blame)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#lang racket

(require racket/cmdline)
(provide mangle mangle-parens? mangle-indents? mangle-newlines?)

(define (random-paren)
  (match (random 0 3)
    (0 (list "(" ")"))
    (1 (list "[" "]"))
    (2 (list "{" "}"))))

(define (random-newline)
  (make-string (random 0 3) #\newline))

(define (random-indentation at-least)
  (make-string (+ at-least (random 0 3)) #\space))

(define indent 0)
(define mangle-parens? (make-parameter #t))
(define mangle-indents? (make-parameter #f))
(define mangle-newlines? (make-parameter #t))

; horrid hack
(define (mangle sexp)
  (string-append "#lang racket \n"
    (mangle-sexp (rest (first (rest (rest (rest (first sexp)))))))))

(define (mangle-sexp sexp)
  (set! indent (+ indent 2))
  (if (not (list? sexp)) (error 'mangle "not an s-exp")
    (if (empty? sexp) ""
      (let ((paren (random-paren)))
        (string-append
          (if (mangle-indents?) (random-indentation indent) "")
          (if (mangle-parens?) (first paren) ")")
          (mangle-params sexp)
          (if (mangle-newlines?) (random-newline) "")
          (if (mangle-indents?) (random-indentation indent) "")
          (if (mangle-parens?) (last paren) ")"))))))

(define (mangle-params sexp)
  (set! indent (max (- indent 2) 0))
  (if (not (list? sexp)) (~a sexp)
    (if (empty? sexp) ""
      (let ((param (first sexp)))
        (string-join (list
          (if (list? param)
            (mangle-sexp param)
            (if (string? param)
              (string-append "\"" (~a param) "\"")
              (~a param)))
          (mangle-params (rest sexp))))))))