[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: Extract function from strings

William James

7/19/2015 6:26:00 AM

> (defun tokenize-line (line)
> (let ((result ()))
> (with-input-from-string (stream line)
> (with-open-stream (output (make-string-output-stream))
> (loop for char = (read-char stream nil nil)
> do (case char
> (#\( (let ((position (file-position stream)))
> (unread-char char stream)
> (handler-case
> (let ((r (read-preserving-whitespace stream)))
> (push (get-output-stream-string output) result)
> (push r result))
> (error () (file-position stream position)
> (write-char char output)))))
> ((nil) (push (get-output-stream-string output) result))
> (t (write-char char output)))
> while char)))
> (nreverse result)))
>
> * (tokenize-line "There are (princ (+ 2 2)) cats in the attic.")
> => ("There are " (PRINC (+ 2 2)) " cats in the attic.")

Gauche Scheme:

(define (tokenize-line line)
(if (eof-object? line)
'()
(let1 p (string-scan line #\()
(if p
(cons (substring line 0 p)
(with-input-from-string (string-copy line p)
(lambda ()
(cons (read) (tokenize-line (read-line))))))
(list line)))))

(tokenize-line "There are (print (+ 2 2)) cats (9) in the attic.")
===>
("There are " (print (+ 2 2)) " cats " (9) " in the attic.")

--
Sahlin [stressed] that her compatriots must accept that the new Sweden is
multi-cultural.... "Like it or not, this is the new Sweden."
fjordman.blogspot.ca/2005/05/is-swedish-democracy-collapsing.html