[Gretl-devel] loop/if/loop bug fixed?

Allin Cottrell cottrell at wfu.edu
Mon Apr 7 22:22:08 EDT 2008


Hello all,

I discovered today (and fixed, I hope) a rather nasty bug that I 
suppose has been around for a long time, if not forever.  It arose 
when you have an loop which contains a series of inner, embedded 
loops, each executed conditionally.  Here is a trivial example:

<script>
nulldata 10
loop i=1..4 -q
  if i%2
    loop j=1..2 -q
      printf "inner loop 1: j = %d\n", j
    endloop
  else
    loop j=2..3 -q
      printf "inner loop 2: j = %d\n", j
    endloop
  endif
endloop
</script>

Up till today, "inner loop 1" would get executed each time round 
the outer loop (and "inner loop 2" would never get executed).  
The reason for this is that inner "child" loops were identified by 
their calling order (child 0, child 1, etc.) within the outer 
loop, on each iteration of the outer loop.  Given the 
conditionality in the script above, only one inner loop gets 
called on each iteration of the outer loop, so although both inner 
loops were correctly compiled and attached, only the first ("child 
0") got called.

The fix for this is to identify the "children" of an outer loop 
not simply by calling order, but by the line number in the outer 
loop at which they are called.  That is, when a "loop" command is 
found within a loop, we now do this:

* What line number are we on, in the outer loop? Call this "j".
* Look for a child loop that is connected to line j.
* Did we find one?  If yes, execute it; if not, it's a bug.

My apologies for this stupid logic error.  I think we should be OK 
now, but please report any fallout you detect.

Allin.



More information about the Gretl-devel mailing list