Creating a Compiler in Raku
A step-by-step coding essay that builds a working compiler in Raku — from a tiny translator through grammars, actions, and an abstract syntax tree to a full calculator and interpreter.
Creating a Simple Translator
Parsing a Number
Creating a Calculator
- Enrolling a sum
- Premature optimization
- More operands
- A diversity test
- Adding more math
- Testing the code
- Adding more power
- Allowing parentheses
A Better Translator
- Skipping comments
- Sophisticated numbers
- Sophisticated expressions
- Using variables
- Declaration with initialisation
Working on Grammar
- Executable
- Composing and inheriting grammars
- Reviewing the calculator
- Using multi-rules
- Get rid of globals
- Better variable names
- Functions take expressions
Dealing with Strings
Arrays and Hashes
- Arrays
- Assigning to an array item
- Off-topic: The joy of syntax
- Accessing array elements
- List assignments
- Printing arrays
- Hashes
- Review and test
Building AST
- Thinking of the if keyword
- The AST blocks
- Value nodes
- Declaring strings
- Declaring arrays and hashes
- Scalar assignment
- Working with indices
- Array and hash assignment and initialisation
- AST for expressions
- Function calls
- Final tuning
Evaluating AST
- Evaluating from the TOP
- Working with variables
- String interpolation
- Using Raku getters
- Interpolating everywhere
- Arithmetic operations
Test suite
Control Flow
- Implementing if
- Implementing else
- Implementing a loop
- Statement blocks
- Blocks in the AST
- A real-life test
- Conditional operators
- Implementing while