Crafting Interpreter Day0
Start following the book of CRAFTING INTERPRETERS in this series. Book always includes large number of information and context. Hope this series of reading notes can help me to review easily in some days. CRAFTING is implementation, I hope I can build up an interpreter with this series. In this book, two interpreters will be created: jlox in Java and clox in C. The programming language to be implemented is Lox (self-defined).
A journey of Implementation
- Scanning Scanner (lexer) takes linear stream of characters into tokens.
- Parsing Parser takes sequence of tokens and builds a tree structure as AST. Also, report syntax errors.
- Static analysis binding or resolution for each identifier, find scope to check the declaration, then figure out the type (or type error). However, Lox is dynamically typed, so type checking would be in runtime.
- Optimization We already understand semantics in the third part, so we can switch program more efficiently to the one which has the same semantics.
- Code generation machine code or bytecode. Machine code can run on CPU directly. Bytecode needs to be translated to machine code, e.g. Java bytecode is translated by JVM.
- Virtual machine we can also write a program which emulates a chip supporting virtual architecture and run the bytecode.
Virtual machine can be separted to a lot of types. The one we are familiar with is system VM which even simulate the hardware environment. The one mentioned here is language VM or process VM.
Some other stories
There are also stories of single-pass compilers, tree-walk interpreters, transpilers (source-to-source compiler), and just-in-time compilation.
Compiler v.s. Interpreter
I love the way by which the book describe the difference. It is just like the relationship between the term fruit and vegetable. There are some overlapped between two domains, but also something exclusive.
Ignition is the interpreter, and Turbofan is the compiler. They would communicate with each other to generate optimized code. This is not the focus of this series, we only need to know many language implementations take both interpreter and compiler.
Lox is the language we want to implement in this series.
The author also made some terminology clear in this part. An argument is an actual value to be passed to a function, while a parameter is a variable which would hold the value of the argument.
Finally, we have figured out the stuff and its features we want to implement. On next day, I would start implementing scanner with java.