[plt-scheme] changing the behavior of 'read'?

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Sun May 25 13:10:46 EDT 2003

At Tue, 20 May 2003 21:51:38 -0700, Aaron Leung wrote:
> I want to embed an interpreter for a Lisp-like language into a DrScheme 
> program.  I'd like to read expressions using DrScheme's 'read', but the 
> lexical structure of the embedded language is different from Scheme's.  
> Is there a way I can change the behavior of 'read'?  For example:
> 
> * when the reader sees [1 2 3], it returns #(1 2 3)
> * when it sees #|some symbol|, it returns |some symbol|
> * when it sees |, it returns \|
> * when it sees ##, it ignores the rest of the line
> * etc
> 
> I skimmed the section on custon input ports in the MzScheme manual, but 
> I found it a little confusing.  Can someone give me an overview of how 
> to handle this, or point me to some simple examples?  Thanks for any 
> info.

Probably you don't need a custom input port. If you want to transform
one text stream into another, create a pipe with `make-pipe', and
create a thread to pump characters from the original port to the write
side of the pipe.

 > (let-values ([(r w) (make-pipe)]
                [(orig) (current-input-port)])
     (thread (lambda ()
               (let loop ()
                 (write-char (char-downcase (read-char orig)) w)
                 (loop))))
     (current-input-port r))

 > "Hello"
 "hello"

I'm not sure that's the best way to accomplish your task, but it avoids
custom ports.

Matthew



Posted on the users mailing list.