Anyway, I see that this is an example, at least in theory. However, I'd probably do it differently in practice. I think this is more efficient, since you don't need any comparisons, and in particular no extra code in the innermost loop which is the most time-critical. If you need the original start values afterwards, copy the array before the first loop, of course.
for a1 := start[1] to max[1] do begin for a2 := start[2] to max[2] do begin
This is much nicer code than my version. However I would still choose to make the loop control variables a[1], a[2], a[3], etc., so that when I take the checkpoint the checkpointing procedure could use a loop like
for n := 1 to depth do writeln (checkfile, a[n]);
But surely regardless of any of this, even if the compiler refuses to allow a[1] (or much worse a[i]) as an index, any for loop can easily be rewritten in to a while loop a1 := start[1]; while a1 < max[1] do begin ... Inc(a1); end;
(possibly adding a temporary for the maximum if appropriate).
Personally, I don't see any particular reason to restrict the compiler to what can be used for an index variable unless there is some good reason for it. If it significantly simplifies the compiler to only allow index variables that are local simple variables, that's fine. But otherwise, why worry about it, as long as the semantics are clearly spelled out and self consistent, then if the user wants to shoot themselves in the foot with weird uses, that's their own fault. I would suggest implementing a for loop as:
for index := lower to upper do begin whatever; end;
exactly equivalent to:
temp_indexptr := @index; temp_indexptr^ := lower; temp_upper := upper while temp_indexptr^ <= temp_upper do begin whatever; Inc(temp_indexptr^); end;
(expanding for the other options of for loops as required).
Then change the documentation from:
Please note: A modification of the index variable may result in unpredictable action.
to
Please note: A modification of the index variable, or any subpart of it, may result in unpredictable action.
or words to that effect.
Enjoy, Peter.