Shrink the role macro, using #:defaults and a local expander

This commit is contained in:
Tony Garnock-Jones 2012-04-05 12:10:27 -04:00
parent 4c76dc29f1
commit 4155f63c28
1 changed files with 21 additions and 35 deletions

56
os2.rkt
View File

@ -116,44 +116,30 @@
(~or (~optional (~seq #:on-presence presence) #:name "#:on-presence handler") (~or (~optional (~seq #:on-presence presence) #:name "#:on-presence handler")
(~optional (~seq #:on-absence absence) #:name "#:on-absence handler") (~optional (~seq #:on-absence absence) #:name "#:on-absence handler")
(~optional (~seq #:on-ready ready) #:name "#:on-ready handler") (~optional (~seq #:on-ready ready) #:name "#:on-ready handler")
(~optional (~seq #:id eid) #:name "#:id") (~optional (~seq #:id eid) #:defaults ([eid #'e0]) #:name "#:id")
(~optional (~seq #:topic topic) #:name "#:topic") (~optional (~seq #:topic topic) #:defaults ([topic #'t0]) #:name "#:topic")
(~optional (~seq #:reason reason) #:name "#:reason")) (~optional (~seq #:reason reason) #:defaults ([reason #'r0]) #:name "#:reason"))
... ...
[message-pattern clause-body ...] [message-pattern clause-body ...]
...) ...)
(with-syntax ([eid (if (attribute eid) #'eid #'dummy-eid)] (define-syntax-rule (build-handler args e-attr)
[topic (if (attribute topic) #'topic #'dummy-topic)] (if (not (attribute e-attr))
[reason (if (attribute reason) #'reason #'dummy-reason)]) #'#f
(with-syntax ([presence-handler (if (not (attribute presence)) #`(lambda (eid . args) (match-lambda [state-pattern e-attr]))))
#'#f (with-syntax ([presence-handler (build-handler (topic) presence)]
#'(lambda (eid topic) [absence-handler (build-handler (topic reason) absence)]
(lambda (state) [ready-handler (build-handler () ready)]
(match state [message-handler #'(lambda (eid topic message-body)
[state-pattern presence]))))] (lambda (state)
[absence-handler (if (not (attribute absence)) (match state
#'#f [state-pattern
#'(lambda (eid topic reason) (match message-body
(lambda (state) [message-pattern clause-body ...]
(match state ...
[state-pattern absence]))))] [_ state])])))])
[ready-handler (if (not (attribute ready)) #'(add-role topic-expr
#'#f (handlers presence-handler absence-handler message-handler)
#'(lambda (eid) ready-handler))])))
(lambda (state)
(match state
[state-pattern ready]))))]
[message-handler #'(lambda (eid topic message-body)
(lambda (state)
(match state
[state-pattern
(match message-body
[message-pattern clause-body ...]
...
[_ state])])))])
#'(add-role topic-expr
(handlers presence-handler absence-handler message-handler)
ready-handler)))])))
;;--------------------------------------------------------------------------- ;;---------------------------------------------------------------------------
;; Smarter constructors for transitions and preactions. ;; Smarter constructors for transitions and preactions.