After all we've done (building a lexer, parser, code-generator, optimiser, and the JIT), we give Kaleidoscope decision-making abilities.
What I built: Commit 5ba5803
What I understood:
If/Then/Else:
1) Lexer:
We add three new tokens, namely tok_if , tok_then , and tok_else , and their corresponding checks in gettok() (through if (IdentifierStr == ...) )
2) AST:
The IfExprAST holds three child expressions — Cond , Then , and Else .
It's worth noting that in Kaleidoscope, everything is an expression. There are no statements. This means that if/then/else doesn't result in an action, and instead returns a value.
This is to keep the language consistent, as the codegen never has to resolve whether something is an expression or a statement, as only the former is allowed.
3) Parser:
The ParseIfExpr() consumes if , parses the condition, expects then , parses the then-expression, expects else , parses the else-expression, and returns an IfExprAST .…