Lox to C compiler, with corresponding runtime implementation in C.
Lox, introduced in the book Crafting Interpreters, is a language that a lot of PL enthusiasts implement compilers and interpreters for. However:
- My (now deleted) implementation is not as fast as clox
- An AOT language (with or without additional runtime), instead of JIT/Interpreted language, is what I've been seeking for.
Thus, the LoxCC project is spawned. Similar to older version of Go, there will be a C implementation of Lox C Runtime, which is compiled together with Lox programs.
Lox is a dynamically typed, GC managed language. However, making it AOT-compatible is not so easy than running it in VM. Currently, we support these features:
- Expressions
- Value types (numbers, booleans, nil)
- Strings
- String interning
- Global variables
- Local variables
- Control flow
- Functions and invocations
-
Closure(Not supported) - Garbage Collection
- Classes and instances
- Methods and initializers
- Superclasses
All of which are the corresponding concepts from the book.
NOTE: In this version, closures will NOT be supported because of implementation difficulties. It may be added in future major or minor versions.
For now, the LoxCC is directly targeting C. The compiler direcly generates corresponding C code, copies the LOXCRT to the output directory, and (optionally) calls the system C compiler.
For more compiler behavior, see build configs.
graph LR
L[Lox Program]
A[AST]
O[Generated C code]
R["Lox C Runtime (LOXCRT)"]
C[System C Compiler]
E[Executable Binaries]
L --analyzer--> A
A --codegen--> O
O --> C
R --> C
C --> E
As the graph describes, the pipeline of compiling a Lox program using LoxCC is as below:
- Parse the source of Lox Program, and creates corresponding AST (Abstract Syntax Tree).
- Generate the AST into C codes.
- Copy the handwritten LOXCRT to the output directory.
- (optional) Call the system C compiler.
The build configs are written in YAML, a very expressive serialization language, and placed at the root of LoxCC.
For detailed behavior, see build-config.yaml