Part 0 and code reference Part 1: Build a parser

Code generation is actually quite easy (in basic level) because LLVM handles optimization and ASM generation for us.

In part 1, we had an AST. Therefore, we just need walk down the tree and print the code accordingly. If you know how to evaluate an expression tree, this is basically the same but ANTLR generated code already contained some boilerplate. The code is super straight-forward. You can refer to the code and read about high level constructs in LLVM IR. The basic idea is as follow:

  • We have different types of expressions
    • If it is a number, we should print a constant(of type f32, u32…) to LLVM IR
    • etc… read the above link to see how each type of expression should look like
  • Variables have scope (a map in a stack for symbol look-up)
  • antlrcpp::Any can be tricky, need static casting when returning
  • Add new stuff is easy (visitor pattern)