Made array.h const correct, and dealt with the consequences.
[pspp-builds.git] / src / language / xforms / compute.c
index e6a17553377703096ecdb5b0ca57a59c52c909c6..46c693bbffe21cdd1f9a319b1e5fece7e923681b 100644 (file)
    02110-1301, USA. */
 
 #include <config.h>
-#include <libpspp/message.h>
+
 #include <stdlib.h>
-#include <libpspp/alloc.h>
+
 #include <data/case.h>
-#include <language/command.h>
 #include <data/dictionary.h>
-#include <libpspp/message.h>
+#include <data/procedure.h>
+#include <data/transformations.h>
+#include <data/variable.h>
+#include <language/command.h>
 #include <language/expressions/public.h>
 #include <language/lexer/lexer.h>
+#include <libpspp/alloc.h>
+#include <libpspp/message.h>
+#include <libpspp/message.h>
 #include <libpspp/misc.h>
 #include <libpspp/str.h>
-#include <data/variable.h>
 
 #include "gettext.h"
 #define _(msgid) gettext (msgid)
@@ -39,11 +43,11 @@ struct lvalue;
 
 /* Target of a COMPUTE or IF assignment, either a variable or a
    vector element. */
-static struct lvalue *lvalue_parse (void);
-static int lvalue_get_type (const struct lvalue *);
+static struct lvalue *lvalue_parse (struct dataset *);
+static int lvalue_get_type (const struct lvalue *, const struct dictionary *);
 static bool lvalue_is_vector (const struct lvalue *);
 static void lvalue_finalize (struct lvalue *,
-                             struct compute_trns *);
+                             struct compute_trns *, struct dictionary *);
 static void lvalue_destroy (struct lvalue *);
 
 /* COMPUTE and IF transformation. */
@@ -65,34 +69,36 @@ struct compute_trns
     struct expression *rvalue;  /* Rvalue expression. */
   };
 
-static struct expression *parse_rvalue (const struct lvalue *);
+static struct expression *parse_rvalue (const struct lvalue *, struct dataset *);
 static struct compute_trns *compute_trns_create (void);
-static trns_proc_func *get_proc_func (const struct lvalue *);
+static trns_proc_func *get_proc_func (const struct lvalue *, const struct dictionary *);
 static trns_free_func compute_trns_free;
 \f
 /* COMPUTE. */
 
 int
