+static std::unique_ptr<ExprAST> ParseIfExpr() {
+ getNextToken(); // eat the if
+
+ auto condExpr = ParseExpression();
+ if (!condExpr) return nullptr;
+
+ if (CurTok != tok_then)
+ return LogError("Expected then");
+ getNextToken(); // eat the then
+
+ auto thenExpr = ParseExpression();
+ if (!thenExpr) return nullptr;
+
+ if (CurTok != tok_else)
+ return LogError("Expected else");
+ getNextToken(); // eat the else
+
+ auto elseExpr = ParseExpression();
+ if (!elseExpr) return nullptr;
+
+ return llvm::make_unique<IfExprAST>(std::move(condExpr),
+ std::move(thenExpr),
+ std::move(elseExpr));
+}
+
+static std::unique_ptr<ExprAST> ParseForExpr() {
+ getNextToken(); // eat the for
+
+ if (CurTok != tok_identifier)
+ return LogError("Expected identifier after for");
+
+ std::string id = IdentifierStr;
+ getNextToken(); // eat the identifier
+
+ if (CurTok != '=')
+ return LogError("Expected = after for");
+ getNextToken(); // eat the =
+
+ auto start = ParseExpression();
+ if (!start) return nullptr;
+
+ if (CurTok != ',')
+ return LogError("Expected , after for start value");
+ getNextToken(); // eat the ,
+
+ auto end = ParseExpression();
+ if (!end)
+ return nullptr;
+
+ // optional step value
+ std::unique_ptr<ExprAST> step;
+ if (CurTok == ',') {
+ getNextToken(); // eat the ,
+ step = ParseExpression();
+ if (!step) return nullptr;
+ }
+
+ if (CurTok != tok_in)
+ return LogError("Expected in after for");
+ getNextToken(); // eat the in
+
+ auto body = ParseExpression();
+ if (!body) return nullptr;
+
+ return llvm::make_unique<ForExprAST>(id,
+ std::move(start),
+ std::move(end),
+ std::move(step),
+ std::move(body));
+}
+