From 3da4b863cf4b278b020d40a7989f0d9bc1f9ab1c Mon Sep 17 00:00:00 2001 From: Matthew Flatt Date: Wed, 3 Jun 2015 11:53:11 -0600 Subject: [PATCH] add `raco read` --- compiler-lib/compiler/commands/expand.rkt | 69 +++++++++++++---------- compiler-lib/compiler/commands/info.rkt | 1 + compiler-lib/compiler/commands/read.rkt | 4 ++ compiler-lib/info.rkt | 2 +- 4 files changed, 45 insertions(+), 31 deletions(-) create mode 100644 compiler-lib/compiler/commands/read.rkt diff --git a/compiler-lib/compiler/commands/expand.rkt b/compiler-lib/compiler/commands/expand.rkt index cb320a2a9a..dba76ae93d 100644 --- a/compiler-lib/compiler/commands/expand.rkt +++ b/compiler-lib/compiler/commands/expand.rkt @@ -1,33 +1,42 @@ #lang racket/base -(require racket/cmdline - raco/command-name - racket/pretty) -(define source-files - (command-line - #:program (short-program+command-name) - #:once-each - [("--columns" "-n") n "Format for columns" - (let ([num (string->number n)]) - (unless (exact-positive-integer? num) - (raise-user-error (string->symbol (short-program+command-name)) - "not a valid column count: ~a" n)) - (pretty-print-columns num))] - #:args source-file - source-file)) +(module expand racket/base + (require racket/cmdline + raco/command-name + racket/pretty) + + (provide show-program) + + (define (show-program expand) + (define source-files + (command-line + #:program (short-program+command-name) + #:once-each + [("--columns" "-n") n "Format for columns" + (let ([num (string->number n)]) + (unless (exact-positive-integer? num) + (raise-user-error (string->symbol (short-program+command-name)) + "not a valid column count: ~a" n)) + (pretty-print-columns num))] + #:args source-file + source-file)) -(for ([src-file source-files]) - (let ([src-file (path->complete-path src-file)]) - (let-values ([(base name dir?) (split-path src-file)]) - (parameterize ([current-load-relative-directory base] - [current-namespace (make-base-namespace)] - [read-accept-reader #t]) - (call-with-input-file* - src-file - (lambda (in) - (port-count-lines! in) - (let loop () - (let ([e (read-syntax src-file in)]) - (unless (eof-object? e) - (pretty-write (syntax->datum (expand e))) - (loop)))))))))) + (for ([src-file source-files]) + (let ([src-file (path->complete-path src-file)]) + (let-values ([(base name dir?) (split-path src-file)]) + (parameterize ([current-load-relative-directory base] + [current-namespace (make-base-namespace)] + [read-accept-reader #t]) + (call-with-input-file* + src-file + (lambda (in) + (port-count-lines! in) + (let loop () + (let ([e (read-syntax src-file in)]) + (unless (eof-object? e) + (pretty-write (syntax->datum (expand e))) + (loop)))))))))))) + +(require (submod "." expand)) +(show-program expand) + diff --git a/compiler-lib/compiler/commands/info.rkt b/compiler-lib/compiler/commands/info.rkt index b7f1ac48d0..d8f9753937 100644 --- a/compiler-lib/compiler/commands/info.rkt +++ b/compiler-lib/compiler/commands/info.rkt @@ -8,6 +8,7 @@ ("decompile" compiler/commands/decompile "decompile bytecode" #f) ("test" compiler/commands/test "run tests associated with files/directories" 15) ("expand" compiler/commands/expand "macro-expand source" #f) + ("read" compiler/commands/read "read and pretty-print source" #f) ("distribute" compiler/commands/exe-dir "prepare executable(s) in a directory for distribution" #f) ("demodularize" compiler/demodularizer/batch "produce a whole program from a single module" #f))) diff --git a/compiler-lib/compiler/commands/read.rkt b/compiler-lib/compiler/commands/read.rkt new file mode 100644 index 0000000000..2f18e3530e --- /dev/null +++ b/compiler-lib/compiler/commands/read.rkt @@ -0,0 +1,4 @@ +#lang racket/base +(require (submod "expand.rkt" expand)) + +(show-program (lambda (e) e)) diff --git a/compiler-lib/info.rkt b/compiler-lib/info.rkt index e25b0a4856..f83bb4e6cb 100644 --- a/compiler-lib/info.rkt +++ b/compiler-lib/info.rkt @@ -13,4 +13,4 @@ (define pkg-authors '(mflatt)) -(define version "1.2") +(define version "1.3")