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" 3 1)))

        ; FIXME: Mz372 doesn't have nan?, infinite?
        (eval '(define (nan? x)
                 (and (real? x)
                      (not (= x x)))))
        (eval '(define (infinite? x)
                 (and (real? x)
                      (not (nan? x))
                      (nan? (- x x)))))

        (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" 3 1))))))
      (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/mutable-strings.ss"
                      "rnrs/mutable-pairs.ss"
                      "rnrs/syntax-case.ss"
                      "rnrs/files.ss"
                      "rnrs/programs.ss"
                      "rnrs/eval/reflection.ss" ;; non-standard
                      
                      ;; RNRS BASE is now available.
                      
                      "rnrs/lists.ss"         ;; rnrs base, control
                      "rnrs/r5rs.ss"          ;; rnrs eval, base, control

                      "rnrs/arithmetic/bitwise.ss"         ;; rnrs base, control, r5rs; bitwise core primitives

                      "rnrs/bytevectors/private/core.ss"   ;; mz bytes primitives
                      "rnrs/bytevectors/private/proto.ss"  ;; rnrs base, control; bytevectors core
                      "rnrs/bytevectors/private/ieee.ss"   ;; rnrs base, control, r5rs; bytevectors core, proto
                      "rnrs/bytevectors/private/string.ss" ;; rnrs base, control, r5rs, mutable-strings, arithmetic bitwise; bytevectors core, proto
                      "rnrs/bytevectors.ss"                ;; rnrs bytevectors private core, proto, ieee, string

                      "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/records/private/explicit.ss"   ;; rnrs base, records procedural
                      "rnrs/records/syntactic.ss"

                      "rnrs.ss"     ;; most of rnrs defn'd so far. 
                                    ;; FIXME: needs to provide sorting, bitwise, etc.
                                    ;; FIXME: bug in records procedural, so it's removed from exports.

                      ;; COMPOSITE RNRS is now available.                                             
                      
                      "ubik/define-values.ss" ;; simple rnrs                     
                      "slib/record.ss"        ;; mz primitives                     

                      "srfi/n5.ss"    ;; rnrs base
                      "srfi/n6.ss"    ;; primitives for srfi 6 (FIXME!)
                      "srfi/n8.ss"    ;; rnrs base
                      "srfi/n9.ss"    ;; rnrs base, rnrs records procedural, rnrs records inspection
                      "srfi/n11.ss"
                      "srfi/n16.ss"
                      "srfi/n17.ss"
                      "srfi/n26.ss"   ;; rnrs base
                      "srfi/n28.ss"   ;; rnrs base, rnrs io simple, srfi 6
                      "srfi/n31.ss"
                      "srfi/n39.ss"
                      "srfi/streams/primitive.ss" ;; rnrs
                    ;;"srfi/streams/derived.ss"   ;; rnrs, including unimplemented io ports.
                    ;;"srfi/streams.ss"
                    ;;"srfi/n41.ss"                    
                      "srfi/n42.ss"   ;; simple rnrs
                    ;;"srfi/n51.ss"   ;; rnrs base, srfi 1
                      "srfi/n54.ss"   ;; rnrs base, lists, r5rs, io simple, unicode        

                      "srfi/n60.ss"   ;; simple rnrs, arithmetic bitwise
                      "srfi/n63.ss"   ;; rnrs base, r5rs (quotient), control, mutable-strings, slib record
                      "srfi/n66.ss"   ;; rnrs base, bytevectors
                      "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                     
                      
                      ;; All depend on srfi 78 and the srfi they test.
                      "srfi/n5/tests.ss"  ;; rnrs base
                      "srfi/n6/tests.ss"  ;; rnrs base, rnrs io simple
                      "srfi/n9/tests.ss"  ;; rnrs base
                      "srfi/n42/tests.ss" ;; simple rnrs, srfi 42, 78
                      "srfi/tests.ss"
 
                      ;;"rnrs/sorting.ss"   ;; rnrs base, srfi 95
                      
                      "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