Ast.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #ifndef _AST_H
  2. #define _AST_H
  3. // LLVM includes
  4. #include "llvm/ADT/STLExtras.h"
  5. #include "llvm/IR/IRBuilder.h"
  6. #include "llvm/IR/LLVMContext.h"
  7. #include "llvm/IR/Module.h"
  8. #include "llvm/IR/DIBuilder.h"
  9. // Local includes
  10. #include "Lexer.h"
  11. using namespace llvm;
  12. //===----------------------------------------------------------------------===//
  13. // Abstract Syntax Tree (aka Parse Tree)
  14. //===----------------------------------------------------------------------===//
  15. namespace ast {
  16. // struct instantiated by the 'main' to access data structures if llvm IR.
  17. struct AstObjects {
  18. public:
  19. static std::unique_ptr<Module> TheModule;
  20. static IRBuilder<> Builder;
  21. static std::map<std::string, AllocaInst*> NamedValues;
  22. };
  23. raw_ostream &indent(raw_ostream &O, int size) {
  24. return O << std::string(size, ' ');
  25. }
  26. /// ExprAST - Base class for all expression nodes.
  27. class ExprAST {
  28. lexer::SourceLocation Loc;
  29. public:
  30. ExprAST(lexer::SourceLocation Loc = lexer::CurLoc) : Loc(Loc) {}
  31. virtual ~ExprAST() {}
  32. virtual Value *codegen() = 0;
  33. int getLine() const { return Loc.Line; }
  34. int getCol() const { return Loc.Col; }
  35. virtual raw_ostream &dump(raw_ostream &out, int ind) {
  36. return out << ':' << getLine() << ':' << getCol() << '\n';
  37. }
  38. };
  39. /// NumberExprAST - Expression class for numeric literals like "1.0".
  40. class NumberExprAST : public ExprAST {
  41. double Val;
  42. public:
  43. NumberExprAST(double Val) : Val(Val) {}
  44. raw_ostream &dump(raw_ostream &out, int ind) override {
  45. return ExprAST::dump(out << Val, ind);
  46. }
  47. Value *codegen() override;
  48. };
  49. /// VariableExprAST - Expression class for referencing a variable, like "a".
  50. class VariableExprAST : public ExprAST {
  51. std::string Name;
  52. public:
  53. VariableExprAST(lexer::SourceLocation Loc, const std::string &Name)
  54. : ExprAST(Loc), Name(Name) {}
  55. const std::string &getName() const { return Name; }
  56. Value *codegen() override;
  57. raw_ostream &dump(raw_ostream &out, int ind) override {
  58. return ExprAST::dump(out << Name, ind);
  59. }
  60. };
  61. /// UnaryExprAST - Expression class for a unary operator.
  62. class UnaryExprAST : public ExprAST {
  63. char Opcode;
  64. std::unique_ptr<ExprAST> Operand;
  65. public:
  66. UnaryExprAST(char Opcode, std::unique_ptr<ExprAST> Operand)
  67. : Opcode(Opcode), Operand(std::move(Operand)) {}
  68. Value *codegen() override;
  69. raw_ostream &dump(raw_ostream &out, int ind) override {
  70. ExprAST::dump(out << "unary" << Opcode, ind);
  71. Operand->dump(out, ind + 1);
  72. return out;
  73. }
  74. };
  75. /// BinaryExprAST - Expression class for a binary operator.
  76. class BinaryExprAST : public ExprAST {
  77. char Op;
  78. std::unique_ptr<ExprAST> LHS, RHS;
  79. public:
  80. BinaryExprAST(lexer::SourceLocation Loc, char Op, std::unique_ptr<ExprAST> LHS,
  81. std::unique_ptr<ExprAST> RHS)
  82. : ExprAST(Loc), Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {}
  83. Value *codegen() override;
  84. raw_ostream &dump(raw_ostream &out, int ind) override {
  85. ExprAST::dump(out << "binary" << Op, ind);
  86. LHS->dump(indent(out, ind) << "LHS:", ind + 1);
  87. RHS->dump(indent(out, ind) << "RHS:", ind + 1);
  88. return out;
  89. }
  90. };
  91. /// CallExprAST - Expression class for function calls.
  92. class CallExprAST : public ExprAST {
  93. std::string Callee;
  94. std::vector<std::unique_ptr<ExprAST>> Args;
  95. public:
  96. CallExprAST(lexer::SourceLocation Loc, const std::string &Callee,
  97. std::vector<std::unique_ptr<ExprAST>> Args)
  98. : ExprAST(Loc), Callee(Callee), Args(std::move(Args)) {}
  99. Value *codegen() override;
  100. raw_ostream &dump(raw_ostream &out, int ind) override {
  101. ExprAST::dump(out << "call " << Callee, ind);
  102. for (const auto &Arg : Args)
  103. Arg->dump(indent(out, ind + 1), ind + 1);
  104. return out;
  105. }
  106. };
  107. /// IfExprAST - Expression class for if/then/else.
  108. class IfExprAST : public ExprAST {
  109. std::unique_ptr<ExprAST> Cond, Then, Else;
  110. public:
  111. IfExprAST(lexer::SourceLocation Loc, std::unique_ptr<ExprAST> Cond,
  112. std::unique_ptr<ExprAST> Then, std::unique_ptr<ExprAST> Else)
  113. : ExprAST(Loc), Cond(std::move(Cond)), Then(std::move(Then)),
  114. Else(std::move(Else)) {}
  115. Value *codegen() override;
  116. raw_ostream &dump(raw_ostream &out, int ind) override {
  117. ExprAST::dump(out << "if", ind);
  118. Cond->dump(indent(out, ind) << "Cond:", ind + 1);
  119. Then->dump(indent(out, ind) << "Then:", ind + 1);
  120. Else->dump(indent(out, ind) << "Else:", ind + 1);
  121. return out;
  122. }
  123. };
  124. /// ForExprAST - Expression class for for/in.
  125. class ForExprAST : public ExprAST {
  126. std::string VarName;
  127. std::unique_ptr<ExprAST> Start, End, Step, Body;
  128. public:
  129. ForExprAST(const std::string &VarName, std::unique_ptr<ExprAST> Start,
  130. std::unique_ptr<ExprAST> End, std::unique_ptr<ExprAST> Step,
  131. std::unique_ptr<ExprAST> Body)
  132. : VarName(VarName), Start(std::move(Start)), End(std::move(End)),
  133. Step(std::move(Step)), Body(std::move(Body)) {}
  134. Value *codegen() override;
  135. raw_ostream &dump(raw_ostream &out, int ind) override {
  136. ExprAST::dump(out << "for", ind);
  137. Start->dump(indent(out, ind) << "Cond:", ind + 1);
  138. End->dump(indent(out, ind) << "End:", ind + 1);
  139. Step->dump(indent(out, ind) << "Step:", ind + 1);
  140. Body->dump(indent(out, ind) << "Body:", ind + 1);
  141. return out;
  142. }
  143. };
  144. /// VarExprAST - Expression class for var/in
  145. class VarExprAST : public ExprAST {
  146. std::vector<std::pair<std::string, std::unique_ptr<ExprAST>>> VarNames;
  147. std::unique_ptr<ExprAST> Body;
  148. public:
  149. VarExprAST(
  150. std::vector<std::pair<std::string, std::unique_ptr<ExprAST>>> VarNames,
  151. std::unique_ptr<ExprAST> Body)
  152. : VarNames(std::move(VarNames)), Body(std::move(Body)) {}
  153. Value *codegen() override;
  154. raw_ostream &dump(raw_ostream &out, int ind) override {
  155. ExprAST::dump(out << "var", ind);
  156. for (const auto &NamedVar : VarNames)
  157. NamedVar.second->dump(indent(out, ind) << NamedVar.first << ':', ind + 1);
  158. Body->dump(indent(out, ind) << "Body:", ind + 1);
  159. return out;
  160. }
  161. };
  162. /// PrototypeAST - This class represents the "prototype" for a function,
  163. /// which captures its name, and its argument names (thus implicitly the number
  164. /// of arguments the function takes), as well as if it is an operator.
  165. class PrototypeAST {
  166. std::string Name;
  167. std::vector<std::string> Args;
  168. bool IsOperator;
  169. unsigned Precedence; // Precedence if a binary op.
  170. int Line;
  171. public:
  172. PrototypeAST(lexer::SourceLocation Loc, const std::string &Name,
  173. std::vector<std::string> Args, bool IsOperator = false,
  174. unsigned Prec = 0)
  175. : Name(Name), Args(std::move(Args)), IsOperator(IsOperator),
  176. Precedence(Prec), Line(Loc.Line) {}
  177. Function *codegen();
  178. const std::string &getName() const { return Name; }
  179. bool isUnaryOp() const { return IsOperator && Args.size() == 1; }
  180. bool isBinaryOp() const { return IsOperator && Args.size() == 2; }
  181. char getOperatorName() const {
  182. assert(isUnaryOp() || isBinaryOp());
  183. return Name[Name.size() - 1];
  184. }
  185. unsigned getBinaryPrecedence() const { return Precedence; }
  186. int getLine() const { return Line; }
  187. };
  188. /// FunctionAST - This class represents a function definition itself.
  189. class FunctionAST {
  190. std::unique_ptr<PrototypeAST> Proto;
  191. std::unique_ptr<ExprAST> Body;
  192. public:
  193. FunctionAST(std::unique_ptr<PrototypeAST> Proto,
  194. std::unique_ptr<ExprAST> Body)
  195. : Proto(std::move(Proto)), Body(std::move(Body)) {}
  196. Function *codegen();
  197. raw_ostream &dump(raw_ostream &out, int ind) {
  198. indent(out, ind) << "FunctionAST\n";
  199. ++ind;
  200. indent(out, ind) << "Body:";
  201. return Body ? Body->dump(out, ind) : out << "null\n";
  202. }
  203. };
  204. #endif