Page 1 of 2

Antireader macros

PostPosted: Sun Sep 09, 2012 4:05 am
by Goheeca
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.

Re: Antireader macros

PostPosted: Sun Sep 09, 2012 8:32 am
by pjstirling
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

PostPosted: Sun Sep 09, 2012 8:44 am
by Goheeca
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.

Re: Antireader macros

PostPosted: Mon Sep 10, 2012 1:20 am
by wvxvw
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

PostPosted: Mon Sep 10, 2012 2:38 am
by Goheeca
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.

Re: Antireader macros

PostPosted: Mon Sep 10, 2012 2:49 am
by wvxvw
If they will be used simultaneously, how then the printer will choose which to use for printing? :/

Re: Antireader macros

PostPosted: Mon Sep 10, 2012 2:53 am
by Goheeca
Simply by invocation of a provided function or by setting a variable.

Re: Antireader macros

PostPosted: Mon Sep 10, 2012 10:11 am
by sylwester
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)

Code: Select all
(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.

Code: Select all
(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.

Re: Antireader macros

PostPosted: Wed Sep 12, 2012 6:29 am
by Goheeca
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.

Re: Antireader macros

PostPosted: Wed Sep 12, 2012 8:02 am
by sylwester
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?