Ast.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. using namespace llvm;
  9. //===----------------------------------------------------------------------===//
  10. // Abstract Syntax Tree (aka Parse Tree)
  11. //===----------------------------------------------------------------------===//
  12. namespace ast{
  13. // struct instantiated by the 'main' to access data structures if llvm IR.
  14. struct AstObjects {
  15. public:
  16. static std::unique_ptr<Module> TheModule;
  17. static IRBuilder<> Builder;
  18. static std::map<std::string, Value *> NamedValues;
  19. };
  20. /// ExprAST - Base class for all expression nodes.
  21. class ExprAST {
  22. public:
  23. virtual ~ExprAST() {}
  24. virtual llvm::Value *codegen() = 0;
  25. };
  26. /// NumberExprAST - Expression class for numeric literals like "1.0".
  27. class NumberExprAST : public ExprAST {
  28. double Val;
  29. public:
  30. NumberExprAST(double Val) : Val(Val) {}
  31. Value *codegen() override;
  32. };
  33. /// VariableExprAST - Expression class for referencing a variable, like "a".
  34. class VariableExprAST : public ExprAST {
  35. std::string Name;
  36. public:
  37. VariableExprAST(const std::string &Name) : Name(Name) {}
  38. Value *codegen() override;
  39. };
  40. /// UnaryExprAST - Expression class for a unary operator.
  41. class UnaryExprAST : public ExprAST {
  42. char Opcode;
  43. std::unique_ptr<ExprAST> Operand;
  44. public:
  45. UnaryExprAST(char Opcode, std::unique_ptr<ExprAST> Operand)
  46. : Opcode(Opcode), Operand(std::move(Operand)) {}
  47. Value *codegen() override;
  48. };
  49. /// BinaryExprAST - Expression class for a binary operator.
  50. class BinaryExprAST : public ExprAST {
  51. char Op;
  52. std::unique_ptr<ExprAST> LHS, RHS;
  53. public:
  54. BinaryExprAST(char Op, std::unique_ptr<ExprAST> LHS,
  55. std::unique_ptr<ExprAST> RHS)
  56. : Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {}
  57. Value *codegen() override;
  58. };
  59. /// ExprAST - Base class for all expression nodes.
  60. /// CallExprAST - Expression class for function calls.
  61. class CallExprAST : public ExprAST {
  62. std::string Callee;
  63. std::vector<std::unique_ptr<ExprAST>> Args;
  64. public:
  65. CallExprAST(const std::string &Callee,
  66. std::vector<std::unique_ptr<ExprAST>> Args) : Callee(Callee), Args(std::move(Args)) {}
  67. Value *codegen() override;
  68. };
  69. /// IfExprAST - Expression class for if/then/else.
  70. class IfExprAST : public ExprAST {
  71. std::unique_ptr<ExprAST> Cond, Then, Else;
  72. public:
  73. IfExprAST(std::unique_ptr<ExprAST> Cond, std::unique_ptr<ExprAST> Then,
  74. std::unique_ptr<ExprAST> Else)
  75. : Cond(std::move(Cond)), Then(std::move(Then)), Else(std::move(Else)) {}
  76. virtual Value *codegen();
  77. };
  78. /// ForExprAST - Expression class for for/in.
  79. class ForExprAST : public ExprAST {
  80. std::string VarName;
  81. std::unique_ptr<ExprAST> Start, End, Step, Body;
  82. public:
  83. ForExprAST(const std::string &VarName, std::unique_ptr<ExprAST> Start,
  84. std::unique_ptr<ExprAST> End, std::unique_ptr<ExprAST> Step,
  85. std::unique_ptr<ExprAST> Body)
  86. : VarName(VarName), Start(std::move(Start)), End(std::move(End)),
  87. Step(std::move(Step)), Body(std::move(Body)) {}
  88. virtual Value *codegen();
  89. };
  90. /// PrototypeAST - This class represents the "prototype" for a function,
  91. /// which captures its name, and its argument names (thus implicitly the number
  92. /// of arguments the function takes), as well as if it is an operator.
  93. class PrototypeAST {
  94. std::string Name;
  95. std::vector<std::string> Args;
  96. bool IsOperator;
  97. unsigned Precedence; // Precedence if a binary op.
  98. public:
  99. PrototypeAST(const std::string &Name, std::vector<std::string> Args,
  100. bool IsOperator = false, unsigned Prec = 0)
  101. : Name(Name), Args(std::move(Args)), IsOperator(IsOperator),
  102. Precedence(Prec) {}
  103. Function *codegen();
  104. const std::string &getName() const { return Name; }
  105. bool isUnaryOp() const { return IsOperator && Args.size() == 1; }
  106. bool isBinaryOp() const { return IsOperator && Args.size() == 2; }
  107. char getOperatorName() const {
  108. assert(isUnaryOp() || isBinaryOp());
  109. return Name[Name.size() - 1];
  110. }
  111. unsigned getBinaryPrecedence() const { return Precedence; }
  112. };
  113. /// FunctionAST - This class represents a function definition itself.
  114. class FunctionAST {
  115. std::unique_ptr<PrototypeAST> Proto;
  116. std::unique_ptr<ExprAST> Body;
  117. public:
  118. FunctionAST(std::unique_ptr<PrototypeAST> Proto,
  119. std::unique_ptr<ExprAST> Body)
  120. : Proto(std::move(Proto)), Body(std::move(Body)) {}
  121. Function *codegen();
  122. };
  123. llvm::Value *ErrorV(const char *Str);
  124. }
  125. #endif