Generated on Tue Feb 11 2025 17:33:26 for Gecode by doxygen 1.12.0
ast.hh
Go to the documentation of this file.
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Guido Tack <tack@gecode.org>
5 *
6 * Copyright:
7 * Guido Tack, 2007
8 *
9 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.org
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files (the
15 * "Software"), to deal in the Software without restriction, including
16 * without limitation the rights to use, copy, modify, merge, publish,
17 * distribute, sublicense, and/or sell copies of the Software, and to
18 * permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 *
32 */
33
34#ifndef __GECODE_FLATZINC_AST_HH__
35#define __GECODE_FLATZINC_AST_HH__
36
37#include <vector>
38#include <string>
39#include <iostream>
40#include <cstdlib>
41
47namespace Gecode { namespace FlatZinc { namespace AST {
48
49 class Call;
50 class Array;
51 class Atom;
52 class SetLit;
53
56 private:
57 std::string _what;
58 public:
59 TypeError() : _what("") {}
60 TypeError(std::string what) : _what(what) {}
61 std::string what(void) const { return _what; }
62 };
63
68 public:
70 virtual ~Node(void);
71
73 void append(Node* n);
74
76 bool hasAtom(const std::string& id);
78 bool isInt(int& i);
80 bool isFloat(double& i);
82 bool isCall(const std::string& id);
84 Call* getCall(void);
86 bool hasCall(const std::string& id);
88 Call* getCall(const std::string& id);
90 Array* getArray(void);
92 Atom* getAtom(void);
94 std::string getVarName(void);
96 int getIntVar(void);
98 int getBoolVar(void);
100 int getFloatVar(void);
102 int getSetVar(void);
103
105 int getInt(void);
107 bool getBool(void);
109 double getFloat(void);
111 SetLit *getSet(void);
112
114 std::string getString(void);
115
117 bool isIntVar(void);
119 bool isBoolVar(void);
121 bool isSetVar(void);
123 bool isFloatVar(void);
125 bool isInt(void);
127 bool isFloat(void);
129 bool isBool(void);
131 bool isString(void);
133 bool isArray(void);
135 bool isSet(void);
137 bool isAtom(void);
138
140 virtual void print(std::ostream&) = 0;
141 };
142
145 public:
146 bool b;
147 BoolLit(bool b0) : b(b0) {}
148 virtual void print(std::ostream& os) {
149 os << "b(" << (b ? "true" : "false") << ")";
150 }
151 };
154 public:
155 int i;
156 IntLit(int i0) : i(i0) {}
157 virtual void print(std::ostream& os) {
158 os << "i("<<i<<")";
159 }
160 };
163 public:
164 double d;
165 FloatLit(double d0) : d(d0) {}
166 virtual void print(std::ostream& os) {
167 os << "f("<<d<<")";
168 }
169 };
172 public:
174 int min; int max;
175 std::vector<int> s;
176 SetLit(void) {}
177 SetLit(int min0, int max0) : interval(true), min(min0), max(max0) {}
178 SetLit(const std::vector<int>& s0) : interval(false), s(s0) {}
179 explicit SetLit(SetLit* s0) : interval(s0->interval), min(s0->min), max(s0->max), s(s0->s) {}
180 bool empty(void) const {
181 return ( (interval && min>max) || (!interval && s.size() == 0));
182 }
183 virtual void print(std::ostream& os) {
184 os << "s()";
185 }
186 };
187
190 public:
191 int i; //< Index
192 std::string n; //< Name
194 Var(int i0, const std::string& n0) : i(i0), n(n0) {}
195 };
198 public:
200 BoolVar(int i0, const std::string& n0="") : Var(i0,n0) {}
201 virtual void print(std::ostream& os) {
202 os << "xb("<<i<<")";
203 }
204 };
207 public:
208 IntVar(int i0, const std::string& n0="") : Var(i0,n0) {}
209 virtual void print(std::ostream& os) {
210 os << "xi("<<i<<")";
211 }
212 };
215 public:
216 FloatVar(int i0, const std::string& n0="") : Var(i0,n0) {}
217 virtual void print(std::ostream& os) {
218 os << "xf("<<i<<")";
219 }
220 };
223 public:
224 SetVar(int i0, const std::string& n0="") : Var(i0,n0) {}
225 virtual void print(std::ostream& os) {
226 os << "xs("<<i<<")";
227 }
228 };
229
232 public:
233 std::vector<Node*> a;
234 Array(const std::vector<Node*>& a0)
235 : a(a0) {}
237 : a(1) { a[0] = n; }
238 Array(int n=0) : a(n) {}
239 virtual void print(std::ostream& os) {
240 os << "[";
241 for (unsigned int i=0; i<a.size(); i++) {
242 a[i]->print(os);
243 if (i<a.size()-1)
244 os << ", ";
245 }
246 os << "]";
247 }
248 ~Array(void) {
249 for (int i=a.size(); i--;)
250 delete a[i];
251 }
252 };
253
256 public:
257 std::string id;
259 Call(const std::string& id0, Node* args0)
260 : id(id0), args(args0) {}
261 ~Call(void) { delete args; }
262 virtual void print(std::ostream& os) {
263 os << id << "("; args->print(os); os << ")";
264 }
265 Array* getArgs(unsigned int n) {
266 Array *a = args->getArray();
267 if (a->a.size() != n)
268 throw TypeError("arity mismatch");
269 return a;
270 }
271 };
272
275 public:
279 : a(a0), idx(idx0) {}
280 ~ArrayAccess(void) { delete a; delete idx; }
281 virtual void print(std::ostream& os) {
282 a->print(os);
283 os << "[";
284 idx->print(os);
285 os << "]";
286 }
287 };
288
291 public:
292 std::string id;
293 Atom(const std::string& id0) : id(id0) {}
294 virtual void print(std::ostream& os) {
295 os << id;
296 }
297 };
298
301 public:
302 std::string s;
303 String(const std::string& s0) : s(s0) {}
304 virtual void print(std::ostream& os) {
305 os << "s(\"" << s << "\")";
306 }
307 };
308
309 inline
310 Node::~Node(void) {}
311
312 inline void
313 Node::append(Node* newNode) {
314 Array* a = dynamic_cast<Array*>(this);
315 if (!a)
316 throw TypeError("array expected");
317 a->a.push_back(newNode);
318 }
319
320 inline bool
321 Node::hasAtom(const std::string& id) {
322 if (Array* a = dynamic_cast<Array*>(this)) {
323 for (int i=a->a.size(); i--;)
324 if (Atom* at = dynamic_cast<Atom*>(a->a[i]))
325 if (at->id == id)
326 return true;
327 } else if (Atom* a = dynamic_cast<Atom*>(this)) {
328 return a->id == id;
329 }
330 return false;
331 }
332
333 inline bool
334 Node::isCall(const std::string& id) {
335 if (Call* a = dynamic_cast<Call*>(this)) {
336 if (a->id == id)
337 return true;
338 }
339 return false;
340 }
341
342 inline Call*
344 if (Call* a = dynamic_cast<Call*>(this))
345 return a;
346 throw TypeError("call expected");
347 }
348
349 inline bool
350 Node::hasCall(const std::string& id) {
351 if (Array* a = dynamic_cast<Array*>(this)) {
352 for (int i=a->a.size(); i--;)
353 if (Call* at = dynamic_cast<Call*>(a->a[i]))
354 if (at->id == id) {
355 return true;
356 }
357 } else if (Call* a = dynamic_cast<Call*>(this)) {
358 return a->id == id;
359 }
360 return false;
361 }
362
363 inline bool
364 Node::isInt(int& i) {
365 if (IntLit* il = dynamic_cast<IntLit*>(this)) {
366 i = il->i;
367 return true;
368 }
369 return false;
370 }
371
372 inline bool
373 Node::isFloat(double& d) {
374 if (FloatLit* fl = dynamic_cast<FloatLit*>(this)) {
375 d = fl->d;
376 return true;
377 }
378 return false;
379 }
380
381 inline Call*
382 Node::getCall(const std::string& id) {
383 if (Array* a = dynamic_cast<Array*>(this)) {
384 for (int i=a->a.size(); i--;)
385 if (Call* at = dynamic_cast<Call*>(a->a[i]))
386 if (at->id == id)
387 return at;
388 } else if (Call* a = dynamic_cast<Call*>(this)) {
389 if (a->id == id)
390 return a;
391 }
392 throw TypeError("call expected");
393 }
394
395 inline Array*
397 if (Array* a = dynamic_cast<Array*>(this))
398 return a;
399 throw TypeError("array expected");
400 }
401
402 inline Atom*
404 if (Atom* a = dynamic_cast<Atom*>(this))
405 return a;
406 throw TypeError("atom expected");
407 }
408
409 inline std::string
411 if (Var* a = dynamic_cast<Var*>(this))
412 return a->n;
413 throw TypeError("variable expected");
414 }
415 inline int
417 if (IntVar* a = dynamic_cast<IntVar*>(this))
418 return a->i;
419 throw TypeError("integer variable expected");
420 }
421 inline int
423 if (BoolVar* a = dynamic_cast<BoolVar*>(this))
424 return a->i;
425 throw TypeError("bool variable expected");
426 }
427 inline int
429 if (FloatVar* a = dynamic_cast<FloatVar*>(this))
430 return a->i;
431 throw TypeError("integer variable expected");
432 }
433 inline int
435 if (SetVar* a = dynamic_cast<SetVar*>(this))
436 return a->i;
437 throw TypeError("set variable expected");
438 }
439 inline int
441 if (IntLit* a = dynamic_cast<IntLit*>(this))
442 return a->i;
443 throw TypeError("integer literal expected");
444 }
445 inline bool
447 if (BoolLit* a = dynamic_cast<BoolLit*>(this))
448 return a->b;
449 throw TypeError("bool literal expected");
450 }
451 inline double
453 if (FloatLit* a = dynamic_cast<FloatLit*>(this))
454 return a->d;
455 throw TypeError("float literal expected");
456 }
457 inline SetLit*
459 if (SetLit* a = dynamic_cast<SetLit*>(this))
460 return a;
461 throw TypeError("set literal expected");
462 }
463 inline std::string
465 if (String* a = dynamic_cast<String*>(this))
466 return a->s;
467 throw TypeError("string literal expected");
468 }
469 inline bool
471 return (dynamic_cast<IntVar*>(this) != NULL);
472 }
473 inline bool
475 return (dynamic_cast<BoolVar*>(this) != NULL);
476 }
477 inline bool
479 return (dynamic_cast<SetVar*>(this) != NULL);
480 }
481 inline bool
483 return (dynamic_cast<FloatVar*>(this) != NULL);
484 }
485 inline bool
487 return (dynamic_cast<IntLit*>(this) != NULL);
488 }
489 inline bool
491 return (dynamic_cast<BoolLit*>(this) != NULL);
492 }
493 inline bool
495 return (dynamic_cast<FloatLit*>(this) != NULL);
496 }
497 inline bool
499 return (dynamic_cast<SetLit*>(this) != NULL);
500 }
501 inline bool
503 return (dynamic_cast<String*>(this) != NULL);
504 }
505 inline bool
507 return (dynamic_cast<Array*>(this) != NULL);
508 }
509 inline bool
511 return (dynamic_cast<Atom*>(this) != NULL);
512 }
513
514 inline Node*
516 if (Array* a = dynamic_cast<Array*>(n)) {
517 if (a->a.size() == 1) {
518 Node *ret = a->a[0];
519 a->a[0] = NULL;
520 delete a;
521 return ret;
522 }
523 }
524 return n;
525 }
526
527}}}
528
529#endif
530
531// STATISTICS: flatzinc-any
struct Gecode::@603::NNF::@65::@66 b
For binary nodes (and, or, eqv)
int n
Number of negative literals for node type.
struct Gecode::@603::NNF::@65::@67 a
For atomic nodes.
T * a
Element array.
Definition array.hpp:544
int size(void) const
Return size of array (number of elements)
Definition array.hpp:1607
Node representing an array access
Definition ast.hh:274
virtual void print(std::ostream &os)
Output string representation.
Definition ast.hh:281
ArrayAccess(Node *a0, Node *idx0)
Definition ast.hh:278
Array(const std::vector< Node * > &a0)
Definition ast.hh:234
virtual void print(std::ostream &os)
Output string representation.
Definition ast.hh:239
std::vector< Node * > a
Definition ast.hh:233
Node representing an atom
Definition ast.hh:290
Atom(const std::string &id0)
Definition ast.hh:293
virtual void print(std::ostream &os)
Output string representation.
Definition ast.hh:294
Boolean literal node.
Definition ast.hh:144
virtual void print(std::ostream &os)
Output string representation.
Definition ast.hh:148
Boolean variable node.
Definition ast.hh:197
BoolVar(int i0, const std::string &n0="")
Constructor.
Definition ast.hh:200
virtual void print(std::ostream &os)
Output string representation.
Definition ast.hh:201
Node representing a function call
Definition ast.hh:255
Call(const std::string &id0, Node *args0)
Definition ast.hh:259
virtual void print(std::ostream &os)
Output string representation.
Definition ast.hh:262
Array * getArgs(unsigned int n)
Definition ast.hh:265
Float literal node.
Definition ast.hh:162
virtual void print(std::ostream &os)
Output string representation.
Definition ast.hh:166
Float variable node.
Definition ast.hh:214
virtual void print(std::ostream &os)
Output string representation.
Definition ast.hh:217
FloatVar(int i0, const std::string &n0="")
Definition ast.hh:216
Integer literal node.
Definition ast.hh:153
virtual void print(std::ostream &os)
Output string representation.
Definition ast.hh:157
Integer variable node.
Definition ast.hh:206
virtual void print(std::ostream &os)
Output string representation.
Definition ast.hh:209
IntVar(int i0, const std::string &n0="")
Definition ast.hh:208
A node in a FlatZinc abstract syntax tree.
Definition ast.hh:67
bool isSetVar(void)
Test if node is a set variable node.
Definition ast.hh:478
Array * getArray(void)
Cast this node to an array node.
Definition ast.hh:396
bool isBool(void)
Test if node is a Boolean node.
Definition ast.hh:490
bool isFloatVar(void)
Test if node is a float variable node.
Definition ast.hh:482
bool isSet(void)
Test if node is a set literal node.
Definition ast.hh:498
double getFloat(void)
Cast this node to a Float node.
Definition ast.hh:452
bool hasCall(const std::string &id)
Test if node is function call or array containing function call id.
Definition ast.hh:350
int getFloatVar(void)
Cast this node to a Float variable node.
Definition ast.hh:428
bool isArray(void)
Test if node is an array node.
Definition ast.hh:506
void append(Node *n)
Append n to an array node.
Definition ast.hh:313
SetLit * getSet(void)
Cast this node to a set literal node.
Definition ast.hh:458
bool isFloat(void)
Test if node is a float node.
Definition ast.hh:494
bool isString(void)
Test if node is a string node.
Definition ast.hh:502
bool hasAtom(const std::string &id)
Test if node has atom with id.
Definition ast.hh:321
bool isInt(void)
Test if node is an integer node.
Definition ast.hh:486
bool isCall(const std::string &id)
Test if node is function call with id.
Definition ast.hh:334
virtual ~Node(void)
Destructor.
Definition ast.hh:310
int getInt(void)
Cast this node to an integer node.
Definition ast.hh:440
bool isAtom(void)
Test if node is an atom node.
Definition ast.hh:510
bool isIntVar(void)
Test if node is an integer variable node.
Definition ast.hh:470
Atom * getAtom(void)
Cast this node to an Atom node.
Definition ast.hh:403
int getIntVar(void)
Cast this node to an integer variable node.
Definition ast.hh:416
bool isBoolVar(void)
Test if node is a Boolean variable node.
Definition ast.hh:474
Call * getCall(void)
Return function call.
Definition ast.hh:343
std::string getVarName(void)
Return name of variable represented by this node.
Definition ast.hh:410
int getBoolVar(void)
Cast this node to a Boolean variable node.
Definition ast.hh:422
virtual void print(std::ostream &)=0
Output string representation.
int getSetVar(void)
Cast this node to a set variable node.
Definition ast.hh:434
bool getBool(void)
Cast this node to a Boolean node.
Definition ast.hh:446
std::string getString(void)
Cast this node to a string node.
Definition ast.hh:464
Set literal node
Definition ast.hh:171
bool empty(void) const
Definition ast.hh:180
SetLit(const std::vector< int > &s0)
Definition ast.hh:178
SetLit(int min0, int max0)
Definition ast.hh:177
virtual void print(std::ostream &os)
Output string representation.
Definition ast.hh:183
std::vector< int > s
Definition ast.hh:175
Set variable node
Definition ast.hh:222
SetVar(int i0, const std::string &n0="")
Definition ast.hh:224
virtual void print(std::ostream &os)
Output string representation.
Definition ast.hh:225
String(const std::string &s0)
Definition ast.hh:303
virtual void print(std::ostream &os)
Output string representation.
Definition ast.hh:304
Exception signaling type error
Definition ast.hh:55
TypeError(std::string what)
Definition ast.hh:60
std::string what(void) const
Definition ast.hh:61
Variable node base class.
Definition ast.hh:189
Var(int i0, const std::string &n0)
Constructor.
Definition ast.hh:194
Node * extractSingleton(Node *n)
Definition ast.hh:515
Gecode toplevel namespace
#define GECODE_VTABLE_EXPORT
Definition support.hh:72