summaryrefslogtreecommitdiff
path: root/antiformatter/src
diff options
context:
space:
mode:
Diffstat (limited to 'antiformatter/src')
-rw-r--r--antiformatter/src/formatter.rkt52
-rw-r--r--antiformatter/src/info.rkt6
-rw-r--r--antiformatter/src/test.rkt11
3 files changed, 69 insertions, 0 deletions
diff --git a/antiformatter/src/formatter.rkt b/antiformatter/src/formatter.rkt
new file mode 100644
index 0000000..770251f
--- /dev/null
+++ b/antiformatter/src/formatter.rkt
@@ -0,0 +1,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))))))))
diff --git a/antiformatter/src/info.rkt b/antiformatter/src/info.rkt
new file mode 100644
index 0000000..b4c6f0b
--- /dev/null
+++ b/antiformatter/src/info.rkt
@@ -0,0 +1,6 @@
+#lang info
+
+(define name "antifmt")
+(define pkg-desc "attempting to make the worst possible passable racket code")
+(define pkg-authors '(apropos))
+(define license '(FAFOL))
diff --git a/antiformatter/src/test.rkt b/antiformatter/src/test.rkt
new file mode 100644
index 0000000..57e6367
--- /dev/null
+++ b/antiformatter/src/test.rkt
@@ -0,0 +1,11 @@
+#lang racket
+
+(define (mangle sexp)
+ (if (not (list? sexp)) (error 'mangle "not an s-exp")
+ (if (empty? sexp) ""
+ (let ((paren (random-paren)))
+ (string-append
+ (first paren)
+ (mangle-params sexp)
+ (random-newline)
+ (last paren))))))