1 Mar 2004 bmastenbrook   » (Master)

So, I've decided to embarrass myself and release my s-expression to C translator. The goal here is to come up with a suitable target representation for a compiler, without having to worry about actual C syntax in the compiler itself. Since it supports macros, you can build the target language a bit higher too. As an example, consider Duff's Device - it can be expressed for a fixed amount of unrolling in plain C, but you can't vary that except with possibly some horrendously complex C++ templates. The macro to generate Duff's Device reads as follows:

#@(defmacro #@make-duff #@(to from count n-type size)
    `#@(let ((,n-type n (/ (+ ,count ,#@(- #@size 1)) ,size)))
         (switch (mod ,count ,size)
                 (case 0)
                  ((setq (deref ,to) (deref (post-incf ,from)))
                   ,@#@(loop for i from (- #@size 1) downto 1
                          append `#@((case ,#@i)
                                     (setq (deref ,to) (deref (post-incf ,from))))))
                  (> (decf n) 0)))))
The #@'s there are to bounce back and forth between readtables - the s-expression C needs to be read into a table which preserves case and also preserves backslashes in strings, so #@ is defined to take you between that table and an ordinary one.

There is documentation up as well, and a developer mailing list. It's not complete yet at all, but already it's useful to me. Ideas, suggestions, and complaints about opening up lisp macros to C are welcome :-)

Also, I should mention my common-idioms package here - I see a lot of similar macros out there, and I recently noted that Pascal Costanza reinvented the destructuring-case macro. Mine has a few more features, so if you're doing some heavy destructuring you might want to take a look at it.

The rest of the package is worthwhile to me too - it contains an anaphoric if with a setf-it macro, an anaphoric if that tests on the second return value and binds "it" to the first (very useful!), multiple-value aggregating reduce (my favorite iteration construct of all time), and some other useful tools.

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!