[plt-scheme] $-expressions

From: Neil Toronto (ntoronto at cs.byu.edu)
Date: Fri Mar 12 10:52:37 EST 2010

Eli Barzilay wrote:
> On Mar 11, Neil Toronto wrote:
>> It plays nicely with at-exp (as long as at-exp goes second; not my
>> fault, AFAICT - it should compose with almost any reader):
> The scribble reader is careful to extend the current readtable rather
> than make up a new one, so it should work in any order.

Heh. So is rest-reader, hence my comment. Perhaps being careful to 
extend the current readtable isn't enough? In my experiments, I noticed 
that reverting to a saved readtable in the middle of a read was enough 
to break composability.

BTW, by "doesn't compose" I mean that this works:

     (list 1 $ list 1 2 @list[3])

but this doesn't:

     @list[1 $list 1 2 $list 3]

if the scribble reader is first. Both work if it's second.

> In any case, if you really want to do
> this the right way, you should create a module reader like at-exp,
> ...

I did that in a few of my experiments. But it didn't allow me to do the 
$module example, so I chucked it. Also, I couldn't test both "valid 
module" cases (see way below), since #lang always seemed to spit out a 
module+#%module-begin. If I knew that were *always* the case I wouldn't 
care so much. Is it?

FWIW, chaining readers with #reader is flexible and fairly 
straightforward compared to chaining reader modules with #lang.

>> That reminds me: when you copy @-expressions into the REPL, they
>> don't parse. But scribble/reader.ss could do this more easily than
>> rest-reader.ss does.
>> (Eli, I have complained. Do I receive? :p)
> I'm not sure what you mean by "could do this more easily than
> rest-reader.ss does" -- both cases should be similar.

Sorry - I was tired when I wrote that. I mean that it wouldn't have to 
play macro tricks to bind the compile-time path of reader.ss in phase 0. 
(See the current-module-path macro and module-path in rest-reader.ss.) 
rest-reader.ss has to do that because it can't assume that it's been 
installed in a particular directory in the collects path.

> The problem
> here is that currently the lang/reader facility has only a reader for
> module code, and it should eventually be extended to also have an
> expression reader, which would then be used by all the usual parties
> (the mzscheme repl, the drscheme repl, the sandbox repl).

For now, rest-reader.ss alters, by cases, the syntax object returned by 
read-syntax. If it's (module name lang (#%module-begin body ...)) or 
(module name lang body ...), it assumes it has read a module and adds 
expressions to require itself and extend the readtable. Runtime calls to 
read thus use the extended readtable, and this includes REPL 
expressions. If the syntax object has some other form, it leaves it 
alone - it's either 1) a malformed module, which won't compile anyway; 
or 2) the result of reading an expression, so the readtable is already 

I haven't tested it with the mzscheme or sandbox REPLs yet.

Neil T

Posted on the users mailing list.