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/>. */
22 #include <data/case.h>
23 #include <data/dictionary.h>
24 #include <data/procedure.h>
25 #include <data/transformations.h>
26 #include <data/variable.h>
27 #include <data/vector.h>
28 #include <language/command.h>
29 #include <language/expressions/public.h>
30 #include <language/lexer/lexer.h>
31 #include <libpspp/message.h>
32 #include <libpspp/misc.h>
33 #include <libpspp/str.h>
38 #define _(msgid) gettext (msgid)
43 /* Target of a COMPUTE or IF assignment, either a variable or a
45 static struct lvalue *lvalue_parse (struct lexer *lexer, struct dataset *);
46 static int lvalue_get_type (const struct lvalue *);
47 static bool lvalue_is_vector (const struct lvalue *);
48 static void lvalue_finalize (struct lvalue *,
49 struct compute_trns *, struct dictionary *);
50 static void lvalue_destroy (struct lvalue *, struct dictionary *);
52 /* COMPUTE and IF transformation. */
55 /* Test expression (IF only). */
56 struct expression *test; /* Test expression. */
58 /* Variable lvalue, if variable != NULL. */
59 struct variable *variable; /* Destination variable, if any. */
60 int width; /* Lvalue string width; 0=numeric. */
62 /* Vector lvalue, if vector != NULL. */
63 const struct vector *vector; /* Destination vector, if any. */
64 struct expression *element; /* Destination vector element expr. */
67 struct expression *rvalue; /* Rvalue expression. */
70 static struct expression *parse_rvalue (struct lexer *lexer,
71 const struct lvalue *,
74 static struct compute_trns *compute_trns_create (void);
75 static trns_proc_func *get_proc_func (const struct lvalue *);
76 static trns_free_func compute_trns_free;
81 cmd_compute (struct lexer *lexer, struct dataset *ds)
83 struct dictionary *dict = dataset_dict (ds);
84 struct lvalue *lvalue = NULL;
85 struct compute_trns *compute = NULL;
87 compute = compute_trns_create ();
89 lvalue = lvalue_parse (lexer, ds);
93 if (!lex_force_match (lexer, '='))
95 compute->rvalue = parse_rvalue (lexer, lvalue, ds);
96 if (compute->rvalue == NULL)
99 add_transformation (ds, get_proc_func (lvalue), compute_trns_free, compute);
101 lvalue_finalize (lvalue, compute, dict);
103 return lex_end_of_command (lexer);
106 lvalue_destroy (lvalue, dict);
107 compute_trns_free (compute);
108 return CMD_CASCADING_FAILURE;
111 /* Transformation functions. */
113 /* Handle COMPUTE or IF with numeric target variable. */
115 compute_num (void *compute_, struct ccase *c, casenumber case_num)
117 struct compute_trns *compute = compute_;
119 if (compute->test == NULL
120 || expr_evaluate_num (compute->test, c, case_num) == 1.0)
121 case_data_rw (c, compute->variable)->f
122 = expr_evaluate_num (compute->rvalue, c, case_num);
124 return TRNS_CONTINUE;
127 /* Handle COMPUTE or IF with numeric vector element target
130 compute_num_vec (void *compute_, struct ccase *c, casenumber case_num)
132 struct compute_trns *compute = compute_;
134 if (compute->test == NULL
135 || expr_evaluate_num (compute->test, c, case_num) == 1.0)
137 double index; /* Index into the vector. */
138 int rindx; /* Rounded index value. */
140 index = expr_evaluate_num (compute->element, c, case_num);
141 rindx = floor (index + EPSILON);
143 || rindx < 1 || rindx > vector_get_var_cnt (compute->vector))
146 msg (SW, _("When executing COMPUTE: SYSMIS is not a valid value "
147 "as an index into vector %s."),
148 vector_get_name (compute->vector));
150 msg (SW, _("When executing COMPUTE: %g is not a valid value as "
151 "an index into vector %s."),
152 index, vector_get_name (compute->vector));
153 return TRNS_CONTINUE;
155 case_data_rw (c, vector_get_var (compute->vector, rindx - 1))->f
156 = expr_evaluate_num (compute->rvalue, c, case_num);
159 return TRNS_CONTINUE;
162 /* Handle COMPUTE or IF with string target variable. */
164 compute_str (void *compute_, struct ccase *c, casenumber case_num)
166 struct compute_trns *compute = compute_;
168 if (compute->test == NULL
169 || expr_evaluate_num (compute->test, c, case_num) == 1.0)
170 expr_evaluate_str (compute->rvalue, c, case_num,
171 case_data_rw (c, compute->variable)->s, compute->width);
173 return TRNS_CONTINUE;
176 /* Handle COMPUTE or IF with string vector element target
179 compute_str_vec (void *compute_, struct ccase *c, casenumber case_num)
181 struct compute_trns *compute = compute_;
183 if (compute->test == NULL
184 || expr_evaluate_num (compute->test, c, case_num) == 1.0)
186 double index; /* Index into the vector. */
187 int rindx; /* Rounded index value. */
188 struct variable *vr; /* Variable reference by indexed vector. */
190 index = expr_evaluate_num (compute->element, c, case_num);
191 rindx = floor (index + EPSILON);
194 msg (SW, _("When executing COMPUTE: SYSMIS is not a valid "
195 "value as an index into vector %s."),
196 vector_get_name (compute->vector));
197 return TRNS_CONTINUE;
199 else if (rindx < 1 || rindx > vector_get_var_cnt (compute->vector))
201 msg (SW, _("When executing COMPUTE: %g is not a valid value as "
202 "an index into vector %s."),
203 index, vector_get_name (compute->vector));
204 return TRNS_CONTINUE;
207 vr = vector_get_var (compute->vector, rindx - 1);
208 expr_evaluate_str (compute->rvalue, c, case_num,
209 case_data_rw (c, vr)->s,
213 return TRNS_CONTINUE;
219 cmd_if (struct lexer *lexer, struct dataset *ds)
221 struct dictionary *dict = dataset_dict (ds);
222 struct compute_trns *compute = NULL;
223 struct lvalue *lvalue = NULL;
225 compute = compute_trns_create ();
227 /* Test expression. */
228 compute->test = expr_parse (lexer, ds, EXPR_BOOLEAN);
229 if (compute->test == NULL)
232 /* Lvalue variable. */
233 lvalue = lvalue_parse (lexer, ds);
237 /* Rvalue expression. */
238 if (!lex_force_match (lexer, '='))
240 compute->rvalue = parse_rvalue (lexer, lvalue, ds);
241 if (compute->rvalue == NULL)
244 add_transformation (ds, get_proc_func (lvalue), compute_trns_free, compute);
246 lvalue_finalize (lvalue, compute, dict);
248 return lex_end_of_command (lexer);
251 lvalue_destroy (lvalue, dict);
252 compute_trns_free (compute);
253 return CMD_CASCADING_FAILURE;
256 /* Code common to COMPUTE and IF. */
258 static trns_proc_func *
259 get_proc_func (const struct lvalue *lvalue)
261 bool is_numeric = lvalue_get_type (lvalue) == VAL_NUMERIC;
262 bool is_vector = lvalue_is_vector (lvalue);
265 ? (is_vector ? compute_num_vec : compute_num)
266 : (is_vector ? compute_str_vec : compute_str));
269 /* Parses and returns an rvalue expression of the same type as
270 LVALUE, or a null pointer on failure. */
271 static struct expression *
272 parse_rvalue (struct lexer *lexer,
273 const struct lvalue *lvalue, struct dataset *ds)
275 bool is_numeric = lvalue_get_type (lvalue) == VAL_NUMERIC;
277 return expr_parse (lexer, ds, is_numeric ? EXPR_NUMBER : EXPR_STRING);
280 /* Returns a new struct compute_trns after initializing its fields. */
281 static struct compute_trns *
282 compute_trns_create (void)
284 struct compute_trns *compute = xmalloc (sizeof *compute);
285 compute->test = NULL;
286 compute->variable = NULL;
287 compute->vector = NULL;
288 compute->element = NULL;
289 compute->rvalue = NULL;
293 /* Deletes all the fields in COMPUTE. */
295 compute_trns_free (void *compute_)
297 struct compute_trns *compute = compute_;
301 expr_free (compute->test);
302 expr_free (compute->element);
303 expr_free (compute->rvalue);
309 /* COMPUTE or IF target variable or vector element.
310 For a variable, the `variable' member is non-null.
311 For a vector element, the `vector' member is non-null. */
314 struct variable *variable; /* Destination variable. */
315 bool is_new_variable; /* Did we create the variable? */
317 const struct vector *vector; /* Destination vector, if any, or NULL. */
318 struct expression *element; /* Destination vector element, or NULL. */
321 /* Parses the target variable or vector element into a new
322 `struct lvalue', which is returned. */
323 static struct lvalue *
324 lvalue_parse (struct lexer *lexer, struct dataset *ds)
326 struct dictionary *dict = dataset_dict (ds);
327 struct lvalue *lvalue;
329 lvalue = xmalloc (sizeof *lvalue);
330 lvalue->variable = NULL;
331 lvalue->is_new_variable = false;
332 lvalue->vector = NULL;
333 lvalue->element = NULL;
335 if (!lex_force_id (lexer))
338 if (lex_look_ahead (lexer) == '(')
341 lvalue->vector = dict_lookup_vector (dict, lex_tokid (lexer));
342 if (lvalue->vector == NULL)
344 msg (SE, _("There is no vector named %s."), lex_tokid (lexer));
348 /* Vector element. */
350 if (!lex_force_match (lexer, '('))
352 lvalue->element = expr_parse (lexer, ds, EXPR_NUMBER);
353 if (lvalue->element == NULL)
355 if (!lex_force_match (lexer, ')'))
361 const char *var_name = lex_tokid (lexer);
362 lvalue->variable = dict_lookup_var (dict, var_name);
363 if (lvalue->variable == NULL)
365 lvalue->variable = dict_create_var_assert (dict, var_name, 0);
366 lvalue->is_new_variable = true;
373 lvalue_destroy (lvalue, dict);
377 /* Returns the type (NUMERIC or ALPHA) of the target variable or
380 lvalue_get_type (const struct lvalue *lvalue)
382 return (lvalue->variable != NULL
383 ? var_get_type (lvalue->variable)
384 : vector_get_type (lvalue->vector));
387 /* Returns true if LVALUE has a vector as its target. */
389 lvalue_is_vector (const struct lvalue *lvalue)
391 return lvalue->vector != NULL;
394 /* Finalizes making LVALUE the target of COMPUTE, by creating the
395 target variable if necessary and setting fields in COMPUTE. */
397 lvalue_finalize (struct lvalue *lvalue,
398 struct compute_trns *compute,
399 struct dictionary *dict)
401 if (lvalue->vector == NULL)
403 compute->variable = lvalue->variable;
404 compute->width = var_get_width (compute->variable);
406 /* Goofy behavior, but compatible: Turn off LEAVE. */
407 if (!var_must_leave (compute->variable))
408 var_set_leave (compute->variable, false);
410 /* Prevent lvalue_destroy from deleting variable. */
411 lvalue->is_new_variable = false;
415 compute->vector = lvalue->vector;
416 compute->element = lvalue->element;
417 lvalue->element = NULL;
420 lvalue_destroy (lvalue, dict);
423 /* Destroys LVALUE. */
425 lvalue_destroy (struct lvalue *lvalue, struct dictionary *dict)
430 if (lvalue->is_new_variable)
431 dict_delete_var (dict, lvalue->variable);
432 expr_free (lvalue->element);