guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

load in environment


From: Jon Wilson
Subject: load in environment
Date: Thu, 05 Jul 2007 23:43:28 -0400
User-agent: Thunderbird 1.5.0.12 (X11/20070604)

Hi,
I was wondering if there is a built-in way to eval the contents of a file inside of an environment other than (current-module)? We have eval and primitive-eval, and it seems that load is currently (conceptually) a read, primitive-eval loop until eof is reached. Why not allow an environment arg to load, making it (conceptually) a read, eval loop until eof is reached?

There are two ways that I've thought of to implement this if it is not implemented in guile already (which it doesn't seem to be). One is to simply read the file repeatedly, and eval the results in the given environment:

(define load-env-1 filename env)
 (let* ((file (open-input-file filename))
        (datum (read file)))
   (while (not (eof-object? datum))
     (eval datum env)
     (set! datum (read file))))

The second way is to make the desired environment temporarily be the current module:

(define load-env-2 filename env)
 (let ((real-current-module (current-module)))
   (set-current-module! env)
   (load filename)
   (set-current-module! real-current-module)))

The second way has the advantage of not reinventing the wheel when it comes to the read-eval loop, but looks rather strange. If anyone knows of a better way to do this, or especially if someone knows of a procedure already in guile to do exactly this, I'd love to hear about it. If anyone has thoughts on ways that one or both of these might be improved, I'd also love to hear about that.
Regards,
Jon





reply via email to

[Prev in Thread] Current Thread [Next in Thread]