mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-05-01 20:30:28 +02:00
This is a followup to87c595c757
("Compile in a fresh module by default.") andf65e2b1ec5
("Honor and confine expansion-time side-effects to `current-reader'."). * doc/ref/api-evaluation.texi (Loading): Explain how to change `current-reader' in a compiler-friendly way. * doc/ref/compiler.texi (The Scheme Compiler): Explain use of a fresh compilation module and separate `current-reader' fluid. * test-suite/tests/compiler.test ("current-reader")["with eval-when"]: New test.
107 lines
3.9 KiB
Scheme
107 lines
3.9 KiB
Scheme
;;;; compiler.test --- tests for the compiler -*- scheme -*-
|
||
;;;; Copyright (C) 2008, 2009 Free Software Foundation, Inc.
|
||
;;;;
|
||
;;;; This library is free software; you can redistribute it and/or
|
||
;;;; modify it under the terms of the GNU Lesser General Public
|
||
;;;; License as published by the Free Software Foundation; either
|
||
;;;; version 3 of the License, or (at your option) any later version.
|
||
;;;;
|
||
;;;; This library 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
|
||
;;;; Lesser General Public License for more details.
|
||
;;;;
|
||
;;;; You should have received a copy of the GNU Lesser General Public
|
||
;;;; License along with this library; if not, write to the Free Software
|
||
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||
|
||
(define-module (test-suite tests compiler)
|
||
:use-module (test-suite lib)
|
||
:use-module (test-suite guile-test)
|
||
:use-module (system base compile)
|
||
:use-module ((system vm vm) #:select (the-vm vm-load)))
|
||
|
||
(define read-and-compile
|
||
(@@ (system base compile) read-and-compile))
|
||
|
||
|
||
|
||
(with-test-prefix "basic"
|
||
|
||
(pass-if "compile to value"
|
||
(equal? (compile 1) 1)))
|
||
|
||
|
||
(with-test-prefix "psyntax"
|
||
|
||
(pass-if "compile uses a fresh module by default"
|
||
(begin
|
||
(compile '(define + -))
|
||
(eq? (compile '+) +)))
|
||
|
||
(pass-if "compile-time definitions are isolated"
|
||
(begin
|
||
(compile '(define foo-bar #t))
|
||
(not (module-variable (current-module) 'foo-bar))))
|
||
|
||
(pass-if "compile in current module"
|
||
(let ((o (begin
|
||
(compile '(define-macro (foo) 'bar)
|
||
#:env (current-module))
|
||
(compile '(let ((bar 'ok)) (foo))
|
||
#:env (current-module)))))
|
||
(and (macro? (module-ref (current-module) 'foo))
|
||
(eq? o 'ok))))
|
||
|
||
(pass-if "compile in fresh module"
|
||
(let* ((m (let ((m (make-module)))
|
||
(beautify-user-module! m)
|
||
m))
|
||
(o (begin
|
||
(compile '(define-macro (foo) 'bar) #:env m)
|
||
(compile '(let ((bar 'ok)) (foo)) #:env m))))
|
||
(and (module-ref m 'foo)
|
||
(eq? o 'ok))))
|
||
|
||
(pass-if "redefinition"
|
||
;; In this case the locally-bound `round' must have the same value as the
|
||
;; imported `round'. See the same test in `syntax.test' for details.
|
||
(let ((m (make-module)))
|
||
(beautify-user-module! m)
|
||
(compile '(define round round) #:env m)
|
||
(eq? round (module-ref m 'round)))))
|
||
|
||
|
||
(with-test-prefix "current-reader"
|
||
|
||
(pass-if "default compile-time current-reader differs"
|
||
(not (eq? (compile 'current-reader)
|
||
current-reader)))
|
||
|
||
(pass-if "compile-time changes are honored and isolated"
|
||
;; Make sure changing `current-reader' as the side-effect of a defmacro
|
||
;; actually works.
|
||
(let ((r (fluid-ref current-reader))
|
||
(input (open-input-string
|
||
"(define-macro (install-reader!)
|
||
;;(format #t \"current-reader = ~A~%\" current-reader)
|
||
(fluid-set! current-reader
|
||
(let ((first? #t))
|
||
(lambda args
|
||
(if first?
|
||
(begin
|
||
(set! first? #f)
|
||
''ok)
|
||
(read (open-input-string \"\"))))))
|
||
#f)
|
||
(install-reader!)
|
||
this-should-be-ignored")))
|
||
(and (eq? (vm-load (the-vm) (read-and-compile input))
|
||
'ok)
|
||
(eq? r (fluid-ref current-reader)))))
|
||
|
||
(pass-if "with eval-when"
|
||
(let ((r (fluid-ref current-reader)))
|
||
(compile '(eval-when (compile eval)
|
||
(fluid-set! current-reader (lambda args 'chbouib))))
|
||
(eq? (fluid-ref current-reader) r))))
|