Make the expression code a little nicer and fix bugs found
[pspp-builds.git] / src / inpt-pgm.c
index 144aceb51caa777aca723edcd724e6e9a65a71f5..69f5b0483db015877d4942ef5ec15196cbed4763 100644 (file)
@@ -18,7 +18,7 @@
    02111-1307, USA. */
 
 #include <config.h>
-#include <assert.h>
+#include "error.h"
 #include <float.h>
 #include <stdlib.h>
 #include "alloc.h"
@@ -46,26 +46,25 @@ enum value_init_type
     INP_REINIT = 0,            /* Reinitialize for each iteration. */
   };
 
-/* Array that tells INPUT PROGRAM how to initialize each `union
-   value'.  */
-static enum value_init_type *inp_init;
-
-/* Number of bytes allocated for inp_init. */
-static size_t inp_nval;
+struct input_program_pgm 
+  {
+    enum value_init_type *init; /* How to initialize each `union value'. */
+    size_t init_cnt;            /* Number of elements in inp_init. */
+    size_t case_size;           /* Size of case in bytes. */
+  };
 
-static int end_case_trns_proc (struct trns_header *, struct ccase *);
-static int end_file_trns_proc (struct trns_header * t, struct ccase * c);
-static int reread_trns_proc (struct trns_header *, struct ccase *);
-static void reread_trns_free (struct trns_header *);
+static trns_proc_func end_case_trns_proc, reread_trns_proc, end_file_trns_proc;
+static trns_free_func reread_trns_free;
 
 int
 cmd_input_program (void)
 {
-  lex_match_id ("INPUT");
-  lex_match_id ("PROGRAM");
   discard_variables ();
 
-  vfm_source = &input_program_source;
+  /* FIXME: we shouldn't do this here, but I'm afraid that other
+     code will check the class of vfm_source. */
+  vfm_source = create_case_source (&input_program_source_class,
+                                   default_dict, NULL);
 
   return lex_end_of_command ();
 }
@@ -73,31 +72,29 @@ cmd_input_program (void)
 int
 cmd_end_input_program (void)
 {
+  struct input_program_pgm *inp;
   size_t i;
 
-  lex_match_id ("END");
-  lex_match_id ("INPUT");
-  lex_match_id ("PROGRAM");
-
-  if (vfm_source != &input_program_source)
+  if (!case_source_is_class (vfm_source, &input_program_source_class))
     {
       msg (SE, _("No matching INPUT PROGRAM command."));
       return CMD_FAILURE;
     }
   
-  if (dict_get_value_cnt (default_dict) == 0)
+  if (dict_get_next_value_idx (default_dict) == 0)
     msg (SW, _("No data-input or transformation commands specified "
         "between INPUT PROGRAM and END INPUT PROGRAM."));
 
-  /* Mark the boundary between INPUT PROGRAM and more-mundane
-     transformations. */
+  /* Mark the boundary between INPUT PROGRAM transformations and
+     ordinary transformations. */
   f_trns = n_trns;
 
-  /* Figure out how to initialize temp_case. */
-  inp_nval = dict_get_value_cnt (default_dict);
-  inp_init = xmalloc (inp_nval * sizeof *inp_init);
-  for (i = 0; i < inp_nval; i++)
-    inp_init[i] = -1;
+  /* Figure out how to initialize each input case. */
+  inp = xmalloc (sizeof *inp);
+  inp->init_cnt = dict_get_next_value_idx (default_dict);
+  inp->init = xmalloc (inp->init_cnt * sizeof *inp->init);
+  for (i = 0; i < inp->init_cnt; i++)
+    inp->init[i] = -1;
   for (i = 0; i < dict_get_var_cnt (default_dict); i++)
     {
       struct variable *var = dict_get_var (default_dict, i);
@@ -105,59 +102,66 @@ cmd_end_input_program (void)
       size_t j;
       
       value_init = var->type == NUMERIC ? INP_NUMERIC : INP_STRING;
-      value_init |= var->left ? INP_INIT_ONCE : INP_REINIT;
+      value_init |= var->reinit ? INP_REINIT : INP_INIT_ONCE;
 
       for (j = 0; j < var->nv; j++)
-        inp_init[j + var->fv] = value_init;
+        inp->init[j + var->fv] = value_init;
     }
-  for (i = 0; i < inp_nval; i++)
-    assert (inp_init[i] != -1);
+  for (i = 0; i < inp->init_cnt; i++)
+    assert (inp->init[i] != -1);
+  inp->case_size = dict_get_case_size (default_dict);
+
+  /* Put inp into vfm_source for later use. */
+  vfm_source->aux = inp;
+
+  /* FIXME: we should use create_case_source() here. */
+  vfm_source->value_cnt = dict_get_next_value_idx (default_dict);
 
   return lex_end_of_command ();
 }
 
-/* Initializes temp_case.  Called before the first case is read. */
+/* Initializes case C.  Called before the first case is read. */
 static void
