Generated on Tue Apr 18 10:21:32 2017 for Gecode by doxygen 1.6.3

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: 2016-04-19 17:19:45 +0200 (Tue, 19 Apr 2016) $ by $Author: schulte $
00011  *     $Revision: 14967 $
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     explicit SetLit(SetLit* s0) : interval(s0->interval), min(s0->min), max(s0->max), s(s0->s) {}
00184     bool empty(void) const {
00185       return ( (interval && min>max) || (!interval && s.size() == 0));
00186     }
00187     virtual void print(std::ostream& os) {
00188       os << "s()";
00189     }
00190   };
00191 
00193   class GECODE_VTABLE_EXPORT Var : public Node {
00194   public:
00195     int i; //< Index
00196     std::string n; //< Name
00198     Var(int i0, const std::string& n0) : i(i0), n(n0) {}
00199   };
00201   class GECODE_VTABLE_EXPORT BoolVar : public Var {
00202   public:
00204     BoolVar(int i0, const std::string& n0="") : Var(i0,n0) {}
00205     virtual void print(std::ostream& os) {
00206       os << "xb("<<i<<")";
00207     }
00208   };
00210   class GECODE_VTABLE_EXPORT IntVar : public Var {
00211   public:
00212     IntVar(int i0, const std::string& n0="") : Var(i0,n0) {}
00213     virtual void print(std::ostream& os) {
00214       os << "xi("<<i<<")";
00215     }
00216   };
00218   class GECODE_VTABLE_EXPORT FloatVar : public Var {
00219   public:
00220     FloatVar(int i0, const std::string& n0="") : Var(i0,n0) {}
00221     virtual void print(std::ostream& os) {
00222       os << "xf("<<i<<")";
00223     }
00224   };
00226   class GECODE_VTABLE_EXPORT SetVar : public Var {
00227   public:
00228     SetVar(int i0, const std::string& n0="") : Var(i0,n0) {}
00229     virtual void print(std::ostream& os) {
00230       os << "xs("<<i<<")";
00231     }
00232   };
00233 
00235   class GECODE_VTABLE_EXPORT Array : public Node {
00236   public:
00237     std::vector<Node*> a;
00238     Array(const std::vector<Node*>& a0)
00239     : a(a0) {}
00240     Array(Node* n)
00241     : a(1) { a[0] = n; }
00242     Array(int n=0) : a(n) {}
00243     virtual void print(std::ostream& os) {
00244       os << "[";
00245       for (unsigned int i=0; i<a.size(); i++) {
00246         a[i]->print(os);
00247         if (i<a.size()-1)
00248           os << ", ";
00249       }
00250       os << "]";
00251     }
00252     ~Array(void) {
00253       for (int i=a.size(); i--;)
00254         delete a[i];
00255     }
00256   };
00257 
00259   class GECODE_VTABLE_EXPORT Call : public Node {
00260   public:
00261     std::string id;
00262     Node* args;
00263     Call(const std::string& id0, Node* args0)
00264     : id(id0), args(args0) {}
00265     ~Call(void) { delete args; }
00266     virtual void print(std::ostream& os) {
00267       os << id << "("; args->print(os); os << ")";
00268     }
00269     Array* getArgs(unsigned int n) {
00270       Array *a = args->getArray();
00271       if (a->a.size() != n)
00272         throw TypeError("arity mismatch");
00273       return a;
00274     }
00275   };
00276 
00278   class GECODE_VTABLE_EXPORT ArrayAccess : public Node {
00279   public:
00280     Node* a;
00281     Node* idx;
00282     ArrayAccess(Node* a0, Node* idx0)
00283     : a(a0), idx(idx0) {}
00284     ~ArrayAccess(void) { delete a; delete idx; }
00285     virtual void print(std::ostream& os) {
00286       a->print(os);
00287       os << "[";
00288       idx->print(os);
00289       os << "]";
00290     }
00291   };
00292 
00294   class GECODE_VTABLE_EXPORT Atom : public Node {
00295   public:
00296     std::string id;
00297     Atom(const std::string& id0) : id(id0) {}
00298     virtual void print(std::ostream& os) {
00299       os << id;
00300     }
00301   };
00302 
00304   class GECODE_VTABLE_EXPORT String : public Node {
00305   public:
00306     std::string s;
00307     String(const std::string& s0) : s(s0) {}
00308     virtual void print(std::ostream& os) {
00309       os << "s(\"" << s << "\")";
00310     }
00311   };
00312 
00313   inline
00314   Node::~Node(void) {}
00315 
00316   inline void
00317   Node::append(Node* newNode) {
00318     Array* a = dynamic_cast<Array*>(this);
00319     if (!a)
00320       throw TypeError("array expected");
00321     a->a.push_back(newNode);
00322   }
00323 
00324   inline bool
00325   Node::hasAtom(const std::string& id) {
00326     if (Array* a = dynamic_cast<Array*>(this)) {
00327       for (int i=a->a.size(); i--;)
00328         if (Atom* at = dynamic_cast<Atom*>(a->a[i]))
00329           if (at->id == id)
00330             return true;
00331     } else if (Atom* a = dynamic_cast<Atom*>(this)) {
00332       return a->id == id;
00333     }
00334     return false;
00335   }
00336 
00337   inline bool
00338   Node::isCall(const std::string& id) {
00339     if (Call* a = dynamic_cast<Call*>(this)) {
00340       if (a->id == id)
00341         return true;
00342     }
00343     return false;
00344   }
00345 
00346   inline Call*
00347   Node::getCall(void) {
00348     if (Call* a = dynamic_cast<Call*>(this))
00349       return a;
00350     throw TypeError("call expected");
00351   }
00352 
00353   inline bool
00354   Node::hasCall(const std::string& id) {
00355     if (Array* a = dynamic_cast<Array*>(this)) {
00356       for (int i=a->a.size(); i--;)
00357         if (Call* at = dynamic_cast<Call*>(a->a[i]))
00358           if (at->id == id) {
00359             return true;
00360           }
00361     } else if (Call* a = dynamic_cast<Call*>(this)) {
00362       return a->id == id;
00363     }
00364     return false;
00365   }
00366 
00367   inline bool
00368   Node::isInt(int& i) {
00369     if (IntLit* il = dynamic_cast<IntLit*>(this)) {
00370       i = il->i;
00371       return true;
00372     }
00373     return false;
00374   }
00375 
00376   inline bool
00377   Node::isFloat(double& d) {
00378     if (FloatLit* fl = dynamic_cast<FloatLit*>(this)) {
00379       d = fl->d;
00380       return true;
00381     }
00382     return false;
00383   }
00384 
00385   inline Call*
00386   Node::getCall(const std::string& id) {
00387     if (Array* a = dynamic_cast<Array*>(this)) {
00388       for (int i=a->a.size(); i--;)
00389         if (Call* at = dynamic_cast<Call*>(a->a[i]))
00390           if (at->id == id)
00391             return at;
00392     } else if (Call* a = dynamic_cast<Call*>(this)) {
00393       if (a->id == id)
00394         return a;
00395     }
00396     throw TypeError("call expected");
00397   }
00398 
00399   inline Array*
00400   Node::getArray(void) {
00401     if (Array* a = dynamic_cast<Array*>(this))
00402       return a;
00403     throw TypeError("array expected");
00404   }
00405 
00406   inline Atom*
00407   Node::getAtom(void) {
00408     if (Atom* a = dynamic_cast<Atom*>(this))
00409       return a;
00410     throw TypeError("atom expected");
00411   }
00412 
00413   inline std::string
00414   Node::getVarName(void) {
00415     if (Var* a = dynamic_cast<Var*>(this))
00416       return a->n;
00417     throw TypeError("variable expected");
00418   }
00419   inline int
00420   Node::getIntVar(void) {
00421     if (IntVar* a = dynamic_cast<IntVar*>(this))
00422       return a->i;
00423     throw TypeError("integer variable expected");
00424   }
00425   inline int
00426   Node::getBoolVar(void) {
00427     if (BoolVar* a = dynamic_cast<BoolVar*>(this))
00428       return a->i;
00429     throw TypeError("bool variable expected");
00430   }
00431   inline int
00432   Node::getFloatVar(void) {
00433     if (FloatVar* a = dynamic_cast<FloatVar*>(this))
00434       return a->i;
00435     throw TypeError("integer variable expected");
00436   }
00437   inline int
00438   Node::getSetVar(void) {
00439     if (SetVar* a = dynamic_cast<SetVar*>(this))
00440       return a->i;
00441     throw TypeError("set variable expected");
00442   }
00443   inline int
00444   Node::getInt(void) {
00445     if (IntLit* a = dynamic_cast<IntLit*>(this))
00446       return a->i;
00447     throw TypeError("integer literal expected");
00448   }
00449   inline bool
00450   Node::getBool(void) {
00451     if (BoolLit* a = dynamic_cast<BoolLit*>(this))
00452       return a->b;
00453     throw TypeError("bool literal expected");
00454   }
00455   inline double
00456   Node::getFloat(void) {
00457     if (FloatLit* a = dynamic_cast<FloatLit*>(this))
00458       return a->d;
00459     throw TypeError("float literal expected");
00460   }
00461   inline SetLit*
00462   Node::getSet(void) {
00463     if (SetLit* a = dynamic_cast<SetLit*>(this))
00464       return a;
00465     throw TypeError("set literal expected");
00466   }
00467   inline std::string
00468   Node::getString(void) {
00469     if (String* a = dynamic_cast<String*>(this))
00470       return a->s;
00471     throw TypeError("string literal expected");
00472   }
00473   inline bool
00474   Node::isIntVar(void) {
00475     return (dynamic_cast<IntVar*>(this) != NULL);
00476   }
00477   inline bool
00478   Node::isBoolVar(void) {
00479     return (dynamic_cast<BoolVar*>(this) != NULL);
00480   }
00481   inline bool
00482   Node::isSetVar(void) {
00483     return (dynamic_cast<SetVar*>(this) != NULL);
00484   }
00485   inline bool
00486   Node::isFloatVar(void) {
00487     return (dynamic_cast<FloatVar*>(this) != NULL);
00488   }
00489   inline bool
00490   Node::isInt(void) {
00491     return (dynamic_cast<IntLit*>(this) != NULL);
00492   }
00493   inline bool
00494   Node::isBool(void) {
00495     return (dynamic_cast<BoolLit*>(this) != NULL);
00496   }
00497   inline bool
00498   Node::isFloat(void) {
00499     return (dynamic_cast<FloatLit*>(this) != NULL);
00500   }
00501   inline bool
00502   Node::isSet(void) {
00503     return (dynamic_cast<SetLit*>(this) != NULL);
00504   }
00505   inline bool
00506   Node::isString(void) {
00507     return (dynamic_cast<String*>(this) != NULL);
00508   }
00509   inline bool
00510   Node::isArray(void) {
00511     return (dynamic_cast<Array*>(this) != NULL);
00512   }
00513   inline bool
00514   Node::isAtom(void) {
00515     return (dynamic_cast<Atom*>(this) != NULL);
00516   }
00517 
00518   inline Node*
00519   extractSingleton(Node* n) {
00520     if (Array* a = dynamic_cast<Array*>(n)) {
00521       if (a->a.size() == 1) {
00522         Node *ret = a->a[0];
00523         a->a[0] = NULL;
00524         delete a;
00525         return ret;
00526       }
00527     }
00528     return n;
00529   }
00530 
00531 }}}
00532 
00533 #endif
00534 
00535 // STATISTICS: flatzinc-any