Generated on Wed Nov 5 2014 05:18:16 for Gecode by doxygen 1.7.6.1
ast.hh
Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Guido Tack <tack@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Guido Tack, 2007
00008  *
00009  *  Last modified:
00010  *     $Date: 2013-05-09 01:32:37 +0200 (Thu, 09 May 2013) $ by $Author: tack $
00011  *     $Revision: 13625 $
00012  *
00013  *  This file is part of Gecode, the generic constraint
00014  *  development environment:
00015  *     http://www.gecode.org
00016  *
00017  *  Permission is hereby granted, free of charge, to any person obtaining
00018  *  a copy of this software and associated documentation files (the
00019  *  "Software"), to deal in the Software without restriction, including
00020  *  without limitation the rights to use, copy, modify, merge, publish,
00021  *  distribute, sublicense, and/or sell copies of the Software, and to
00022  *  permit persons to whom the Software is furnished to do so, subject to
00023  *  the following conditions:
00024  *
00025  *  The above copyright notice and this permission notice shall be
00026  *  included in all copies or substantial portions of the Software.
00027  *
00028  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00029  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00030  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00031  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00032  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00033  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00034  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00035  *
00036  */
00037 
00038 #ifndef __GECODE_FLATZINC_AST_HH__
00039 #define __GECODE_FLATZINC_AST_HH__
00040 
00041 #include <vector>
00042 #include <string>
00043 #include <iostream>
00044 #include <cstdlib>
00045 
00051 namespace Gecode { namespace FlatZinc { namespace AST {
00052   
00053   class Call;
00054   class Array;
00055   class Atom;
00056   class SetLit;
00057   
00059   class GECODE_VTABLE_EXPORT TypeError {
00060   private:
00061     std::string _what;
00062   public:
00063     TypeError() : _what("") {}
00064     TypeError(std::string what) : _what(what) {}
00065     std::string what(void) const { return _what; }
00066   };
00067   
00071   class GECODE_VTABLE_EXPORT Node {
00072   public:
00074     virtual ~Node(void);
00075     
00077     void append(Node* n);
00078 
00080     bool hasAtom(const std::string& id);
00082     bool isInt(int& i);
00084     bool isFloat(double& i);
00086     bool isCall(const std::string& id);
00088     Call* getCall(void);
00090     bool hasCall(const std::string& id);
00092     Call* getCall(const std::string& id);
00094     Array* getArray(void);
00096     Atom* getAtom(void);
00098     std::string getVarName(void);
00100     int getIntVar(void);
00102     int getBoolVar(void);
00104     int getFloatVar(void);
00106     int getSetVar(void);
00107     
00109     int getInt(void);
00111     bool getBool(void);
00113     double getFloat(void);
00115     SetLit *getSet(void);
00116     
00118     std::string getString(void);
00119     
00121     bool isIntVar(void);
00123     bool isBoolVar(void);
00125     bool isSetVar(void);
00127     bool isFloatVar(void);
00129     bool isInt(void);
00131     bool isFloat(void);
00133     bool isBool(void);
00135     bool isString(void);
00137     bool isArray(void);
00139     bool isSet(void);
00141     bool isAtom(void);
00142     
00144     virtual void print(std::ostream&) = 0;
00145   };
00146 
00148   class GECODE_VTABLE_EXPORT BoolLit : public Node {
00149   public:
00150     bool b;
00151     BoolLit(bool b0) : b(b0) {}
00152     virtual void print(std::ostream& os) {
00153       os << "b(" << (b ? "true" : "false") << ")";
00154     }
00155   };
00157   class GECODE_VTABLE_EXPORT IntLit : public Node {
00158   public:
00159     int i;
00160     IntLit(int i0) : i(i0) {}
00161     virtual void print(std::ostream& os) {
00162       os << "i("<<i<<")";
00163     }
00164   };
00166   class GECODE_VTABLE_EXPORT FloatLit : public Node {
00167   public:
00168     double d;
00169     FloatLit(double d0) : d(d0) {}
00170     virtual void print(std::ostream& os) {
00171       os << "f("<<d<<")";
00172     }
00173   };
00175   class GECODE_VTABLE_EXPORT SetLit : public Node {
00176   public:
00177     bool interval;
00178     int min; int max;
00179     std::vector<int> s;
00180     SetLit(void) {}
00181     SetLit(int min0, int max0) : interval(true), min(min0), max(max0) {}
00182     SetLit(const std::vector<int>& s0) : interval(false), s(s0) {}
00183     bool empty(void) const {
00184       return ( (interval && min>max) || (!interval && s.size() == 0));
00185     }
00186     virtual void print(std::ostream& os) {
00187       os << "s()";
00188     }
00189   };
00190   
00192   class GECODE_VTABLE_EXPORT Var : public Node {
00193   public:
00194     int i; //< Index
00195     std::string n; //< Name
00197     Var(int i0, const std::string& n0) : i(i0), n(n0) {}
00198   };
00200   class GECODE_VTABLE_EXPORT BoolVar : public Var {
00201   public:
00203     BoolVar(int i0, const std::string& n0="") : Var(i0,n0) {}
00204     virtual void print(std::ostream& os) {
00205       os << "xb("<<i<<")";
00206     }
00207   };
00209   class GECODE_VTABLE_EXPORT IntVar : public Var {
00210   public:
00211     IntVar(int i0, const std::string& n0="") : Var(i0,n0) {}
00212     virtual void print(std::ostream& os) {
00213       os << "xi("<<i<<")";
00214     }
00215   };
00217   class GECODE_VTABLE_EXPORT FloatVar : public Var {
00218   public:
00219     FloatVar(int i0, const std::string& n0="") : Var(i0,n0) {}
00220     virtual void print(std::ostream& os) {
00221       os << "xf("<<i<<")";
00222     }
00223   };
00225   class GECODE_VTABLE_EXPORT SetVar : public Var {
00226   public:
00227     SetVar(int i0, const std::string& n0="") : Var(i0,n0) {}
00228     virtual void print(std::ostream& os) {
00229       os << "xs("<<i<<")";
00230     }
00231   };
00232   
00234   class GECODE_VTABLE_EXPORT Array : public Node {
00235   public:
00236     std::vector<Node*> a;
00237     Array(const std::vector<Node*>& a0)
00238     : a(a0) {}
00239     Array(Node* n)
00240     : a(1) { a[0] = n; }
00241     Array(int n=0) : a(n) {}
00242     virtual void print(std::ostream& os) {
00243       os << "[";
00244       for (unsigned int i=0; i<a.size(); i++) {
00245         a[i]->print(os);
00246         if (i<a.size()-1)
00247           os << ", ";
00248       }
00249       os << "]";
00250     }
00251     ~Array(void) {
00252       for (int i=a.size(); i--;)
00253         delete a[i];
00254     }
00255   };
00256 
00258   class GECODE_VTABLE_EXPORT Call : public Node {
00259   public:
00260     std::string id;
00261     Node* args;
00262     Call(const std::string& id0, Node* args0)
00263     : id(id0), args(args0) {}
00264     ~Call(void) { delete args; }
00265     virtual void print(std::ostream& os) {
00266       os << id << "("; args->print(os); os << ")";
00267     }
00268     Array* getArgs(unsigned int n) {
00269       Array *a = args->getArray();
00270       if (a->a.size() != n)
00271         throw TypeError("arity mismatch");
00272       return a;
00273     }
00274   };
00275 
00277   class GECODE_VTABLE_EXPORT ArrayAccess : public Node {
00278   public:
00279     Node* a;
00280     Node* idx;
00281     ArrayAccess(Node* a0, Node* idx0)
00282     : a(a0), idx(idx0) {}
00283     ~ArrayAccess(void) { delete a; delete idx; }
00284     virtual void print(std::ostream& os) {
00285       a->print(os);
00286       os << "[";
00287       idx->print(os);
00288       os << "]";
00289     }
00290   };
00291 
00293   class GECODE_VTABLE_EXPORT Atom : public Node {
00294   public:
00295     std::string id;
00296     Atom(const std::string& id0) : id(id0) {}
00297     virtual void print(std::ostream& os) {
00298       os << id;
00299     }
00300   };
00301 
00303   class GECODE_VTABLE_EXPORT String : public Node {
00304   public:
00305     std::string s;
00306     String(const std::string& s0) : s(s0) {}
00307     virtual void print(std::ostream& os) {
00308       os << "s(\"" << s << "\")";
00309     }
00310   };
00311   
00312   inline
00313   Node::~Node(void) {}
00314   
00315   inline void
00316   Node::append(Node* newNode) {
00317     Array* a = dynamic_cast<Array*>(this);
00318     if (!a)
00319       throw TypeError("array expected");
00320     a->a.push_back(newNode);
00321   }
00322 
00323   inline bool
00324   Node::hasAtom(const std::string& id) {
00325     if (Array* a = dynamic_cast<Array*>(this)) {
00326       for (int i=a->a.size(); i--;)
00327         if (Atom* at = dynamic_cast<Atom*>(a->a[i]))
00328           if (at->id == id)
00329             return true;
00330     } else if (Atom* a = dynamic_cast<Atom*>(this)) {
00331       return a->id == id;
00332     }
00333     return false;
00334   }
00335 
00336   inline bool
00337   Node::isCall(const std::string& id) {
00338     if (Call* a = dynamic_cast<Call*>(this)) {
00339       if (a->id == id)
00340         return true;
00341     }
00342     return false;
00343   }
00344 
00345   inline Call*
00346   Node::getCall(void) {
00347     if (Call* a = dynamic_cast<Call*>(this))
00348       return a;
00349     throw TypeError("call expected");
00350   }
00351 
00352   inline bool
00353   Node::hasCall(const std::string& id) {
00354     if (Array* a = dynamic_cast<Array*>(this)) {
00355       for (int i=a->a.size(); i--;)
00356         if (Call* at = dynamic_cast<Call*>(a->a[i]))
00357           if (at->id == id) {
00358             return true;
00359           }
00360     } else if (Call* a = dynamic_cast<Call*>(this)) {
00361       return a->id == id;
00362     }
00363     return false;
00364   }
00365 
00366   inline bool
00367   Node::isInt(int& i) {
00368     if (IntLit* il = dynamic_cast<IntLit*>(this)) {
00369       i = il->i;
00370       return true;
00371     }
00372     return false;
00373   }
00374 
00375   inline bool
00376   Node::isFloat(double& d) {
00377     if (FloatLit* fl = dynamic_cast<FloatLit*>(this)) {
00378       d = fl->d;
00379       return true;
00380     }
00381     return false;
00382   }
00383 
00384   inline Call*
00385   Node::getCall(const std::string& id) {
00386     if (Array* a = dynamic_cast<Array*>(this)) {
00387       for (int i=a->a.size(); i--;)
00388         if (Call* at = dynamic_cast<Call*>(a->a[i]))
00389           if (at->id == id)
00390             return at;
00391     } else if (Call* a = dynamic_cast<Call*>(this)) {
00392       if (a->id == id)
00393         return a;
00394     }
00395     throw TypeError("call expected");
00396   }
00397   
00398   inline Array*
00399   Node::getArray(void) {
00400     if (Array* a = dynamic_cast<Array*>(this))
00401       return a;
00402     throw TypeError("array expected");
00403   }
00404 
00405   inline Atom*
00406   Node::getAtom(void) {
00407     if (Atom* a = dynamic_cast<Atom*>(this))
00408       return a;
00409     throw TypeError("atom expected");
00410   }
00411   
00412   inline std::string
00413   Node::getVarName(void) {
00414     if (Var* a = dynamic_cast<Var*>(this))
00415       return a->n;
00416     throw TypeError("variable expected");
00417   }
00418   inline int
00419   Node::getIntVar(void) {
00420     if (IntVar* a = dynamic_cast<IntVar*>(this))
00421       return a->i;
00422     throw TypeError("integer variable expected");
00423   }
00424   inline int
00425   Node::getBoolVar(void) {
00426     if (BoolVar* a = dynamic_cast<BoolVar*>(this))
00427       return a->i;
00428     throw TypeError("bool variable expected");
00429   }
00430   inline int
00431   Node::getFloatVar(void) {
00432     if (FloatVar* a = dynamic_cast<FloatVar*>(this))
00433       return a->i;
00434     throw TypeError("integer variable expected");
00435   }
00436   inline int
00437   Node::getSetVar(void) {
00438     if (SetVar* a = dynamic_cast<SetVar*>(this))
00439       return a->i;
00440     throw TypeError("set variable expected");
00441   }
00442   inline int
00443   Node::getInt(void) {
00444     if (IntLit* a = dynamic_cast<IntLit*>(this))
00445       return a->i;
00446     throw TypeError("integer literal expected");
00447   }
00448   inline bool
00449   Node::getBool(void) {
00450     if (BoolLit* a = dynamic_cast<BoolLit*>(this))
00451       return a->b;
00452     throw TypeError("bool literal expected");
00453   }
00454   inline double
00455   Node::getFloat(void) {
00456     if (FloatLit* a = dynamic_cast<FloatLit*>(this))
00457       return a->d;
00458     throw TypeError("float literal expected");
00459   }
00460   inline SetLit*
00461   Node::getSet(void) {
00462     if (SetLit* a = dynamic_cast<SetLit*>(this))
00463       return a;
00464     throw TypeError("set literal expected");
00465   }
00466   inline std::string
00467   Node::getString(void) {
00468     if (String* a = dynamic_cast<String*>(this))
00469       return a->s;
00470     throw TypeError("string literal expected");
00471   }
00472   inline bool
00473   Node::isIntVar(void) {
00474     return (dynamic_cast<IntVar*>(this) != NULL);
00475   }
00476   inline bool
00477   Node::isBoolVar(void) {
00478     return (dynamic_cast<BoolVar*>(this) != NULL);
00479   }
00480   inline bool
00481   Node::isSetVar(void) {
00482     return (dynamic_cast<SetVar*>(this) != NULL);
00483   }
00484   inline bool
00485   Node::isFloatVar(void) {
00486     return (dynamic_cast<FloatVar*>(this) != NULL);
00487   }
00488   inline bool
00489   Node::isInt(void) {
00490     return (dynamic_cast<IntLit*>(this) != NULL);
00491   }
00492   inline bool
00493   Node::isBool(void) {
00494     return (dynamic_cast<BoolLit*>(this) != NULL);
00495   }
00496   inline bool
00497   Node::isFloat(void) {
00498     return (dynamic_cast<FloatLit*>(this) != NULL);
00499   }
00500   inline bool
00501   Node::isSet(void) {
00502     return (dynamic_cast<SetLit*>(this) != NULL);
00503   }
00504   inline bool
00505   Node::isString(void) {
00506     return (dynamic_cast<String*>(this) != NULL);
00507   }
00508   inline bool
00509   Node::isArray(void) {
00510     return (dynamic_cast<Array*>(this) != NULL);
00511   }
00512   inline bool
00513   Node::isAtom(void) {
00514     return (dynamic_cast<Atom*>(this) != NULL);
00515   }
00516 
00517   inline Node*
00518   extractSingleton(Node* n) {
00519     if (Array* a = dynamic_cast<Array*>(n)) {
00520       if (a->a.size() == 1) {
00521         Node *ret = a->a[0];
00522         a->a[0] = NULL;
00523         delete a;
00524         return ret;
00525       }
00526     }
00527     return n;
00528   }
00529 
00530 }}}
00531 
00532 #endif
00533 
00534 // STATISTICS: flatzinc-any