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 /* COMPUTE or IF target variable or vector element.
45 For a variable, the `variable' member is non-null.
46 For a vector element, the `vector' member is non-null. */
49 struct msg_location *location; /* Syntax for variable or vector. */
51 struct variable *variable; /* Destination variable. */
52 bool is_new_variable; /* Did we create the variable? */
54 const struct vector *vector; /* Destination vector, if any, or NULL. */
55 struct expression *element; /* Destination vector element, or NULL. */
58 /* Target of a COMPUTE or IF assignment, either a variable or a
60 static struct lvalue *lvalue_parse (struct lexer *lexer, struct dataset *);
61 static int lvalue_get_type (const struct lvalue *);
62 static bool lvalue_is_vector (const struct lvalue *);
63 static void lvalue_finalize (struct lvalue *,
64 struct compute_trns *, struct dictionary *);
65 static void lvalue_destroy (struct lvalue *, struct dictionary *);
67 /* COMPUTE and IF transformation. */
70 /* Test expression (IF only). */
71 struct expression *test; /* Test expression. */
73 /* Variable lvalue, if variable != NULL. */
74 struct variable *variable; /* Destination variable, if any. */
75 int width; /* Lvalue string width; 0=numeric. */
77 /* Vector lvalue, if vector != NULL. */
78 const struct vector *vector; /* Destination vector, if any. */
79 struct expression *element; /* Destination vector element expr. */
81 struct msg_location *lvalue_location;
84 struct expression *rvalue; /* Rvalue expression. */
87 static struct expression *parse_rvalue (struct lexer *lexer,
88 const struct lvalue *,
91 static struct compute_trns *compute_trns_create (void);
92 static bool compute_trns_free (void *compute_);
93 static const struct trns_class *get_trns_class (const struct lvalue *);
98 cmd_compute (struct lexer *lexer, struct dataset *ds)
100 struct dictionary *dict = dataset_dict (ds);
101 struct lvalue *lvalue = NULL;
102 struct compute_trns *compute = NULL;
104 compute = compute_trns_create ();
106 lvalue = lvalue_parse (lexer, ds);
110 if (!lex_force_match (lexer, T_EQUALS))
112 compute->rvalue = parse_rvalue (lexer, lvalue, ds);
113 if (compute->rvalue == NULL)
116 add_transformation (ds, get_trns_class (lvalue), compute);
118 lvalue_finalize (lvalue, compute, dict);
123 lvalue_destroy (lvalue, dict);
124 compute_trns_free (compute);
125 return CMD_CASCADING_FAILURE;
128 /* Transformation functions. */
130 /* Handle COMPUTE or IF with numeric target variable. */
131 static enum trns_result
132 compute_num (void *compute_, struct ccase **c, casenumber case_num)
134 struct compute_trns *compute = compute_;
136 if (compute->test == NULL
137 || expr_evaluate_num (compute->test, *c, case_num) == 1.0)
139 *c = case_unshare (*c);
140 *case_num_rw (*c, compute->variable)
141 = expr_evaluate_num (compute->rvalue, *c, case_num);
144 return TRNS_CONTINUE;
147 /* Handle COMPUTE or IF with numeric vector element target
149 static enum trns_result
150 compute_num_vec (void *compute_, struct ccase **c, casenumber case_num)
152 struct compute_trns *compute = compute_;
154 if (compute->test == NULL
155 || expr_evaluate_num (compute->test, *c, case_num) == 1.0)
157 double index; /* Index into the vector. */
158 int rindx; /* Rounded index value. */
160 index = expr_evaluate_num (compute->element, *c, case_num);
161 rindx = floor (index + EPSILON);
163 || rindx < 1 || rindx > vector_get_n_vars (compute->vector))
166 msg_at (SW, compute->lvalue_location,
167 _("When executing COMPUTE: SYSMIS is not a valid value "
168 "as an index into vector %s."),
169 vector_get_name (compute->vector));
171 msg_at (SW, compute->lvalue_location,
172 _("When executing COMPUTE: %.*g is not a valid value as "
173 "an index into vector %s."),
174 DBL_DIG + 1, index, vector_get_name (compute->vector));
175 return TRNS_CONTINUE;
178 *c = case_unshare (*c);
179 *case_num_rw (*c, vector_get_var (compute->vector, rindx - 1))
180 = expr_evaluate_num (compute->rvalue, *c, case_num);
183 return TRNS_CONTINUE;
186 /* Handle COMPUTE or IF with string target variable. */
187 static enum trns_result
188 compute_str (void *compute_, struct ccase **c, casenumber case_num)
190 struct compute_trns *compute = compute_;
192 if (compute->test == NULL
193 || expr_evaluate_num (compute->test, *c, case_num) == 1.0)
197 *c = case_unshare (*c);
198 s = CHAR_CAST_BUG (char *, case_str_rw (*c, compute->variable));
199 expr_evaluate_str (compute->rvalue, *c, case_num, s, compute->width);
202 return TRNS_CONTINUE;
205 /* Handle COMPUTE or IF with string vector element target
207 static enum trns_result
208 compute_str_vec (void *compute_, struct ccase **c, casenumber case_num)
210 struct compute_trns *compute = compute_;
212 if (compute->test == NULL
213 || expr_evaluate_num (compute->test, *c, case_num) == 1.0)
215 double index; /* Index into the vector. */
216 int rindx; /* Rounded index value. */
217 struct variable *vr; /* Variable reference by indexed vector. */
219 index = expr_evaluate_num (compute->element, *c, case_num);
220 rindx = floor (index + EPSILON);
223 msg_at (SW, compute->lvalue_location,
224 _("When executing COMPUTE: SYSMIS is not a valid "
225 "value as an index into vector %s."),
226 vector_get_name (compute->vector));
227 return TRNS_CONTINUE;
229 else if (rindx < 1 || rindx > vector_get_n_vars (compute->vector))
231 msg_at (SW, compute->lvalue_location,
232 _("When executing COMPUTE: %.*g is not a valid value as "
233 "an index into vector %s."),
234 DBL_DIG + 1, index, vector_get_name (compute->vector));
235 return TRNS_CONTINUE;
238 vr = vector_get_var (compute->vector, rindx - 1);
239 *c = case_unshare (*c);
240 expr_evaluate_str (compute->rvalue, *c, case_num,
241 CHAR_CAST_BUG (char *, case_str_rw (*c, vr)),
245 return TRNS_CONTINUE;
251 cmd_if (struct lexer *lexer, struct dataset *ds)
253 struct dictionary *dict = dataset_dict (ds);
254 struct compute_trns *compute = NULL;
255 struct lvalue *lvalue = NULL;
257 compute = compute_trns_create ();
259 /* Test expression. */
260 compute->test = expr_parse_bool (lexer, ds);
261 if (compute->test == NULL)
264 /* Lvalue variable. */
265 lvalue = lvalue_parse (lexer, ds);
269 /* Rvalue expression. */
270 if (!lex_force_match (lexer, T_EQUALS))
272 compute->rvalue = parse_rvalue (lexer, lvalue, ds);
273 if (compute->rvalue == NULL)
276 add_transformation (ds, get_trns_class (lvalue), compute);
278 lvalue_finalize (lvalue, compute, dict);
283 lvalue_destroy (lvalue, dict);
284 compute_trns_free (compute);
285 return CMD_CASCADING_FAILURE;
288 /* Code common to COMPUTE and IF. */
290 static const struct trns_class *
291 get_trns_class (const struct lvalue *lvalue)
293 static const struct trns_class classes[2][2] = {
296 .execute = compute_str,
297 .destroy = compute_trns_free
301 .execute = compute_str_vec,
302 .destroy = compute_trns_free
306 .execute = compute_num,
307 .destroy = compute_trns_free
311 .execute = compute_num_vec,
312 .destroy = compute_trns_free
316 bool is_numeric = lvalue_get_type (lvalue) == VAL_NUMERIC;
317 bool is_vector = lvalue_is_vector (lvalue);
318 return &classes[is_numeric][is_vector];
321 /* Parses and returns an rvalue expression of the same type as
322 LVALUE, or a null pointer on failure. */
323 static struct expression *
324 parse_rvalue (struct lexer *lexer,
325 const struct lvalue *lvalue, struct dataset *ds)
327 if (lvalue->is_new_variable)
328 return expr_parse_new_variable (lexer, ds, var_get_name (lvalue->variable),
331 return expr_parse (lexer, ds, lvalue_get_type (lvalue));
334 /* Returns a new struct compute_trns after initializing its fields. */
335 static struct compute_trns *
336 compute_trns_create (void)
338 struct compute_trns *compute = xmalloc (sizeof *compute);
339 *compute = (struct compute_trns) { .test = NULL };
343 /* Deletes all the fields in COMPUTE. */
345 compute_trns_free (void *compute_)
347 struct compute_trns *compute = compute_;
351 msg_location_destroy (compute->lvalue_location);
352 expr_free (compute->test);
353 expr_free (compute->element);
354 expr_free (compute->rvalue);
360 /* Parses the target variable or vector element into a new
361 `struct lvalue', which is returned. */
362 static struct lvalue *
363 lvalue_parse (struct lexer *lexer, struct dataset *ds)
365 struct dictionary *dict = dataset_dict (ds);
367 struct lvalue *lvalue = xmalloc (sizeof *lvalue);
368 *lvalue = (struct lvalue) { .variable = NULL };
370 if (!lex_force_id (lexer))
373 int start_ofs = lex_ofs (lexer);
374 if (lex_next_token (lexer, 1) == T_LPAREN)
377 lvalue->vector = dict_lookup_vector (dict, lex_tokcstr (lexer));
378 if (lvalue->vector == NULL)
380 lex_error (lexer, _("There is no vector named %s."),
381 lex_tokcstr (lexer));
385 /* Vector element. */
387 if (!lex_force_match (lexer, T_LPAREN))
389 lvalue->element = expr_parse (lexer, ds, VAL_NUMERIC);
390 if (lvalue->element == NULL)
392 if (!lex_force_match (lexer, T_RPAREN))
398 const char *var_name = lex_tokcstr (lexer);
399 lvalue->variable = dict_lookup_var (dict, var_name);
400 if (lvalue->variable == NULL)
402 lvalue->variable = dict_create_var_assert (dict, var_name, 0);
403 lvalue->is_new_variable = true;
407 int end_ofs = lex_ofs (lexer) - 1;
408 lvalue->location = lex_ofs_location (lexer, start_ofs, end_ofs);
412 lvalue_destroy (lvalue, dict);
416 /* Returns the type (NUMERIC or ALPHA) of the target variable or
419 lvalue_get_type (const struct lvalue *lvalue)
421 return (lvalue->variable != NULL
422 ? var_get_type (lvalue->variable)
423 : vector_get_type (lvalue->vector));
426 /* Returns true if LVALUE has a vector as its target. */
428 lvalue_is_vector (const struct lvalue *lvalue)
430 return lvalue->vector != NULL;
433 /* Finalizes making LVALUE the target of COMPUTE, by creating the
434 target variable if necessary and setting fields in COMPUTE. */
436 lvalue_finalize (struct lvalue *lvalue,
437 struct compute_trns *compute,
438 struct dictionary *dict)
440 compute->lvalue_location = lvalue->location;
441 lvalue->location = NULL;
443 if (lvalue->vector == NULL)
445 compute->variable = lvalue->variable;
446 compute->width = var_get_width (compute->variable);
448 /* Goofy behavior, but compatible: Turn off LEAVE. */
449 if (!var_must_leave (compute->variable))
450 var_set_leave (compute->variable, false);
452 /* Prevent lvalue_destroy from deleting variable. */
453 lvalue->is_new_variable = false;
457 compute->vector = lvalue->vector;
458 compute->element = lvalue->element;
459 lvalue->element = NULL;
462 lvalue_destroy (lvalue, dict);
465 /* Destroys LVALUE. */
467 lvalue_destroy (struct lvalue *lvalue, struct dictionary *dict)
472 if (lvalue->is_new_variable)
473 dict_delete_var (dict, lvalue->variable);
474 expr_free (lvalue->element);
475 msg_location_destroy (lvalue->location);