Rolling Your Own Syntax

Over at Abstract Heresies, Joe Marshall has a great post on one of the reasons Lisp is such a great family of Languages. I say “family” because there isn’t a single Lisp language. These days, the four main Lisps are Common Lisp, Scheme, Clojure, and Elisp although there are others. I’ve been using all but Clojure for about 20 years. Recently, I do most things in Elisp simply because it’s quick and easy to get something going in Emacs with it.

One of the main sources of Lisp’s power is that Lisp programs are defined in terms of Lisp data structures. Specifically, a Lisp program is a set of nested lists. That’s the source of the well known aphorism that in Lisp, programs are data and data are programs. Marshall’s post looks at another outcome of Lisp programs being data: macros.

Because Lisp programs are data, it’s possible, through macros, to write code at compile time that then becomes part of the program being compiled. A consequence of this is that unlike almost every other language it’s possible to add new syntax to the language. Marshall gives a couple of examples of how easy it is.

Another, less exciting example, that I used to deal with all the time was adding a while construct. One of the Lisps I mentioned above, I no longer remember which, didn’t have a while operator at the time but it was almost trivial to add it in terms of existing constructs. Try doing that in any other language. Even if it’s possible you’re going to have to modify the parser, and perhaps, the lexer and code generator as well.

This capability is why Lisp is called the programmable programming language. The fact that Emacs is written in a Lisp is why it’s fair to describe it—and only it—as a programmable programming editor.

This entry was posted in General and tagged , . Bookmark the permalink.