llvm cookbook 2.3 定义AST

定义抽象语法树,也就是各种表达式的数据结构。

class BaseAST {
 public:
  virtual ~BaseAST();
};

class VariableAST : public BaseAST {
  std::string var_name_;
 public:
  VariableAST(std::string &name) : var_name_(name) {}
};

class NumericAST : public BaseAST {
  int numeric_val_;
 public:
  NumericAST(int val) : numeric_val_(val) {}
};

class BinaryAST : public BaseAST {
  std::string operator_;
  BaseAST *lhs_, *rhs_;
 public:
  BinaryAST(std::string op, BaseAST* lhs, BaseAST* rhs) :
      operator_(op), lhs_(lhs), rhs_(rhs) {}
};

class FunctionDeclAST {
  std::string func_name_;
  std::vector args_;
 public:
  FunctionDeclAST(const std::string &name, const std::vector &args) :
      func_name_(name), args_(args) {}
};

class FunctionDefnAST {
  FunctionDeclAST *func_decl_;
  BaseAST* body_;
 public:
  FunctionDefnAST(FunctionDeclAST *proto, BaseAST *body) :
      func_decl_(proto), body_(body) {}
};

class FunctionCallAST : public BaseAST {
  std::string callee_;
  std::vector args_;
 public:
  FunctionCallAST(const std::string &callee, const std::vector args) :
      callee_(callee), args_(args) {}
};

你可能感兴趣的:(llvm cookbook 2.3 定义AST)