add `report/line` and `report/file`

pull/5/head
Matthew Butterick 10 years ago
parent 18aca553ff
commit 48821f2a7d

@ -51,8 +51,18 @@ But be careful — in the example below, the result of the @racket[if] expressi
(report x)]
@defform*[((report/line expr) (report/line expr maybe-name))]
Same as @racket[report], but also shows the line number of @racket[_expr].
@defform*[((report/file expr) (report/file expr maybe-name))]
Same as @racket[report], but also shows the line number and source-file name of @racket[_expr].
@deftogether[(
@defform[(report* expr ...)]
Apply @racket[report] separately to each @racket[_expr] in the list.
@defform[(report*/line expr ...)]
@defform[(report*/file expr ...)]
)]
Apply the relevant @racket[report] macro separately to each @racket[_expr] in the list.
@defform[(repeat num expr ...)]
Evaluate @racket[_expr] repeatedly — @racket[_num] times, in fact — and return the last value.

@ -1,22 +1,46 @@
#lang typed/racket/base
(require (for-syntax racket/base racket/syntax))
(provide (all-defined-out))
(define-syntax (report stx)
(syntax-case stx ()
[(_ expr) #'(report expr expr)]
[(_ expr #:line) #'(report expr expr #:line)]
[(_ expr name)
#'(let ([x expr])
(eprintf "~a = ~v\n" 'name x)
x)]
[(_ expr name #:line)
#'(let ([expr-result expr])
(eprintf "~a = ~v\n" 'name expr-result)
expr-result)]))
(define-syntax (report/line stx)
(syntax-case stx ()
[(_ expr) #'(report/line expr expr)]
[(_ expr name)
(with-syntax ([line (syntax-line #'expr)])
#'(let ([x expr])
(eprintf "~a = ~v on line ~v\n" 'name x line)
x))]
))
#'(let ([expr-result expr])
(eprintf "~a = ~v on line ~v\n" 'name expr-result line)
expr-result))]))
(define-syntax (report/file stx)
(syntax-case stx ()
[(_ expr) #'(report/file expr expr)]
[(_ expr name)
(with-syntax ([file (syntax-source #'expr)]
[line (syntax-line #'expr)])
#'(let ([expr-result expr])
(eprintf "~a = ~v on line ~v in \"~a\"\n" 'name expr-result line file)
expr-result))]))
(define-syntax-rule (define-multi-version multi-name name)
(define-syntax-rule (multi-name x (... ...))
(begin (name x) (... ...))))
(define-multi-version report* report)
(define-multi-version report*/line report/line)
(define-multi-version report*/file report/file)
(define-syntax report-apply
(syntax-rules ()
@ -27,8 +51,7 @@
[(report-apply proc expr #:line)
(let ([lst expr])
(report (apply proc lst) (apply proc expr) #:line)
lst)]
))
lst)]))
#|
(define-syntax (verbalize stx)
@ -40,9 +63,7 @@
(report (proc args ...))))]))
|#
(define-syntax (report* stx)
(syntax-case stx ()
[(_ expr ...) #'(begin (report expr) ...)]))
(define-syntax-rule (repeat num expr ...)

Loading…
Cancel
Save