Encapsulated the static data of procedure.[ch] into a single object, to be
[pspp-builds.git] / src / language / xforms / compute.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21
22 #include <stdlib.h>
23
24 #include <data/case.h>
25 #include <data/dictionary.h>
26 #include <data/procedure.h>
27 #include <data/transformations.h>
28 #include <data/variable.h>
29 #include <language/command.h>
30 #include <language/expressions/public.h>
31 #include <language/lexer/lexer.h>
32 #include <libpspp/alloc.h>
33 #include <libpspp/message.h>
34 #include <libpspp/message.h>
35 #include <libpspp/misc.h>
36 #include <libpspp/str.h>
37
38 #include "gettext.h"
39 #define _(msgid) gettext (msgid)
40
41 struct compute_trns;
42 struct lvalue;
43
44 /* Target of a COMPUTE or IF assignment, either a variable or a
45    vector element. */
46 static struct lvalue *lvalue_parse (void);
47 static int lvalue_get_type (const struct lvalue *);
48 static bool lvalue_is_vector (const struct lvalue *);
49 static void lvalue_finalize (struct lvalue *,
50                              struct compute_trns *);
51 static void lvalue_destroy (struct lvalue *);
52
53 /* COMPUTE and IF transformation. */
54 struct compute_trns
55   {
56     /* Test expression (IF only). */
57     struct expression *test;     /* Test expression. */
58
59     /* Variable lvalue, if variable != NULL. */
60     struct variable *variable;   /* Destination variable, if any. */
61     int fv;                      /* `value' index of destination variable. */
62     int width;                   /* Lvalue string width; 0=numeric. */
63
64     /* Vector lvalue, if vector != NULL. */
65     const struct vector *vector; /* Destination vector, if any. */
66     struct expression *element;  /* Destination vector element expr. */
67
68     /* Rvalue. */
69     struct expression *rvalue;   /* Rvalue expression. */
70   };
71
72 static struct expression *parse_rvalue (const struct lvalue *);
73 static struct compute_trns *compute_trns_create (void);
74 static trns_proc_func *get_proc_func (const struct lvalue *);
75 static trns_free_func compute_trns_free;
76 \f
77 /* COMPUTE. */
78
79 int
80 cmd_compute (void)
81 {
82   struct lvalue *lvalue = NULL;
83   struct compute_trns *compute = NULL;
84
85   compute = compute_trns_create ();
86
87   lvalue = lvalue_parse ();
88   if (lvalue == NULL)
89     goto fail;
90
91   if (!lex_force_match ('='))
92     goto fail;
93   compute->rvalue = parse_rvalue (lvalue);
94   if (compute->rvalue == NULL)
95     goto fail;
96
97   add_transformation (current_dataset, 
98                       get_proc_func (lvalue), compute_trns_free, compute);
99
100   lvalue_finalize (lvalue, compute);
101
102   return lex_end_of_command ();
103
104  fail:
105   lvalue_destroy (lvalue);
106   compute_trns_free (compute);
107   return CMD_CASCADING_FAILURE;
108 }
109 \f
110 /* Transformation functions. */
111
112 /* Handle COMPUTE or IF with numeric target variable. */
113 static int
114 compute_num (void *compute_, struct ccase *c, casenum_t case_num)
115 {
116   struct compute_trns *compute = compute_;
117
118   if (compute->test == NULL
119       || expr_evaluate_num (compute->test, c, case_num) == 1.0) 
120     case_data_rw (c, compute->fv)->f = expr_evaluate_num (compute->rvalue, c,
121                                                           case_num); 
122   
123   return TRNS_CONTINUE;
124 }
125
126 /* Handle COMPUTE or IF with numeric vector element target
127    variable. */
128 static int
129 compute_num_vec (void *compute_, struct ccase *c, casenum_t case_num)
130 {
131   struct compute_trns *compute = compute_;
132
133   if (compute->test == NULL
134       || expr_evaluate_num (compute->test, c, case_num) == 1.0) 
135     {
136       double index;     /* Index into the vector. */
137       int rindx;        /* Rounded index value. */
138
139       index = expr_evaluate_num (compute->element, c, case_num);
140       rindx = floor (index + EPSILON);
141       if (index == SYSMIS || rindx < 1 || rindx > compute->vector->cnt)
142         {
143           if (index == SYSMIS)
144             msg (SW, _("When executing COMPUTE: SYSMIS is not a valid value as "
145                        "an index into vector %s."), compute->vector->name);
146           else
147             msg (SW, _("When executing COMPUTE: %g is not a valid value as "
148                        "an index into vector %s."),
149                  index, compute->vector->name);
150           return TRNS_CONTINUE;
151         }
152       case_data_rw (c, compute->vector->var[rindx - 1]->fv)->f
153         = expr_evaluate_num (compute->rvalue, c, case_num);
154     }
155   
156   return TRNS_CONTINUE;
157 }
158
159 /* Handle COMPUTE or IF with string target variable. */
160 static int
161 compute_str (void *compute_, struct ccase *c, casenum_t case_num)
162 {
163   struct compute_trns *compute = compute_;
164
165   if (compute->test == NULL
166       || expr_evaluate_num (compute->test, c, case_num) == 1.0) 
167     expr_evaluate_str (compute->rvalue, c, case_num,
168                        case_data_rw (c, compute->fv)->s, compute->width);
169   
170   return TRNS_CONTINUE;
171 }
172
173 /* Handle COMPUTE or IF with string vector element target
174    variable. */
175 static int
176 compute_str_vec (void *compute_, struct ccase *c, casenum_t case_num)
177 {
178   struct compute_trns *compute = compute_;
179
180   if (compute->test == NULL
181       || expr_evaluate_num (compute->test, c, case_num) == 1.0) 
182     {
183       double index;             /* Index into the vector. */
184       int rindx;                /* Rounded index value. */
185       struct variable *vr;      /* Variable reference by indexed vector. */
186
187       index = expr_evaluate_num (compute->element, c, case_num);
188       rindx = floor (index + EPSILON);
189       if (index == SYSMIS) 
190         {
191           msg (SW, _("When executing COMPUTE: SYSMIS is not a valid "
192                      "value as an index into vector %s."),
193                compute->vector->name);
194           return TRNS_CONTINUE; 
195         }
196       else if (rindx < 1 || rindx > compute->vector->cnt)
197         {
198           msg (SW, _("When executing COMPUTE: %g is not a valid value as "
199                      "an index into vector %s."),
200                index, compute->vector->name);
201           return TRNS_CONTINUE;
202         }
203
204       vr = compute->vector->var[rindx - 1];
205       expr_evaluate_str (compute->rvalue, c, case_num,
206                          case_data_rw (c, vr->fv)->s, vr->width);
207     }
208   
209   return TRNS_CONTINUE;
210 }
211 \f
212 /* IF. */
213
214 int
215 cmd_if (void)
216 {
217   struct compute_trns *compute = NULL;
218   struct lvalue *lvalue = NULL;
219
220   compute = compute_trns_create ();
221
222   /* Test expression. */
223   compute->test = expr_parse (dataset_dict (current_dataset), EXPR_BOOLEAN);
224   if (compute->test == NULL)
225     goto fail;
226
227   /* Lvalue variable. */
228   lvalue = lvalue_parse ();
229   if (lvalue == NULL)
230     goto fail;
231
232   /* Rvalue expression. */
233   if (!lex_force_match ('='))
234     goto fail;
235   compute->rvalue = parse_rvalue (lvalue);
236   if (compute->rvalue == NULL)
237     goto fail;
238
239   add_transformation (current_dataset, 
240                       get_proc_func (lvalue), compute_trns_free, compute);
241
242   lvalue_finalize (lvalue, compute);
243
244   return lex_end_of_command ();
245
246  fail:
247   lvalue_destroy (lvalue);
248   compute_trns_free (compute);
249   return CMD_CASCADING_FAILURE;
250 }
251 \f
252 /* Code common to COMPUTE and IF. */
253
254 static trns_proc_func *
255 get_proc_func (const struct lvalue *lvalue) 
256 {
257   bool is_numeric = lvalue_get_type (lvalue) == NUMERIC;
258   bool is_vector = lvalue_is_vector (lvalue);
259
260   return (is_numeric
261           ? (is_vector ? compute_num_vec : compute_num)
262           : (is_vector ? compute_str_vec : compute_str));
263 }
264
265 /* Parses and returns an rvalue expression of the same type as
266    LVALUE, or a null pointer on failure. */
267 static struct expression *
268 parse_rvalue (const struct lvalue *lvalue)
269 {
270   bool is_numeric = lvalue_get_type (lvalue) == NUMERIC;
271
272   return expr_parse (dataset_dict (current_dataset), is_numeric ? EXPR_NUMBER : EXPR_STRING);
273 }
274
275 /* Returns a new struct compute_trns after initializing its fields. */
276 static struct compute_trns *
277 compute_trns_create (void)
278 {
279   struct compute_trns *compute = xmalloc (sizeof *compute);
280   compute->test = NULL;
281   compute->variable = NULL;
282   compute->vector = NULL;
283   compute->element = NULL;
284   compute->rvalue = NULL;
285   return compute;
286 }
287
288 /* Deletes all the fields in COMPUTE. */
289 static bool
290 compute_trns_free (void *compute_)
291 {
292   struct compute_trns *compute = compute_;
293
294   if (compute != NULL) 
295     {
296       expr_free (compute->test);
297       expr_free (compute->element);
298       expr_free (compute->rvalue);
299       free (compute);
300     }
301   return true;
302 }
303 \f
304 /* COMPUTE or IF target variable or vector element. */
305 struct lvalue
306   {
307     char var_name[LONG_NAME_LEN + 1];   /* Destination variable name, or "". */
308     const struct vector *vector; /* Destination vector, if any, or NULL. */
309     struct expression *element;  /* Destination vector element, or NULL. */
310   };
311
312 /* Parses the target variable or vector element into a new
313    `struct lvalue', which is returned. */
314 static struct lvalue *
315 lvalue_parse (void) 
316 {
317   struct lvalue *lvalue;
318
319   lvalue = xmalloc (sizeof *lvalue);
320   lvalue->var_name[0] = '\0';
321   lvalue->vector = NULL;
322   lvalue->element = NULL;
323
324   if (!lex_force_id ())
325     goto lossage;
326   
327   if (lex_look_ahead () == '(')
328     {
329       /* Vector. */
330       lvalue->vector = dict_lookup_vector (dataset_dict (current_dataset), tokid);
331       if (lvalue->vector == NULL)
332         {
333           msg (SE, _("There is no vector named %s."), tokid);
334           goto lossage;
335         }
336
337       /* Vector element. */
338       lex_get ();
339       if (!lex_force_match ('('))
340         goto lossage;
341       lvalue->element = expr_parse (dataset_dict (current_dataset), EXPR_NUMBER);
342       if (lvalue->element == NULL)
343         goto lossage;
344       if (!lex_force_match (')'))
345         goto lossage;
346     }
347   else
348     {
349       /* Variable name. */
350       str_copy_trunc (lvalue->var_name, sizeof lvalue->var_name, tokid);
351       lex_get ();
352     }
353   return lvalue;
354
355  lossage:
356   lvalue_destroy (lvalue);
357   return NULL;
358 }
359
360 /* Returns the type (NUMERIC or ALPHA) of the target variable or
361    vector in LVALUE. */
362 static int
363 lvalue_get_type (const struct lvalue *lvalue) 
364 {
365   if (lvalue->vector == NULL) 
366     {
367       struct variable *var = dict_lookup_var (dataset_dict (current_dataset), lvalue->var_name);
368       if (var == NULL)
369         return NUMERIC;
370       else
371         return var->type;
372     }
373   else 
374     return lvalue->vector->var[0]->type;
375 }
376
377 /* Returns true if LVALUE has a vector as its target. */
378 static bool
379 lvalue_is_vector (const struct lvalue *lvalue) 
380 {
381   return lvalue->vector != NULL;
382 }
383
384 /* Finalizes making LVALUE the target of COMPUTE, by creating the
385    target variable if necessary and setting fields in COMPUTE. */
386 static void
387 lvalue_finalize (struct lvalue *lvalue, struct compute_trns *compute) 
388 {
389   if (lvalue->vector == NULL)
390     {
391       compute->variable = dict_lookup_var (dataset_dict (current_dataset), lvalue->var_name);
392       if (compute->variable == NULL)
393           compute->variable = dict_create_var_assert (dataset_dict (current_dataset),
394                                                       lvalue->var_name, 0);
395
396       compute->fv = compute->variable->fv;
397       compute->width = compute->variable->width;
398
399       /* Goofy behavior, but compatible: Turn off LEAVE. */
400       if (dict_class_from_id (compute->variable->name) != DC_SCRATCH)
401         compute->variable->leave = false;
402     }
403   else 
404     {
405       compute->vector = lvalue->vector;
406       compute->element = lvalue->element;
407       lvalue->element = NULL;
408     }
409
410   lvalue_destroy (lvalue);
411 }
412
413 /* Destroys LVALUE. */
414 static void 
415 lvalue_destroy (struct lvalue *lvalue) 
416 {
417   if (lvalue == NULL) 
418      return;
419
420   expr_free (lvalue->element);
421   free (lvalue);
422 }