Fix memory leaks.
[pspp-builds.git] / src / loop.c
index 1989a0748a6964a10593e0639eda0b79993a6c74..5e3f16991393149b27e9f136a921c8cd0db1763f 100644 (file)
@@ -18,8 +18,9 @@
    02111-1307, USA. */
 
 #include <config.h>
-#include <assert.h>
+#include "error.h"
 #include "alloc.h"
+#include "case.h"
 #include "command.h"
 #include "do-ifP.h"
 #include "error.h"
@@ -32,7 +33,6 @@
 
 #include "debug-print.h"
 
-/* *INDENT-OFF* */
 /* LOOP strategy:
 
    Each loop causes 3 different transformations to be output.  The
    transformation to -1.  This ensures that the pass number is set to
    -1 every time the loop is encountered, before the first iteration.
 
-   The second transformation increments the pass number.  If there is
-   no indexing or test clause on either LOOP or END LOOP, then the
-   pass number is checked against MXLOOPS and control may pass out of
-   the loop; otherwise the indexing or test clause(s) on LOOP are
-   checked, and again control may pass out of the loop.
+   The second transformation increments the pass number.  If
+   there is no indexing or test clause on either LOOP or END
+   LOOP, then the pass number is checked against MXLOOPS and
+   control may pass out of the loop.  Otherwise the indexing or
+   test clause(s) on LOOP are checked, and again control may pass
+   out of the loop.
 
-   After the second transformation the body of the loop is executed.
+   After the second transformation the body of the loop is
+   executed.
 
    The last transformation checks the test clause if present and
-   either jumps back up to the second transformation or terminates the
-   loop.
-
-   Flow of control: (The characters ^V<> represents arrows.)
-
-     1. LOOP (sets pass # to -1)
-        V
-        V
-   >>2. LOOP (increment pass number)
-   ^         (test optional indexing clause)
-   ^         (test optional IF clause)
-   ^    if we need another trip     if we're done with the loop>>V
-   ^     V                                                       V
-   ^     V                                                         V
-   ^ *. execute loop body                                          V
-   ^    .                                                          V
-   ^    .   (any number of transformations)                        V
-   ^    .                                                          V
-   ^                                                             V
-   ^ 3. END LOOP (test optional IF clause)                         V
-   ^<<<<if we need another trip     if we're done with the loop>>V
-                                                                V
-                                                                V
-     *. transformations after loop body<<<<<<<<<<<<<<<<<<<<<<<<<<<
+   either jumps back up to the second transformation or
+   terminates the loop.
+
+   Flow of control:
+
+   1. LOOP.  Sets pass number to -1 and continues to next
+      transformation.
+
+   2. LOOP.  Increments pass number.  Tests optional indexing
+      clause and optional IF clause.  If we're done with the
+      loop, we jump to the transformation just after LOOP
+      transformation 3.
+
+      Otherwise, we continue through the transformations in the
+      loop body.
+
+   3. END LOOP.  We test the optional IF clause.  If we need to
+      make another pass through the loop, we jump to LOOP
+      transformation 2.
 
+      Otherwise, we continue with the transformation jump after
+      the loop.
  */
-/* *INDENT-ON* */
 
 /* Types of limits on loop execution. */
 enum
@@ -169,8 +167,6 @@ internal_cmd_loop (void)
   /* Name of indexing variable if applicable. */
   char name[9];
 
-  lex_match_id ("LOOP");
-
   /* Create and initialize transformations to facilitate
      error-handling. */
   two = xmalloc (sizeof *two);
@@ -203,7 +199,7 @@ internal_cmd_loop (void)
       assert (token == '=');
       lex_get ();
 
-      one->init = expr_parse (PXP_NUMERIC);
+      one->init = expr_parse (EXPR_NUMERIC);
       if (!one->init)
        return 0;
 
@@ -212,7 +208,7 @@ internal_cmd_loop (void)
          expr_free (one->init);
          return 0;
        }
-      one->term = expr_parse (PXP_NUMERIC);
+      one->term = expr_parse (EXPR_NUMERIC);
       if (!one->term)
        {
          expr_free (one->init);
@@ -221,7 +217,7 @@ internal_cmd_loop (void)
 
       if (lex_match (T_BY))
        {
-         one->incr = expr_parse (PXP_NUMERIC);
+         one->incr = expr_parse (EXPR_NUMERIC);
          if (!one->incr)
            return 0;
        }
@@ -234,7 +230,7 @@ internal_cmd_loop (void)
     {
       two->flags |= LPC_COND;
 
-      two->cond = expr_parse (PXP_BOOLEAN);
+      two->cond = expr_parse (EXPR_BOOLEAN);
       if (!two->cond)
        return 0;
     }
@@ -264,15 +260,6 @@ internal_cmd_loop (void)
   add_transformation ((struct trns_header *) one);
   add_transformation ((struct trns_header *) two);
 
-#if DEBUGGING
-  printf ("LOOP");
-  if (two->flags & LPC_INDEX)
-    printf ("(INDEX)");
-  if (two->flags & LPC_COND)
-    printf ("(IF)");
-  printf ("\n");
-#endif
-
   return 1;
 }
 
@@ -318,7 +305,7 @@ internal_cmd_end_loop (void)
   /* Parse the expression if any. */
   if (lex_match_id ("IF"))
     {
-      thr->cond = expr_parse (PXP_BOOLEAN);
+      thr->cond = expr_parse (EXPR_BOOLEAN);
       if (!thr->cond)
        return 0;
     }
@@ -333,13 +320,6 @@ internal_cmd_end_loop (void)
   /* Pop off the top of stack. */
   ctl_stack = ctl_stack->down;
 
-#if DEBUGGING
-  printf ("END LOOP");
-  if (thr->cond)
-    printf ("(IF)");
-  printf ("\n");
-#endif
-
   return 1;
 }
 
@@ -365,7 +345,7 @@ loop_1_trns_proc (struct trns_header * trns, struct ccase * c,
 
       /* Even if the loop is never entered, force the index variable
          to assume the initial value. */
-      c->data[two->index->fv].f = t1.f;
+      case_data_rw (c, two->index->fv)->f = t1.f;
 
       /* Throw out various pathological cases. */
       if (!finite (t1.f) || !finite (t2.f) || !finite (t3.f) || t2.f == 0.0)
@@ -421,7 +401,7 @@ loop_2_trns_proc (struct trns_header * trns, struct ccase * c,
   if (two->flags == 0)
     {
       two->pass++;
-      if (two->pass > set_mxloops)
+      if (two->pass > get_mxloops() )
          return two->loop_term;
     }
 
@@ -433,7 +413,7 @@ loop_2_trns_proc (struct trns_header * trns, struct ccase * c,
        return two->loop_term;
 
       /* Set the current value into the case. */
-      c->data[two->index->fv].f = two->curr;
+      case_data_rw (c, two->index->fv)->f = two->curr;
 
       /* Decrement the current value. */
       two->curr += two->incr;
@@ -446,7 +426,7 @@ loop_2_trns_proc (struct trns_header * trns, struct ccase * c,
        return two->loop_term;
 
       /* Set the current value into the case. */
-      c->data[two->index->fv].f = two->curr;
+      case_data_rw (c, two->index->fv)->f = two->curr;
 
       /* Increment the current value. */
       two->curr += two->incr;
@@ -505,8 +485,6 @@ cmd_break (void)
   /* New transformation. */
   struct break_trns *t;
 
-  lex_match_id ("BREAK");
-
   for (loop = ctl_stack; loop; loop = loop->down)
     if (loop->type == CST_LOOP)
       break;