cleaner
parent
7dcce997d0
commit
a36fbc2df6
@ -1,63 +1,92 @@
|
|||||||
#lang br
|
#lang br
|
||||||
(require (for-syntax br/syntax br/scope racket/string)
|
(require (for-syntax br/syntax br/scope racket/string) rackunit racket/file)
|
||||||
"hdlprint.rkt" rackunit racket/file)
|
|
||||||
(provide #%top-interaction #%module-begin #%datum #%app (all-defined-out))
|
(provide #%top-interaction #%module-begin #%datum #%app (all-defined-out))
|
||||||
|
|
||||||
|
|
||||||
|
(define (print-cell val fmt)
|
||||||
|
(match-define (list _ radix-letter number-strings) (regexp-match #px"^%(.)(.*)$" fmt)) ; like %B1.16.1
|
||||||
|
(match-define (list left-margin width right-margin) (map string->number (string-split number-strings ".")))
|
||||||
|
(cond
|
||||||
|
[(number? val)
|
||||||
|
(define radix (case radix-letter
|
||||||
|
[("B") 2]))
|
||||||
|
(string-append (make-string left-margin #\space)
|
||||||
|
(~r val #:min-width width #:pad-string "0" #:base radix)
|
||||||
|
(make-string right-margin #\space))]
|
||||||
|
[(string? val) (~a val #:min-width (+ left-margin width right-margin) #:pad-string " " #:align 'center)]
|
||||||
|
[else (error 'unknown-value)]))
|
||||||
|
|
||||||
|
|
||||||
|
(define (print-line output-filename cells)
|
||||||
|
(with-output-to-file output-filename
|
||||||
|
(λ () (printf (format "~a\n" (string-join cells "|" #:before-first "|" #:after-last "|"))))
|
||||||
|
#:mode 'text
|
||||||
|
#:exists 'append))
|
||||||
|
|
||||||
|
(module+ test
|
||||||
|
(require rackunit)
|
||||||
|
(define a 123)
|
||||||
|
(check-equal? (print-cell a "%B1.16.1") " 0000000001111011 ")
|
||||||
|
(check-equal? (print-cell "out" "%B1.16.1") " out ")
|
||||||
|
(check-equal? (print-cell "out" "%B3.1.3") " out ")
|
||||||
|
(check-equal? (print-cell "in" "%B3.1.3") " in "))
|
||||||
|
|
||||||
|
|
||||||
(define-for-syntax chip-prefix #f)
|
(define-for-syntax chip-prefix #f)
|
||||||
|
|
||||||
|
|
||||||
(define-macro (tst-program ARG ...)
|
(define-macro (tst-program EXPR ...)
|
||||||
(let-shared-id (compare output-file)
|
#'(begin
|
||||||
#'(begin ARG ...
|
EXPR ...
|
||||||
(close-output-port output-file)
|
(compare-files)))
|
||||||
(compare))))
|
|
||||||
|
|
||||||
|
|
||||||
(define-macro (load-expr CHIPFILE-STRING)
|
(define-macro (load-expr CHIPFILE-STRING)
|
||||||
(set! chip-prefix (string-replace (syntax->datum #'CHIPFILE-STRING) ".hdl" ""))
|
(set! chip-prefix (string-replace (syntax->datum #'CHIPFILE-STRING) ".hdl" ""))
|
||||||
(let-syntax-pattern ([CHIPFILE.RKT (format-string "~a.rkt" #'CHIPFILE-STRING)])
|
(let-syntax-pattern
|
||||||
|
([CHIPFILE.RKT (format-string "~a.rkt" #'CHIPFILE-STRING)])
|
||||||
#'(require CHIPFILE.RKT)))
|
#'(require CHIPFILE.RKT)))
|
||||||
|
|
||||||
|
|
||||||
(define-macro (output-file-expr OUTPUT-FILE-STRING)
|
(define-macro (output-file-expr OUTPUT-FILE-STRING)
|
||||||
(let-shared-id (output-file output-filename)
|
(introduce-id
|
||||||
|
(output-file output-filename)
|
||||||
#'(begin
|
#'(begin
|
||||||
(define output-filename OUTPUT-FILE-STRING)
|
(define output-filename OUTPUT-FILE-STRING)
|
||||||
(define output-file (open-output-file output-filename #:exists 'replace)))))
|
(with-output-to-file output-filename
|
||||||
|
(λ () (printf ""))
|
||||||
|
#:mode 'text
|
||||||
|
#:exists 'replace))))
|
||||||
|
|
||||||
|
|
||||||
(define-macro (compare-to-expr COMPARE-FILE-STRING)
|
(define-macro (compare-to-expr COMPARE-FILE-STRING)
|
||||||
(let-shared-id (compare output-filename)
|
(introduce-id
|
||||||
#'(define (compare)
|
(compare-files)
|
||||||
|
#'(define (compare-files)
|
||||||
(check-equal? (file->lines output-filename) (file->lines COMPARE-FILE-STRING)))))
|
(check-equal? (file->lines output-filename) (file->lines COMPARE-FILE-STRING)))))
|
||||||
|
|
||||||
|
|
||||||
(define-macro (output-list-expr (COL-NAME FORMAT-SPEC) ...)
|
(define-macro (output-list-expr (COL-NAME FORMAT-SPEC) ...)
|
||||||
(let-shared-id (output output-file eval-result eval-thunk)
|
(introduce-id
|
||||||
(let-syntax-pattern ([(COL-ID ...) (suffix-ids #'(COL-NAME ...))]
|
(eval-result eval-chip output)
|
||||||
|
(let-syntax-pattern
|
||||||
|
([(COL-ID ...) (suffix-ids #'(COL-NAME ...))]
|
||||||
[(CHIP-COL-ID ...) (prefix-ids chip-prefix "-" #'(COL-NAME ...))])
|
[(CHIP-COL-ID ...) (prefix-ids chip-prefix "-" #'(COL-NAME ...))])
|
||||||
#'(begin
|
#'(begin
|
||||||
(define (output COL-ID ...)
|
(define (output COL-ID ...)
|
||||||
(fprintf output-file
|
(print-line output-filename (map print-cell (list COL-ID ...) (list FORMAT-SPEC ...))))
|
||||||
(format "~a\n" (string-join (list (hdlprint COL-ID FORMAT-SPEC) ...) "|"
|
|
||||||
#:before-first "|"
|
|
||||||
#:after-last "|"))))
|
|
||||||
(define eval-result #f)
|
(define eval-result #f)
|
||||||
(define (eval-thunk) (list (CHIP-COL-ID) ...))
|
(define (eval-chip) (list (CHIP-COL-ID) ...))
|
||||||
(output COL-NAME ...)))))
|
(output COL-NAME ...)))))
|
||||||
|
|
||||||
|
|
||||||
(define-macro (set-expr IN-BUS IN-VAL)
|
(define-macro (set-expr IN-BUS IN-VAL)
|
||||||
(let-syntax-pattern ([CHIP-IN-BUS-ID-WRITE (prefix-id chip-prefix "-" (suffix-id #'IN-BUS "-write"))])
|
(let-syntax-pattern
|
||||||
|
([CHIP-IN-BUS-ID-WRITE (prefix-id chip-prefix "-" (suffix-id #'IN-BUS "-write"))])
|
||||||
#'(CHIP-IN-BUS-ID-WRITE IN-VAL)))
|
#'(CHIP-IN-BUS-ID-WRITE IN-VAL)))
|
||||||
|
|
||||||
|
|
||||||
(define-macro (eval-expr)
|
(define-macro (eval-expr) #'(set! eval-result (eval-chip)))
|
||||||
(let-shared-id (eval-result eval-thunk)
|
|
||||||
#'(set! eval-result (eval-thunk))))
|
|
||||||
|
|
||||||
|
|
||||||
(define-macro (output-expr)
|
(define-macro (output-expr) #'(apply output eval-result))
|
||||||
(let-shared-id (output eval-result)
|
|
||||||
#'(apply output eval-result)))
|
|
||||||
|
@ -1,23 +0,0 @@
|
|||||||
#lang racket
|
|
||||||
(provide hdlprint)
|
|
||||||
|
|
||||||
(define (hdlprint val fmt)
|
|
||||||
(match-define (list _ radix-letter number-strings) (regexp-match #px"^%(.)(.*)$" fmt)) ; like %B1.16.1
|
|
||||||
(match-define (list left-margin width right-margin) (map string->number (string-split number-strings ".")))
|
|
||||||
(cond
|
|
||||||
[(number? val)
|
|
||||||
(define radix (case radix-letter
|
|
||||||
[("B") 2]))
|
|
||||||
(string-append (make-string left-margin #\space)
|
|
||||||
(~r val #:min-width width #:pad-string "0" #:base radix)
|
|
||||||
(make-string right-margin #\space))]
|
|
||||||
[(string? val) (~a val #:min-width (+ left-margin width right-margin) #:pad-string " " #:align 'center)]
|
|
||||||
[else (error 'unknown-value)]))
|
|
||||||
|
|
||||||
(module+ test
|
|
||||||
(require rackunit)
|
|
||||||
(define a 123)
|
|
||||||
(check-equal? (hdlprint a "%B1.16.1") " 0000000001111011 ")
|
|
||||||
(check-equal? (hdlprint "out" "%B1.16.1") " out ")
|
|
||||||
(check-equal? (hdlprint "out" "%B3.1.3") " out ")
|
|
||||||
(check-equal? (hdlprint "in" "%B3.1.3") " in "))
|
|
Loading…
Reference in New Issue