syntaxing

pull/2/head
Matthew Butterick 9 years ago
parent 1f0e0eec61
commit 187230041e

@ -1,6 +1,6 @@
#lang racket/base #lang racket/base
(require (for-syntax racket/base syntax/parse racket/syntax syntax/strip-context) (require (for-syntax racket/base syntax/parse racket/syntax syntax/strip-context)
syntax/strip-context racket/function racket/list) syntax/strip-context racket/function racket/list racket/syntax)
(provide (all-defined-out) (all-from-out syntax/strip-context)) (provide (all-defined-out) (all-from-out syntax/strip-context))
@ -33,29 +33,37 @@
(for/list ([arg (in-list (syntax->list args))]) (for/list ([arg (in-list (syntax->list args))])
(_proc arg)))])) (_proc arg)))]))
(define identity (λ(arg) arg)) (define identity (λ(arg) arg))
(define-syntax (partition-syntax-case stx) (define-syntax (syntax-case-partition stx)
(syntax-case stx () (syntax-case stx ()
[(_ (_matchers ...) _stx-list) [(_ _stx-list literals . _matchers)
#'(let* ([stx-list _stx-list] #'(partition (λ(stx-item)
[stxs (cond (with-handlers ([exn:fail:syntax? (λ (exn) #f)])
[(and (syntax? stx-list) (syntax->list stx-list)) => identity] (syntax-case stx-item literals
[(and (list? stx-list) (andmap syntax? list)) stx-list] . _matchers))) (if (syntax? _stx-list)
[else (raise-argument-error 'partition-syntax-case "syntaxed list or list of syntax objects" stx-list)])]) (syntax->list _stx-list)
(partition (λ(stx-item) (syntax-case stx-item () _stx-list))]))
_matchers ...)) stxs))]))
(define-syntax (syntax-case-filter stx)
(define-syntax (filter-syntax stx)
(syntax-case stx () (syntax-case stx ()
[(_ _proc _args) [(_ _stx-list literals . _matchers)
#'(let ([args _args]) #'(let-values ([(matches others) (syntax-case-partition _stx-list literals . _matchers)])
(datum->syntax args matches)]))
(if (and (syntax? args) (list? (syntax-e args)))
(for*/list ([arg (in-list (syntax->list args))]
[result (in-value (_proc (syntax->datum arg)))] (define-syntax (syntax-case-map stx)
#:when result) (syntax-case stx ()
arg) [(_ _stx-list literals . _matchers)
(error 'not-syntax-list))))])) #'(map (λ(stx-item)
(syntax-case stx-item literals
. _matchers)) (if (syntax? _stx-list)
(syntax->list _stx-list)
_stx-list))]))
(define-syntax-rule (reformat-id fmt id0 id ...)
(format-id id0 fmt id0 id ...))
#;(define-syntax syntax-variable (make-rename-transformer #'format-id)) #;(define-syntax syntax-variable (make-rename-transformer #'format-id))

@ -7,9 +7,11 @@
(in-spec (_input-pin _input-width ...) ...) (in-spec (_input-pin _input-width ...) ...)
(out-spec (_output-pin _output-width ...) ...) (out-spec (_output-pin _output-width ...) ...)
_part ...) _part ...)
(with-syntax* ([chip-prefix (format-id #'_chipname "~a-" #'_chipname)] (with-syntax* ([chip-prefix (reformat-id "~a-" #'_chipname)]
[(in-pin-write ...) (map (λ(iw) (format-id iw "~a-write" iw)) (syntax->list #'(_input-pin ...)))] [(in-pin-write ...) (syntax-case-map #'(_input-pin ...) ()
[(prefixed-output-pin ...) (map (λ(op) (format-id op "~a~a" #'chip-prefix op)) (syntax->list #'(_output-pin ...)))]) [iw (reformat-id "~a-write" #'iw)])]
[(prefixed-output-pin ...) (syntax-case-map #'(_output-pin ...) ()
[op (format-id #'op "~a~a" #'chip-prefix #'op)])])
#'(begin #'(begin
(provide (prefix-out chip-prefix (combine-out _input-pin ... in-pin-write ...))) (provide (prefix-out chip-prefix (combine-out _input-pin ... in-pin-write ...)))
(define-input-bus _input-pin _input-width ...) ... (define-input-bus _input-pin _input-width ...) ...
@ -19,7 +21,8 @@
(define #'(part _prefix ((_wire . _wireargs) _wirevalue) ...) (define #'(part _prefix ((_wire . _wireargs) _wirevalue) ...)
(with-syntax ([(prefixed-wire ...) (map (λ(s) (format-id s "~a-~a" #'_prefix s)) (syntax->list #'(_wire ...)))] (with-syntax ([(prefixed-wire ...) (syntax-case-map #'(_wire ...) ()
[s (format-id #'s "~a-~a" #'_prefix #'s)])]
[chip-module-path (datum->syntax #'_prefix (format "~a.hdl.rkt" (syntax->datum #'_prefix)))]) [chip-module-path (datum->syntax #'_prefix (format "~a.hdl.rkt" (syntax->datum #'_prefix)))])
#'(begin #'(begin
(require (import-chip chip-module-path) (for-syntax (import-chip chip-module-path))) (require (import-chip chip-module-path) (for-syntax (import-chip chip-module-path)))
@ -36,12 +39,12 @@
(define #'(handle-wires _wire-assignments ...) (define #'(handle-wires _wire-assignments ...)
(let-values ([(in-wire-stxs out-wire-stxs) (let-values ([(in-wire-stxs out-wire-stxs)
(partition-syntax-case (syntax-case-partition #'(_wire-assignments ...) ()
([((prefixed-wire . _wireargs) _) [((prefixed-wire . _wireargs) _)
(syntax-local-eval (syntax-shift-phase-level #'(input-bus? prefixed-wire) 1))]) (syntax-local-eval (syntax-shift-phase-level #'(input-bus? prefixed-wire) 1))])])
#'(_wire-assignments ...))])
(with-syntax* ([(((in-wire in-arg ...) input-expr) ...) in-wire-stxs] (with-syntax* ([(((in-wire in-arg ...) input-expr) ...) in-wire-stxs]
[(in-wire-write ...) (map (λ(iw) (format-id iw "~a-write" iw)) (syntax->list #'(in-wire ...)))] [(in-wire-write ...) (syntax-case-map #'(in-wire ...) ()
[iw (reformat-id "~a-write" #'iw)])]
[(((out-wire out-arg ...) (out-bus)) ...) out-wire-stxs]) [(((out-wire out-arg ...) (out-bus)) ...) out-wire-stxs])
#'(begin #'(begin
(define-output-bus out-bus (define-output-bus out-bus

Loading…
Cancel
Save