expressions: Convert SYSMIS into int as INT_MIN during optimization too.
[pspp.git] / src / language / expressions / evaluate.c
blob8b47f445ee050df33f112c25a2c85a526d9f7a4d
1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2007, 2009, 2010, 2011, 2012 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/>. */
17 #include <config.h>
19 #include "language/expressions/private.h"
20 #include "evaluate.h"
22 #include <ctype.h>
24 #include "libpspp/assertion.h"
25 #include "libpspp/message.h"
26 #include "language/expressions/helpers.h"
27 #include "language/lexer/format-parser.h"
28 #include "language/lexer/value-parser.h"
29 #include "libpspp/pool.h"
30 #include "output/driver.h"
32 #include "xalloc.h"
34 static void
35 expr_evaluate (struct expression *e, const struct ccase *c, int case_idx,
36 void *result)
38 struct dataset *ds = e->ds;
39 union operation_data *op = e->ops;
41 double *ns = e->number_stack;
42 struct substring *ss = e->string_stack;
44 /* Without a dictionary/dataset, the expression can't refer to variables,
45 and you don't need to specify a case when you evaluate the
46 expression. With a dictionary/dataset, the expression can refer
47 to variables, so you must specify a case when you evaluate the
48 expression. */
49 assert ((c != NULL) == (e->ds != NULL));
51 pool_clear (e->eval_pool);
53 for (;;)
55 assert (op < e->ops + e->n_ops);
56 switch (op++->operation)
58 case OP_number:
59 case OP_boolean:
60 *ns++ = op++->number;
61 break;
63 case OP_string:
65 const struct substring *s = &op++->string;
66 *ss++ = copy_string (e, s->string, s->length);
68 break;
70 case OP_return_number:
71 *(double *) result = isfinite (ns[-1]) ? ns[-1] : SYSMIS;
72 return;
74 case OP_return_string:
75 *(struct substring *) result = ss[-1];
76 return;
78 #include "evaluate.inc"
80 default:
81 NOT_REACHED ();
86 double
87 expr_evaluate_num (struct expression *e, const struct ccase *c, int case_idx)
89 double d;
91 assert (e->type == OP_number || e->type == OP_boolean);
92 expr_evaluate (e, c, case_idx, &d);
93 return d;
96 void
97 expr_evaluate_str (struct expression *e, const struct ccase *c, int case_idx,
98 char *dst, size_t dst_size)
100 struct substring s;
102 assert (e->type == OP_string);
103 assert ((dst == NULL) == (dst_size == 0));
104 expr_evaluate (e, c, case_idx, &s);
106 buf_copy_rpad (dst, dst_size, s.string, s.length, ' ');
109 #include "language/lexer/lexer.h"
110 #include "language/command.h"
112 static bool default_optimize = true;
115 cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
117 bool optimize = default_optimize;
118 int retval = CMD_FAILURE;
119 bool dump_postfix = false;
120 bool set_defaults = false;
122 struct ccase *c = NULL;
124 struct dataset *ds = NULL;
126 char *name = NULL;
127 char *title = NULL;
128 struct fmt_spec format;
129 bool has_format = false;
131 struct expression *expr;
133 struct dictionary *d = NULL;
135 for (;;)
137 if (lex_match_id (lexer, "NOOPTIMIZE"))
138 optimize = false;
139 else if (lex_match_id (lexer, "OPTIMIZE"))
140 optimize = true;
141 else if (lex_match_id (lexer, "POSTFIX"))
142 dump_postfix = 1;
143 else if (lex_match_id (lexer, "SET"))
144 set_defaults = true;
145 else if (lex_match (lexer, T_LPAREN))
147 struct variable *v;
149 if (!lex_force_id (lexer))
150 goto done;
151 name = xstrdup (lex_tokcstr (lexer));
153 lex_get (lexer);
154 if (!lex_force_match (lexer, T_EQUALS))
155 goto done;
157 union value value;
158 int width;
159 if (lex_is_number (lexer))
161 width = 0;
162 value.f = lex_number (lexer);
163 lex_get (lexer);
165 else if (lex_match_id (lexer, "SYSMIS"))
167 width = 0;
168 value.f = SYSMIS;
170 else if (lex_is_string (lexer))
172 width = ss_length (lex_tokss (lexer));
173 value.s = CHAR_CAST (uint8_t *, ss_xstrdup (lex_tokss (lexer)));
174 lex_get (lexer);
176 else
178 lex_error (lexer, _("expecting number or string"));
179 goto done;
182 if (ds == NULL)
184 ds = dataset_create (NULL, "");
185 d = dataset_dict (ds);
188 v = dict_create_var (d, name, width);
189 if (v == NULL)
191 msg (SE, _("Duplicate variable name %s."), name);
192 value_destroy (&value, width);
193 goto done;
195 free (name);
196 name = NULL;
198 if (lex_match_id (lexer, "MISSING"))
200 struct missing_values mv;
201 mv_init (&mv, width);
202 mv_add_value (&mv, &value);
203 var_set_missing_values (v, &mv);
204 mv_destroy (&mv);
207 if (c == NULL)
208 c = case_create (dict_get_proto (d));
209 else
210 c = case_unshare_and_resize (c, dict_get_proto (d));
211 value_swap (case_data_rw (c, v), &value);
212 value_destroy (&value, width);
214 if (!lex_force_match (lexer, T_RPAREN))
215 goto done;
217 else if (lex_match_id (lexer, "VECTOR"))
219 struct variable **vars;
220 size_t n;
221 dict_get_vars_mutable (d, &vars, &n, 0);
222 dict_create_vector_assert (d, "V", vars, n);
223 free (vars);
225 else if (lex_match_id (lexer, "FORMAT"))
227 lex_match (lexer, T_EQUALS);
228 if (!parse_format_specifier (lexer, &format)
229 || !fmt_check_output (&format)
230 || !fmt_check_type_compat (&format, VAL_NUMERIC))
231 goto done;
232 has_format = true;
234 else
235 break;
238 if (set_defaults)
240 retval = CMD_SUCCESS;
241 default_optimize = optimize;
242 goto done;
245 if (!lex_force_match (lexer, T_SLASH))
246 goto done;
248 for (size_t i = 1; ; i++)
249 if (lex_next_token (lexer, i) == T_ENDCMD)
251 title = lex_next_representation (lexer, 0, i - 1);
252 break;
255 expr = expr_parse_any (lexer, ds, optimize);
256 if (!expr || lex_end_of_command (lexer) != CMD_SUCCESS)
258 if (expr != NULL)
259 expr_free (expr);
260 output_log ("%s => error", title);
261 goto done;
264 if (dump_postfix)
265 expr_debug_print_postfix (expr);
266 else
267 switch (expr->type)
269 case OP_number:
270 case OP_num_vec_elem:
272 double d = expr_evaluate_num (expr, c, 0);
273 if (has_format)
275 char *output = data_out (&(const union value) { .f = d },
276 NULL, &format,
277 settings_get_fmt_settings ());
278 output_log ("%s => %s", title, output);
279 free (output);
281 else if (d == SYSMIS)
282 output_log ("%s => sysmis", title);
283 else
284 output_log ("%s => %.2f", title, d);
286 break;
288 case OP_boolean:
290 double b = expr_evaluate_num (expr, c, 0);
291 output_log ("%s => %s", title,
292 b == SYSMIS ? "sysmis" : b == 0.0 ? "false" : "true");
294 break;
296 case OP_string:
298 struct substring out;
299 expr_evaluate (expr, c, 0, &out);
300 output_log ("%s => \"%.*s\"", title, (int) out.length, out.string);
301 break;
304 default:
305 NOT_REACHED ();
308 expr_free (expr);
309 retval = CMD_SUCCESS;
311 done:
312 dataset_destroy (ds);
314 case_unref (c);
316 free (name);
317 free (title);
319 return retval;
322 void
323 expr_debug_print_postfix (const struct expression *e)
325 struct string s = DS_EMPTY_INITIALIZER;
327 for (size_t i = 0; i < e->n_ops; i++)
329 union operation_data *op = &e->ops[i];
330 if (i > 0)
331 ds_put_byte (&s, ' ');
332 switch (e->op_types[i])
334 case OP_operation:
335 if (op->operation == OP_return_number)
336 ds_put_cstr (&s, "return_number");
337 else if (op->operation == OP_return_string)
338 ds_put_cstr (&s, "return_string");
339 else if (is_function (op->operation))
340 ds_put_format (&s, "%s", operations[op->operation].prototype);
341 else if (is_composite (op->operation))
342 ds_put_format (&s, "%s", operations[op->operation].name);
343 else
344 ds_put_format (&s, "%s:", operations[op->operation].name);
345 break;
346 case OP_number:
347 if (op->number != SYSMIS)
348 ds_put_format (&s, "n<%g>", op->number);
349 else
350 ds_put_cstr (&s, "n<SYSMIS>");
351 break;
352 case OP_string:
353 ds_put_cstr (&s, "s<");
354 ds_put_substring (&s, op->string);
355 ds_put_byte (&s, '>');
356 break;
357 case OP_format:
359 char str[FMT_STRING_LEN_MAX + 1];
360 fmt_to_string (op->format, str);
361 ds_put_format (&s, "f<%s>", str);
363 break;
364 case OP_variable:
365 ds_put_format (&s, "v<%s>", var_get_name (op->variable));
366 break;
367 case OP_vector:
368 ds_put_format (&s, "vec<%s>", vector_get_name (op->vector));
369 break;
370 case OP_integer:
371 ds_put_format (&s, "i<%d>", op->integer);
372 break;
373 case OP_expr_node:
374 ds_put_cstr (&s, "expr_node");
375 break;
376 default:
377 NOT_REACHED ();
380 output_log_nocopy (ds_steal_cstr (&s));