repl.ss
(module repl mzscheme
  (require "reader.ss"
           (lib "util.ss" "planet"))

  (define prim-eval (current-eval))
  (define prim-namespace
    (let ((src (current-namespace)))
      (parameterize ((current-namespace (make-namespace 'empty)))
        (namespace-attach-module src 'mzscheme)
        (namespace-require 
         'mzscheme ;; Eventually this should be more restricted, ie:
         #;'(only mzscheme #%app #%top #%datum
                  quote lambda begin define let if set!
                  values
                  null? pair? list? cons car cdr
                  map list apply append
                  and > vector? = >= ;; etc.
                  ))
        (namespace-require 
         '(planet "expander.ss" ("dvanhorn" "r6rs-expander-vantonder.plt" 2 5)))
        (current-namespace))))
  
  (define library-path
    (let-values (((base name must-be-dir?)
                  (split-path
                   (resolve-planet-path 
                    '(planet "expander.ss" ("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.
                 (current-prompt-read
                  (λ () (printf "R6RS> ") 
                    (let ((x (read)))
                      (if (mzprim? x)
                          (namespace-require (mzprim-quoted-require x))
                          x))))
                 (current-eval 
                  (λ (x)
                    (parameterize ((current-eval prim-eval)
                                   (current-namespace prim-namespace))
                      (eval `(ex:repl '(,(if (and (pair? x)
                                                  (eq? '#%top-interaction (car x)))
                                             (cdr x)
                                             x))))))))
    
    ;; Load all the auxiliary libraries.
    (parameterize ((current-directory library-path)
                   (current-load 
                    (λ (path expected-module-name)
                      (let ((p (open-input-file path)))
                        (let loop ((x (read p)))
                          (cond 
                            ((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.ss"   
                      
                      "core/define-values.ss"
                      "core/vector-types.ss"
                      
                      ;; CORE PRIMITIVES now available.
                      ;; CORE now available.
                      
                      ;; depend only on core primitives.
                      "rnrs/eval.ss"  
                      "rnrs/io/simple.ss"
                      "rnrs/unicode.ss"
                      "rnrs/eval.ss"

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

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

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

                      "rnrs/composite.ss"     ;; most of rnrs defn'd so far.  FIXME: needs to provide records, sorting, bitwise, etc.
                      
                      ;; SIMPLE RNRS is now available.
                      
                      "ubik/define-values.ss"      ;; simple rnrs                     
                      "slib/record.ss"        ;; mz primitives                     
                      "srfi/n9.ss"            ;; rnrs base, slib records                     
                      "srfi/n26.ss"       ;; rnrs base
                      "srfi/n42.ss"       ;; simple rnrs
                      "srfi/n60.ss"       ;; simple rnrs, arithmetic bitwise
                      "srfi/n63.ss"       ;; rnrs, slib records
                      "srfi/n66.ss"       ;; mz primitives
                      "srfi/n69.ss"       ;; simple rnrs, srfi-9
                      "srfi/n74.ss"       ;; rnrs base, srfis 26, 60, 66, mz system-big-endian?
                      "srfi/n78.ss"       ;; simple rnrs, srfi 42
                      "srfi/n95.ss"       ;; simple rnrs, srfi 63
                      "rnrs/sorting.ss"   ;; rnrs base, srfi 95
                      "srfi/n42/tests.ss" ;; simple rnrs, srfi 42, 78
                      
                      "ubik/explicit-renaming.ss"  ;; van tonder expander
                      "ubik/include.ss"            ;; simple rnrs
                      
                      ;"ubik/control.ss"
                      
                      ;; UBIK is now available.  Spray as directed.
                      ))
      
      (read-eval-print-loop)))
  
  (newline)
  (exit)
  
  ) ; end of module