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/Chapter4/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/Chapter4/toy.cpp')
-rw-r--r-- | llvm/examples/Kaleidoscope/Chapter4/toy.cpp | 70 |
1 files changed, 41 insertions, 29 deletions
diff --git a/llvm/examples/Kaleidoscope/Chapter4/toy.cpp b/llvm/examples/Kaleidoscope/Chapter4/toy.cpp index f0665f29831..eba0ab1f27d 100644 --- a/llvm/examples/Kaleidoscope/Chapter4/toy.cpp +++ b/llvm/examples/Kaleidoscope/Chapter4/toy.cpp @@ -106,6 +106,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; @@ -114,6 +115,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; @@ -123,6 +125,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) @@ -134,6 +137,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) @@ -147,9 +151,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(); }; @@ -157,10 +162,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 @@ -207,6 +213,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* ')' @@ -243,26 +269,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; -} - /// primary /// ::= identifierexpr /// ::= numberexpr @@ -312,8 +318,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)); } } @@ -368,8 +374,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; @@ -806,9 +812,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; @@ -832,6 +838,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. //===----------------------------------------------------------------------===// |