#ifndef __AST_HPP__ #define __AST_HPP__ 1 #include #include using namespace std; #include "llvm/IR/Module.h" #include "llvm/IR/Constants.h" #include "llvm/IR/IRBuilder.h" #include "llvm/IR/Verifier.h" #include "llvm/IR/LegacyPassManager.h" #include "llvm/Transforms/Scalar.h" #include "llvm/Support/TargetRegistry.h" #include "llvm/Target/TargetOptions.h" #include "llvm/Target/TargetMachine.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/TargetSelect.h" using namespace llvm; using namespace llvm::legacy; class ExprAST { public: virtual ~ExprAST(); virtual Value* codegen() const = 0; }; class NumberExprAST : public ExprAST { public: NumberExprAST(double d) :_d(d) {} Value* codegen() const; private: double _d; }; class VariableExprAST : public ExprAST { public: VariableExprAST(string v) :_v(v) {} Value* codegen() const; private: string _v; }; class InnerExprAST : public ExprAST { public: InnerExprAST(ExprAST* e1); InnerExprAST(ExprAST* e1, ExprAST* e2); InnerExprAST(ExprAST* e1, ExprAST* e2, ExprAST* e3); InnerExprAST(ExprAST* e1, ExprAST* e2, ExprAST* e3, ExprAST* e4); InnerExprAST(vector v) :_v(v) {} ~InnerExprAST(); protected: vector _v; private: InnerExprAST(const InnerExprAST&); InnerExprAST& operator=(const InnerExprAST&); }; class AddExprAST : public InnerExprAST { public: AddExprAST(ExprAST* e1, ExprAST* e2) :InnerExprAST(e1, e2) {} Value* codegen() const; }; class SubExprAST : public InnerExprAST { public: SubExprAST(ExprAST* e1, ExprAST* e2) :InnerExprAST(e1, e2) {} Value* codegen() const; }; class MulExprAST : public InnerExprAST { public: MulExprAST(ExprAST* e1, ExprAST* e2) :InnerExprAST(e1, e2) {} Value* codegen() const; }; class DivExprAST : public InnerExprAST { public: DivExprAST(ExprAST* e1, ExprAST* e2) :InnerExprAST(e1, e2) {} Value* codegen() const; }; class LessThenExprAST : public InnerExprAST { public: LessThenExprAST(ExprAST* e1, ExprAST* e2) :InnerExprAST(e1, e2) {} Value* codegen() const; }; class GreaterThenExprAST : public InnerExprAST { public: GreaterThenExprAST(ExprAST* e1, ExprAST* e2) :InnerExprAST(e1, e2) {} Value* codegen() const; }; class SeqExprAST : public InnerExprAST { public: SeqExprAST(ExprAST* e1, ExprAST* e2) :InnerExprAST(e1, e2) {} Value* codegen() const; }; class CallExprAST : public InnerExprAST { public: CallExprAST(string f, vector v) : InnerExprAST(v), _f(f) {} Value* codegen() const; private: string _f; }; class IfThenElse : public InnerExprAST { public: IfThenElse(ExprAST* e1, ExprAST* e2, ExprAST* e3) :InnerExprAST(e1, e2, e3) {} Value* codegen() const; }; class ForIn : public InnerExprAST { public: ForIn(string s, ExprAST* e1, ExprAST* e2, ExprAST* e3, ExprAST* e4) :InnerExprAST(e1, e2, e3, e4), _s(s) {} Value* codegen() const; private: string _s; }; class AssignExprAST : public InnerExprAST { public: AssignExprAST(string s, ExprAST* e) :InnerExprAST(e), _s(s) {} Value* codegen() const; private: string _s; }; class VarExprAST : public InnerExprAST { public: VarExprAST(vector< pair > v, ExprAST *e) :InnerExprAST(e), _vars(v) {} Value* codegen() const; ~VarExprAST(); private: VarExprAST(const VarExprAST&); VarExprAST& operator=(const VarExprAST&); vector< pair > _vars; }; class PrototypeAST { public: PrototypeAST(string f, vector args) :_f(f), _args(args) {} Function* codegen() const; string getName() const { return _f; } private: string _f; vector _args; }; class FunctionAST { public: FunctionAST(const PrototypeAST &p, ExprAST *e) :_proto(p), _e(e) {} ~FunctionAST(); Function* codegen() const; private: FunctionAST(const FunctionAST&); FunctionAST& operator=(const FunctionAST&); PrototypeAST _proto; ExprAST *_e; }; void InitializeModuleAndPassManager(); AllocaInst *CreateEntryBlockAlloca(Function *TheFunction, const string &VarName); #endif