diff options
author | Lang Hames <lhames@gmail.com> | 2015-08-19 18:15:58 +0000 |
---|---|---|
committer | Lang Hames <lhames@gmail.com> | 2015-08-19 18:15:58 +0000 |
commit | 59b0da886efd85ed8b93c74d3e32da7e7252e4a0 (patch) | |
tree | 0f55d1804f4c34aa4bdfa43ce9a8fb80fd2b4ab9 /llvm/examples/Kaleidoscope/Chapter5/toy.cpp | |
parent | be6e0539381fa5aab0de22bd598688973b72ab85 (diff) | |
download | bcm5719-llvm-59b0da886efd85ed8b93c74d3e32da7e7252e4a0.tar.gz bcm5719-llvm-59b0da886efd85ed8b93c74d3e32da7e7252e4a0.zip |
[Kaleidoscope] Clang-format the Kaleidoscope tutorials.
Also reduces changes between tutorial chapters.
llvm-svn: 245472
Diffstat (limited to 'llvm/examples/Kaleidoscope/Chapter5/toy.cpp')
-rw-r--r-- | llvm/examples/Kaleidoscope/Chapter5/toy.cpp | 107 |
1 files changed, 60 insertions, 47 deletions
diff --git a/llvm/examples/Kaleidoscope/Chapter5/toy.cpp b/llvm/examples/Kaleidoscope/Chapter5/toy.cpp index 042d974626d..c191d61d562 100644 --- a/llvm/examples/Kaleidoscope/Chapter5/toy.cpp +++ b/llvm/examples/Kaleidoscope/Chapter5/toy.cpp @@ -123,6 +123,7 @@ public: /// NumberExprAST - Expression class for numeric literals like "1.0". class NumberExprAST : public ExprAST { double Val; + public: NumberExprAST(double Val) : Val(Val) {} Value *Codegen() override; @@ -131,6 +132,7 @@ public: /// VariableExprAST - Expression class for referencing a variable, like "a". class VariableExprAST : public ExprAST { std::string Name; + public: VariableExprAST(const std::string &Name) : Name(Name) {} Value *Codegen() override; @@ -140,6 +142,7 @@ public: class BinaryExprAST : public ExprAST { char Op; std::unique_ptr<ExprAST> LHS, RHS; + public: BinaryExprAST(char Op, std::unique_ptr<ExprAST> LHS, std::unique_ptr<ExprAST> RHS) @@ -151,6 +154,7 @@ public: class CallExprAST : public ExprAST { std::string Callee; std::vector<std::unique_ptr<ExprAST>> Args; + public: CallExprAST(const std::string &Callee, std::vector<std::unique_ptr<ExprAST>> Args) @@ -161,6 +165,7 @@ public: /// IfExprAST - Expression class for if/then/else. class IfExprAST : public ExprAST { std::unique_ptr<ExprAST> Cond, Then, Else; + public: IfExprAST(std::unique_ptr<ExprAST> Cond, std::unique_ptr<ExprAST> Then, std::unique_ptr<ExprAST> Else) @@ -172,6 +177,7 @@ public: class ForExprAST : public ExprAST { std::string VarName; std::unique_ptr<ExprAST> Start, End, Step, Body; + public: ForExprAST(const std::string &VarName, std::unique_ptr<ExprAST> Start, std::unique_ptr<ExprAST> End, std::unique_ptr<ExprAST> Step, @@ -187,9 +193,10 @@ public: class PrototypeAST { std::string Name; std::vector<std::string> Args; + public: - PrototypeAST(const std::string &name, std::vector<std::string> Args) - : Name(name), Args(std::move(Args)) {} + PrototypeAST(const std::string &Name, std::vector<std::string> Args) + : Name(Name), Args(std::move(Args)) {} Function *Codegen(); }; @@ -197,10 +204,11 @@ public: class FunctionAST { std::unique_ptr<PrototypeAST> Proto; std::unique_ptr<ExprAST> Body; + public: FunctionAST(std::unique_ptr<PrototypeAST> Proto, std::unique_ptr<ExprAST> Body) - : Proto(std::move(Proto)), Body(std::move(Body)) {} + : Proto(std::move(Proto)), Body(std::move(Body)) {} Function *Codegen(); }; } // end anonymous namespace @@ -247,6 +255,26 @@ std::unique_ptr<FunctionAST> ErrorF(const char *Str) { static std::unique_ptr<ExprAST> ParseExpression(); +/// numberexpr ::= number +static std::unique_ptr<ExprAST> ParseNumberExpr() { + auto Result = llvm::make_unique<NumberExprAST>(NumVal); + getNextToken(); // consume the number + return std::move(Result); +} + +/// parenexpr ::= '(' expression ')' +static std::unique_ptr<ExprAST> ParseParenExpr() { + getNextToken(); // eat (. + auto V = ParseExpression(); + if (!V) + return nullptr; + + if (CurTok != ')') + return Error("expected ')'"); + getNextToken(); // eat ). + return V; +} + /// identifierexpr /// ::= identifier /// ::= identifier '(' expression* ')' @@ -283,26 +311,6 @@ static std::unique_ptr<ExprAST> ParseIdentifierExpr() { return llvm::make_unique<CallExprAST>(IdName, std::move(Args)); } -/// numberexpr ::= number -static std::unique_ptr<ExprAST> ParseNumberExpr() { - auto Result = llvm::make_unique<NumberExprAST>(NumVal); - getNextToken(); // consume the number - return std::move(Result); -} - -/// parenexpr ::= '(' expression ')' -static std::unique_ptr<ExprAST> ParseParenExpr() { - getNextToken(); // eat (. - auto V = ParseExpression(); - if (!V) - return nullptr; - - if (CurTok != ')') - return Error("expected ')'"); - getNextToken(); // eat ). - return V; -} - /// ifexpr ::= 'if' expression 'then' expression 'else' expression static std::unique_ptr<ExprAST> ParseIfExpr() { getNextToken(); // eat the if. @@ -434,8 +442,8 @@ static std::unique_ptr<ExprAST> ParseBinOpRHS(int ExprPrec, } // Merge LHS/RHS. - LHS = llvm::make_unique<BinaryExprAST>(BinOp, std::move(LHS), - std::move(RHS)); + LHS = + llvm::make_unique<BinaryExprAST>(BinOp, std::move(LHS), std::move(RHS)); } } @@ -490,8 +498,8 @@ static std::unique_ptr<FunctionAST> ParseDefinition() { static std::unique_ptr<FunctionAST> ParseTopLevelExpr() { if (auto E = ParseExpression()) { // Make an anonymous proto. - auto Proto = llvm::make_unique<PrototypeAST>("", - std::vector<std::string>()); + auto Proto = + llvm::make_unique<PrototypeAST>("", std::vector<std::string>()); return llvm::make_unique<FunctionAST>(std::move(Proto), std::move(E)); } return nullptr; @@ -624,23 +632,22 @@ Value *IfExprAST::Codegen() { return PN; } +// Output for-loop as: +// ... +// start = startexpr +// goto loop +// loop: +// variable = phi [start, loopheader], [nextvariable, loopend] +// ... +// bodyexpr +// ... +// loopend: +// step = stepexpr +// nextvariable = variable + step +// endcond = endexpr +// br endcond, loop, endloop +// outloop: Value *ForExprAST::Codegen() { - // Output this as: - // ... - // start = startexpr - // goto loop - // loop: - // variable = phi [start, loopheader], [nextvariable, loopend] - // ... - // bodyexpr - // ... - // loopend: - // step = stepexpr - // nextvariable = variable + step - // endcond = endexpr - // br endcond, loop, endloop - // outloop: - // Emit the start code first, without 'variable' in scope. Value *StartVal = Start->Codegen(); if (!StartVal) @@ -676,7 +683,7 @@ Value *ForExprAST::Codegen() { return nullptr; // Emit the step value. - Value *StepVal; + Value *StepVal = nullptr; if (Step) { StepVal = Step->Codegen(); if (!StepVal) @@ -691,7 +698,7 @@ Value *ForExprAST::Codegen() { // Compute the end condition. Value *EndCond = End->Codegen(); if (!EndCond) - return EndCond; + return nullptr; // Convert condition to a bool by comparing equal to 0.0. EndCond = Builder.CreateFCmpONE( @@ -849,9 +856,9 @@ static void MainLoop() { switch (CurTok) { case tok_eof: return; - case ';': + case ';': // ignore top-level semicolons. getNextToken(); - break; // ignore top-level semicolons. + break; case tok_def: HandleDefinition(); break; @@ -875,6 +882,12 @@ extern "C" double putchard(double X) { return 0; } +/// printd - printf that takes a double prints it as "%f\n", returning 0. +extern "C" double printd(double X) { + printf("%f\n", X); + return 0; +} + //===----------------------------------------------------------------------===// // Main driver code. //===----------------------------------------------------------------------===// |