Context: When executing code via `guile --language LANG -s', Guile
initialises a module according to the `make-default-environment' field
of the language definition.  However, execution of the guile REPL via
`guile --language=LANG' for these languages shows different behaviour:
it uses the `guile-user' environment instead. This means that

(a) builtin bindings in a given language are inaccessible in the REPL
    when they were available during script-execution and
(b) scheme bindings are present in the REPL while they weren't
    declared to be part of the implemented language's environment.

This patch fixes this behaviour for languages, that do not change the
`make-default-environment' field and are not Scheme.  Incidentally,
this means that guile-user is continued to be loaded for `elisp' and
`ecmascript', since for those languages `make-default-environment' is
not overwritten..

* module/ice-9/top-repl.scm (top-repl): Use `make-default-environment'
  if it isn't the default value of the language record.
  (initial-repl-module) New auxiliary procedure.
---
 module/ice-9/top-repl.scm | 23 ++++++++++++++++++++---
 1 file changed, 20 insertions(+), 3 deletions(-)

diff --git a/module/ice-9/top-repl.scm b/module/ice-9/top-repl.scm
index fa26e61e7..613fb4498 100644
--- a/module/ice-9/top-repl.scm
+++ b/module/ice-9/top-repl.scm
@@ -20,6 +20,10 @@
 (define-module (ice-9 top-repl)
   #:use-module (ice-9 top-repl)
   #:use-module ((system repl repl) #:select (start-repl))
+  #:use-module ((system base language)
+                #:select (lookup-language
+                          default-environment
+                          language-make-default-environment))
 
   ;; #:replace, as with deprecated code enabled these will be in the root env
   #:replace (top-repl))
@@ -44,13 +48,26 @@
                   ;; restore original C handler.
                   (sigaction SIGINT #f))))))))
 
-(define (top-repl)
-  (let ((guile-user-module (resolve-module '(guile-user))))
 
+
+(define (initial-repl-module)
+  "Returns the module to be used in the REPL, `guile-user' for scheme or
+if the language doesn't overwrite @var{make-default-environment}, if it
+overwrites this field in the language spec, it loads the module
+specified by @var{make-default-environment}."
+  (let* ((lang (lookup-language (current-language)))
+         (make-default-env (language-make-default-environment lang)))
+    (if (or (eq? (current-language) 'scheme)
+            (eq? make-default-env make-fresh-user-module))
+      (resolve-module '(guile-user))
+      (default-environment (current-language)))))
+
+(define (top-repl)
+  (let ((guile-user-module (initial-repl-module)))
     ;; Use some convenient modules (in reverse order)
 
     (set-current-module guile-user-module)
-    (process-use-modules 
+    (process-use-modules
      (append
       '(((ice-9 session)))
       (if (provided? 'regex)
-- 
2.28.0


Reply via email to