I'm interested in learning how to create a compiler for a dynamically typed language. Most compiler books, college courses and articles/tutorials I've come across are specifically for statically typed languages. I've thought of a few ways to do it, but I'd like to know how it's usually done. I know type inferencing is a pretty common strategy, but what about others?

Where can I find out more about how to create a dynamically typed language?

Edit 1: I meant dynamically typed. Sorry about the confusion.

I've written toy compilers for statically typed languages and written some interpreters for dynamically typed languages. Now, I'm interested in learning more about creating compilers for a dynamically typed language. I'm specifically experimenting with LLVM and since I need to specify the type of every method and argument, I'm thinking of ways to implement a dynamically typed language on something like LLVM.

closed as off-topic by gnat, user40980, GlenH7, ozz, Michael Kohne Oct 1 '13 at 12:18

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking us to recommend a tool, library or favorite off-site resource are off-topic for Programmers as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – gnat, Community, GlenH7, ozz, Michael Kohne
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Dynamic compilers are almost exclusively only used in emulators as dynamic recompilers. This wouldn't happen to be your intention for researching the subject area, would it? And speaking of this, examining the source code to said emulators is probably your best bet for learning about dynamic compilers. – Rob S. Mar 10 '11 at 2:44
  • @Rob S, I'm thinking more along the lines of something like a scheme or common lisp compiler. I know most popular dynamic languages are interpreted, but I was thinking there have to be some compilers as well. – Tachy Mar 10 '11 at 3:29
  • llvm is not different from any native target. You must have some form of runtime type information, you'd need an efficient implementation of a dynamic dispatch (e.g., for all the arithmetic operations over all of the types from Scheme numeric tower), and, if you're bold, you can use an abstract interpretation in order to specialise types wherever it is possible and explicitly get rid of a dynamic dispatch. That's it. The rest is pretty much the same as for the statically typed languages. – SK-logic Mar 10 '11 at 15:52

The Structure and Interpretation of Computer Programs finishes with completing an entire interpreter for Scheme in Scheme. Which can give you a good overview of how one could implement a dynamic language.

I would also recommend reading the Ruby source code. I have not attempted to read it in many years. But when I last tried, it was remarkably clear and easy to understand. There are a lot of good ideas in there.


There's also Write Yourself A Scheme In 48 Hours, in my eyes a great tutorial to implement a dynamic language (Scheme) in a strict language (Haskell).

  • After seeing Norvigs blog, I never trust these sort of Learn X in Y Hours/Days/Weeks title anymore – user Apr 10 '12 at 22:19

If you are interested in a common lisp compiler--how it's done and so forth--you might check out the source code for Armed Bear Common Lisp and target some specific questions to that community. Mac Ruby is a compiler for Ruby that uses the LLVM infrastructure. The source code and community around either project could be valuable resources for you.

  • Both of those projects look awesome. I am actually trying to write toy compilers using LLVM, so the Mac Ruby implementation might be just what I need. – Tachy Mar 10 '11 at 4:17

Where can I find out more about how to create a dynamically typed language?

["dynamically typed" is scary. "dynamic" is pretty common.]

Step 1. Pick a dynamic language. Really. Choose one.

Step 2. Google the language. Really. Use Google.

Step 3. Find the source for the language. It's easy. Python, Ruby, Lua, Haskell, etc., the compilers and run-times are all online. http://svn.python.org/view/, http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/, http://www.lua.org/source/5.1/, http://hackage.haskell.org/package/haskell-src-, http://code.google.com/p/fina-forth/source/browse/

Step 4. Read. Really. Read the source for the compiler. It's easy. It's online. The authors offer extensive documentation on their language.

What part of this is giving you problems? Please be specific and update your question.

  • 4
    Haskell?!? Dynamic?!? – SK-logic Mar 10 '11 at 11:19
  • @SK-logic: I couldn't parse the question. "Dynamic Type", "Dynamic". I gave up and threw in a ringer for fun. – S.Lott Mar 10 '11 at 11:21
  • 1
    I bet that OP meant c2.com/cgi/wiki?DynamicTyping – SK-logic Mar 10 '11 at 11:22
  • @SK-logic: Quite possibly true. The term still scares me because it leads to much confusion. Example: folks asking for the Python equivalent to a cast operator. – S.Lott Mar 10 '11 at 12:26
  • @S.Lott, it actually makes sense. A dynamically typed language still can be annotated (by such 'casts'), which helps to fall back to a type inference in a smart static compiler. Many Common Lisp implementations are doing this. – SK-logic Mar 10 '11 at 12:55

For Scheme, read this: http://www.iro.umontreal.ca/~boucherd/mslug/meetings/20041020/minutes-en.html

IronPython sources could be useful too.

For the most advanced dynamic language compiler, see V8: http://en.wikipedia.org/wiki/V8_%28JavaScript_engine%29

  • Sure that's more advanced than, say, every Common Lisp compiler? – David Thornley Mar 10 '11 at 16:00
  • @David, surprisingly, yes, it is more advanced. Simply because Javascript is a way much more dynamic than Common Lisp, and thus a compiler should be much more complicated. Common Lisp compilers are relatively trivial, and they're providing enough functionality for improving performance manually (e.g., type hints). Javascript is a completely different beast. – SK-logic Mar 10 '11 at 16:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.