preserves/implementations/racket/preserves/preserves/tool.rkt

45 lines
1.7 KiB
Racket

#lang racket/base
(require "main.rkt")
(require racket/match)
(require racket/port)
(module+ main
(require racket/cmdline)
(define input-format 'text)
(define output-format 'binary)
(define indent? #t)
(define annotations? #t)
(command-line #:once-each
["--atob" "Text to binary"
(begin (set! input-format 'text)
(set! output-format 'binary))]
["--btoa" "Binary to text"
(begin (set! input-format 'binary)
(set! output-format 'text))]
[("--ib" "--input-binary") "Set binary input mode"
(set! input-format 'binary)]
[("--it" "--input-text") "Set text input mode"
(set! input-format 'text)]
[("--ob" "--output-binary") "Set binary output mode"
(set! output-format 'binary)]
[("--ot" "--output-text") "Set text output mode"
(set! output-format 'text)]
["--indent" "Enable indent for text output"
(set! indent? #t)]
["--no-indent" "Disable indent for text output"
(set! indent? #f)]
["--annotations" "Output annotations"
(set! annotations? #t)]
["--no-annotations" "Strip annotations"
(set! annotations? #f)])
(define v ((if annotations? values strip-annotations)
(match input-format
['text (read-preserve-syntax #:source "<stdin>")]
['binary (decode-syntax (port->bytes))])))
(void (match output-format
['text (write-preserve v #:indent indent?)]
['binary (write-bytes (encode v))]))
(flush-output))