2007-10-25 16:27:34 -04:00
|
|
|
;;; Ikarus Scheme -- A compiler for R6RS Scheme.
|
2008-01-29 00:34:34 -05:00
|
|
|
;;; Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum
|
2007-10-25 16:27:34 -04:00
|
|
|
;;;
|
|
|
|
;;; This program is free software: you can redistribute it and/or modify
|
|
|
|
;;; it under the terms of the GNU General Public License version 3 as
|
|
|
|
;;; published by the Free Software Foundation.
|
|
|
|
;;;
|
|
|
|
;;; This program is distributed in the hope that it will be useful, but
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
;;; General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2007-05-06 20:39:42 -04:00
|
|
|
|
|
|
|
(library (ikarus load)
|
2007-05-09 07:35:31 -04:00
|
|
|
(export load load-r6rs-top-level)
|
2007-05-06 20:39:42 -04:00
|
|
|
(import
|
|
|
|
(except (ikarus) load)
|
2008-02-18 21:58:11 -05:00
|
|
|
(only (ikarus.compiler) compile-core-expr)
|
|
|
|
(only (psyntax library-manager)
|
|
|
|
serialize-all current-precompiled-library-loader)
|
2008-05-01 06:02:36 -04:00
|
|
|
(only (psyntax expander) compile-r6rs-top-level)
|
2008-05-06 15:38:05 -04:00
|
|
|
(only (ikarus reader) read-initial read-script-source-file))
|
2007-05-06 20:39:42 -04:00
|
|
|
|
2008-02-18 21:58:11 -05:00
|
|
|
|
|
|
|
(define-struct serialized-library (contents))
|
|
|
|
|
2008-02-18 23:29:49 -05:00
|
|
|
(define fasl-extension ".ikarus-fasl")
|
|
|
|
|
2008-02-18 21:58:11 -05:00
|
|
|
(define (load-serialized-library filename sk)
|
|
|
|
;;; TODO: check file last-modified date
|
2008-02-18 23:29:49 -05:00
|
|
|
(let ([ikfasl (string-append filename fasl-extension)])
|
2008-02-18 22:05:29 -05:00
|
|
|
(cond
|
|
|
|
[(not (file-exists? ikfasl)) #f]
|
|
|
|
[(<= (file-ctime ikfasl) (file-ctime filename))
|
2008-02-18 23:37:01 -05:00
|
|
|
(fprintf (current-error-port)
|
2008-02-18 22:05:29 -05:00
|
|
|
"WARNING: not using fasl file ~s because it is older \
|
|
|
|
than the source file ~s\n"
|
|
|
|
ikfasl
|
|
|
|
filename)
|
|
|
|
#f]
|
|
|
|
[else
|
|
|
|
(let ([x
|
|
|
|
(let ([p (open-file-input-port ikfasl)])
|
|
|
|
(let ([x (fasl-read p)])
|
|
|
|
(close-input-port p)
|
|
|
|
x))])
|
|
|
|
(if (serialized-library? x)
|
|
|
|
(apply sk (serialized-library-contents x))
|
|
|
|
(begin
|
2008-02-18 23:37:01 -05:00
|
|
|
(fprintf (current-error-port)
|
2008-02-18 22:05:29 -05:00
|
|
|
"WARNING: not using fasl file ~s because it was \
|
2008-02-19 00:15:18 -05:00
|
|
|
compiled with a different instance of ikarus.\n"
|
2008-02-18 22:05:29 -05:00
|
|
|
ikfasl)
|
|
|
|
#f)))])))
|
2008-02-18 21:58:11 -05:00
|
|
|
|
|
|
|
(define (do-serialize-library filename contents)
|
2008-02-18 23:29:49 -05:00
|
|
|
(let ([ikfasl (string-append filename fasl-extension)])
|
2008-02-19 00:15:18 -05:00
|
|
|
(fprintf (current-error-port) "Serializing ~s ...\n" ikfasl)
|
2008-02-18 21:58:11 -05:00
|
|
|
(let ([p (open-file-output-port ikfasl (file-options no-fail))])
|
|
|
|
(fasl-write (make-serialized-library contents) p)
|
|
|
|
(close-output-port p))))
|
|
|
|
|
2007-05-06 20:39:42 -04:00
|
|
|
(define load-handler
|
|
|
|
(lambda (x)
|
2008-05-01 06:02:36 -04:00
|
|
|
(eval x (interaction-environment))))
|
|
|
|
|
2007-05-06 20:39:42 -04:00
|
|
|
(define read-and-eval
|
|
|
|
(lambda (p eval-proc)
|
|
|
|
(let ([x (read p)])
|
|
|
|
(unless (eof-object? x)
|
|
|
|
(eval-proc x)
|
|
|
|
(read-and-eval p eval-proc)))))
|
|
|
|
(define load
|
|
|
|
(case-lambda
|
|
|
|
[(x) (load x load-handler)]
|
|
|
|
[(x eval-proc)
|
|
|
|
(unless (string? x)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'load "not a string" x))
|
2007-05-06 20:39:42 -04:00
|
|
|
(unless (procedure? eval-proc)
|
2007-12-15 08:22:49 -05:00
|
|
|
(die 'load "not a procedure" eval-proc))
|
2007-05-06 20:39:42 -04:00
|
|
|
(let ([p (open-input-file x)])
|
|
|
|
(let ([x (read-initial p)])
|
|
|
|
(unless (eof-object? x)
|
|
|
|
(eval-proc x)
|
|
|
|
(read-and-eval p eval-proc)))
|
2007-05-09 07:35:31 -04:00
|
|
|
(close-input-port p))]))
|
|
|
|
(define load-r6rs-top-level
|
2008-02-18 20:28:54 -05:00
|
|
|
(lambda (x how)
|
2008-05-06 15:38:05 -04:00
|
|
|
(let ([prog (read-script-source-file x)])
|
2008-02-18 20:28:54 -05:00
|
|
|
(let ([thunk (compile-r6rs-top-level prog)])
|
|
|
|
(case how
|
|
|
|
[(run) (thunk)]
|
2008-02-18 21:58:11 -05:00
|
|
|
[(compile)
|
|
|
|
(serialize-all
|
|
|
|
(lambda (file-name contents)
|
|
|
|
(do-serialize-library file-name contents))
|
|
|
|
(lambda (core-expr)
|
|
|
|
(compile-core-expr core-expr)))]
|
2008-02-18 20:28:54 -05:00
|
|
|
[else (error 'load-r6rs-top-level "invali argument" how)])))))
|
2008-02-18 21:58:11 -05:00
|
|
|
|
|
|
|
(current-precompiled-library-loader load-serialized-library)
|
|
|
|
|
2007-05-09 07:35:31 -04:00
|
|
|
)
|