From f0f5c29ec7ff009100e063f38bebf613054f44d7 Mon Sep 17 00:00:00 2001 From: JJ Date: Tue, 2 May 2023 13:13:08 -0700 Subject: Add the antiformatter --- antiformatter/src/formatter.rkt | 52 +++++++++++++++++++++++++++++++++++++++++ antiformatter/src/info.rkt | 6 +++++ antiformatter/src/test.rkt | 11 +++++++++ 3 files changed, 69 insertions(+) create mode 100644 antiformatter/src/formatter.rkt create mode 100644 antiformatter/src/info.rkt create mode 100644 antiformatter/src/test.rkt (limited to 'antiformatter/src') 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)))))) -- cgit v1.2.3-70-g09d2