Encapsulated lexer and updated calling functions accordingly.
[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 (struct lexer *lexer, struct dataset *);
47 static int lvalue_get_type (const struct lvalue *, const struct dictionary *);
48 static bool lvalue_is_vector (const struct lvalue *);
49 static void lvalue_finalize (struct lvalue *,
50                              struct compute_trns *, struct dictionary *);
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 (struct lexer *lexer, 
73                                         const struct lvalue *, 
74                                         struct dataset *);
75
76 static struct compute_trns *compute_trns_create (void);
77 static trns_proc_func *get_proc_func (const struct lvalue *, const struct dictionary *);
78 static trns_free_func compute_trns_free;
79 \f
80 /* COMPUTE. */
81
82 int
83 cmd_compute (struct lexer *lexer, struct dataset *ds)
84 {
85   struct dictionary *dict = dataset_dict (ds);
86   struct lvalue *lvalue = NULL;
87   struct compute_trns *compute = NULL;
88
89   compute = compute_trns_create ();
90
91   lvalue = lvalue_parse (lexer, ds);
92   if (lvalue == NULL)
93     goto fail;
94
95   if (!lex_force_match (lexer, '='))
96     goto fail;
97   compute->rvalue = parse_rvalue (lexer, lvalue, ds);
98   if (compute->rvalue == NULL)
99     goto fail;
100
101   add_transformation (ds, get_proc_func (lvalue, dict), 
102                       compute_trns_free, compute);
103
104   lvalue_finalize (lvalue, compute, dict);
105
106   return lex_end_of_command (lexer);
107
108  fail:
109   lvalue_destroy (lvalue);
110   compute_trns_free (compute);
111   return CMD_CASCADING_FAILURE;
112 }
113 \f
114 /* Transformation functions. */
115
116 /* Handle COMPUTE or IF with numeric target variable. */
117 static int
118 compute_num (void *compute_, struct ccase *c, casenumber case_num)
119 {
120   struct compute_trns *compute = compute_;
121
122   if (compute->test == NULL
123       || expr_evaluate_num (compute->test, c, case_num) == 1.0) 
124     case_data_rw (c, compute->fv)->f = expr_evaluate_num (compute->rvalue, c,
125                                                           case_num); 
126   
127   return TRNS_CONTINUE;
128 }
129
130 /* Handle COMPUTE or IF with numeric vector element target
131    variable. */
132 static int
133 compute_num_vec (void *compute_, struct ccase *c, casenumber case_num)
134 {
135   struct compute_trns *compute = compute_;
136
137   if (compute->test == NULL
138       || expr_evaluate_num (compute->test, c, case_num) == 1.0) 
139     {
140       double index;     /* Index into the vector. */
141       int rindx;        /* Rounded index value. */
142
143       index = expr_evaluate_num (compute->element, c, case_num);
144       rindx = floor (index + EPSILON);
145       if (index == SYSMIS || rindx < 1 || rindx > compute->vector->cnt)
146         {
147           if (index == SYSMIS)
148             msg (SW, _("When executing COMPUTE: SYSMIS is not a valid value as "
149                        "an index into vector %s."), compute->vector->name);
150           else
151             msg (SW, _("When executing COMPUTE: %g is not a valid value as "
152                        "an index into vector %s."),
153                  index, compute->vector->name);
154           return TRNS_CONTINUE;
155         }
156       case_data_rw (c, compute->vector->var[rindx - 1]->fv)->f
157         = expr_evaluate_num (compute->rvalue, c, case_num);
158     }
159   
160   return TRNS_CONTINUE;
161 }
162
163 /* Handle COMPUTE or IF with string target variable. */
164 static int
165 compute_str (void *compute_, struct ccase *c, casenumber case_num)
166 {
167   struct compute_trns *compute = compute_;
168
169   if (compute->test == NULL
170       || expr_evaluate_num (compute->test, c, case_num) == 1.0) 
171     expr_evaluate_str (compute->rvalue, c, case_num,
172                        case_data_rw (c, compute->fv)->s, compute->width);
173   
174   return TRNS_CONTINUE;
175 }
176
177 /* Handle COMPUTE or IF with string vector element target
178    variable. */
179 static int
180 compute_str_vec (void *compute_, struct ccase *c, casenumber case_num)
181 {
182   struct compute_trns *compute = compute_;
183
184   if (compute->test == NULL
185       || expr_evaluate_num (compute->test, c, case_num) == 1.0) 
186     {
187       double index;             /* Index into the vector. */
188       int rindx;                /* Rounded index value. */
189       struct variable *vr;      /* Variable reference by indexed vector. */
190
191       index = expr_evaluate_num (compute->element, c, case_num);
192       rindx = floor (index + EPSILON);
193       if (index == SYSMIS) 
194         {
195           msg (SW, _("When executing COMPUTE: SYSMIS is not a valid "
196                      "value as an index into vector %s."),
197                compute->vector->name);
198           return TRNS_CONTINUE; 
199         }
200       else if (rindx < 1 || rindx > compute->vector->cnt)
201         {
202           msg (SW, _("When executing COMPUTE: %g is not a valid value as "
203                      "an index into vector %s."),
204                index, compute->vector->name);
205           return TRNS_CONTINUE;
206         }
207
208       vr = compute->vector->var[rindx - 1];
209       expr_evaluate_str (compute->rvalue, c, case_num,
210                          case_data_rw (c, vr->fv)->s, vr->width);
211     }
212   
213   return TRNS_CONTINUE;
214 }
215 \f
216 /* IF. */
217
218 int
219 cmd_if (struct lexer *lexer, struct dataset *ds)
220 {
221   struct dictionary *dict = dataset_dict (ds);
222   struct compute_trns *compute = NULL;
223   struct lvalue *lvalue = NULL;
224
225   compute = compute_trns_create ();
226
227   /* Test expression. */
228   compute->test = expr_parse (lexer, ds, EXPR_BOOLEAN);
229   if (compute->test == NULL)
230     goto fail;
231
232   /* Lvalue variable. */
233   lvalue = lvalue_parse (lexer, ds);
234   if (lvalue == NULL)
235     goto fail;
236
237   /* Rvalue expression. */
238   if (!lex_force_match (lexer, '='))
239     goto fail;
240   compute->rvalue = parse_rvalue (lexer, lvalue, ds);
241   if (compute->rvalue == NULL)
242     goto fail;
243
244   add_transformation (ds, get_proc_func (lvalue, dict), 
245                       compute_trns_free, compute);
246
247   lvalue_finalize (lvalue, compute, dict);
248
249   return lex_end_of_command (lexer);
250
251  fail:
252   lvalue_destroy (lvalue);
253   compute_trns_free (compute);
254   return CMD_CASCADING_FAILURE;
255 }
256 \f
257 /* Code common to COMPUTE and IF. */
258
259 static trns_proc_func *
260 get_proc_func (const struct lvalue *lvalue, const struct dictionary *dict) 
261 {
262   bool is_numeric = lvalue_get_type (lvalue, dict) == NUMERIC;
263   bool is_vector = lvalue_is_vector (lvalue);
264
265   return (is_numeric
266           ? (is_vector ? compute_num_vec : compute_num)
267           : (is_vector ? compute_str_vec : compute_str));
268 }
269
270 /* Parses and returns an rvalue expression of the same type as
271    LVALUE, or a null pointer on failure. */
272 static struct expression *
273 parse_rvalue (struct lexer *lexer, 
274               const struct lvalue *lvalue, struct dataset *ds)
275 {
276   const struct dictionary *dict = dataset_dict (ds);
277   bool is_numeric = lvalue_get_type (lvalue, dict) == NUMERIC;
278
279   return expr_parse (lexer, ds, is_numeric ? EXPR_NUMBER : EXPR_STRING);
280 }
281
282 /* Returns a new struct compute_trns after initializing its fields. */
283 static struct compute_trns *
284 compute_trns_create (void)
285 {
286   struct compute_trns *compute = xmalloc (sizeof *compute);
287   compute->test = NULL;
288   compute->variable = NULL;
289   compute->vector = NULL;
290   compute->element = NULL;
291   compute->rvalue = NULL;
292   return compute;
293 }
294
295 /* Deletes all the fields in COMPUTE. */
296 static bool
297 compute_trns_free (void *compute_)
298 {
299   struct compute_trns *compute = compute_;
300
301   if (compute != NULL) 
302     {
303       expr_free (compute->test);
304       expr_free (compute->element);
305       expr_free (compute->rvalue);
306       free (compute);
307     }
308   return true;
309 }
310 \f
311 /* COMPUTE or IF target variable or vector element. */
312 struct lvalue
313   {
314     char var_name[LONG_NAME_LEN + 1];   /* Destination variable name, or "". */
315     const struct vector *vector; /* Destination vector, if any, or NULL. */
316     struct expression *element;  /* Destination vector element, or NULL. */
317   };
318
319 /* Parses the target variable or vector element into a new
320    `struct lvalue', which is returned. */
321 static struct lvalue *
322 lvalue_parse (struct lexer *lexer, struct dataset *ds) 
323 {
324   struct lvalue *lvalue;
325
326   lvalue = xmalloc (sizeof *lvalue);
327   lvalue->var_name[0] = '\0';
328   lvalue->vector = NULL;
329   lvalue->element = NULL;
330
331   if (!lex_force_id (lexer))
332     goto lossage;
333   
334   if (lex_look_ahead (lexer) == '(')
335     {
336       /* Vector. */
337       lvalue->vector = dict_lookup_vector (dataset_dict (ds), lex_tokid (lexer));
338       if (lvalue->vector == NULL)
339         {
340           msg (SE, _("There is no vector named %s."), lex_tokid (lexer));
341           goto lossage;
342         }
343
344       /* Vector element. */
345       lex_get (lexer);
346       if (!lex_force_match (lexer, '('))
347         goto lossage;
348       lvalue->element = expr_parse (lexer, ds, EXPR_NUMBER);
349       if (lvalue->element == NULL)
350         goto lossage;
351       if (!lex_force_match (lexer, ')'))
352         goto lossage;
353     }
354   else
355     {
356       /* Variable name. */
357       str_copy_trunc (lvalue->var_name, sizeof lvalue->var_name, lex_tokid (lexer));
358       lex_get (lexer);
359     }
360   return lvalue;
361
362  lossage:
363   lvalue_destroy (lvalue);
364   return NULL;
365 }
366
367 /* Returns the type (NUMERIC or ALPHA) of the target variable or
368    vector in LVALUE. */
369 static int
370 lvalue_get_type (const struct lvalue *lvalue, const struct dictionary *dict) 
371 {
372   if (lvalue->vector == NULL) 
373     {
374       struct variable *var = dict_lookup_var (dict, lvalue->var_name);
375       if (var == NULL)
376         return NUMERIC;
377       else
378         return var->type;
379     }
380   else 
381     return lvalue->vector->var[0]->type;
382 }
383
384 /* Returns true if LVALUE has a vector as its target. */
385 static bool
386 lvalue_is_vector (const struct lvalue *lvalue) 
387 {
388   return lvalue->vector != NULL;
389 }
390
391 /* Finalizes making LVALUE the target of COMPUTE, by creating the
392    target variable if necessary and setting fields in COMPUTE. */
393 static void
394 lvalue_finalize (struct lvalue *lvalue, 
395                  struct compute_trns *compute, 
396                  struct dictionary *dict) 
397 {
398   if (lvalue->vector == NULL)
399     {
400       compute->variable = dict_lookup_var (dict, lvalue->var_name);
401       if (compute->variable == NULL)
402           compute->variable = dict_create_var_assert (dict, lvalue->var_name, 0);
403
404       compute->fv = compute->variable->fv;
405       compute->width = compute->variable->width;
406
407       /* Goofy behavior, but compatible: Turn off LEAVE. */
408       if (dict_class_from_id (compute->variable->name) != DC_SCRATCH)
409         compute->variable->leave = false;
410     }
411   else 
412     {
413       compute->vector = lvalue->vector;
414       compute->element = lvalue->element;
415       lvalue->element = NULL;
416     }
417
418   lvalue_destroy (lvalue);
419 }
420
421 /* Destroys LVALUE. */
422 static void 
423 lvalue_destroy (struct lvalue *lvalue) 
424 {
425   if (lvalue == NULL) 
426      return;
427
428   expr_free (lvalue->element);
429   free (lvalue);
430 }