//===- AST.cpp - Helper for printing out the Toy AST ----------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This file implements the AST dump for the Toy language. // //===----------------------------------------------------------------------===// #include "toy/AST.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/Twine.h" #include "llvm/ADT/TypeSwitch.h" #include "llvm/Support/Casting.h" #include "llvm/Support/raw_ostream.h" #include <string> usingnamespacetoy; namespace { // RAII helper to manage increasing/decreasing the indentation as we traverse // the AST struct Indent { … }; /// Helper class that implement the AST tree traversal and print the nodes along /// the way. The only data member is the current indentation level. class ASTDumper { … }; } // namespace /// Return a formatted string for the location of any node template <typename T> static std::string loc(T *node) { … } // Helper Macro to bump the indentation level and print the leading spaces for // the current indentations #define INDENT() … /// Dispatch to a generic expressions to the appropriate subclass using RTTI void ASTDumper::dump(ExprAST *expr) { … } /// A variable declaration is printing the variable name, the type, and then /// recurse in the initializer value. void ASTDumper::dump(VarDeclExprAST *varDecl) { … } /// A "block", or a list of expression void ASTDumper::dump(ExprASTList *exprList) { … } /// A literal number, just print the value. void ASTDumper::dump(NumberExprAST *num) { … } /// Helper to print recursively a literal. This handles nested array like: /// [ [ 1, 2 ], [ 3, 4 ] ] /// We print out such array with the dimensions spelled out at every level: /// <2,2>[<2>[ 1, 2 ], <2>[ 3, 4 ] ] void printLitHelper(ExprAST *litOrNum) { … } /// Print a literal, see the recursive helper above for the implementation. void ASTDumper::dump(LiteralExprAST *node) { … } /// Print a variable reference (just a name). void ASTDumper::dump(VariableExprAST *node) { … } /// Return statement print the return and its (optional) argument. void ASTDumper::dump(ReturnExprAST *node) { … } /// Print a binary operation, first the operator, then recurse into LHS and RHS. void ASTDumper::dump(BinaryExprAST *node) { … } /// Print a call expression, first the callee name and the list of args by /// recursing into each individual argument. void ASTDumper::dump(CallExprAST *node) { … } /// Print a builtin print call, first the builtin name and then the argument. void ASTDumper::dump(PrintExprAST *node) { … } /// Print type: only the shape is printed in between '<' and '>' void ASTDumper::dump(const VarType &type) { … } /// Print a function prototype, first the function name, and then the list of /// parameters names. void ASTDumper::dump(PrototypeAST *node) { … } /// Print a function, first the prototype and then the body. void ASTDumper::dump(FunctionAST *node) { … } /// Print a module, actually loop over the functions and print them in sequence. void ASTDumper::dump(ModuleAST *node) { … } namespace toy { // Public API void dump(ModuleAST &module) { … } } // namespace toy