46 Seq<const Stmt*> repeats;
55 Seq<const Stmt*> repeats;
73 for (
int k=0; k<
a.
size(); k++)
82 for (
const Stmt* x : rs)
95 whole_array(
const Expr& e,
int k) {
98 return (e.kind ==
Expr::Kind::CALL) && ((e.s ==
"size") || (e.s ==
"defined"));
111 explicit Resolver(
const std::string& o)
112 : origin(o), stmt(nullptr) {}
123 Seq<Definition> definitions;
127 Seq<const Stmt*> repeats;
133 [[noreturn]]
void fail(
const std::string& m)
const {
134 fail_at(origin, stmt->line, m);
137 void define(
const std::string& n) {
138 if (n.empty() || (n ==
"_"))
143 definitions.push(std::move(d));
146 void define(
const Pattern& p) {
147 for (
const Pattern::Item& it : p.items)
152 void define(
const Block& b) {
153 for (
const std::unique_ptr<Stmt>& s : b) {
164 if (!s->list.empty() || !s->each.empty()) {
168 lists.push(std::move(l));
170 repeats.push(s.get());
176 for (
const Arm& a : s->arms) {
194 bool names_records(
const std::string& n)
const {
195 for (
const List& l : lists)
196 if ((l.repeat->list == n) || (l.repeat->each == n))
202 void check_name(
const std::string& n,
int i)
const {
203 const Stmt*
r = lists[
i].repeat;
206 for (
int j=0; j<
i; j++)
207 if ((lists[j].repeat->list == n) || (lists[j].repeat->each == n))
208 fail_at(origin,
r->line,
"'" + n +
"' already names records");
209 for (
const Definition& d : definitions)
211 fail_at(origin,
r->line,
"'" + n +
"' names records, so it cannot also be defined");
215 void check_lists(
void)
const {
216 for (
int i=0;
i<lists.
size();
i++) {
217 const Stmt*
r = lists[
i].repeat;
218 check_name(
r->list, i);
219 if (
r->each ==
r->list)
220 fail_at(origin,
r->line,
"'" +
r->each +
"' cannot name both the list and each record");
221 check_name(
r->each, i);
225 const List* find_list(
const std::string& n)
const {
226 for (
const List& l : lists)
227 if (l.repeat->list == n)
232 const List* find_each(
const std::string& n)
const {
233 for (
const List& l : lists)
234 if (l.repeat->each == n)
239 static const std::string& records_name(
const Stmt* r) {
240 return r->list.empty() ?
r->each :
r->list;
243 bool is_bound(
const std::string& n)
const {
244 for (
const Bound& b : bound)
251 const List* nested_list(
const Stmt* r,
const std::string& n)
const {
252 const List* m = find_list(n);
253 if ((m ==
nullptr) || !contains(m->repeats, r))
255 if (m->repeats.back() != r)
256 fail(
"'" + n +
"' is inside a repeat without a list name in the records of '" + records_name(r) +
257 "'; name that list with 'as' to reach it");
258 if (contains(repeats, m->repeat))
259 fail(
"'" + n +
"' cannot be used inside its own repeat");
263 std::string fields(
const Stmt* r)
const {
265 for (
const Definition& d : definitions)
266 if (contains(
d.repeats, r))
268 for (
const List& l : lists)
269 if (!l.repeats.empty() && (l.repeats.back() == r) && !l.repeat->list.empty())
270 f.push(l.repeat->list);
272 for (
int i=0;
i<
f.size();
i++) {
274 for (
int j=0; j<
i; j++)
275 seen = seen || (f[j] == f[i]);
277 s += (s.empty() ?
"" :
", ") +
f[
i];
283 std::string example(
const Stmt* r,
bool list)
const {
284 std::string
f = fields(r);
285 size_t c =
f.find(
',');
286 std::string x = (list ||
r->each.empty()) ?
r->list +
"[0]" :
r->each;
287 return f.empty() ? x : x +
"." + ((
c == std::string::npos) ?
f :
f.substr(0, c));
290 static int position(
const Seq<const Stmt*>& rs,
const Stmt* r) {
291 for (
int k=0; k<rs.size(); k++)
299 bool incomplete(
const Definition& d,
const Stmt* r)
const {
301 while ((common <
d.repeats.
size()) && (common < repeats.size()) && (
d.repeats[common] == repeats[common]))
303 return common >
position(
d.repeats, r) + 1;
306 void require_field(
const Stmt* r,
const std::string& f)
const {
307 bool inside =
false, outside =
false;
308 for (
const Definition& d : definitions)
310 inside = inside || contains(
d.repeats, r);
311 outside = outside || !contains(
d.repeats, r);
314 std::string known = fields(r);
315 fail(
"the records of '" + records_name(r) +
"' have no field '" + f +
"'" +
316 (known.empty() ? std::string(
" (they have none)") :
" (they have " + known +
")"));
319 fail(
"'" + f +
"' is defined inside and outside the repeat of '" + records_name(r) +
320 "', so it cannot be a field of its records");
321 for (
const Definition& d : definitions)
322 if ((
d.name == f) && incomplete(d, r))
323 fail(
"the field '" + f +
"' cannot be used inside the repeat that reads it");
326 void require_value(
const Expr& e)
const {
327 const Stmt*
r = e.records;
331 fail(
"a record of '" + records_name(r) +
"' is not a value; use one of its fields, as in '" +
332 example(r,
false) +
"'");
333 fail(
"'" +
r->list +
"' is a list of records, not a value; use size(" +
r->list +
334 "), an index, or a field of a record, as in '" + example(r,
true) +
"'");
338 void visit(
Block& b) {
339 for (std::unique_ptr<Stmt>& s : b) {
343 repeats.push(s.get());
348 for (Arm& a : s->arms)
353 void statement(Stmt& s) {
357 for (Arm& a : s.arms)
359 for (std::unique_ptr<Expr>& d : s.dims)
378 void pattern(Pattern& p) {
379 for (Pattern::Item& it : p.items)
384 bool lifting(
void)
const {
389 void check_chain(
const Stmt& s)
const {
391 for (
int i=1;
i<s.records.
size();
i++)
392 if (s.records[i].repeats.size() > s.records[deepest].repeats.size())
394 for (
int i=0;
i<s.records.
size();
i++)
395 if (!is_prefix(s.records[i].repeats, s.records[deepest].repeats))
396 fail(
"a check can only range over the records of one repeat, but '" +
397 s.records[std::min(i, deepest)].label +
"' and '" + s.records[std::max(i, deepest)].label +
398 "' are defined in different repeats");
402 void value(Expr& e) {
407 void resolve(Expr& e,
bool whole) {
425 for (std::unique_ptr<Expr>& a : e.args)
431 void name(Expr& e,
bool whole) {
432 for (
int k=bound.size(); k--; )
433 if (bound[k].name == e.s) {
434 e.records = bound[k].records;
437 if (
const List* l = find_list(e.s))
439 else if (
const List* r = find_each(e.s))
441 else if (lifting() && !whole)
442 e.record = record(e.s);
445 Seq<const Stmt*> beyond(
const Seq<const Stmt*>& d)
const {
447 while ((common <
d.
size()) && (common < repeats.size()) && (d[common] == repeats[common]))
450 for (
int k=common; k<
d.
size(); k++)
455 void list(Expr& e,
const List& l) {
456 e.records = l.repeat;
458 if (contains(repeats, l.repeat))
459 fail(
"'" + e.s +
"' cannot be used inside its own repeat");
460 if (beyond(l.repeats).empty())
463 const std::string& outer = l.repeats.back()->each;
464 fail(
"'" + e.s +
"' is a list in each record of the repeat around it; " +
465 (outer.empty() ?
"name each of these records with 'each' and use its field '" + e.s +
"'"
466 :
"use '" + outer +
"." + e.s +
"'"));
474 void record_name(Expr& e,
const List& l) {
475 e.records = l.repeat;
476 if (contains(repeats, l.repeat))
479 fail(
"'" + e.s +
"' is each record of a repeat, so only checks can use it outside that repeat");
480 Seq<const Stmt*> o = beyond(l.repeats);
485 void field(Expr& e) {
486 Expr&
base = *e.args[0];
488 const Stmt*
r =
base.records;
489 const std::string&
f = e.s;
491 fail(
"only records have fields such as '." + f +
"'; to name each record of a repeat, write "
492 "'repeat n as nodes { each node ... }' and use 'node." + f +
"' or 'nodes[0]." + f +
"'");
494 fail(
"'" +
r->list +
"' is a list of records, so '." + f +
"' needs one of them: use '" +
r->list +
"[i]." +
495 f +
"', or " + (
r->each.empty() ?
"name each record with 'each' in the repeat and use its field"
496 :
"'" +
r->each +
"." + f +
"' for each record"));
497 if (
const List* m = nested_list(r, f)) {
498 e.records = m->repeat;
505 void index(Expr& e) {
506 Expr&
a = *e.args[0];
509 if (
a.records ==
nullptr)
512 fail(
"a record of '" + records_name(
a.records) +
"' cannot be indexed; use one of its fields, as in '" +
513 example(
a.records,
false) +
"'");
514 e.records =
a.records;
517 const Stmt* elements(
const Expr& a)
const {
518 if (
a.records !=
nullptr)
521 return outermost(
a.s,
a.args[0]->records);
524 return outermost(
a.s,
nullptr);
533 const Stmt* outermost(
const std::string& n,
const Stmt* r)
const {
534 const Stmt* o =
nullptr;
536 for (
const Definition& d : definitions)
538 int k = (
r ==
nullptr) ?
d.repeats.
size() - beyond(
d.repeats).size() :
position(
d.repeats, r) + 1;
539 const Stmt* x = (k <
d.repeats.
size()) ?
d.repeats[k] :
nullptr;
540 if (found && (x != o))
548 void comprehension(Expr& e) {
549 if (names_records(e.var))
550 fail(
"'" + e.var +
"' is bound by a comprehension but names records");
551 Expr&
a = *e.args[0];
553 if ((
a.records !=
nullptr) && (
a.open == 0))
554 fail(
"a record of '" + records_name(
a.records) +
"' is not a list; use one of its fields");
555 e.elements = elements(a);
558 b.records =
a.records;
565 for (
int k=0; k<e.args.size(); k++) {
566 Expr&
a = *e.args[k];
568 bool counted = (e.s ==
"size") || (e.s ==
"count") || (e.s ==
"defined");
569 if (!counted || (
a.open == 0))
581 int record(
const std::string& n) {
583 Seq<const Stmt*> outside;
584 for (
const Definition& d : definitions) {
587 Seq<const Stmt*> o = beyond(
d.repeats);
588 if (found && !(is_prefix(o, outside) && is_prefix(outside, o)))
589 fail(
"'" + n +
"' is defined in several repeats, or inside and outside a repeat, so "
590 "a check here cannot range over its records");
591 outside = std::move(o);
596 return lift(n, outside, n);
601 int lift(
const std::string& n,
const Seq<const Stmt*>& rs,
const std::string& label) {
602 Seq<Record>&
records = stmt->records;
604 if ((records[i].name == n) && (records[i].repeats.size() == rs.size()))
620 Resolver(origin).
run(b);
int size(void) const
Return size of array (number of elements).
FloatNum size(void) const
Return size of float value (distance between maximum and minimum).
Growable array on the Gecode heap.
unsigned int size(void) const
Return size (cardinality) of set.
virtual bool run(void)
Run the test.
Reading instance files described by reader specs.
void resolve(Block &b, const std::string &origin)
Resolve the names in the expressions of b, the body of a spec from origin.
std::string record_starts(int id)
Return the name of the record starts of the repeat numbered id.
void fail_at(const std::string &origin, int line, const std::string &m)
Throw an error with message m for line line of the spec from origin.
Seq< std::unique_ptr< Stmt > > Block
Sequence of statements.
const double base
Base for geometric restart sequence.
Gecode toplevel namespace
Gecode::FloatVal b(9, 12)
Gecode::FloatVal a(-8, 5)
Case records("Read::Records", [](void) { const char *rows="n: int repeat n { list row: int until eol } width: int\n" "check sum(row) + count(row) - 1 <= width \"the clues do not fit\"";CHECK(read(rows, "2\n1 1\n2\n3").size("row")==2);CHECK(fails(rows, "2\n1 1\n 2 2\n3", "test.txt:3:3: check failed for record 2 of 'row': the clues do not fit"));CHECK(read(rows, "2\n1 1\n2\n3").entries()==3);const char *windows="n: int repeat n { ready: int due: int } check ready <= due \"window\"";CHECK(read(windows, "2 3 5 0 1").size("ready")==2);CHECK(fails(windows, "3 0 5 2 4 6 1", "check failed for record 3 of 'ready': window"));const char *jobs="j: int m: int repeat j { k: int repeat m { x: int } } check x < k \"x below k\"";CHECK(read(jobs, "2 2\n5 1 2\n3 1 2\n").size("x")==2);CHECK(fails(jobs, "2 2\n5 1 2\n3 1 4\n", "test.txt:3:5: check failed for record 2 of record 2 of 'x': x below k"));Data d=read("n: int repeat n { list row: int until eol }\n" "check size(row) == 2 && count(row) == 3 && sum(r in row: sum(r)) == 21\n" "check count(r in row: count(r) == 3) == size(row) && defined(row)", "2\n1 2 3\n4 5 6\n");CHECK(d.size("row")==2);CHECK(fails("n: int lim[2]: int repeat n { x: int } check all(l in lim: x < l) \"below\"", "2 5 3 1 4", "check failed for element 2 of 'lim' in record 2 of 'x': below"));const char *optional="sections { \"A\" optional { repeat 2 { x: int } } \"B\" optional } check !defined(x) || x > 0 \"positive\"";CHECK(!read(optional, "B").has("x"));CHECK(read(optional, "A\n1 2").size("x")==2);CHECK(fails(optional, "A\n1 0", "check failed for record 2 of 'x': positive"));CHECK(fails("n: int repeat n { list row: int until eol check count(row) == 2 \"two\" }", "2\n1 2\n3\n", "check failed: two"));CHECK(fails("n: int repeat n { list row: int until eol check count(row) == 2 \"two\" }", "2\n1 2\n3\n", "while reading: repeat [2 of 2]"));CHECK(read("repeat 2 { repeat 2 { x: int } check size(x) == 2 && x > 0 }", "1 2 3 4").size("x")==2);CHECK(fails("repeat 2 { repeat 2 { x: int } check x > 0 \"positive\" }", "1 2 3 0", "check failed for record 2 of 'x': positive"));Data l=read("n: int repeat n { x: int } let s = sum(x) let f = x[0] if count(x) == 2 { let t = 1 }", "2 3 4");CHECK(l.integer("s")==7);CHECK(l.integer("f")==3);CHECK(l.has("t"));CHECK(read("n: int repeat n { x: int } check x > 0 && 1 > 2", "0").size("x")==0);return true;})
Gecode::IntArgs i({1, 2, 3, 4})
double position(const Space &home, IntVar x, int i)