From 59b0da886efd85ed8b93c74d3e32da7e7252e4a0 Mon Sep 17 00:00:00 2001 From: Lang Hames Date: Wed, 19 Aug 2015 18:15:58 +0000 Subject: [Kaleidoscope] Clang-format the Kaleidoscope tutorials. Also reduces changes between tutorial chapters. llvm-svn: 245472 --- llvm/examples/Kaleidoscope/Chapter4/toy.cpp | 70 +++++++++++++++++------------ 1 file changed, 41 insertions(+), 29 deletions(-) (limited to 'llvm/examples/Kaleidoscope/Chapter4/toy.cpp') 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 LHS, RHS; + public: BinaryExprAST(char Op, std::unique_ptr LHS, std::unique_ptr RHS) @@ -134,6 +137,7 @@ public: class CallExprAST : public ExprAST { std::string Callee; std::vector> Args; + public: CallExprAST(const std::string &Callee, std::vector> Args) @@ -147,9 +151,10 @@ public: class PrototypeAST { std::string Name; std::vector Args; + public: - PrototypeAST(const std::string &name, std::vector Args) - : Name(name), Args(std::move(Args)) {} + PrototypeAST(const std::string &Name, std::vector Args) + : Name(Name), Args(std::move(Args)) {} Function *Codegen(); }; @@ -157,10 +162,11 @@ public: class FunctionAST { std::unique_ptr Proto; std::unique_ptr Body; + public: FunctionAST(std::unique_ptr Proto, std::unique_ptr 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 ErrorF(const char *Str) { static std::unique_ptr ParseExpression(); +/// numberexpr ::= number +static std::unique_ptr ParseNumberExpr() { + auto Result = llvm::make_unique(NumVal); + getNextToken(); // consume the number + return std::move(Result); +} + +/// parenexpr ::= '(' expression ')' +static std::unique_ptr 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 ParseIdentifierExpr() { return llvm::make_unique(IdName, std::move(Args)); } -/// numberexpr ::= number -static std::unique_ptr ParseNumberExpr() { - auto Result = llvm::make_unique(NumVal); - getNextToken(); // consume the number - return std::move(Result); -} - -/// parenexpr ::= '(' expression ')' -static std::unique_ptr 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 ParseBinOpRHS(int ExprPrec, } // Merge LHS/RHS. - LHS = llvm::make_unique(BinOp, std::move(LHS), - std::move(RHS)); + LHS = + llvm::make_unique(BinOp, std::move(LHS), std::move(RHS)); } } @@ -368,8 +374,8 @@ static std::unique_ptr ParseDefinition() { static std::unique_ptr ParseTopLevelExpr() { if (auto E = ParseExpression()) { // Make an anonymous proto. - auto Proto = llvm::make_unique("", - std::vector()); + auto Proto = + llvm::make_unique("", std::vector()); return llvm::make_unique(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. //===----------------------------------------------------------------------===// -- cgit v1.2.3