-init_case (void)
+init_case (const struct input_program_pgm *inp, struct ccase *c)
 {
   size_t i;
 
-  for (i = 0; i < inp_nval; i++)
-    switch (inp_init[i]) 
+  for (i = 0; i < inp->init_cnt; i++)
+    switch (inp->init[i]) 
       {
       case INP_NUMERIC | INP_INIT_ONCE:
-        temp_case->data[i].f = 0.0;
+        c->data[i].f = 0.0;
         break;
       case INP_NUMERIC | INP_REINIT:
-        temp_case->data[i].f = SYSMIS;
+        c->data[i].f = SYSMIS;
         break;
       case INP_STRING | INP_INIT_ONCE:
       case INP_STRING | INP_REINIT:
-        memset (temp_case->data[i].s, ' ', sizeof temp_case->data[i].s);
+        memset (c->data[i].s, ' ', sizeof c->data[i].s);
         break;
       default:
         assert (0);
       }
 }
 
-/* Clears temp_case.  Called between reading successive records. */
+/* Clears case C.  Called between reading successive records. */
 static void
-clear_case (void)
+clear_case (const struct input_program_pgm *inp, struct ccase *c)
 {
   size_t i;
 
-  for (i = 0; i < inp_nval; i++)
-    switch (inp_init[i]) 
+  for (i = 0; i < inp->init_cnt; i++)
+    switch (inp->init[i]) 
       {
       case INP_NUMERIC | INP_INIT_ONCE:
         break;
       case INP_NUMERIC | INP_REINIT:
-        temp_case->data[i].f = SYSMIS;
+        c->data[i].f = SYSMIS;
         break;
       case INP_STRING | INP_INIT_ONCE:
         break;
       case INP_STRING | INP_REINIT:
-        memset (temp_case->data[i].s, ' ', sizeof temp_case->data[i].s);
+        memset (c->data[i].s, ' ', sizeof c->data[i].s);
         break;
       default:
         assert (0);
@@ -169,64 +173,63 @@ clear_case (void)
    file.  -1 means go on to the next transformation.  Otherwise the
    return value is the index of the transformation to go to next. */
 static void
-input_program_source_read (write_case_func *write_case,
+input_program_source_read (struct case_source *source,
+                           struct ccase *c,
+                           write_case_func *write_case,
                            write_case_data wc_data)
 {
+  struct input_program_pgm *inp = source->aux;
   int i;
 
   /* Nonzero if there were any END CASE commands in the set of
-     transformations. */
+     transformations.  If so, we don't automatically write out
+     cases. */
   int end_case = 0;
 
-  /* We don't automatically write out cases if the user took over
-     that prerogative.  */
+  /* FIXME?  This is the number of cases sent out of the input
+     program, not the number of cases written to the procedure.
+     The difference should only show up in $CASENUM in COMPUTE.
+     We should check behavior against SPSS. */
+  int cases_written = 0;
+
+  assert (inp != NULL);
+
+  /* Figure end_case. */
   for (i = 0; i < f_trns; i++)
     if (t_trns[i]->proc == end_case_trns_proc)
       end_case = 1;
 
-  /* FIXME: This code should not be necessary.  It is an ugly
-     kluge. */
+  /* FIXME: This is an ugly kluge. */
   for (i = 0; i < f_trns; i++)
     if (t_trns[i]->proc == repeating_data_trns_proc)
       repeating_data_set_write_case (t_trns[i], write_case, wc_data);
 
-  init_case ();
+  init_case (inp, c);
   for (;;)
     {
-      /* Index of current transformation. */
-      int i;
-
-      /* Return value of last-called transformation. */
-      int code;
-
-      debug_printf (("input-program: "));
-
       /* Perform transformations on `blank' case. */
-      for (i = 0; i < f_trns;)
+      for (i = 0; i < f_trns; )
        {
-#if DEBUGGING
-         printf ("/%d", i);
-         if (t_trns[i]->proc == end_case_trns_proc)
-           printf ("\n");
-#endif
+          int code;     /* Return value of last-called transformation. */
 
           if (t_trns[i]->proc == end_case_trns_proc) 
             {
+              cases_written++;
               if (!write_case (wc_data))
-                return;
-              clear_case ();
+                goto done;
+              clear_case (inp, c);
               i++;
               continue;
             }
 
-         code = t_trns[i]->proc (t_trns[i], temp_case);
+         code = t_trns[i]->proc (t_trns[i], c, cases_written + 1);
          switch (code)
            {
            case -1:
              i++;
              break;
            case -2:
-             return;
+             goto done;
            case -3:
              goto next_case;
            default:
@@ -235,39 +238,42 @@ input_program_source_read (write_case_func *write_case,
            }
        }
 
-#if DEBUGGING
-      if (!end_case)
-       printf ("\n");
-#endif
-
       /* Write the case if appropriate. */
-      if (!end_case)
-       if (!write_case (wc_data))
-         return;
+      if (!end_case) 
+        {
+          cases_written++;
+          if (!write_case (wc_data))
+            break;
+        }
 
       /* Blank out the case for the next iteration. */
     next_case:
-      clear_case ();
+      clear_case (inp, c);
     }
+ done: ;
 }
 
+/* Destroys an INPUT PROGRAM source. */
 static void
-input_program_source_destroy_source (void)
+input_program_source_destroy (struct case_source *source)
 {
+  struct input_program_pgm *inp = source->aux;
+
   cancel_transformations ();
-  free (inp_init);
-  inp_init = NULL;
+
+  if (inp != NULL) 
+    {
+      free (inp->init);
+      free (inp);
+    }
 }
 
-struct case_stream input_program_source =
+const struct case_source_class input_program_source_class =
   {
+    "INPUT PROGRAM",
     NULL,
     input_program_source_read,
-    NULL,
-    NULL,
-    input_program_source_destroy_source,
-    NULL,
-    "INPUT PROGRAM",
+    input_program_source_destroy,
   };
 \f
 int
@@ -275,10 +281,7 @@ cmd_end_case (void)
 {
   struct trns_header *t;
 
-  lex_match_id ("END");
-  lex_match_id ("CASE");
-
-  if (vfm_source != &input_program_source)
+  if (!case_source_is_class (vfm_source, &input_program_source_class))
     {
       msg (SE, _("This command may only be executed between INPUT PROGRAM "
                 "and END INPUT PROGRAM."));
@@ -293,10 +296,14 @@ cmd_end_case (void)
   return lex_end_of_command ();
 }
 
+/* Should never be called, because this is handled in
+   input_program_source_read(). */
 int
-end_case_trns_proc (struct trns_header *t UNUSED, struct ccase * c UNUSED)
+end_case_trns_proc (struct trns_header *t UNUSED, struct ccase * c UNUSED,
+                    int case_num UNUSED)
 {
   assert (0);
+  abort ();
 }
 
 /* REREAD transformation. */
@@ -321,8 +328,6 @@ cmd_reread (void)
   /* Created transformation. */
   struct reread_trns *t;
 
-  lex_match_id ("REREAD");
-
   h = default_handle;
   e = NULL;
   while (token != '.')
@@ -338,21 +343,15 @@ cmd_reread (void)
              return CMD_FAILURE;
            }
          
-         e = expr_parse (PXP_NUMERIC);
+         e = expr_parse (EXPR_NUMERIC);
          if (!e)
            return CMD_FAILURE;
        }
       else if (lex_match_id ("FILE"))
        {
          lex_match ('=');
-         if (token != T_ID)
-           {
-             lex_error (_("expecting file handle name"));
-             expr_free (e);
-             return CMD_FAILURE;
-           }
-         h = fh_get_handle_by_name (tokid);
-         if (!h)
+          h = fh_parse_file_handle ();
+         if (h == NULL)
            {
              expr_free (e);
              return CMD_FAILURE;
@@ -376,8 +375,10 @@ cmd_reread (void)
   return CMD_SUCCESS;
 }
 
+/* Executes a REREAD transformation. */
 static int
-reread_trns_proc (struct trns_header * pt, struct ccase * c)
+reread_trns_proc (struct trns_header * pt, struct ccase * c,
+                  int case_num)
 {
   struct reread_trns *t = (struct reread_trns *) pt;
 
@@ -387,7 +388,7 @@ reread_trns_proc (struct trns_header * pt, struct ccase * c)
     {
       union value column;
 
-      expr_evaluate (t->column, c, &column);
+      expr_evaluate (t->column, c, case_num, &column);
       if (!finite (column.f) || column.f < 1)
        {
          msg (SE, _("REREAD: Column numbers must be positive finite "
@@ -400,6 +401,7 @@ reread_trns_proc (struct trns_header * pt, struct ccase * c)
   return -1;
 }
 
+/* Frees a REREAD transformation. */
 static void
 reread_trns_free (struct trns_header * t)
 {
@@ -412,10 +414,7 @@ cmd_end_file (void)
 {
   struct trns_header *t;
 
-  lex_match_id ("END");
-  lex_match_id ("FILE");
-
-  if (vfm_source != &input_program_source)
+  if (!case_source_is_class (vfm_source, &input_program_source_class))
     {
       msg (SE, _("This command may only be executed between INPUT PROGRAM "
                 "and END INPUT PROGRAM."));
@@ -430,11 +429,10 @@ cmd_end_file (void)
   return lex_end_of_command ();
 }
 
+/* Executes an END FILE transformation. */
 static int
-end_file_trns_proc (struct trns_header * t UNUSED, struct ccase * c UNUSED)
+end_file_trns_proc (struct trns_header * t UNUSED, struct ccase * c UNUSED,
+                    int case_num UNUSED)
 {
-#if DEBUGGING
-  printf ("END FILE\n");
-#endif
   return -2;
 }