Generated on Mon Nov 30 23:53:18 2009 for Gecode by doxygen 1.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: 2009-09-10 11:44:51 +0200 (Thu, 10 Sep 2009) $ by $Author: tack $
00011  *     $Revision: 9700 $
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 SetLit;
00056   
00058   class TypeError {
00059   private:
00060     std::string _what;
00061   public:
00062     TypeError() : _what("") {}
00063     TypeError(std::string what) : _what(what) {}
00064     std::string what(void) const { return _what; }
00065   };
00066   
00070   class Node {
00071   public:
00073     virtual ~Node(void);
00074     
00076     void append(Node* n);
00077 
00079     bool hasAtom(const std::string& id);
00081     bool isInt(int& i);
00083     bool isCall(const std::string& id);
00085     Call* getCall(void);
00087     bool hasCall(const std::string& id);
00089     Call* getCall(const std::string& id);
00091     Array* getArray(void);
00093     int getIntVar(void);
00095     int getBoolVar(void);
00097     int getSetVar(void);
00098     
00100     int getInt(void);
00102     bool getBool(void);
00104     SetLit *getSet(void);
00105     
00107     std::string getString(void);
00108     
00110     bool isIntVar(void);
00112     bool isBoolVar(void);
00114     bool isSetVar(void);
00116     bool isInt(void);
00118     bool isBool(void);
00120     bool isString(void);
00122     bool isArray(void);
00124     bool isSet(void);
00125     
00127     virtual void print(std::ostream&) = 0;
00128   };
00129 
00131   class BoolLit : public Node {
00132   public:
00133     bool b;
00134     BoolLit(bool b0) : b(b0) {}
00135     virtual void print(std::ostream& os) {
00136       os << "b(" << (b ? "true" : "false") << ")";
00137     }
00138   };
00140   class IntLit : public Node {
00141   public:
00142     int i;
00143     IntLit(int i0) : i(i0) {}
00144     virtual void print(std::ostream& os) {
00145       os << "i("<<i<<")";
00146     }
00147   };
00149   class FloatLit : public Node {
00150   public:
00151     double d;
00152     FloatLit(double d0) : d(d0) {}
00153     virtual void print(std::ostream& os) {
00154       os << "f("<<d<<")";
00155     }
00156   };
00158   class SetLit : public Node {
00159   public:
00160     bool interval;
00161     int min; int max;
00162     std::vector<int> s;
00163     SetLit(void) {}
00164     SetLit(int min0, int max0) : interval(true), min(min0), max(max0) {}
00165     SetLit(const std::vector<int>& s0) : interval(false), s(s0) {}
00166     bool empty(void) const {
00167       return ( (interval && min>max) || (!interval && s.size() == 0));
00168     }
00169     virtual void print(std::ostream& os) {
00170       os << "s()";
00171     }
00172   };
00173   
00175   class Var : public Node {
00176   public:
00177     int i;
00178     Var(int i0) : i(i0) {}
00179   };
00181   class BoolVar : public Var {
00182   public:
00183     BoolVar(int i0) : Var(i0) {}
00184     virtual void print(std::ostream& os) {
00185       os << "xb("<<i<<")";
00186     }
00187   };
00189   class IntVar : public Var {
00190   public:
00191     IntVar(int i0) : Var(i0) {}
00192     virtual void print(std::ostream& os) {
00193       os << "xi("<<i<<")";
00194     }
00195   };
00197   class FloatVar : public Var {
00198   public:
00199     FloatVar(int i0) : Var(i0) {}
00200     virtual void print(std::ostream& os) {
00201       os << "xf("<<i<<")";
00202     }
00203   };
00205   class SetVar : public Var {
00206   public:
00207     SetVar(int i0) : Var(i0) {}
00208     virtual void print(std::ostream& os) {
00209       os << "xs("<<i<<")";
00210     }
00211   };
00212   
00214   class Array : public Node {
00215   public:
00216     std::vector<Node*> a;
00217     Array(const std::vector<Node*>& a0)
00218     : a(a0) {}
00219     Array(Node* n)
00220     : a(1) { a[0] = n; }
00221     Array(int n=0) : a(n) {}
00222     virtual void print(std::ostream& os) {
00223       os << "[";
00224       for (unsigned int i=0; i<a.size(); i++) {
00225         a[i]->print(os);
00226         if (i<a.size()-1)
00227           os << ", ";
00228       }
00229       os << "]";
00230     }
00231     ~Array(void) {
00232       for (int i=a.size(); i--;)
00233         delete a[i];
00234     }
00235   };
00236 
00238   class Call : public Node {
00239   public:
00240     std::string id;
00241     Node* args;
00242     Call(const std::string& id0, Node* args0)
00243     : id(id0), args(args0) {}
00244     ~Call(void) { delete args; }
00245     virtual void print(std::ostream& os) {
00246       os << id << "("; args->print(os); os << ")";
00247     }
00248     Array* getArgs(unsigned int n) {
00249       Array *a = args->getArray();
00250       if (a->a.size() != n)
00251         throw TypeError("arity mismatch");
00252       return a;
00253     }
00254   };
00255 
00257   class ArrayAccess : public Node {
00258   public:
00259     Node* a;
00260     Node* idx;
00261     ArrayAccess(Node* a0, Node* idx0)
00262     : a(a0), idx(idx0) {}
00263     ~ArrayAccess(void) { delete a; delete idx; }
00264     virtual void print(std::ostream& os) {
00265       a->print(os);
00266       os << "[";
00267       idx->print(os);
00268       os << "]";
00269     }
00270   };
00271 
00273   class Atom : public Node {
00274   public:
00275     std::string id;
00276     Atom(const std::string& id0) : id(id0) {}
00277     virtual void print(std::ostream& os) {
00278       os << id;
00279     }
00280   };
00281 
00283   class String : public Node {
00284   public:
00285     std::string s;
00286     String(const std::string& s0) : s(s0) {}
00287     virtual void print(std::ostream& os) {
00288       os << "s(\"" << s << "\")";
00289     }
00290   };
00291   
00292   inline
00293   Node::~Node(void) {}
00294   
00295   inline void
00296   Node::append(Node* newNode) {
00297     Array* a = dynamic_cast<Array*>(this);
00298     if (!a) {
00299       std::cerr << "type error" << std::endl;
00300       std::exit(-1);
00301     }
00302     a->a.push_back(newNode);
00303   }
00304 
00305   inline bool
00306   Node::hasAtom(const std::string& id) {
00307     if (Array* a = dynamic_cast<Array*>(this)) {
00308       for (int i=a->a.size(); i--;)
00309         if (Atom* at = dynamic_cast<Atom*>(a->a[i]))
00310           if (at->id == id)
00311             return true;
00312     } else if (Atom* a = dynamic_cast<Atom*>(this)) {
00313       return a->id == id;
00314     }
00315     return false;
00316   }
00317 
00318   inline bool
00319   Node::isCall(const std::string& id) {
00320     if (Call* a = dynamic_cast<Call*>(this)) {
00321       if (a->id == id)
00322         return true;
00323     }
00324     return false;
00325   }
00326 
00327   inline Call*
00328   Node::getCall(void) {
00329     if (Call* a = dynamic_cast<Call*>(this))
00330       return a;
00331     throw TypeError("call expected");
00332   }
00333 
00334   inline bool
00335   Node::hasCall(const std::string& id) {
00336     if (Array* a = dynamic_cast<Array*>(this)) {
00337       for (int i=a->a.size(); i--;)
00338         if (Call* at = dynamic_cast<Call*>(a->a[i]))
00339           if (at->id == id) {
00340             return true;
00341           }
00342     } else if (Call* a = dynamic_cast<Call*>(this)) {
00343       return a->id == id;
00344     }
00345     return false;
00346   }
00347 
00348   inline bool
00349   Node::isInt(int& i) {
00350     if (IntLit* il = dynamic_cast<IntLit*>(this)) {
00351       i = il->i;
00352       return true;
00353     }
00354     return false;
00355   }
00356 
00357   inline Call*
00358   Node::getCall(const std::string& id) {
00359     if (Array* a = dynamic_cast<Array*>(this)) {
00360       for (int i=a->a.size(); i--;)
00361         if (Call* at = dynamic_cast<Call*>(a->a[i]))
00362           if (at->id == id)
00363             return at;
00364     } else if (Call* a = dynamic_cast<Call*>(this)) {
00365       if (a->id == id)
00366         return a;
00367     }
00368     throw TypeError("call expected");
00369   }
00370   
00371   inline Array*
00372   Node::getArray(void) {
00373     if (Array* a = dynamic_cast<Array*>(this))
00374       return a;
00375     throw TypeError("array expected");
00376   }
00377   
00378   inline int
00379   Node::getIntVar(void) {
00380     if (IntVar* a = dynamic_cast<IntVar*>(this))
00381       return a->i;
00382     throw TypeError("integer variable expected");
00383   }
00384   inline int
00385   Node::getBoolVar(void) {
00386     if (BoolVar* a = dynamic_cast<BoolVar*>(this))
00387       return a->i;
00388     throw TypeError("bool variable expected");
00389   }
00390   inline int
00391   Node::getSetVar(void) {
00392     if (SetVar* a = dynamic_cast<SetVar*>(this))
00393       return a->i;
00394     throw TypeError("set variable expected");
00395   }
00396   inline int
00397   Node::getInt(void) {
00398     if (IntLit* a = dynamic_cast<IntLit*>(this))
00399       return a->i;
00400     throw TypeError("integer literal expected");
00401   }
00402   inline bool
00403   Node::getBool(void) {
00404     if (BoolLit* a = dynamic_cast<BoolLit*>(this))
00405       return a->b;
00406     throw TypeError("bool literal expected");
00407   }
00408   inline SetLit*
00409   Node::getSet(void) {
00410     if (SetLit* a = dynamic_cast<SetLit*>(this))
00411       return a;
00412     throw TypeError("set literal expected");
00413   }
00414   inline std::string
00415   Node::getString(void) {
00416     if (String* a = dynamic_cast<String*>(this))
00417       return a->s;
00418     throw TypeError("string literal expected");
00419   }
00420   inline bool
00421   Node::isIntVar(void) {
00422     return (dynamic_cast<IntVar*>(this) != NULL);
00423   }
00424   inline bool
00425   Node::isBoolVar(void) {
00426     return (dynamic_cast<BoolVar*>(this) != NULL);
00427   }
00428   inline bool
00429   Node::isSetVar(void) {
00430     return (dynamic_cast<SetVar*>(this) != NULL);
00431   }
00432   inline bool
00433   Node::isInt(void) {
00434     return (dynamic_cast<IntLit*>(this) != NULL);
00435   }
00436   inline bool
00437   Node::isBool(void) {
00438     return (dynamic_cast<BoolLit*>(this) != NULL);
00439   }
00440   inline bool
00441   Node::isSet(void) {
00442     return (dynamic_cast<SetLit*>(this) != NULL);
00443   }
00444   inline bool
00445   Node::isString(void) {
00446     return (dynamic_cast<String*>(this) != NULL);
00447   }
00448   inline bool
00449   Node::isArray(void) {
00450     return (dynamic_cast<Array*>(this) != NULL);
00451   }
00452 
00453   inline Node*
00454   extractSingleton(Node* n) {
00455     if (Array* a = dynamic_cast<Array*>(n)) {
00456       if (a->a.size() == 1) {
00457         Node *ret = a->a[0];
00458         a->a[0] = NULL;
00459         delete a;
00460         return ret;
00461       }
00462     }
00463     return n;
00464   }
00465 
00466 }}}
00467 
00468 #endif
00469 
00470 // STATISTICS: flatzinc-any