Antti wrote:
bwat wrote:there are plenty of good compiler books that might suit you better
What is your recommendation?
I like books that go from interpreters to compilers. The connection between the two is important.
The one book which I've used to write a compiler which I've then properly used is
Lisp in Small Pieces by Christian Queinnec. If you know Scheme/Lisp then it'll get you writing a compiler in no time. The chapters are:
1) Writing an interpreter for Scheme/Lisp.
2) Name spaces and recursion.
3) Exceptions and call-with-current-continuation.
4) Assignments and quotations.
5) Denotational semantics. This is the theory part but it's explained nicely.
6) Making the interpreter faster.
7) Compiling to byte code.
8) Reflection. This chapter covers things most programmers don't know are possible. The reflective interpreter is cool.
9) Macros.
10) Compiling to C. I never bothered implementing this part as the bytecode compiled system is fast enough.
11) An object system. I never bothered with implementing this part either.
A similar book is
Programming Languages, An Interpreter-Based Approach by Samuel N. Kamin. Basically, the book gives Pascal code for 8 different languages. The languages are the ones that were hot in the late 80s and early 90s (it's an old book). The languages in the book are much simpler than the real languages but you get a taste of what the real ones are like. The chapters are
1) A basic evaluator, i.e. an interpreter that deals with integers and arithmetic and boolean operations. Simple function definition is possible.
2) LISP. This is Chapter 1 with lists.
3) APL. This is Chapter 1 with arrays.
4) Scheme. This is Chapter 2 with functions as first class values.
5) SASL. A lazy version of Chapter 4.
6) CLU. This is Chapter 1 with clusters (a kind of data type definition).
7) Smalltalk. This is Chapter 7 with classes.
8) Prolog. This is the most different chapter, basically simple SLD resolution.
9) Compilation to bytecode. Translation from Chapters 1 and 4, to a stack machine is shown.
10) Memory Management. This covers simple garbage collection (mark-scan, semi-space, reference counting).
The book
Abstract Computing Machines, A Lambda Calculus Perspective by Werner Kluge is another great book but as the name suggests it uses the lambda calculus as the programming language to be interpreted/compiled and that might put people off. Personally, I loved the book.