gopikchr: a yakshave

I just completed part of a long yak-shave to add Pikchr support to my blog, generated with Hugo. I hope it helps to spread of Pikchr, and spurs implementations in other publishing pipelines.

Actually, it’s just the bottom part of an even longer yak-shave!

open my garage with a raspberry pi achieve accurate timing on a raspberry pi understand how to use DMA for gpio sequences “I should document this in a blog post” “I need Pikchr for diagrams”: make Pikchr work in hugo make Pikchr work in hugo port Pikchr to go port the lemon parser to go  ✓  ✓  ✓  ✓ (Click to see source)

Pikchr is a modern version of Brian Kernighan’s PIC markup language, created by the folks who make SQLite and Fossil. Like so many things from Bell Labs, it is extremely powerful, once you invest just a little time to understand how it works.

Pikchr is implemented in a single file, pikchr.y, processed using the Lemon Parser to generate lemon.c.

I decided to port both the Lemon Parser and Pikchr from C to Go, by hand.

Why?

Go and Pikchr are great! A pure-Go port of Pikchr might be useful to others.

Hugo might more readily accept pure-Go additions.

Sheer bloody-mindedness. At work we always have to make pragmatic decisions. The repressed urge to fully shave the yak comes out in my own time.

I respect the SQLite folks: their software is an amazing gift to our community. Think of this as a gift too.

How?

By hand. I looked at ccgo, the amazing C-to-Go compiler used to generate modernc sqlite, a pure-Go translation of sqlite. However, the code it generats to perform its magic is (necessarily) complicated and ugly, which seems contrary to the spirit of Pikchr. Nothing for it, but to dive in by hand. It can be almost meditative:

  1. Translate lemon.c to Go, but still generate C code
  2. Find all the bugs until it compiles and generates the exact same C code
  3. Translate the parser template, lempar.tpl to Go, and generate Go code
  4. Laboriously find all the bugs
  5. Translate pikchr.y from C to Go
  6. Once again, fix all the bugs until the Go version generates exactly the same Pikchr output

In all, it was almost 12,000 lines of code. I started out trying to be clever, but learned to keep things as similar as possible.

What next?

 

 

Comments? Reach me on Twitter