From: ramza2
Subject: Simple Read File and Eval
Date: 
Message-ID: <1102648469.479662.283640@c13g2000cwb.googlegroups.com>
This may or may not be what I am looking for.  I want to load a file,
part of the file is lisp and the other part is something else.  Kind of
a like a config file or annotated file.  I could use the simple 'load'
if the file was all lisp, but I am going to read the lisp part of the
file and get the pure lisp syntax and I want to 'evaluate once I have
loaded it'

Can I just read it into a string and then call 'eval' or am I looking
for something else.

File:

###
### fslsjlfj
### lsfjksjfls
###

Here is the lisp code
#$
(print "something")
$#

---- End of File --

Read the above into a stream?
once I have the code above, what do I do to evaluate it just 'eval'?

From: Pascal Bourguignon
Subject: Re: Simple Read File and Eval
Date: 
Message-ID: <87fz2eu6r8.fsf@thalassa.informatimago.com>
"ramza2" <············@gmail.com> writes:

> This may or may not be what I am looking for.  I want to load a file,
> part of the file is lisp and the other part is something else.  Kind of
> a like a config file or annotated file.  I could use the simple 'load'
> if the file was all lisp, but I am going to read the lisp part of the
> file and get the pure lisp syntax and I want to 'evaluate once I have
> loaded it'
> 
> Can I just read it into a string and then call 'eval' or am I looking
> for something else.
> 
> File:
> 
> ###
> ### fslsjlfj
> ### lsfjksjfls
> ###
> 
> Here is the lisp code
> #$
> (print "something")
> $#

You have basically two choices:

- have lisp somewhat "interpret" the non-lisp part of the file using
  reader-macros, then you can just (load "the-file")

- implement your own parser reading the lines or characters of the
  file, and filtering out the sexps that you will pass to eval.

It depends on the syntax of the rest of the file, and whether you want
to consider the file lisp with embedded alien stuff, or alien with
embedded lisp stuff.  If the syntax of the rest of the file is simple,
it would be much easier to write a couple of reader macro and just
load the file, relying on the lisp reader to do the hard work.

For example, if all the non-lisp lines begin with the string "##", you
could write this reader macro:

    
(SET-DISPATCH-MACRO-CHARACTER
 (CHARACTER "#") (CHARACTER "#")
 (LAMBDA (STREAM CHAR ARG)
   (DECLARE (IGNORE CHAR ARG))
   ;; read up to the end of line and return the read string
   (values (read-line stream nil nil t))))

Then you can load such a file:

###
### fslsjlfj
### lsfjksjfls
###
## Here is the lisp code
##
(print "something")
##


Or, if all the non-lisp lines are parenthesized with "#!" and "!#",
you could write this reader macro:


(SET-DISPATCH-MACRO-CHARACTER
    (CHARACTER "#") (CHARACTER "!")
  (LAMBDA (STREAM CHAR ARG)
    (DECLARE (IGNORE CHAR ARG))
    ;; read characters up to the first occurence of "!#",
    ;; and return the string read.
    (loop for curr = (read-char stream nil nil t)
          while (and curr
                     (or (char/= (character "!") curr)
                         (char/= (character "#") 
                                 (peek-char nil stream nil nil t))))
          collect curr into str
          finally (read-char stream nil nil t)
          (return (coerce str 'string)))))

Then you can load such a file:

#!
###
### fslsjlfj
### lsfjksjfls
###
!#

#! Here is the lisp code !#

(print "something")

#! that's all folks !#


Of course, you can use both symbol macros and load files such as:

###
### fslsjlfj
### lsfjksjfls
###

#! Here is the lisp code !#

(print "something")

## that's all folks 

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
The world will now reboot; don't bother saving your artefacts.
From: Rob Warnock
Subject: Re: Simple Read File and Eval
Date: 
Message-ID: <vYSdnWdfZpB_00_cRVn-og@speakeasy.net>
Pascal Bourguignon  <····@mouse-potato.com> wrote:
+---------------
| "ramza2" <············@gmail.com> writes:
| > This may or may not be what I am looking for.  I want to load a file,
| > part of the file is lisp and the other part is something else.  Kind of
| > a like a config file or annotated file.  I could use the simple 'load'
| > if the file was all lisp, but I am going to read the lisp part of the
| > file and get the pure lisp syntax and I want to 'evaluate once I have
| > loaded it'
| ...
| You have basically two choices:
| - have lisp somewhat "interpret" the non-lisp part of the file using
|   reader-macros, then you can just (load "the-file")
| - implement your own parser reading the lines or characters of the
|   file, and filtering out the sexps that you will pass to eval.
+---------------

Third choice: If the file is non-Lisp followed by pure Lisp, then you
can read the non-Lisp part with your own parser, then at a clean token
boundary call the normal Lisp LOAD with the already-open stream, e.g.:

	(with-open-file (stream "filename")
	  (loop for line = (read-line stream nil nil)
		while (and line (not (last-special-line-p line)))
	    do (process-special-line line)
	    finally (load stream)))

[LAST-SPECIAL-LINE-P & PROCESS-SPECIAL-LINE are left as exercises
for the reader...]


-Rob

-----
Rob Warnock			<····@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607