[racket-dev] zo-parse and implicit requires from template-phased syntax?

From: Danny Yoo (dyoo at cs.wpi.edu)
Date: Tue Mar 13 17:39:02 EDT 2012

> That is, if "test.rkt" requires "helper.rkt" with a phase shift of 1,
> and "helper.rkt" requires `racket/list' with a phase shift of -1, then
> "test.rkt" needs `racket/list' at phase 0.

Ah!  Thank you!  That makes much more sense to me now.  So I really
must do something like this to get the full set of phase-0
dependencies.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#lang racket/base
(require compiler/demodularizer/mpi
         syntax/modcode
         racket/path
         racket/pretty)

(read-accept-reader #t)
(current-namespace (make-base-namespace))

;; Produce a list of the phase-0 dependencies of a module.
(define (phase-0-dependencies a-mod-path)
  (define ht (make-hash))

  (define marked-for-visit? (make-hash))
  (parameterize ([MODULE-PATHS (make-hash)])

    (let recur ([a-mod-path a-mod-path]
                [current-phase 0])
      (define modcode (get-module-code a-mod-path))
      (define imports (module-compiled-imports modcode))
      (for ([phase+mpis (in-list imports)]
            #:when (number? (car phase+mpis)))
           (define phase (+ (car phase+mpis) current-phase))
           (define paths
             (parameterize ([current-module-path a-mod-path])
               (for/list ([mpi (in-list (cdr phase+mpis))])
                         (mpi->path! mpi))))
           (when (= phase 0)
             (for ([p paths]) (hash-set! ht p #t)))
           (for ([p (in-list paths)])
                (when (and (path? p) (not (hash-has-key?
marked-for-visit? (list p phase))))
                  (hash-set! marked-for-visit? (list p phase) #t)
                  (recur p phase))))))

  ;; Return all the paths we've seen.
  (for/list ([p (in-hash-keys ht)])
     p))

(pretty-print
 (phase-0-dependencies (normalize-path (build-path "temp.rkt"))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Posted on the dev mailing list.