Skip to content

Commit

Permalink
Libraries now invoked only once under all circumstances (ticket #655).
Browse files Browse the repository at this point in the history
  • Loading branch information
WillClinger committed Jul 3, 2017
1 parent ce5babf commit 8309025
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
30 changes: 23 additions & 7 deletions lib/R6RS/r6rs-expander.sch
Original file line number Diff line number Diff line change
Expand Up @@ -2876,16 +2876,32 @@
;; For importing and evaluating stuff in the persistent
;; interactive environment, see REPL above.

;; FIXME: Since expand-toplevel-sequence calls eval on every
;; library in the sequence, the following procedure calls eval
;; on every library twice. That can double the compile time.

;; NOTE: expand-toplevel-sequence calls eval on every library
;; in the sequence. (It calls scan-sequence, which calls
;; expand-library, which calls expand-library-or-program,
;; which calls eval on the expanded library.) That's why
;; run-r6rs-sequence calls eval only on the expressions
;; obtained by expanding the program, if present.
;; See ticket #655.

(define (run-r6rs-sequence forms)
(with-toplevel-parameters
(lambda ()
(for-each (lambda (exp) (eval exp (interaction-environment)))
(expand-toplevel-sequence (normalize forms))))))

(let* ((forms (normalize forms)))
(call-with-values
(lambda ()
(partition (lambda (form)
(and (pair? form)
(eq? 'program (car form))))
forms))
(lambda (pgms libs)
(expand-toplevel-sequence libs)
(if (not (null? pgms))
(let* ((exps (expand-toplevel-sequence pgms)))
(for-each (lambda (exp)
(eval exp (interaction-environment)))
exps)))))))))

(define (run-r6rs-program filename)
(run-r6rs-sequence (read-file filename)))

Expand Down
17 changes: 1 addition & 16 deletions lib/R6RS/r6rsmode.sch
Original file line number Diff line number Diff line change
Expand Up @@ -441,26 +441,11 @@
; loaded by load-r6rs-program. The target-filename can be
; compiled before it is loaded.
;
; FIXME: This uses a hack to avoid compiling a library twice
; in systems that compile on eval. If the file contains only
; one library, and nothing else, then we can compile to a file
; and then load the file instead of calling eval on the result
; of expansion. That doesn't work if the file contains two
; libraries and the second one imports from the first.
;
; FIXME: The io here should be protected against errors.

(define (expand-r6rs-program filename target-filename)
(larceny:load-r6rs-package)
(let ((nlibs (call-with-input-file
filename
(lambda (in)
(do ((x (read in) (read in))
(n 0 (if (pair? x) (+ n 1) n)))
((or (eof-object? x) (> n 1))
n))))))
(parameterize ((larceny:r6rs-expand-only (= nlibs 1)))
(ex:expand-file filename target-filename))))
(ex:expand-file filename target-filename))

; This parameter determines whether expanded libraries
; and programs are evaluated immediately, as in van Tonder's
Expand Down

0 comments on commit 8309025

Please sign in to comment.