From: Yeh, Chih Hao
Subject: [Q]How to evaluate a string as lisp code?
Date: 
Message-ID: <8bs26b$ocv$1@gemini.ntu.edu.tw>
I am new to lisp, and is writing a application mapping some tags into lisp
codes. I heard LISP can run-time interpret string as code.
I can build strings which contains legal lisp codes now, but I don't know
how to make the string run. Any suggestions?
Yeh, Chih Hao

From: Hartmann Schaffer
Subject: Re: [Q]How to evaluate a string as lisp code?
Date: 
Message-ID: <38e269d2@news.sentex.net>
In article <············@gemini.ntu.edu.tw>,
	"Yeh, Chih Hao" <········@im.ntu.edu.tw> writes:
> I am new to lisp, and is writing a application mapping some tags into lisp
> codes. I heard LISP can run-time interpret string as code.
> I can build strings which contains legal lisp codes now, but I don't know
> how to make the string run. Any suggestions?

if you build only one impression:  use eval and with-input-from-string.
on the other hand, wouldn't it be easier to build a list rather than a
string?

-- 

Hartmann Schaffer
From: David J. Cooper
Subject: Re: [Q]How to evaluate a string as lisp code?
Date: 
Message-ID: <38E1904E.D87C908C@genworks.com>
"Yeh, Chih Hao" wrote:
> 
> I am new to lisp, and is writing a application mapping some tags into lisp
> codes. I heard LISP can run-time interpret string as code.
> I can build strings which contains legal lisp codes now, but I don't know
> how to make the string run. Any suggestions?
> Yeh, Chih Hao

Let's say your string is in the variable called string.

(eval (read-from-string string))

This will evaluate exactly one expression from the string.
You'll have to do something more (probably with subseq or
something) to read and evaluate multiple expressions from
a string.

Also, make absolutely sure that you really have to wait until
runtime to do this. The use of eval sends up a red flag because
newbies will sometimes slip into using it when they are probably
trying to do something they really don't know how to do yet, and
which might better be accomplished with using macros, funcall, or
apply.

But it sounds like you are trying to interpret XML as code or
something, in which case i suppose the use of eval might be
appropriate, but i really don't know much about XML yet.

Yours,

 -dave

-- 
David J. Cooper Jr, Chief Engineer	Genworks International
·······@genworks.com			5777 West Maple, Suite 130
(248) 932-2512 (Genworks HQ/voicemail)	West Bloomfield, MI 48322-2268
(248) 407-0633 (pager)			http://www.genworks.com
From: Erik Naggum
Subject: Re: [Q]How to evaluate a string as lisp code?
Date: 
Message-ID: <3163345278914696@naggum.no>
* "David J. Cooper" <·······@genworks.com>
| Let's say your string is in the variable called string.
| 
| (eval (read-from-string string))
| 
| This will evaluate exactly one expression from the string.  You'll have
| to do something more (probably with subseq or something) to read and
| evaluate multiple expressions from a string.

  note that read-from-string returns two values, the secondary of which is
  the position from which you could continue to read from the string.

#:Erik
From: Alex Henderson
Subject: Re: [Q]How to evaluate a string as lisp code?
Date: 
Message-ID: <38E1ADA7.D35B0BA2@hermes.cam.ac.uk>
> Let's say your string is in the variable called string.
> 
> (eval (read-from-string string))
> 
> This will evaluate exactly one expression from the string.
> You'll have to do something more (probably with subseq or
> something) to read and evaluate multiple expressions from
> a string.

Better still, use a string-stream:

(do ((stream (make-string-input-stream string))
     read-list)
    ((not (listen stream)) (reverse read-list))
  (push (eval (read stream)) read-list))

A

-- 
Writer: Someone who's never seen a chasm that didn't yawn.
From: Pierre R. Mai
Subject: Re: [Q]How to evaluate a string as lisp code?
Date: 
Message-ID: <87ya72rn8h.fsf@orion.dent.isdn.cs.tu-berlin.de>
Alex Henderson <·····@hermes.cam.ac.uk> writes:

> > Let's say your string is in the variable called string.
> > 
> > (eval (read-from-string string))
> > 
> > This will evaluate exactly one expression from the string.
> > You'll have to do something more (probably with subseq or
> > something) to read and evaluate multiple expressions from
> > a string.
> 
> Better still, use a string-stream:
> 
> (do ((stream (make-string-input-stream string))
>      read-list)
>     ((not (listen stream)) (reverse read-list))
>   (push (eval (read stream)) read-list))

For fans of loop:

(with-input-from-string (stream string)
  (loop with eof-value = (gensym)
        for expression = (read stream nil eof-value)
        while (not (eq expression eof-value))
        collect (eval expression)))

Regs, Pierre.

-- 
Pierre Mai <····@acm.org>         PGP and GPG keys at your nearest Keyserver
  "One smaller motivation which, in part, stems from altruism is Microsoft-
   bashing." [Microsoft memo, see http://www.opensource.org/halloween1.html]
From: David J. Cooper
Subject: Re: [Q]How to evaluate a string as lisp code?
Date: 
Message-ID: <38E190F2.3DD429AD@genworks.com>
"Yeh, Chih Hao" wrote:
> 
> I am new to lisp, and is writing a application mapping some tags into lisp
> codes. I heard LISP can run-time interpret string as code.
> I can build strings which contains legal lisp codes now, but I don't know
> how to make the string run. Any suggestions?
> Yeh, Chih Hao

Also, take a good hard look at why you are building strings containing
code, rather than just building expressions directly, with ` and , (backquote
and comma). If this is possible you will probably find it much more 
convenient, and it is compatible with the usual way of doing things
with macros.

 -dave

-- 
David J. Cooper Jr, Chief Engineer	Genworks International
·······@genworks.com			5777 West Maple, Suite 130
(248) 932-2512 (Genworks HQ/voicemail)	West Bloomfield, MI 48322-2268
(248) 407-0633 (pager)			http://www.genworks.com