Antireader macros

Discussion of Common Lisp
Goheeca
Posts: 271
Joined: Thu May 10, 2012 12:54 pm
Contact:

Antireader macros

Post by Goheeca » Sun Sep 09, 2012 4:05 am

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.

pjstirling
Posts: 166
Joined: Sun Nov 28, 2010 4:21 pm

Re: Antireader macros

Post by pjstirling » Sun Sep 09, 2012 8:32 am

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

Goheeca
Posts: 271
Joined: Thu May 10, 2012 12:54 pm
Contact:

Re: Antireader macros

Post by Goheeca » Sun Sep 09, 2012 8:44 am

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.

wvxvw
Posts: 127
Joined: Sat Mar 26, 2011 6:23 am

Re: Antireader macros

Post by wvxvw » Mon Sep 10, 2012 1:20 am

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.

Goheeca
Posts: 271
Joined: Thu May 10, 2012 12:54 pm
Contact:

Re: Antireader macros

Post by Goheeca » Mon Sep 10, 2012 2:38 am

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.

wvxvw
Posts: 127
Joined: Sat Mar 26, 2011 6:23 am

Re: Antireader macros

Post by wvxvw » Mon Sep 10, 2012 2:49 am

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

Goheeca
Posts: 271
Joined: Thu May 10, 2012 12:54 pm
Contact:

Re: Antireader macros

Post by Goheeca » Mon Sep 10, 2012 2:53 am

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.

sylwester
Posts: 133
Joined: Mon Jul 11, 2011 2:53 pm

Re: Antireader macros

Post by sylwester » Mon Sep 10, 2012 10:11 am

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.
I'm the author of two useless languages that uses BF as target machine.
Currently I'm planning a Scheme compiler :p

Goheeca
Posts: 271
Joined: Thu May 10, 2012 12:54 pm
Contact:

Re: Antireader macros

Post by Goheeca » Wed Sep 12, 2012 6:29 am

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.

sylwester
Posts: 133
Joined: Mon Jul 11, 2011 2:53 pm

Re: Antireader macros

Post by sylwester » Wed Sep 12, 2012 8:02 am

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 Wed Sep 12, 2012 8:16 am, 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

Post Reply