This is the mail archive of the guile@sourceware.cygnus.com mailing list for the Guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: loading files locally


Klaus Schilling <Klaus.Schilling@home.ivm.de> writes:

> is there a way to load files in a way that they get evaluated
> in the current environment instead of the global one?

Yes, from Kent R. Dybvigs book "The Scheme Programming Language"
(http://www.scheme.com/tspl2ed.html):

(use-modules (ice-9 syncase))

(define-syntax include
  (lambda (x)
    (define read-file
      (lambda (fn k)
        (let ((p (open-input-file fn)))
          (let f ((x (read p)))
            (if (eof-object? x)
                (begin (close-input-port p) '())
                (cons (datum->syntax-object k x)
                      (f (read p))))))))
    (syntax-case x ()
       ((k filename)
        (let ((fn (syntax-object->datum (syntax filename))))
          (with-syntax (((exp ...) (read-file fn (syntax k))))
            (syntax (begin exp ...))))))))

If I now put:

  (set! a 2)

in "/tmp/a.scm", the following expression:

  (let ((a 1))
    (include "/tmp/a.scm")
    a)

yields the result

  2

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]