sexpc
TOC »
sexpc is a tiny li'l Unix filter that reads one sexp from stdin and outputs the same expression in C syntax, using foof's fmt-c combinators. (The percent signs didn't make sense to me so I made them optional.)
It's like three lines, since foof did all the heavy lifting (and that tree library I wrote a few years back), it's just a toy.
The cool part is that you can use it in Emacs. Put this in your Emacs once you have the sexpc binary in your path:
(defun sexpc () (interactive) (shell-command-on-region (progn (beginning-of-defun) (point)) (save-excursion (end-of-defun) (point)) "sexpc" nil t))
Here's an example. Someone sends you a file and you wanna hack in it, but, it's C instead of a normal sexp language.
Let's say you wanna add a fib function.
You type:
(fun #f fib (n) (if (<= n 1) 1 (+ (fib (- n 1)) (fib (- n 2)))))
Easy enough with Paredit and friends.
Then, with your cursor anywhere in the sexp, you call sexpc (via M-x or via a binding), and it's replaced by:
int fib (int n) { if (n <= 1) { return 1; } else { return fib(n - 1) + fib(n - 2); } }
Roadmap
Of course, down the line we'd wanna figure out a way to do macros, quasiquoting etc, and support other C-like languages like JavaScript or Golang. That might require a setup beyond just reading one sexp. But sometimes cool stuff can grow from what starts as just a toy.
Source code
git clone https://idiomdrottning.org/sexpc