This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Antireader macros

15 posts · 15555 views

If I create a reader macro that the Lisp reader treats with curly brackets like parentheses, how to program the Lisp printer to do the opposite.
It's something with pretty printing probably, but I can't find some example and the documentation is bald resp. it would take me too much time and perhaps here is somebody, which has some experience with this.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

Re: Antireader macros

If you genuinely wish things in curly braces to be returned to the compiler as lists, then printing the code has no chance to distinguish between "normal" lists and your new ones, so either you change the print-object for all lists (probably not what you want), or you live with it.

If your curly braces are producing an object that isn't a list then you can choose how to print it whatever way you want

Re: Antireader macros

Actually this is only an example. And I want just to print all lists with curly brackets in this case.
// It has to do with an alternative syntax in my signature.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

Re: Antireader macros

You really don't want that to happen for all lists because it will break too many other things potentially... This sounds like a rather peculiar task for some of your custom data / purpose. I'd just go with format'ing them with whatever style you like, else you might have to change too much of other code to work with your kind of parenthesis.

Re: Antireader macros

No. For example in this case the curly brackets would work simultaneously with parentheses and the antireader macro would be used only with the reader macro so nothing would break.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

Re: Antireader macros

If they will be used simultaneously, how then the printer will choose which to use for printing? :/

Re: Antireader macros

Simply by invocation of a provided function or by setting a variable.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

Re: Antireader macros

If you know exactly where there are curlies and where there are normal parenthesis such that the structure makes it obvious you can just make a function my-print to deal with it.
If there is no way of knowing where the curlies are they are kind of like how Racket reads all types of brackets. (Yes, I know this is the CL forum)
(define test '(a b {c d} e f))
test
=> '(a b (c d) e f)
As you can see. Just like people are telling here, there is no way to actually tell the two apart after it's parsed unless you do some other tricks.
One trick would be to tag the different list so that your reader actualy adds something instead of just converting it to a parenthesis.
(define tag #(curly))
(define test '(a b {c d} e f))
test
=> '(a b (#(curly) c d) e f)
Making a print function to print your structuire would be a piece of cake and even stripping it off if you need to eval it too.
I'm the author of two useless languages that uses BF as target machine.
Currently I'm planning a Scheme compiler :p

Re: Antireader macros

The reading I have got solved, the issue is printing lisp-objects in my syntax by the standard print function, so REPL returns results in my syntax.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

Re: Antireader macros

Goheeca wrote:The reading I have got solved, the issue is printing lisp-objects in my syntax by the standard print function, so REPL returns results in my syntax.
If I understand you correctly your reader reads {} as () so that ANY place in an expression it could be entered as curlies.
You can overload print, but you cannot "undo" what the reader does exactly unless you tag the ines with different brackets. Print could show curlies where you would like culries to be in place of parenthesis follwing a pattern but not dictated by the original input (unless the input followed the same scheme).
Can you give an example of input for the reader and how the result is returned from the reader and how it should look when printed if you feed your reader the same expression with:

A) only curlies in place of parenthesis everywhere?
B) mixed curlies and parenthsis?

Last edited by sylwester on , edited 1 time in total.

I'm the author of two useless languages that uses BF as target machine.
Currently I'm planning a Scheme compiler :p

Re: Antireader macros

It's user's arbitrariness (like *print-circle* etc.).
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

Re: Antireader macros

Just spinning my wheels here, what about defining a type or class that's instantiated when the reader encounters curly-braces, possibly a subclass/subtype of list, and specialising print-object on that type/class?

Note: this is based on exactly zero fact-checking, combined with a sketchy grasp of CL's type system.

Re: Antireader macros

sylwester wrote:A) only curlies in place of parenthesis everywhere?
B) mixed curlies and parenthsis?
A) - I don't mind that I'm going to lose information during a reading part.
JamesF wrote:Just spinning my wheels here, what about defining a type or class that's instantiated when the reader encounters curly-braces, possibly a subclass/subtype of list, and specialising print-object on that type/class?
The reader macro is more generic actually it's for differently written lists, no special structure, so I would say that doesn't fit.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

Re: Antireader macros

So I'm probably blind and possibly set-pprint-dispatch will be sufficient.
Here is a simple solution for my factitious problem, although it deals bad with a syntactic sugar like quote, quasiquote, unquote, etc.
(set-pprint-dispatch '(cons t t)
                     #'(lambda (s input) (format s "{~{~s~^ ~}}" input)))
// I wonder you haven't kicked me to read the documentation properly.
And now I'll see how good is it for cl-2dsynax.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.