1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000 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/>. */
21 #include <data/case.h>
22 #include <data/dictionary.h>
23 #include <data/procedure.h>
24 #include <data/transformations.h>
25 #include <data/variable.h>
26 #include <data/vector.h>
27 #include <language/command.h>
28 #include <language/expressions/public.h>
29 #include <language/lexer/lexer.h>
30 #include <libpspp/alloc.h>
31 #include <libpspp/message.h>
32 #include <libpspp/message.h>
33 #include <libpspp/misc.h>
34 #include <libpspp/str.h>
37 #define _(msgid) gettext (msgid)
42 /* Target of a COMPUTE or IF assignment, either a variable or a
44 static struct lvalue *lvalue_parse (struct lexer *lexer, struct dataset *);
45 static int lvalue_get_type (const struct lvalue *);
46 static bool lvalue_is_vector (const struct lvalue *);
47 static void lvalue_finalize (struct lvalue *,
48 struct compute_trns *, struct dictionary *);
49 static void lvalue_destroy (struct lvalue *, struct dictionary *);
51 /* COMPUTE and IF transformation. */
54 /* Test expression (IF only). */
55 struct expression *test; /* Test expression. */
57 /* Variable lvalue, if variable != NULL. */
58 struct variable *variable; /* Destination variable, if any. */
59 int width; /* Lvalue string width; 0=numeric. */
61 /* Vector lvalue, if vector != NULL. */
62 const struct vector *vector; /* Destination vector, if any. */
63 struct expression *element; /* Destination vector element expr. */
66 struct expression *rvalue; /* Rvalue expression. */
69 static struct expression *parse_rvalue (struct lexer *lexer,
70 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;
80 cmd_compute (struct lexer *lexer, struct dataset *ds)
82 struct dictionary *dict = dataset_dict (ds);
83 struct lvalue *lvalue = NULL;
84 struct compute_trns *compute = NULL;
86 compute = compute_trns_create ();
88 lvalue = lvalue_parse (lexer, ds);
92 if (!lex_force_match (lexer, '='))
94 compute->rvalue = parse_rvalue (lexer, lvalue, ds);
95 if (compute->rvalue == NULL)
98 add_transformation (ds, get_proc_func (lvalue), compute_trns_free, compute);
100 lvalue_finalize (lvalue, compute, dict);
102 return lex_end_of_command (lexer);
105 lvalue_destroy (lvalue, dict);
106 compute_trns_free (compute);
107 return CMD_CASCADING_FAILURE;
110 /* Transformation functions. */
112 /* Handle COMPUTE or IF with numeric target variable. */
114 compute_num (void *compute_, struct ccase *c, casenumber case_num)
116 struct compute_trns *compute = compute_;
118 if (compute->test == NULL
119 || expr_evaluate_num (compute->test, c, case_num) == 1.0)
120 case_data_rw (c, compute->variable)->f
121 = expr_evaluate_num (compute->rvalue, c, case_num);
123 return TRNS_CONTINUE;
126 /* Handle COMPUTE or IF with numeric vector element target
129 compute_num_vec (void *compute_, struct ccase *c, casenumber case_num)
131 struct compute_trns *compute = compute_;
133 if (compute->test == NULL
134 || expr_evaluate_num (compute->test, c, case_num) == 1.0)
136 double index; /* Index into the vector. */
137 int rindx; /* Rounded index value. */
139 index = expr_evaluate_num (compute->element, c, case_num);
140 rindx = floor (index + EPSILON);
142 || rindx < 1 || rindx > vector_get_var_cnt (compute->vector))
145 msg (SW, _("When executing COMPUTE: SYSMIS is not a valid value "
146 "as an index into vector %s."),
147 vector_get_name (compute->vector));
149 msg (SW, _("When executing COMPUTE: %g is not a valid value as "
150 "an index into vector %s."),
151 index, vector_get_name (compute->vector));
152 return TRNS_CONTINUE;
154 case_data_rw (c, vector_get_var (compute->vector, rindx - 1))->f
155 = expr_evaluate_num (compute->rvalue, c, case_num);
158 return TRNS_CONTINUE;
161 /* Handle COMPUTE or IF with string target variable. */
163 compute_str (void *compute_, struct ccase *c, casenumber case_num)
165 struct compute_trns *compute = compute_;
167 if (compute->test == NULL
168 || expr_evaluate_num (compute->test, c, case_num) == 1.0)
169 expr_evaluate_str (compute->rvalue, c, case_num,
170 case_data_rw (c, compute->variable)->s, compute->width);
172 return TRNS_CONTINUE;
175 /* Handle COMPUTE or IF with string vector element target
178 compute_str_vec (void *compute_, struct ccase *c, casenumber case_num)
180 struct compute_trns *compute = compute_;
182 if (compute->test == NULL
183 || expr_evaluate_num (compute->test, c, case_num) == 1.0)
185 double index; /* Index into the vector. */
186 int rindx; /* Rounded index value. */
187 struct variable *vr; /* Variable reference by indexed vector. */
189 index = expr_evaluate_num (compute->element, c, case_num);
190 rindx = floor (index + EPSILON);
193 msg (SW, _("When executing COMPUTE: SYSMIS is not a valid "
194 "value as an index into vector %s."),
195 vector_get_name (compute->vector));
196 return TRNS_CONTINUE;
198 else if (rindx < 1 || rindx > vector_get_var_cnt (compute->vector))
200 msg (SW, _("When executing COMPUTE: %g is not a valid value as "
201 "an index into vector %s."),
202 index, vector_get_name (compute->vector));
203 return TRNS_CONTINUE;
206 vr = vector_get_var (compute->vector, rindx - 1);
207 expr_evaluate_str (compute->rvalue, c, case_num,
208 case_data_rw (c, vr)->s,
212 return TRNS_CONTINUE;
218 cmd_if (struct lexer *lexer, struct dataset *ds)
220 struct dictionary *dict = dataset_dict (ds);
221 struct compute_trns *compute = NULL;
222 struct lvalue *lvalue = NULL;
224 compute = compute_trns_create ();
226 /* Test expression. */
227 compute->test = expr_parse (lexer, ds, EXPR_BOOLEAN);
228 if (compute->test == NULL)
231 /* Lvalue variable. */
232 lvalue = lvalue_parse (lexer, ds);
236 /* Rvalue expression. */
237 if (!lex_force_match (lexer, '='))
239 compute->rvalue = parse_rvalue (lexer, lvalue, ds);
240 if (compute->rvalue == NULL)
243 add_transformation (ds, get_proc_func (lvalue), compute_trns_free, compute);
245 lvalue_finalize (lvalue, compute, dict);
247 return lex_end_of_command (lexer);
250 lvalue_destroy (lvalue, dict);
251 compute_trns_free (compute);
252 return CMD_CASCADING_FAILURE;
255 /* Code common to COMPUTE and IF. */
257 static trns_proc_func *
258 get_proc_func (const struct lvalue *lvalue)
260 bool is_numeric = lvalue_get_type (lvalue) == VAR_NUMERIC;
261 bool is_vector = lvalue_is_vector (lvalue);
264 ? (is_vector ? compute_num_vec : compute_num)
265 : (is_vector ? compute_str_vec : compute_str));
268 /* Parses and returns an rvalue expression of the same type as
269 LVALUE, or a null pointer on failure. */
270 static struct expression *
271 parse_rvalue (struct lexer *lexer,
272 const struct lvalue *lvalue, struct dataset *ds)
274 bool is_numeric = lvalue_get_type (lvalue) == VAR_NUMERIC;
276 return expr_parse (lexer, ds, is_numeric ? EXPR_NUMBER : EXPR_STRING);
279 /* Returns a new struct compute_trns after initializing its fields. */
280 static struct compute_trns *
281 compute_trns_create (void)
283 struct compute_trns *compute = xmalloc (sizeof *compute);
284 compute->test = NULL;
285 compute->variable = NULL;
286 compute->vector = NULL;
287 compute->element = NULL;
288 compute->rvalue = NULL;
292 /* Deletes all the fields in COMPUTE. */
294 compute_trns_free (void *compute_)
296 struct compute_trns *compute = compute_;
300 expr_free (compute->test);
301 expr_free (compute->element);
302 expr_free (compute->rvalue);
308 /* COMPUTE or IF target variable or vector element.
309 For a variable, the `variable' member is non-null.
310 For a vector element, the `vector' member is non-null. */
313 struct variable *variable; /* Destination variable. */
314 bool is_new_variable; /* Did we create the variable? */
316 const struct vector *vector; /* Destination vector, if any, or NULL. */
317 struct expression *element; /* Destination vector element, or NULL. */
320 /* Parses the target variable or vector element into a new
321 `struct lvalue', which is returned. */
322 static struct lvalue *
323 lvalue_parse (struct lexer *lexer, struct dataset *ds)
325 struct dictionary *dict = dataset_dict (ds);
326 struct lvalue *lvalue;
328 lvalue = xmalloc (sizeof *lvalue);
329 lvalue->variable = NULL;
330 lvalue->is_new_variable = false;
331 lvalue->vector = NULL;
332 lvalue->element = NULL;
334 if (!lex_force_id (lexer))
337 if (lex_look_ahead (lexer) == '(')
340 lvalue->vector = dict_lookup_vector (dict, lex_tokid (lexer));
341 if (lvalue->vector == NULL)
343 msg (SE, _("There is no vector named %s."), lex_tokid (lexer));
347 /* Vector element. */
349 if (!lex_force_match (lexer, '('))
351 lvalue->element = expr_parse (lexer, ds, EXPR_NUMBER);
352 if (lvalue->element == NULL)
354 if (!lex_force_match (lexer, ')'))
360 const char *var_name = lex_tokid (lexer);
361 lvalue->variable = dict_lookup_var (dict, var_name);
362 if (lvalue->variable == NULL)
364 lvalue->variable = dict_create_var_assert (dict, var_name, 0);
365 lvalue->is_new_variable = true;
372 lvalue_destroy (lvalue, dict);
376 /* Returns the type (NUMERIC or ALPHA) of the target variable or
379 lvalue_get_type (const struct lvalue *lvalue)
381 return (lvalue->variable != NULL
382 ? var_get_type (lvalue->variable)
383 : vector_get_type (lvalue->vector));
386 /* Returns true if LVALUE has a vector as its target. */
388 lvalue_is_vector (const struct lvalue *lvalue)
390 return lvalue->vector != NULL;
393 /* Finalizes making LVALUE the target of COMPUTE, by creating the
394 target variable if necessary and setting fields in COMPUTE. */
396 lvalue_finalize (struct lvalue *lvalue,
397 struct compute_trns *compute,
398 struct dictionary *dict)
400 if (lvalue->vector == NULL)
402 compute->variable = lvalue->variable;
403 compute->width = var_get_width (compute->variable);
405 /* Goofy behavior, but compatible: Turn off LEAVE. */
406 if (!var_must_leave (compute->variable))
407 var_set_leave (compute->variable, false);
409 /* Prevent lvalue_destroy from deleting variable. */
410 lvalue->is_new_variable = false;
414 compute->vector = lvalue->vector;
415 compute->element = lvalue->element;
416 lvalue->element = NULL;
419 lvalue_destroy (lvalue, dict);
422 /* Destroys LVALUE. */
424 lvalue_destroy (struct lvalue *lvalue, struct dictionary *dict)
429 if (lvalue->is_new_variable)
430 dict_delete_var (dict, lvalue->variable);
431 expr_free (lvalue->element);