unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* load in environment
@ 2007-07-06  3:43 Jon Wilson
  2007-07-06  4:26 ` Stephen Compall
  0 siblings, 1 reply; 7+ messages in thread
From: Jon Wilson @ 2007-07-06  3:43 UTC (permalink / raw)
  To: Guile Users

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



_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 7+ messages in thread
* Re: load in environment
@ 2007-07-07  8:52 Marco Maggi
  2007-07-09 17:22 ` Jon Wilson
  0 siblings, 1 reply; 7+ messages in thread
From: Marco Maggi @ 2007-07-07  8:52 UTC (permalink / raw)
  To: guile-user

"Jon Wilson" wrote:
>I'm writing a function to load up some data from a file and
>stick it in a hash table.

Lemmesee if I get it:

1. data representation is stored in a file;
2. the  representation  format  is an  Application  Specific
   Language (ASL), so the file is really a script in ASL;
3. ASL happens to be Scheme-like;
4. to  convert  the   file  representation  in  a  process's
   internal representation the script must be evaluatd in an
   ASL interpreter;
5. nobody  wants the ASL  script to  mess with  the process'
   state or, worst, mess with the file system, etc;

this can be done using pure modules.


(use-modules (ice-9 rdelim))

(define (make-asl-interp funcs)
  (let ((asl-interp (make-module)))
    (purify-module! asl-interp)
    (for-each (lambda (p)
		(module-define! asl-interp (car p) (cdr p)))
      funcs)
    asl-interp))

(define (asl-eval file-name)
  (let* ((data-table	(make-hash-table))
         (item		(lambda (name text number)
			  (hash-set! data-table name
				     (make-item text number))))
	 (asl-interp	(make-asl-interp (list
					  (cons 'item item)))))
    (with-input-from-file file-name
      (lambda ()
	(eval-string (read-delimited "") asl-interp)))
    data-table))

;; ------------------------------------------------------------

(define make-item list)
(define table (asl-eval "data.asl"))

(format #t "dumping table:~%")
(hash-for-each (lambda (key val)
		 (format #t "~/key ~S, val ~S~%" key val))
	       table)

--
Marco Maggi

"They say jump!, you say how high?"
Rage Against the Machine - "Bullet in the Head"



_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2007-07-09 17:22 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-07-06  3:43 load in environment Jon Wilson
2007-07-06  4:26 ` Stephen Compall
2007-07-06  5:19   ` Jon Wilson
2007-07-06  6:03     ` Stephen Compall
2007-07-06  6:06       ` Jon Wilson
  -- strict thread matches above, loose matches on Subject: below --
2007-07-07  8:52 Marco Maggi
2007-07-09 17:22 ` Jon Wilson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).