-cmd_compute (void)
+cmd_compute (struct dataset *ds)
 {
+  struct dictionary *dict = dataset_dict (ds);
   struct lvalue *lvalue = NULL;
   struct compute_trns *compute = NULL;
 
   compute = compute_trns_create ();
 
-  lvalue = lvalue_parse ();
+  lvalue = lvalue_parse (ds);
   if (lvalue == NULL)
     goto fail;
 
   if (!lex_force_match ('='))
     goto fail;
-  compute->rvalue = parse_rvalue (lvalue);
+  compute->rvalue = parse_rvalue (lvalue, ds);
   if (compute->rvalue == NULL)
     goto fail;
 
-  add_transformation (get_proc_func (lvalue), compute_trns_free, compute);
+  add_transformation (ds, get_proc_func (lvalue, dict), 
+                     compute_trns_free, compute);
 
-  lvalue_finalize (lvalue, compute);
+  lvalue_finalize (lvalue, compute, dict);
 
   return lex_end_of_command ();
 
@@ -106,7 +112,7 @@ cmd_compute (void)
 
 /* Handle COMPUTE or IF with numeric target variable. */
 static int
-compute_num (void *compute_, struct ccase *c, int case_num)
+compute_num (void *compute_, struct ccase *c, casenumber case_num)
 {
   struct compute_trns *compute = compute_;
 
@@ -121,7 +127,7 @@ compute_num (void *compute_, struct ccase *c, int case_num)
 /* Handle COMPUTE or IF with numeric vector element target
    variable. */
 static int
-compute_num_vec (void *compute_, struct ccase *c, int case_num)
+compute_num_vec (void *compute_, struct ccase *c, casenumber case_num)
 {
   struct compute_trns *compute = compute_;
 
@@ -153,7 +159,7 @@ compute_num_vec (void *compute_, struct ccase *c, int case_num)
 
 /* Handle COMPUTE or IF with string target variable. */
 static int
-compute_str (void *compute_, struct ccase *c, int case_num)
+compute_str (void *compute_, struct ccase *c, casenumber case_num)
 {
   struct compute_trns *compute = compute_;
 
@@ -168,7 +174,7 @@ compute_str (void *compute_, struct ccase *c, int case_num)
 /* Handle COMPUTE or IF with string vector element target
    variable. */
 static int
-compute_str_vec (void *compute_, struct ccase *c, int case_num)
+compute_str_vec (void *compute_, struct ccase *c, casenumber case_num)
 {
   struct compute_trns *compute = compute_;
 
@@ -207,33 +213,35 @@ compute_str_vec (void *compute_, struct ccase *c, int case_num)
 /* IF. */
 
 int
-cmd_if (void)
+cmd_if (struct dataset *ds)
 {
+  struct dictionary *dict = dataset_dict (ds);
   struct compute_trns *compute = NULL;
   struct lvalue *lvalue = NULL;
 
   compute = compute_trns_create ();
 
   /* Test expression. */
-  compute->test = expr_parse (default_dict, EXPR_BOOLEAN);
+  compute->test = expr_parse (ds, EXPR_BOOLEAN);
   if (compute->test == NULL)
     goto fail;
 
   /* Lvalue variable. */
-  lvalue = lvalue_parse ();
+  lvalue = lvalue_parse (ds);
   if (lvalue == NULL)
     goto fail;
 
   /* Rvalue expression. */
   if (!lex_force_match ('='))
     goto fail;
-  compute->rvalue = parse_rvalue (lvalue);
+  compute->rvalue = parse_rvalue (lvalue, ds);
   if (compute->rvalue == NULL)
     goto fail;
 
-  add_transformation (get_proc_func (lvalue), compute_trns_free, compute);
+  add_transformation (ds, get_proc_func (lvalue, dict), 
+                     compute_trns_free, compute);
 
-  lvalue_finalize (lvalue, compute);
+  lvalue_finalize (lvalue, compute, dict);
 
   return lex_end_of_command ();
 
@@ -246,9 +254,9 @@ cmd_if (void)
 /* Code common to COMPUTE and IF. */
 
 static trns_proc_func *
-get_proc_func (const struct lvalue *lvalue) 
+get_proc_func (const struct lvalue *lvalue, const struct dictionary *dict
 {
-  bool is_numeric = lvalue_get_type (lvalue) == NUMERIC;
+  bool is_numeric = lvalue_get_type (lvalue, dict) == NUMERIC;
   bool is_vector = lvalue_is_vector (lvalue);
 
   return (is_numeric
@@ -259,11 +267,12 @@ get_proc_func (const struct lvalue *lvalue)
 /* Parses and returns an rvalue expression of the same type as
    LVALUE, or a null pointer on failure. */
 static struct expression *
-parse_rvalue (const struct lvalue *lvalue)
+parse_rvalue (const struct lvalue *lvalue, struct dataset *ds)
 {
-  bool is_numeric = lvalue_get_type (lvalue) == NUMERIC;
+  const struct dictionary *dict = dataset_dict (ds);
+  bool is_numeric = lvalue_get_type (lvalue, dict) == NUMERIC;
 
-  return expr_parse (default_dict, is_numeric ? EXPR_NUMBER : EXPR_STRING);
+  return expr_parse (ds, is_numeric ? EXPR_NUMBER : EXPR_STRING);
 }
 
 /* Returns a new struct compute_trns after initializing its fields. */
@@ -306,7 +315,7 @@ struct lvalue
 /* Parses the target variable or vector element into a new
    `struct lvalue', which is returned. */
 static struct lvalue *
-lvalue_parse (void
+lvalue_parse (struct dataset *ds
 {
   struct lvalue *lvalue;
 
@@ -321,7 +330,7 @@ lvalue_parse (void)
   if (lex_look_ahead () == '(')
     {
       /* Vector. */
-      lvalue->vector = dict_lookup_vector (default_dict, tokid);
+      lvalue->vector = dict_lookup_vector (dataset_dict (ds), tokid);
       if (lvalue->vector == NULL)
        {
          msg (SE, _("There is no vector named %s."), tokid);
@@ -332,7 +341,7 @@ lvalue_parse (void)
       lex_get ();
       if (!lex_force_match ('('))
        goto lossage;
-      lvalue->element = expr_parse (default_dict, EXPR_NUMBER);
+      lvalue->element = expr_parse (ds, EXPR_NUMBER);
       if (lvalue->element == NULL)
         goto lossage;
       if (!lex_force_match (')'))
@@ -354,11 +363,11 @@ lvalue_parse (void)
 /* Returns the type (NUMERIC or ALPHA) of the target variable or
    vector in LVALUE. */
 static int
-lvalue_get_type (const struct lvalue *lvalue) 
+lvalue_get_type (const struct lvalue *lvalue, const struct dictionary *dict
 {
   if (lvalue->vector == NULL) 
     {
-      struct variable *var = dict_lookup_var (default_dict, lvalue->var_name);
+      struct variable *var = dict_lookup_var (dict, lvalue->var_name);
       if (var == NULL)
         return NUMERIC;
       else
@@ -368,7 +377,7 @@ lvalue_get_type (const struct lvalue *lvalue)
     return lvalue->vector->var[0]->type;
 }
 
-/* Returns nonzero if LVALUE has a vector as its target. */
+/* Returns true if LVALUE has a vector as its target. */
 static bool
 lvalue_is_vector (const struct lvalue *lvalue) 
 {
@@ -378,14 +387,15 @@ lvalue_is_vector (const struct lvalue *lvalue)
 /* Finalizes making LVALUE the target of COMPUTE, by creating the
    target variable if necessary and setting fields in COMPUTE. */
 static void
-lvalue_finalize (struct lvalue *lvalue, struct compute_trns *compute) 
+lvalue_finalize (struct lvalue *lvalue, 
+                struct compute_trns *compute, 
+                struct dictionary *dict) 
 {
   if (lvalue->vector == NULL)
     {
-      compute->variable = dict_lookup_var (default_dict, lvalue->var_name);
+      compute->variable = dict_lookup_var (dict, lvalue->var_name);
       if (compute->variable == NULL)
-         compute->variable = dict_create_var_assert (default_dict,
-                                                     lvalue->var_name, 0);
+         compute->variable = dict_create_var_assert (dict, lvalue->var_name, 0);
 
       compute->fv = compute->variable->fv;
       compute->width = compute->variable->width;