language/dracula-module-begin.scm
(module dracula-module-begin mzscheme
  (require (lib "etc.ss")
           (lib "unit.ss"))
  
  (require-for-syntax "dracula-module-begin-util.scm"
                      "defun-state.scm"
                      "event-form.scm")
  
  (provide dracula-module-begin)
  
  (define-for-syntax *pe* '| ::print-expression|)
  
  (define-syntax (transform-d/e stx)
    (syntax-case stx ()
      [(_ (pe) form0 form ...)
       (event-form? #'form0)
       #'(begin form0 (transform-d/e (pe) form ...))]
      [(_ (pe) (ch x ...) form ...)
       (and (identifier? #'ch)
            (memq (syntax-e #'ch) 
                  '(check-expect check-within check-error generate-report)))
       (with-syntax ([(sig^ ...) (get-sigs)])
         #`(begin 
             (begin-for-syntax (register-unit! #'expr@))
             (define-unit expr@
               (import sig^ ...)
               (export)
               (init-depend sig^ ...)
               ;; check-expect testing forms expand to defns.
               #,(syntax/loc #'ch (ch x ...)))
             (transform-d/e (pe) form ...)))]
      [(_ (pe) expr form ...)
       (with-syntax ([(sig^ ...) (get-sigs)])
         (syntax/loc #'expr
           (begin 
             (begin-for-syntax (register-unit! #'expr@))
             (define-unit expr@
               (import sig^ ...)
               (export)
               (init-depend sig^ ...)
               (pe expr))
             (transform-d/e (pe) form ...))))]
      [(_ (pe) ) #'(begin)]))
  
  (define-syntax (transform-module-body stx)
    (syntax-case stx (begin)
      [(_ (pe))
       (with-syntax ([((f^ ...)
                       (f@ ...)
                       ((f  ...) ...)
                       ((f* ...) ...))
                      
                      (get-link-info)])
         #'(begin
             (define-compound-unit/infer the-program@
               (import)
               (export f^ ...)
               (link f@ ...))
             (define-values/invoke-unit the-program@
               (import)
               (export [rename f^ [f* f] ...] ...))
             (provide [rename f* f] ... ...)))]
      [(_ (pe)
          form ...)
       (let*-values ([(pkg-forms include:rest) 
                      (break-at-includes (syntax->list #'(form ...)))]
                     [(includes rest) (split-after-includes include:rest)])
         (with-syntax ([(def/expr ...) pkg-forms]
                       [(include ...) includes]
                       [(more ...) rest])
           #`(begin
               (transform-d/e (pe) def/expr ...)
               include ...
               (transform-module-body (pe) more ...))))]))
  
  (define-syntax (dracula-module-begin stx)
    (syntax-case stx ()
      [(_) #'(#%plain-module-begin 42)]
      [(_ form ... )
       (with-syntax ([print print]
                     [pe (datum->syntax-object #f *pe*)])
         #'(#%plain-module-begin
            (define pe identity) ;; make sure this defn occurs first
            (transform-module-body (pe)
                                   form ...)))])))