ast.hh vector string iostream cstdlib /usr/include/gecode/flatzinc/conexpr.hh /usr/include/gecode/flatzinc/parser.hh /usr/include/gecode/flatzinc/varspec.hh /usr/include/gecode/flatzinc.hh Gecode::FlatZinc::AST::TypeError Gecode::FlatZinc::AST::Node Gecode::FlatZinc::AST::BoolLit Gecode::FlatZinc::AST::IntLit Gecode::FlatZinc::AST::FloatLit Gecode::FlatZinc::AST::SetLit Gecode::FlatZinc::AST::Var Gecode::FlatZinc::AST::BoolVar Gecode::FlatZinc::AST::IntVar Gecode::FlatZinc::AST::FloatVar Gecode::FlatZinc::AST::SetVar Gecode::FlatZinc::AST::Array Gecode::FlatZinc::AST::Call Gecode::FlatZinc::AST::ArrayAccess Gecode::FlatZinc::AST::Atom Gecode::FlatZinc::AST::String Gecode::FlatZinc::AST Gecode Gecode::FlatZinc /*-*-mode:C++;c-basic-offset:2;indent-tabs-mode:nil-*-*/ /* *Mainauthors: *GuidoTack<tack@gecode.org> * *Copyright: *GuidoTack,2007 * *Lastmodified: *$Date:2013-05-0901:32:37+0200(Thu,09May2013)$by$Author:tack$ *$Revision:13625$ * *ThisfileispartofGecode,thegenericconstraint *developmentenvironment: *http://www.gecode.org * *Permissionisherebygranted,freeofcharge,toanypersonobtaining *acopyofthissoftwareandassociateddocumentationfiles(the *"Software"),todealintheSoftwarewithoutrestriction,including *withoutlimitationtherightstouse,copy,modify,merge,publish, *distribute,sublicense,and/orsellcopiesoftheSoftware,andto *permitpersonstowhomtheSoftwareisfurnishedtodoso,subjectto *thefollowingconditions: * *Theabovecopyrightnoticeandthispermissionnoticeshallbe *includedinallcopiesorsubstantialportionsoftheSoftware. * *THESOFTWAREISPROVIDED"ASIS",WITHOUTWARRANTYOFANYKIND, *EXPRESSORIMPLIED,INCLUDINGBUTNOTLIMITEDTOTHEWARRANTIESOF *MERCHANTABILITY,FITNESSFORAPARTICULARPURPOSEAND *NONINFRINGEMENT.INNOEVENTSHALLTHEAUTHORSORCOPYRIGHTHOLDERSBE *LIABLEFORANYCLAIM,DAMAGESOROTHERLIABILITY,WHETHERINANACTION *OFCONTRACT,TORTOROTHERWISE,ARISINGFROM,OUTOFORINCONNECTION *WITHTHESOFTWAREORTHEUSEOROTHERDEALINGSINTHESOFTWARE. * */ #ifndef__GECODE_FLATZINC_AST_HH__ #define__GECODE_FLATZINC_AST_HH__ #include<vector> #include<string> #include<iostream> #include<cstdlib> namespaceGecode{namespaceFlatZinc{namespaceAST{ classCall; classArray; classAtom; classSetLit; classGECODE_VTABLE_EXPORTTypeError{ private: std::string_what; public: TypeError():_what(""){} TypeError(std::stringwhat):_what(what){} std::stringwhat(void)const{return_what;} }; classGECODE_VTABLE_EXPORTNode{ public: virtual~Node(void); voidappend(Node*n); boolhasAtom(conststd::string&id); boolisInt(int&i); boolisFloat(double&i); boolisCall(conststd::string&id); Call*getCall(void); boolhasCall(conststd::string&id); Call*getCall(conststd::string&id); Array*getArray(void); Atom*getAtom(void); std::stringgetVarName(void); intgetIntVar(void); intgetBoolVar(void); intgetFloatVar(void); intgetSetVar(void); intgetInt(void); boolgetBool(void); doublegetFloat(void); SetLit*getSet(void); std::stringgetString(void); boolisIntVar(void); boolisBoolVar(void); boolisSetVar(void); boolisFloatVar(void); boolisInt(void); boolisFloat(void); boolisBool(void); boolisString(void); boolisArray(void); boolisSet(void); boolisAtom(void); virtualvoidprint(std::ostream&)=0; }; classGECODE_VTABLE_EXPORTBoolLit:publicNode{ public: boolb; BoolLit(boolb0):b(b0){} virtualvoidprint(std::ostream&os){ os<<"b("<<(b?"true":"false")<<")"; } }; classGECODE_VTABLE_EXPORTIntLit:publicNode{ public: inti; IntLit(inti0):i(i0){} virtualvoidprint(std::ostream&os){ os<<"i("<<i<<")"; } }; classGECODE_VTABLE_EXPORTFloatLit:publicNode{ public: doubled; FloatLit(doubled0):d(d0){} virtualvoidprint(std::ostream&os){ os<<"f("<<d<<")"; } }; classGECODE_VTABLE_EXPORTSetLit:publicNode{ public: boolinterval; intmin;intmax; std::vector<int>s; SetLit(void){} SetLit(intmin0,intmax0):interval(true),min(min0),max(max0){} SetLit(conststd::vector<int>&s0):interval(false),s(s0){} boolempty(void)const{ return((interval&&min>max)||(!interval&&s.size()==0)); } virtualvoidprint(std::ostream&os){ os<<"s()"; } }; classGECODE_VTABLE_EXPORTVar:publicNode{ public: inti;//<Index std::stringn;//<Name Var(inti0,conststd::string&n0):i(i0),n(n0){} }; classGECODE_VTABLE_EXPORTBoolVar:publicVar{ public: BoolVar(inti0,conststd::string&n0=""):Var(i0,n0){} virtualvoidprint(std::ostream&os){ os<<"xb("<<i<<")"; } }; classGECODE_VTABLE_EXPORTIntVar:publicVar{ public: IntVar(inti0,conststd::string&n0=""):Var(i0,n0){} virtualvoidprint(std::ostream&os){ os<<"xi("<<i<<")"; } }; classGECODE_VTABLE_EXPORTFloatVar:publicVar{ public: FloatVar(inti0,conststd::string&n0=""):Var(i0,n0){} virtualvoidprint(std::ostream&os){ os<<"xf("<<i<<")"; } }; classGECODE_VTABLE_EXPORTSetVar:publicVar{ public: SetVar(inti0,conststd::string&n0=""):Var(i0,n0){} virtualvoidprint(std::ostream&os){ os<<"xs("<<i<<")"; } }; classGECODE_VTABLE_EXPORTArray:publicNode{ public: std::vector<Node*>a; Array(conststd::vector<Node*>&a0) :a(a0){} Array(Node*n) :a(1){a[0]=n;} Array(intn=0):a(n){} virtualvoidprint(std::ostream&os){ os<<"["; for(unsignedinti=0;i<a.size();i++){ a[i]->print(os); if(i<a.size()-1) os<<","; } os<<"]"; } ~Array(void){ for(inti=a.size();i--;) deletea[i]; } }; classGECODE_VTABLE_EXPORTCall:publicNode{ public: std::stringid; Node*args; Call(conststd::string&id0,Node*args0) :id(id0),args(args0){} ~Call(void){deleteargs;} virtualvoidprint(std::ostream&os){ os<<id<<"(";args->print(os);os<<")"; } Array*getArgs(unsignedintn){ Array*a=args->getArray(); if(a->a.size()!=n) throwTypeError("aritymismatch"); returna; } }; classGECODE_VTABLE_EXPORTArrayAccess:publicNode{ public: Node*a; Node*idx; ArrayAccess(Node*a0,Node*idx0) :a(a0),idx(idx0){} ~ArrayAccess(void){deletea;deleteidx;} virtualvoidprint(std::ostream&os){ a->print(os); os<<"["; idx->print(os); os<<"]"; } }; classGECODE_VTABLE_EXPORTAtom:publicNode{ public: std::stringid; Atom(conststd::string&id0):id(id0){} virtualvoidprint(std::ostream&os){ os<<id; } }; classGECODE_VTABLE_EXPORTString:publicNode{ public: std::strings; String(conststd::string&s0):s(s0){} virtualvoidprint(std::ostream&os){ os<<"s(\""<<s<<"\")"; } }; inline Node::~Node(void){} inlinevoid Node::append(Node*newNode){ Array*a=dynamic_cast<Array*>(this); if(!a) throwTypeError("arrayexpected"); a->a.push_back(newNode); } inlinebool Node::hasAtom(conststd::string&id){ if(Array*a=dynamic_cast<Array*>(this)){ for(inti=a->a.size();i--;) if(Atom*at=dynamic_cast<Atom*>(a->a[i])) if(at->id==id) returntrue; }elseif(Atom*a=dynamic_cast<Atom*>(this)){ returna->id==id; } returnfalse; } inlinebool Node::isCall(conststd::string&id){ if(Call*a=dynamic_cast<Call*>(this)){ if(a->id==id) returntrue; } returnfalse; } inlineCall* Node::getCall(void){ if(Call*a=dynamic_cast<Call*>(this)) returna; throwTypeError("callexpected"); } inlinebool Node::hasCall(conststd::string&id){ if(Array*a=dynamic_cast<Array*>(this)){ for(inti=a->a.size();i--;) if(Call*at=dynamic_cast<Call*>(a->a[i])) if(at->id==id){ returntrue; } }elseif(Call*a=dynamic_cast<Call*>(this)){ returna->id==id; } returnfalse; } inlinebool Node::isInt(int&i){ if(IntLit*il=dynamic_cast<IntLit*>(this)){ i=il->i; returntrue; } returnfalse; } inlinebool Node::isFloat(double&d){ if(FloatLit*fl=dynamic_cast<FloatLit*>(this)){ d=fl->d; returntrue; } returnfalse; } inlineCall* Node::getCall(conststd::string&id){ if(Array*a=dynamic_cast<Array*>(this)){ for(inti=a->a.size();i--;) if(Call*at=dynamic_cast<Call*>(a->a[i])) if(at->id==id) returnat; }elseif(Call*a=dynamic_cast<Call*>(this)){ if(a->id==id) returna; } throwTypeError("callexpected"); } inlineArray* Node::getArray(void){ if(Array*a=dynamic_cast<Array*>(this)) returna; throwTypeError("arrayexpected"); } inlineAtom* Node::getAtom(void){ if(Atom*a=dynamic_cast<Atom*>(this)) returna; throwTypeError("atomexpected"); } inlinestd::string Node::getVarName(void){ if(Var*a=dynamic_cast<Var*>(this)) returna->n; throwTypeError("variableexpected"); } inlineint Node::getIntVar(void){ if(IntVar*a=dynamic_cast<IntVar*>(this)) returna->i; throwTypeError("integervariableexpected"); } inlineint Node::getBoolVar(void){ if(BoolVar*a=dynamic_cast<BoolVar*>(this)) returna->i; throwTypeError("boolvariableexpected"); } inlineint Node::getFloatVar(void){ if(FloatVar*a=dynamic_cast<FloatVar*>(this)) returna->i; throwTypeError("integervariableexpected"); } inlineint Node::getSetVar(void){ if(SetVar*a=dynamic_cast<SetVar*>(this)) returna->i; throwTypeError("setvariableexpected"); } inlineint Node::getInt(void){ if(IntLit*a=dynamic_cast<IntLit*>(this)) returna->i; throwTypeError("integerliteralexpected"); } inlinebool Node::getBool(void){ if(BoolLit*a=dynamic_cast<BoolLit*>(this)) returna->b; throwTypeError("boolliteralexpected"); } inlinedouble Node::getFloat(void){ if(FloatLit*a=dynamic_cast<FloatLit*>(this)) returna->d; throwTypeError("floatliteralexpected"); } inlineSetLit* Node::getSet(void){ if(SetLit*a=dynamic_cast<SetLit*>(this)) returna; throwTypeError("setliteralexpected"); } inlinestd::string Node::getString(void){ if(String*a=dynamic_cast<String*>(this)) returna->s; throwTypeError("stringliteralexpected"); } inlinebool Node::isIntVar(void){ return(dynamic_cast<IntVar*>(this)!=NULL); } inlinebool Node::isBoolVar(void){ return(dynamic_cast<BoolVar*>(this)!=NULL); } inlinebool Node::isSetVar(void){ return(dynamic_cast<SetVar*>(this)!=NULL); } inlinebool Node::isFloatVar(void){ return(dynamic_cast<FloatVar*>(this)!=NULL); } inlinebool Node::isInt(void){ return(dynamic_cast<IntLit*>(this)!=NULL); } inlinebool Node::isBool(void){ return(dynamic_cast<BoolLit*>(this)!=NULL); } inlinebool Node::isFloat(void){ return(dynamic_cast<FloatLit*>(this)!=NULL); } inlinebool Node::isSet(void){ return(dynamic_cast<SetLit*>(this)!=NULL); } inlinebool Node::isString(void){ return(dynamic_cast<String*>(this)!=NULL); } inlinebool Node::isArray(void){ return(dynamic_cast<Array*>(this)!=NULL); } inlinebool Node::isAtom(void){ return(dynamic_cast<Atom*>(this)!=NULL); } inlineNode* extractSingleton(Node*n){ if(Array*a=dynamic_cast<Array*>(n)){ if(a->a.size()==1){ Node*ret=a->a[0]; a->a[0]=NULL; deletea; returnret; } } returnn; } }}} #endif //STATISTICS:flatzinc-any