There’s a lot of fiddly detail in the concrete syntax tree that we’re not very interested in. The parser produces a data structure called a concrete syntax tree, or a parse tree, which contains a lot of information about how the parsed string breaks down into all its component parts. SyntaxNode+Multiply1+Multiply0 offset=11, "x * 3" (left,right): SyntaxNode+Assign1+Assign0 offset=7, "y = x * 3" (name,expression): SyntaxNode+Assign1+Assign0 offset=0, "x = 2" (name,expression): => SyntaxNode+Sequence1+Sequence0 offset=0, "x = 2 y = x * 3" (first,second): Here are some example Simple programs: a = 19 + 23
It’s very straightforward and looks a little bit like Ruby, but it’s not exactly the same. Let’s investigate each of those steps by building an interpreter for a toy language called Simple (“ simple imperative languag e”).
#FREE VIDEO COMPILER CODE#
For a bare-metal physical machine that means the program has to be written in low-level machine code. In practice, though, this simple picture is only possible if the program is written in a language that the machine can execute directly. In principle, the program executes on the machine, reads the inputs, and produces some output: Then we have some inputs for that program - command-line arguments, or configuration files, or standard input, or whatever - so we feed all of those inputs into the machine too. To execute the program, the first step is to put the program into the machine. Say we have a program that we want to run, as well as a machine to run it on. We’ll begin by considering something easy and familiar: executing programs. I don’t have a complicated technical point to make I just want to tell you a cool story, show you that programs which manipulate other programs are interesting, and hopefully inspire you to find out some more about them.
#FREE VIDEO COMPILER SOFTWARE#
It’s the purest and most self-referential kind of software that we can write.īy showing you around this world I’d like to make you look at programs differently. This is the world of interpreters, compilers and static analysers, and I think it’s fascinating. These are programs which ingest data that itself represents a program, and then do something with that representation, like analyse it, evaluate it, translate it, or transform it. When Ruby programmers talk about metaprogramming they usually mean programs that write programs, because Ruby has various features - like BasicObject#instance_eval, Module#define_method and BasicObject#method_missing - which make it possible to write programs that grow new functionality at run time.īut there’s another aspect of metaprogramming that I find even more interesting: programs that manipulate representations of programs. I’m fascinated by programming, and by metaprogramming in particular. The video of the talk is below, and the slides are also available. This article is adapted from a talk I gave at Rub圜onf 2013, which itself was based on my notes for an unrealised chapter of Understanding Computation (interpreters and compilers are explored in more detail in the sample chapter).
I’ll use Ruby to take you on a tour of how interpreters and compilers work, introduce the ideas behind a technique called partial evaluation, and explain a surprising computer science result which allows compilers to be generated automatically. Programs that manipulate other programs are powerful, interesting and fun.