(module repl mzscheme
  (require ""
           (lib "" "planet"))

  (define prim-eval (current-eval))
  (define prim-namespace
    (let ((src (current-namespace)))
      (parameterize ((current-namespace (make-namespace 'empty)))
        (namespace-attach-module src 'mzscheme)
         'mzscheme ;; Eventually this should be more restricted, ie:
         #;'(only mzscheme #%app #%top #%datum
                  quote lambda begin define let if set!
                  null? pair? list? cons car cdr
                  map list apply append
                  and > vector? = >= ;; etc.
         '(planet "" ("dvanhorn" "r6rs-expander-vantonder.plt" 2 5)))
  (define library-path
    (let-values (((base name must-be-dir?)
                    '(planet "" ("dvanhorn" "r6rs-expander-vantonder.plt" 2 5))))))
      (build-path base "lib/")))
  (parameterize ((read-curly-brace-as-paren #f) ;; Reserved for future use.
                 (read-accept-box #f)
                 (read-accept-compiled #f)
                 (read-accept-graph #f)
                 (read-accept-dot #t)
                 (read-accept-infix-dot #f)
                 (read-accept-quasiquote #t)
                 (read-accept-reader #f)
                 (current-readtable readtable)  ;; Handles #!r6rs, #vu8, etc.
                  (λ () (printf "R6RS> ") 
                    (let ((x (read)))
                      (if (mzprim? x)
                          (namespace-require (mzprim-quoted-require x))
                  (λ (x)
                    (parameterize ((current-eval prim-eval)
                                   (current-namespace prim-namespace))
                      (eval `(ex:repl '(,(if (and (pair? x)
                                                  (eq? '#%top-interaction (car x)))
                                             (cdr x)
    ;; Load all the auxiliary libraries.
    (parameterize ((current-directory library-path)
                    (λ (path expected-module-name)
                      (let ((p (open-input-file path)))
                        (let loop ((x (read p)))
                            ((eof-object? x) (close-input-port p) (values))
                            (else (eval x) (loop (read p)))))))))
      ;; core = core primitives, rest of core libraries.
      ;; simple rnrs = simple rnrs, control, mutable-pairs, r5rs, io simple, mutable-strings ...
      ;; full rnrs = simple rnrs, rest of standard libraries.
      (for-each load (list
                      ;; CORE PRIMITIVES now available.
                      ;; CORE now available.
                      ;; depend only on core primitives.

                      ;; depend only on core.
                      ;; RNRS BASE is now available.
                      "rnrs/"         ;; rnrs base, control
                      "rnrs/"          ;; rnrs eval, base, control
                      ""               ;; misc

                      "rnrs/records/private/" ;; rnrs base, lists {find, forall}, core vector-types
                      "rnrs/records/"   ;; records core
                      "rnrs/records/"   ;; records core

                      "rnrs/arithmetic/" ;; simple rnrs, bitwise core primitives

                      "rnrs/"     ;; most of rnrs defn'd so far.  FIXME: needs to provide records, sorting, bitwise, etc.
                      ;; SIMPLE RNRS is now available.
                      "ubik/"      ;; simple rnrs                     
                      "slib/"        ;; mz primitives                     
                      "srfi/"            ;; rnrs base, slib records                     
                      "srfi/"       ;; rnrs base
                      "srfi/"       ;; simple rnrs
                      "srfi/"       ;; simple rnrs, arithmetic bitwise
                      "srfi/"       ;; rnrs, slib records
                      "srfi/"       ;; mz primitives
                      "srfi/"       ;; simple rnrs, srfi-9
                      "srfi/"       ;; rnrs base, srfis 26, 60, 66, mz system-big-endian?
                      "srfi/"       ;; simple rnrs, srfi 42
                      "srfi/"       ;; simple rnrs, srfi 63
                      "rnrs/"   ;; rnrs base, srfi 95
                      "srfi/n42/" ;; simple rnrs, srfi 42, 78
                      "ubik/"  ;; van tonder expander
                      "ubik/"            ;; simple rnrs
                      ;; UBIK is now available.  Spray as directed.
  ) ; end of module