guile-user
[Top][All Lists]
Advanced

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

Re: load in environment


From: Marco Maggi
Subject: Re: load in environment
Date: Sat, 7 Jul 2007 10:52:53 +0200

"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"





reply via email to

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