Home > Articles > Programming

  • Print
  • + Share This
From the author of

Metacircular Evaluation

One test that I've seen proposed for determining how expressive a language is: How easy is it to write a metacircular evaluator in that language? In other words, how easily can you write an interpreter for the language, using the language itself?

In Lisp, this task is very easy. From first principles, it's actually easy because Lisp provides tools for reading S-expressions, and a Lisp interpreter just has to dispatch based on the first element in each expression. More importantly, Lisp contains an eval keyword, which is effectively a function that takes some Lisp code as an argument and runs it.

eval crops up in a lot of languages. It's probably most famous for the security holes that it has caused in poorly written PHP applications, but in Lisp eval is very powerful. In a lot of Lisp implementations, it drives the incremental compiler, rather than just an interpreter, so you can use it to add compiled functions to a program.

It's also used for metaprogramming in a lot of Lisp code. You can construct some S-expressions (trivially—they're just lists, which are the simplest compound data type in Lisp), and then run them as Lisp code. This capability makes Lisp a very popular language for writing interpreters; if you construct Lisp code from your new language and eval it, you often get to take advantage of the Lisp implementation's optimizing compiler.

  • + Share This
  • 🔖 Save To Your Account