1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2009, 2010, 2011, 2014 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "data/case.h"
24 #include "data/dataset.h"
25 #include "data/dictionary.h"
26 #include "data/transformations.h"
27 #include "data/variable.h"
28 #include "data/vector.h"
29 #include "language/command.h"
30 #include "language/expressions/public.h"
31 #include "language/lexer/lexer.h"
32 #include "libpspp/message.h"
33 #include "libpspp/misc.h"
34 #include "libpspp/str.h"
36 #include "gl/xalloc.h"
39 #define _(msgid) gettext (msgid)
44 /* Target of a COMPUTE or IF assignment, either a variable or a
46 static struct lvalue *lvalue_parse (struct lexer *lexer, struct dataset *);
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 *, struct dictionary *);
51 static void lvalue_destroy (struct lvalue *, struct dictionary *);
53 /* COMPUTE and IF transformation. */
56 /* Test expression (IF only). */
57 struct expression *test; /* Test expression. */
59 /* Variable lvalue, if variable != NULL. */
60 struct variable *variable; /* Destination variable, if any. */
61 int width; /* Lvalue string width; 0=numeric. */
63 /* Vector lvalue, if vector != NULL. */
64 const struct vector *vector; /* Destination vector, if any. */
65 struct expression *element; /* Destination vector element expr. */
68 struct expression *rvalue; /* Rvalue expression. */
71 static struct expression *parse_rvalue (struct lexer *lexer,
72 const struct lvalue *,
75 static struct compute_trns *compute_trns_create (void);
76 static trns_proc_func *get_proc_func (const struct lvalue *);
77 static trns_free_func compute_trns_free;
82 cmd_compute (struct lexer *lexer, struct dataset *ds)
84 struct dictionary *dict = dataset_dict (ds);
85 struct lvalue *lvalue = NULL;
86 struct compute_trns *compute = NULL;
88 compute = compute_trns_create ();
90 lvalue = lvalue_parse (lexer, ds);
94 if (!lex_force_match (lexer, T_EQUALS))
96 compute->rvalue = parse_rvalue (lexer, lvalue, ds);
97 if (compute->rvalue == NULL)
100 add_transformation (ds, get_proc_func (lvalue), compute_trns_free, compute);
102 lvalue_finalize (lvalue, compute, dict);
107 lvalue_destroy (lvalue, dict);
108 compute_trns_free (compute);
109 return CMD_CASCADING_FAILURE;
112 /* Transformation functions. */
114 /* Handle COMPUTE or IF with numeric target variable. */
116 compute_num (void *compute_, struct ccase **c, casenumber case_num)
118 struct compute_trns *compute = compute_;
120 if (compute->test == NULL
121 || expr_evaluate_num (compute->test, *c, case_num) == 1.0)
123 *c = case_unshare (*c);
124 case_data_rw (*c, compute->variable)->f
125 = expr_evaluate_num (compute->rvalue, *c, case_num);
128 return TRNS_CONTINUE;
131 /* Handle COMPUTE or IF with numeric vector element target
134 compute_num_vec (void *compute_, struct ccase **c, casenumber case_num)
136 struct compute_trns *compute = compute_;
138 if (compute->test == NULL
139 || expr_evaluate_num (compute->test, *c, case_num) == 1.0)
141 double index; /* Index into the vector. */
142 int rindx; /* Rounded index value. */
144 index = expr_evaluate_num (compute->element, *c, case_num);
145 rindx = floor (index + EPSILON);
147 || rindx < 1 || rindx > vector_get_var_cnt (compute->vector))
150 msg (SW, _("When executing COMPUTE: SYSMIS is not a valid value "
151 "as an index into vector %s."),
152 vector_get_name (compute->vector));
154 msg (SW, _("When executing COMPUTE: %.*g is not a valid value as "
155 "an index into vector %s."),
156 DBL_DIG + 1, index, vector_get_name (compute->vector));
157 return TRNS_CONTINUE;
160 *c = case_unshare (*c);
161 case_data_rw (*c, vector_get_var (compute->vector, rindx - 1))->f
162 = expr_evaluate_num (compute->rvalue, *c, case_num);
165 return TRNS_CONTINUE;
168 /* Handle COMPUTE or IF with string target variable. */
170 compute_str (void *compute_, struct ccase **c, casenumber case_num)
172 struct compute_trns *compute = compute_;
174 if (compute->test == NULL
175 || expr_evaluate_num (compute->test, *c, case_num) == 1.0)
179 *c = case_unshare (*c);
180 s = CHAR_CAST_BUG (char *, case_str_rw (*c, compute->variable));
181 expr_evaluate_str (compute->rvalue, *c, case_num, s, compute->width);
184 return TRNS_CONTINUE;
187 /* Handle COMPUTE or IF with string vector element target
190 compute_str_vec (void *compute_, struct ccase **c, casenumber case_num)
192 struct compute_trns *compute = compute_;
194 if (compute->test == NULL
195 || expr_evaluate_num (compute->test, *c, case_num) == 1.0)
197 double index; /* Index into the vector. */
198 int rindx; /* Rounded index value. */
199 struct variable *vr; /* Variable reference by indexed vector. */
201 index = expr_evaluate_num (compute->element, *c, case_num);
202 rindx = floor (index + EPSILON);
205 msg (SW, _("When executing COMPUTE: SYSMIS is not a valid "
206 "value as an index into vector %s."),
207 vector_get_name (compute->vector));
208 return TRNS_CONTINUE;
210 else if (rindx < 1 || rindx > vector_get_var_cnt (compute->vector))
212 msg (SW, _("When executing COMPUTE: %.*g is not a valid value as "
213 "an index into vector %s."),
214 DBL_DIG + 1, index, vector_get_name (compute->vector));
215 return TRNS_CONTINUE;
218 vr = vector_get_var (compute->vector, rindx - 1);
219 *c = case_unshare (*c);
220 expr_evaluate_str (compute->rvalue, *c, case_num,
221 CHAR_CAST_BUG (char *, case_str_rw (*c, vr)),
225 return TRNS_CONTINUE;
231 cmd_if (struct lexer *lexer, struct dataset *ds)
233 struct dictionary *dict = dataset_dict (ds);
234 struct compute_trns *compute = NULL;
235 struct lvalue *lvalue = NULL;
237 compute = compute_trns_create ();
239 /* Test expression. */
240 compute->test = expr_parse (lexer, ds, EXPR_BOOLEAN);
241 if (compute->test == NULL)
244 /* Lvalue variable. */
245 lvalue = lvalue_parse (lexer, ds);
249 /* Rvalue expression. */
250 if (!lex_force_match (lexer, T_EQUALS))
252 compute->rvalue = parse_rvalue (lexer, lvalue, ds);
253 if (compute->rvalue == NULL)
256 add_transformation (ds, get_proc_func (lvalue), compute_trns_free, compute);
258 lvalue_finalize (lvalue, compute, dict);
263 lvalue_destroy (lvalue, dict);
264 compute_trns_free (compute);
265 return CMD_CASCADING_FAILURE;
268 /* Code common to COMPUTE and IF. */
270 static trns_proc_func *
271 get_proc_func (const struct lvalue *lvalue)
273 bool is_numeric = lvalue_get_type (lvalue) == VAL_NUMERIC;
274 bool is_vector = lvalue_is_vector (lvalue);
277 ? (is_vector ? compute_num_vec : compute_num)
278 : (is_vector ? compute_str_vec : compute_str));
281 /* Parses and returns an rvalue expression of the same type as
282 LVALUE, or a null pointer on failure. */
283 static struct expression *
284 parse_rvalue (struct lexer *lexer,
285 const struct lvalue *lvalue, struct dataset *ds)
287 bool is_numeric = lvalue_get_type (lvalue) == VAL_NUMERIC;
289 return expr_parse (lexer, ds, is_numeric ? EXPR_NUMBER : EXPR_STRING);
292 /* Returns a new struct compute_trns after initializing its fields. */
293 static struct compute_trns *
294 compute_trns_create (void)
296 struct compute_trns *compute = xmalloc (sizeof *compute);
297 compute->test = NULL;
298 compute->variable = NULL;
299 compute->vector = NULL;
300 compute->element = NULL;
301 compute->rvalue = NULL;
305 /* Deletes all the fields in COMPUTE. */
307 compute_trns_free (void *compute_)
309 struct compute_trns *compute = compute_;
313 expr_free (compute->test);
314 expr_free (compute->element);
315 expr_free (compute->rvalue);
321 /* COMPUTE or IF target variable or vector element.
322 For a variable, the `variable' member is non-null.
323 For a vector element, the `vector' member is non-null. */
326 struct variable *variable; /* Destination variable. */
327 bool is_new_variable; /* Did we create the variable? */
329 const struct vector *vector; /* Destination vector, if any, or NULL. */
330 struct expression *element; /* Destination vector element, or NULL. */
333 /* Parses the target variable or vector element into a new
334 `struct lvalue', which is returned. */
335 static struct lvalue *
336 lvalue_parse (struct lexer *lexer, struct dataset *ds)
338 struct dictionary *dict = dataset_dict (ds);
339 struct lvalue *lvalue;
341 lvalue = xmalloc (sizeof *lvalue);
342 lvalue->variable = NULL;
343 lvalue->is_new_variable = false;
344 lvalue->vector = NULL;
345 lvalue->element = NULL;
347 if (!lex_force_id (lexer))
350 if (lex_next_token (lexer, 1) == T_LPAREN)
353 lvalue->vector = dict_lookup_vector (dict, lex_tokcstr (lexer));
354 if (lvalue->vector == NULL)
356 msg (SE, _("There is no vector named %s."), lex_tokcstr (lexer));
360 /* Vector element. */
362 if (!lex_force_match (lexer, T_LPAREN))
364 lvalue->element = expr_parse (lexer, ds, EXPR_NUMBER);
365 if (lvalue->element == NULL)
367 if (!lex_force_match (lexer, T_RPAREN))
373 const char *var_name = lex_tokcstr (lexer);
374 lvalue->variable = dict_lookup_var (dict, var_name);
375 if (lvalue->variable == NULL)
377 lvalue->variable = dict_create_var_assert (dict, var_name, 0);
378 lvalue->is_new_variable = true;
385 lvalue_destroy (lvalue, dict);
389 /* Returns the type (NUMERIC or ALPHA) of the target variable or
392 lvalue_get_type (const struct lvalue *lvalue)
394 return (lvalue->variable != NULL
395 ? var_get_type (lvalue->variable)
396 : vector_get_type (lvalue->vector));
399 /* Returns true if LVALUE has a vector as its target. */
401 lvalue_is_vector (const struct lvalue *lvalue)
403 return lvalue->vector != NULL;
406 /* Finalizes making LVALUE the target of COMPUTE, by creating the
407 target variable if necessary and setting fields in COMPUTE. */
409 lvalue_finalize (struct lvalue *lvalue,
410 struct compute_trns *compute,
411 struct dictionary *dict)
413 if (lvalue->vector == NULL)
415 compute->variable = lvalue->variable;
416 compute->width = var_get_width (compute->variable);
418 /* Goofy behavior, but compatible: Turn off LEAVE. */
419 if (!var_must_leave (compute->variable))
420 var_set_leave (compute->variable, false);
422 /* Prevent lvalue_destroy from deleting variable. */
423 lvalue->is_new_variable = false;
427 compute->vector = lvalue->vector;
428 compute->element = lvalue->element;
429 lvalue->element = NULL;
432 lvalue_destroy (lvalue, dict);
435 /* Destroys LVALUE. */
437 lvalue_destroy (struct lvalue *lvalue, struct dictionary *dict)
442 if (lvalue->is_new_variable)
443 dict_delete_var (dict, lvalue->variable);
444 expr_free (lvalue->element);