Generated on for Gecode by doxygen 1.18.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.dev>
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.dev
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
46
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);
79 virtual void release(void);
80
82 void append(Node* n);
83
85 bool hasAtom(const std::string& id);
87 bool isInt(int& i);
89 bool isFloat(double& i);
91 bool isCall(const std::string& id);
93 Call* getCall(void);
95 bool hasCall(const std::string& id);
97 Call* getCall(const std::string& id);
99 Array* getArray(void);
101 Atom* getAtom(void);
103 std::string getVarName(void);
105 int getIntVar(void);
107 int getBoolVar(void);
109 int getFloatVar(void);
111 int getSetVar(void);
112
114 int getInt(void);
116 bool getBool(void);
118 double getFloat(void);
120 SetLit *getSet(void);
121
123 std::string getString(void);
124
126 bool isIntVar(void);
128 bool isBoolVar(void);
130 bool isSetVar(void);
132 bool isFloatVar(void);
134 bool isInt(void);
136 bool isFloat(void);
138 bool isBool(void);
140 bool isString(void);
142 bool isArray(void);
144 bool isSet(void);
146 bool isAtom(void);
147
149 virtual void print(std::ostream&) = 0;
150 };
151
154 public:
155 bool b;
156 BoolLit(bool b0) : b(b0) {}
157 virtual void print(std::ostream& os) {
158 os << "b(" << (b ? "true" : "false") << ")";
159 }
160 };
161
163 public:
164 int i;
165 IntLit(int i0) : i(i0) {}
166 virtual void print(std::ostream& os) {
167 os << "i("<<i<<")";
168 }
169 };
170
172 public:
173 double d;
174 FloatLit(double d0) : d(d0) {}
175 virtual void print(std::ostream& os) {
176 os << "f("<<d<<")";
177 }
178 };
179
181 public:
183 int min; int max;
184 std::vector<int> s;
185 SetLit(void) {}
186 SetLit(int min0, int max0) : interval(true), min(min0), max(max0) {}
187 SetLit(const std::vector<int>& s0) : interval(false), s(s0) {}
188 explicit SetLit(SetLit* s0) : interval(s0->interval), min(s0->min), max(s0->max), s(s0->s) {}
189 bool empty(void) const {
190 return ( (interval && min>max) || (!interval && s.size() == 0));
191 }
192 virtual void print(std::ostream& os) {
193 os << "s()";
194 }
195 };
196
199 public:
200 int i; //< Index
201 std::string n; //< Name
203 Var(int i0, const std::string& n0) : i(i0), n(n0) {}
204 };
205
207 public:
209 BoolVar(int i0, const std::string& n0="") : Var(i0,n0) {}
210 virtual void print(std::ostream& os) {
211 os << "xb("<<i<<")";
212 }
213 };
214
216 public:
217 IntVar(int i0, const std::string& n0="") : Var(i0,n0) {}
218 virtual void print(std::ostream& os) {
219 os << "xi("<<i<<")";
220 }
221 };
222
224 public:
225 FloatVar(int i0, const std::string& n0="") : Var(i0,n0) {}
226 virtual void print(std::ostream& os) {
227 os << "xf("<<i<<")";
228 }
229 };
230
232 public:
233 SetVar(int i0, const std::string& n0="") : Var(i0,n0) {}
234 virtual void print(std::ostream& os) {
235 os << "xs("<<i<<")";
236 }
237 };
238
241 public:
242 std::vector<Node*> a;
243 Array(const std::vector<Node*>& a0)
244 : a(a0) {}
246 : a(1) { a[0] = n; }
247 Array(int n=0) : a(n) {}
248 virtual void print(std::ostream& os) {
249 os << "[";
250 for (unsigned int i=0; i<a.size(); i++) {
251 a[i]->print(os);
252 if (i<a.size()-1)
253 os << ", ";
254 }
255 os << "]";
256 }
257 ~Array(void);
258 };
259
271 protected:
273 unsigned int refs;
274 public:
276 SharedArray(int n) : Array(n), refs(1) {}
278 SharedArray* share(void) { refs++; return this; }
280 virtual void release(void) {
281 if (--refs == 0)
282 delete this;
283 }
284 };
285
287 inline void
289 if (n != nullptr)
290 n->release();
291 }
292
295 public:
296 std::string id;
298 Call(const std::string& id0, Node* args0)
299 : id(id0), args(args0) {}
301 virtual void print(std::ostream& os) {
302 os << id << "("; args->print(os); os << ")";
303 }
304 Array* getArgs(unsigned int n) {
305 Array *a = args->getArray();
306 if (a->a.size() != n)
307 throw TypeError("arity mismatch");
308 return a;
309 }
310 };
311
314 public:
318 : a(a0), idx(idx0) {}
320 virtual void print(std::ostream& os) {
321 a->print(os);
322 os << "[";
323 idx->print(os);
324 os << "]";
325 }
326 };
327
330 public:
331 std::string id;
332 Atom(const std::string& id0) : id(id0) {}
333 virtual void print(std::ostream& os) {
334 os << id;
335 }
336 };
337
340 public:
341 std::string s;
342 String(const std::string& s0) : s(s0) {}
343 virtual void print(std::ostream& os) {
344 os << "s(\"" << s << "\")";
345 }
346 };
347
348 inline
349 Node::~Node(void) {}
350
351 inline void
353 delete this;
354 }
355
356 inline
358 for (int i=a.size(); i--;)
359 release_node(a[i]);
360 }
361
362 inline void
363 Node::append(Node* newNode) {
364 Array* a = dynamic_cast<Array*>(this);
365 if (!a)
366 throw TypeError("array expected");
367 a->a.push_back(newNode);
368 }
369
370 inline bool
371 Node::hasAtom(const std::string& id) {
372 if (Array* a = dynamic_cast<Array*>(this)) {
373 for (int i=a->a.size(); i--;)
374 if (Atom* at = dynamic_cast<Atom*>(a->a[i]))
375 if (at->id == id)
376 return true;
377 } else if (Atom* a = dynamic_cast<Atom*>(this)) {
378 return a->id == id;
379 }
380 return false;
381 }
382
383 inline bool
384 Node::isCall(const std::string& id) {
385 if (Call* a = dynamic_cast<Call*>(this)) {
386 if (a->id == id)
387 return true;
388 }
389 return false;
390 }
391
392 inline Call*
394 if (Call* a = dynamic_cast<Call*>(this))
395 return a;
396 throw TypeError("call expected");
397 }
398
399 inline bool
400 Node::hasCall(const std::string& id) {
401 if (Array* a = dynamic_cast<Array*>(this)) {
402 for (int i=a->a.size(); i--;)
403 if (Call* at = dynamic_cast<Call*>(a->a[i]))
404 if (at->id == id) {
405 return true;
406 }
407 } else if (Call* a = dynamic_cast<Call*>(this)) {
408 return a->id == id;
409 }
410 return false;
411 }
412
413 inline bool
414 Node::isInt(int& i) {
415 if (IntLit* il = dynamic_cast<IntLit*>(this)) {
416 i = il->i;
417 return true;
418 }
419 return false;
420 }
421
422 inline bool
423 Node::isFloat(double& d) {
424 if (FloatLit* fl = dynamic_cast<FloatLit*>(this)) {
425 d = fl->d;
426 return true;
427 }
428 return false;
429 }
430
431 inline Call*
432 Node::getCall(const std::string& id) {
433 if (Array* a = dynamic_cast<Array*>(this)) {
434 for (int i=a->a.size(); i--;)
435 if (Call* at = dynamic_cast<Call*>(a->a[i]))
436 if (at->id == id)
437 return at;
438 } else if (Call* a = dynamic_cast<Call*>(this)) {
439 if (a->id == id)
440 return a;
441 }
442 throw TypeError("call expected");
443 }
444
445 inline Array*
447 if (Array* a = dynamic_cast<Array*>(this))
448 return a;
449 throw TypeError("array expected");
450 }
451
452 inline Atom*
454 if (Atom* a = dynamic_cast<Atom*>(this))
455 return a;
456 throw TypeError("atom expected");
457 }
458
459 inline std::string
461 if (Var* a = dynamic_cast<Var*>(this))
462 return a->n;
463 throw TypeError("variable expected");
464 }
465 inline int
467 if (IntVar* a = dynamic_cast<IntVar*>(this))
468 return a->i;
469 throw TypeError("integer variable expected");
470 }
471 inline int
473 if (BoolVar* a = dynamic_cast<BoolVar*>(this))
474 return a->i;
475 throw TypeError("bool variable expected");
476 }
477 inline int
479 if (FloatVar* a = dynamic_cast<FloatVar*>(this))
480 return a->i;
481 throw TypeError("integer variable expected");
482 }
483 inline int
485 if (SetVar* a = dynamic_cast<SetVar*>(this))
486 return a->i;
487 throw TypeError("set variable expected");
488 }
489 inline int
491 if (IntLit* a = dynamic_cast<IntLit*>(this))
492 return a->i;
493 throw TypeError("integer literal expected");
494 }
495 inline bool
497 if (BoolLit* a = dynamic_cast<BoolLit*>(this))
498 return a->b;
499 throw TypeError("bool literal expected");
500 }
501 inline double
503 if (FloatLit* a = dynamic_cast<FloatLit*>(this))
504 return a->d;
505 throw TypeError("float literal expected");
506 }
507 inline SetLit*
509 if (SetLit* a = dynamic_cast<SetLit*>(this))
510 return a;
511 throw TypeError("set literal expected");
512 }
513 inline std::string
515 if (String* a = dynamic_cast<String*>(this))
516 return a->s;
517 throw TypeError("string literal expected");
518 }
519 inline bool
521 return (dynamic_cast<IntVar*>(this) != nullptr);
522 }
523 inline bool
525 return (dynamic_cast<BoolVar*>(this) != nullptr);
526 }
527 inline bool
529 return (dynamic_cast<SetVar*>(this) != nullptr);
530 }
531 inline bool
533 return (dynamic_cast<FloatVar*>(this) != nullptr);
534 }
535 inline bool
537 return (dynamic_cast<IntLit*>(this) != nullptr);
538 }
539 inline bool
541 return (dynamic_cast<BoolLit*>(this) != nullptr);
542 }
543 inline bool
545 return (dynamic_cast<FloatLit*>(this) != nullptr);
546 }
547 inline bool
549 return (dynamic_cast<SetLit*>(this) != nullptr);
550 }
551 inline bool
553 return (dynamic_cast<String*>(this) != nullptr);
554 }
555 inline bool
557 return (dynamic_cast<Array*>(this) != nullptr);
558 }
559 inline bool
561 return (dynamic_cast<Atom*>(this) != nullptr);
562 }
563
564 inline Node*
566 // A shared array belongs to other owners too, so it is left intact.
567 if (dynamic_cast<SharedArray*>(n) != nullptr)
568 return n;
569 if (Array* a = dynamic_cast<Array*>(n)) {
570 if (a->a.size() == 1) {
571 Node *ret = a->a[0];
572 a->a[0] = nullptr;
573 delete a;
574 return ret;
575 }
576 }
577 return n;
578 }
579
580}}}
581
582#endif
583
584// STATISTICS: flatzinc-any
virtual void print(std::ostream &os)
Output string representation.
Definition ast.hh:320
ArrayAccess(Node *a0, Node *idx0)
Definition ast.hh:317
Array(const std::vector< Node * > &a0)
Definition ast.hh:243
virtual void print(std::ostream &os)
Output string representation.
Definition ast.hh:248
std::vector< Node * > a
Definition ast.hh:242
Node representing an atom
Definition ast.hh:329
Atom(const std::string &id0)
Definition ast.hh:332
virtual void print(std::ostream &os)
Output string representation.
Definition ast.hh:333
Boolean literal node.
Definition ast.hh:153
virtual void print(std::ostream &os)
Output string representation.
Definition ast.hh:157
Boolean variable node.
Definition ast.hh:206
BoolVar(int i0, const std::string &n0="")
Constructor.
Definition ast.hh:209
virtual void print(std::ostream &os)
Output string representation.
Definition ast.hh:210
Node representing a function call
Definition ast.hh:294
Call(const std::string &id0, Node *args0)
Definition ast.hh:298
virtual void print(std::ostream &os)
Output string representation.
Definition ast.hh:301
Array * getArgs(unsigned int n)
Definition ast.hh:304
Float literal node.
Definition ast.hh:171
virtual void print(std::ostream &os)
Output string representation.
Definition ast.hh:175
Float variable node.
Definition ast.hh:223
virtual void print(std::ostream &os)
Output string representation.
Definition ast.hh:226
FloatVar(int i0, const std::string &n0="")
Definition ast.hh:225
Integer literal node.
Definition ast.hh:162
virtual void print(std::ostream &os)
Output string representation.
Definition ast.hh:166
Integer variable node.
Definition ast.hh:215
virtual void print(std::ostream &os)
Output string representation.
Definition ast.hh:218
IntVar(int i0, const std::string &n0="")
Definition ast.hh:217
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:528
virtual void release(void)
Give up one owner's hold on this node.
Definition ast.hh:352
Array * getArray(void)
Cast this node to an array node.
Definition ast.hh:446
bool isBool(void)
Test if node is a Boolean node.
Definition ast.hh:540
bool isFloatVar(void)
Test if node is a float variable node.
Definition ast.hh:532
bool isSet(void)
Test if node is a set literal node.
Definition ast.hh:548
double getFloat(void)
Cast this node to a Float node.
Definition ast.hh:502
bool hasCall(const std::string &id)
Test if node is function call or array containing function call id.
Definition ast.hh:400
int getFloatVar(void)
Cast this node to a Float variable node.
Definition ast.hh:478
bool isArray(void)
Test if node is an array node.
Definition ast.hh:556
void append(Node *n)
Append n to an array node.
Definition ast.hh:363
SetLit * getSet(void)
Cast this node to a set literal node.
Definition ast.hh:508
bool isFloat(void)
Test if node is a float node.
Definition ast.hh:544
bool isString(void)
Test if node is a string node.
Definition ast.hh:552
bool hasAtom(const std::string &id)
Test if node has atom with id.
Definition ast.hh:371
bool isInt(void)
Test if node is an integer node.
Definition ast.hh:536
bool isCall(const std::string &id)
Test if node is function call with id.
Definition ast.hh:384
bool isFloat(double &i)
Test if node is float, if yes set d to the value.
Definition ast.hh:423
virtual ~Node(void)
Destructor.
Definition ast.hh:349
bool isInt(int &i)
Test if node is int, if yes set i to the value.
Definition ast.hh:414
int getInt(void)
Cast this node to an integer node.
Definition ast.hh:490
bool isAtom(void)
Test if node is an atom node.
Definition ast.hh:560
bool isIntVar(void)
Test if node is an integer variable node.
Definition ast.hh:520
Atom * getAtom(void)
Cast this node to an Atom node.
Definition ast.hh:453
int getIntVar(void)
Cast this node to an integer variable node.
Definition ast.hh:466
bool isBoolVar(void)
Test if node is a Boolean variable node.
Definition ast.hh:524
Call * getCall(void)
Return function call.
Definition ast.hh:393
std::string getVarName(void)
Return name of variable represented by this node.
Definition ast.hh:460
int getBoolVar(void)
Cast this node to a Boolean variable node.
Definition ast.hh:472
virtual void print(std::ostream &)=0
Output string representation.
int getSetVar(void)
Cast this node to a set variable node.
Definition ast.hh:484
bool getBool(void)
Cast this node to a Boolean node.
Definition ast.hh:496
std::string getString(void)
Cast this node to a string node.
Definition ast.hh:514
Set literal node
Definition ast.hh:180
bool empty(void) const
Definition ast.hh:189
SetLit(const std::vector< int > &s0)
Definition ast.hh:187
SetLit(int min0, int max0)
Definition ast.hh:186
virtual void print(std::ostream &os)
Output string representation.
Definition ast.hh:192
std::vector< int > s
Definition ast.hh:184
Set variable node
Definition ast.hh:231
SetVar(int i0, const std::string &n0="")
Definition ast.hh:233
virtual void print(std::ostream &os)
Output string representation.
Definition ast.hh:234
Array node shared by all references to an array parameter
Definition ast.hh:270
unsigned int refs
Number of owners.
Definition ast.hh:273
SharedArray(int n)
Construct with n elements and one owner.
Definition ast.hh:276
virtual void release(void)
Remove an owner, deleting the node when none is left.
Definition ast.hh:280
SharedArray * share(void)
Add an owner and return this node.
Definition ast.hh:278
String(const std::string &s0)
Definition ast.hh:342
virtual void print(std::ostream &os)
Output string representation.
Definition ast.hh:343
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:198
Var(int i0, const std::string &n0)
Constructor.
Definition ast.hh:203
Abstract syntax trees for the FlatZinc interpreter.
Node * extractSingleton(Node *n)
Definition ast.hh:565
void release_node(Node *n)
Release n if it is not null (see Node::release).
Definition ast.hh:288
Interpreter for the FlatZinc language.
Gecode toplevel namespace
#define GECODE_VTABLE_EXPORT
Definition support.hh:76