美文网首页
llvm cookbook 2.3 定义AST

llvm cookbook 2.3 定义AST

作者: peteyuan | 来源:发表于2018-11-16 10:41 被阅读13次

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

    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<std::string> args_;
     public:
      FunctionDeclAST(const std::string &name, const std::vector<std::string> &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<BaseAST*> args_;
     public:
      FunctionCallAST(const std::string &callee, const std::vector<BaseAST*> args) :
          callee_(callee), args_(args) {}
    };
    

    相关文章

      网友评论

          本文标题:llvm cookbook 2.3 定义AST

          本文链接:https://www.haomeiwen.com/subject/uncyfqtx.html