Learn how to Write a Compiler in 2000 Lines of Code
This book explains the code of the λ☶ compiler to anyone who is interested in compiler architecture or functional language compilers. The code is roughly 2000 LOC in length, but it is very terse, so the explanation takes a while. The generated code is in the format of GNU Assembly for legibility. Binary targets are just as easy to write, but harder to read.
- The S-Expression Type Signature
- Why are S-Expressions Good
- How Do We Construct An S-Expression in Assembly
- What Common Functions Operate on S-Expressions
- What Is a Function?
- What Do We Expect From a Function Inside the Compiler?
- System Calls
- The
print-sFunction - The
strlenFunction - The
notFunction - The
write-fileFunction - The
load-fileFunction
- The Abstract Syntax Tree
- The
parse-programFunction - The
parse-many-expressionsFunction - The
parse-one-expressionFunction - The
parse-lambdaFunction
- Defining a Type Context
- Parsing a Type Definition
- Enumerating Inference Cases
- Infer Type of an Ascription
- Infer Type of a Literal
- Infer Type of a Variable
- Infer Type of an Application
- Infer Type of a Lambda
- The Expression Datatype
- The
compile-exprFunction - Codegen
thisExpressions - Codegen
localExpressions - Codegen
setExpressions - Codegen
matchExpressions - Codegen
whileExpressions - Codegen
ifExpressions - Codegen Function Application Expressions
- Codegen Literal Expressions
- Codegen
argvExpressions - Codegen Cons Expressions
- Codegen Nil Expressions
- Codegen Variable Expressions