1. 24
  1.  

  2. 5

    Because C/C++ became popular long before auto-formatters became popular (and e.g. part of pre-submit hooks), there are many different (and incompatible) C/C++ styles in use.

    classic indent(1) is from some time around 1977. See https://github.com/freebsd/freebsd/blob/master/usr.bin/indent/README for a bit of its history.

    1. 2

      Similar idea to Andy Wingo’s recent work on a “baseline compiler” for Guile that does a lot less, but does enough for the context it’s used, and does it a lot faster.

      1. 2

        This is really cool, but also doesn’t answer the question of why in the world you care about pretty-printing the output of your compiler at all…?

        1. 5

          Not following – have you ever worked with a code generator that outputs say everything on one line?

          Those code generators quickly get fixed, or don’t get used at all.


          edit: Looking more closely, I think the issue is that they wrote the original compiler assuming that its output would be run through a separate pretty printer. That is, they wrote it without regard to formatting.

          So it’s reasonable to ask why they wouldn’t just fix the original code to output indented text.

          In other words, I think that code generators should always output readable code (within the constraints of the problem, e.g. parse tables). But that doesn’t mean they should be pretty-printed with a separate tool!

          1. 2

            Maybe a better question is why would you care about the performance of your compiler in “pretty-print-mode” when the default should be “fast mode” and pretty mode is only used when you’re sending the compiler output to something other than another compiler?

            1. 2

              Having two modes is overkill – who is going to bother to set that up in their build system?

              That said, I agree the architecture described in this post is kinda weird:

              The ‘unformatted’ C code, by construction, already has line breaks in sensible places. The formatter only needs to fix up the horizontal formatting (i.e. indentation) due to nested {} braces and () parentheses.

              I don’t think it’s that hard to output correctly indented code in the first place. I’ve done this in several code generators. It might be annoying if your meta-language doesn’t have multiline strings… I think theirs is Go, and it might not?

              Wrapping code is a little trickier, but also not hard to reasonable job in the tool itself. I found a nice language-independent heuristic in a code generator I borrowed.

              1. 2

                Go definitely has multiline strings, via backticks.

              2. 2

                If the user wants the output pretty printed during debugging for example they can just pipe it through their favourite formatter. No modes necessary.

              3. 2

                Not following – have you ever worked with a code generator that outputs say everything on one line?

                One line? no, but the output of most code generators isn’t particularly pretty.

                Most code generators only have output to assist debugging serious issues, and most users don’t care too much about the output of code generators – to the point that they often never even materialize the generated code, and just feed it directly into the compiler. (This is, essentially, what a macro system like Lisp’s, Rust’s, or even the C preprocessor does)

              4. 4

                I assume to make debugging the intermediate output easier.

              5. 1

                I like how the page says “Wuffs is a memory-safe programming language and a standard library written in that language.”, and the README for Wuffs says “…while technically possible, it is unlikely that a Wuffs compiler would be worth writing entirely in Wuffs.”.

                1. 2

                  I don’t see any contradiction there. There are many reasons that the stdlib could be suitable for Wuffs but the compiler itself isn’t. Most compilers are not bootstrapped for years, e.g. Go took 8-10 years I think. The compiler was written in C but the stdlib was in Go.

                  1. 1

                    You’re completely right, I completely thought I read something else.