4 ms·
Ask HN: Is there an easy example of a Lisp pretty-printer?
I'm working on a pretty printer for my own (toy) Lisp and it's harder than I thought. I've been looking for easy to understand example code, a pretty printer in Scheme or Lisp that I could adapt and extend but so far have only found academic overview papers from the 80s. It doesn't help that most of the stuff is for CommonLisp and uses format. I don't have any special string formatting directives and my Lisp is rather different from CL, more akin to XLisp.
Do any gray-bearded Lisp programmers remember some old Lisp or Scheme example code that I could study?
- eatonphil 5y agoPretty printing of what? Just s-expressions? Of functions and vectors and other sometimes builtin objects too?
- jonathanstrange 5y agoGood question. Pretty printing of any datum that can be bound to a symbol. I have numbers, strings, dicts (hash maps), arrays, lists, symbols and a few types of values that are not externalizable. The current pretty printer lays out everything as code, i.e., an input like '(let ...) would be interpreted as let form and formatted accordingly. But that's not the problem, I have troubles dealing with line-breaks. They are currently not intelligent enough, e.g. a list may break directly after an opening parenthesis even if the whole list would fit on the next line + indentation. It seems that a good pretty printer needs to look ahead? Anyway, looking for a simple clean example. Just a function recursing into a list and formatting it as code with a fixed margin and without using any fancy formatting instructions would be very instructive.
- kwhitefoot 5y agoYou could examine the Emacs Lisp mode indent functions.
- luizfelberti 5y agoPretty printing is a hard problem. Read Philip Wadler's paper on implementing Haskell's pretty printer [0], and then maybe study the Rust library called `pretty` [1] which is an implementation of it for tree like structures. [0] https://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf https://homepages.inf.ed.ac.uk/wadler/papers/prettier/pretti... [1] https://github.com/Marwes/pretty.rs https://github.com/Marwes/pretty.rs
- presz 5y agoMaybe take a look at parinfer https://shaunlebron.github.io/parinfer/ https://shaunlebron.github.io/parinfer/
- mhcolburn 5y agoHere is a paper on pretty printing: https://dspace.mit.edu/bitstream/handle/1721.1/6503/AIM-1102.pdf https://dspace.mit.edu/bitstream/handle/1721.1/6503/AIM-1102... Here is an article on a pretty printer written in Pico Lisp: https://picolisp.com/wiki/?prettyPrint https://picolisp.com/wiki/?prettyPrint
- freemint 5y agoThe PicoLisp pretty printer function printing itself looks like this: https://pastebin.com/K99n5pCC https://pastebin.com/K99n5pCC I attempted to also include all the helper functions which are not standard lisp manually. print and println put a space between arguments when printing prin and prinl do not. The ones with an l do line breaks, the others not. pretty prints it's first argument and the second number is an optional argument what level of indentation to start with.
- jonathanstrange 5y agoThank you so much! I think this is the simplest & clearest example so far and not so different in structure from what I've come up with. I'll study it in more detail.