language/defun-state.scm
#|
Definition forms accumulate expansion time state
here in order to later generate proper unit linking code.
|#
(module defun-state mzscheme
  (require (lib "etc.ss")
           (lib "unit-exptime.ss")
           (lib "list.ss" "srfi" "1"))
  (provide register-unit!
           get-sigs
           get-link-info
           already-defined?)
  
  (define-values (sigs names units) (values null null null))
  
  (define (already-defined? id)
    (s:member id (apply append names) module-identifier=?))
  
  (define register-unit!
    (opt-lambda (unit-id [sig-id #f])
      (add-unit! unit-id)
      (when sig-id
        (add-sig! sig-id)
        (let-values ([(_parent names _sig-defined sig-defsyntaxed)
                      (signature-members sig-id sig-id)])
          ;(printf "sig ~a = ~a~n" (syntax-e sig-id) (map syntax-e names))
          (add-name! (append names sig-defsyntaxed))))))
  #|
  [((f^ ...)
   (f@ ...)
   ((f  ...) ...)
   ((f* ...) ...)) (get-link-info)]
  |#
  (define (get-link-info)
    (let ([rev-names (reverse names)])
      (list (reverse sigs)
            (reverse units)
            rev-names
            (map generate-temporaries rev-names))))
  
  (define (get-sigs) sigs)
  
  (define (add-sig! sig-id) (set! sigs (cons sig-id sigs)))
  (define (add-name! id) (set! names (cons id names)))
  (define (add-unit! u-id) (set! units (cons u-id units))))