Table of Contents
- Introduction to Apprentice Compiler Design
- Language Specification
- Infrastructure
- Language Design Decisions
- Tokens
- Scanner
- AST
- Parser
- Assembly
- C Runtime
- Using the C Runtime (Assembly Part 2)
- Compiler, Code Generation
- Front End
Introduction
Welcome! It's been a long time coming!
This series is the second I've written on compilers. The first one can be found here and if you've not already done so, I highly recommend starting there first because this series builds upon the code and ideas started there.
Calc 2 is an evolution of the language. Calc 1 achieved my goal of being a basic calculator and a teaching tool but it leaves much to be desired for being a much more useful, general purpose tool.
While Calc 1 demonstrated how to take raw source code and compile it into a working binary, it did little to demonstrate how languages are actually transformed into machine language. Short of doing some basic arithmetic, it does little else.
I also did not answer many questions I posed in the first series. Questions about different types of parsers, abstract syntax trees and language design decisions. I hope to address more of these questions while still keeping the discussion on the task at hand.
In this second series, I have set a new goal for Calc. We will give it the ability to calculate a Fibonacci number or a factorial.
So, hold on tight because here we go!
Forewarning
You need to understand the material covered in the first series before tackling anything covered here. This series takes a considerable jump in difficulty and complexity.
I’ll do my best to explain what I can but I will, like in the last series, be making some assumptions about your level of programming skill. A moderate level of competency in Go is a must.
I should also caution you that I am not a professional programmer nor am i an expert on compilers. I am merely trying to pass on some of the knowledge I’ve gained in the hopes of helping you learn how to write a compiler yourself.
Also, when we reach the topic of the C runtime and assembly you will find that having experience with pointers and understanding how memory addressing works will be an asset.
Consider yourself warned.
More Credit
You can find the source code for Go at golang.org.
Runtime
This runtime is very small and implements a tiny subset of pseudo-assembly instructions to make code generation easier. The library resides within Calc’s own directory so once Calc is purged from your system so is the runtime. It is never installed into the main OS’s system directories to lay forgotten, taking up space. Lurking...waiting...hungering…
You need never look at it if you don’t want to but you probably should.
Assembly
Not to mislead you, because we won’t be actually using assembly, but in order to understand how the C runtime works you will need to know the strengths and advantages of assembly as it pertains to language implementation.
Language Features
Calc 1 already does the arithmetic we require to make the needed calculations but it lacks the ability to actually facilitate our needs in a meaningful way.
In order to add a meaningful level of abstraction, Calc 2 will need to have the following implemented:
- Functions
- Variables
- Looping
- Branching
Compiler Features
I will also be including the ability to compile a directory containing multiple source files.
Bugs
I have attempted to be as thorough as possible but I can’t make a 100% bug-free claim. If you discover a bug, please report it in the issue tracker.
Moving Forward
There is a lot to digest in this series. It is a considerable amount longer and a lot of new information will be introduced. I hope you bear with me and take the time to make it all the way through.
I wish you the best of luck!
No comments:
Post a Comment