Implementing the compiler in D
How would we implement the compiler in D?
- Use Bison as the compiler generator. Recent Bison supports D.
- This requires me to write AST creation stuff manually, which kind of sucks.
- But I don’t have to write an AST-to-semantic copier, which is nice.
- Hand-written lexer?
- Can autogenerate the detect-literal portion of it using a prefix tree.
- Use LLVM-C API instead of writing to a file.
- This lets me keep a local variable basic block open, which means fewer separate visitors.
- Use RTTI-switch visitor instead of indirection visitor.
- Use reflection to implement
visitChildren
.
- Use reflection to implement rewrite visitor.
- Use
make
instead of dub
to build, because we really don’t need dub.
String interpolation
The hand-written lexer makes string interpolation easier:
"Hello $name! You are ${person:years} old!"
becomes:
INTERP_START
STRING("Hello ")
IDENT("name")
STRING("! You are ")
IDENT("person")
CHILD
IDENT("years")
STRING(" old!")
INTERP_END
Because the hand-written lexer can do a lot more. Then I have productions like:
string: STRING | INTERP_START exp* INTERP_END;