1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2007, 2009, 2010, 2011 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/>. */
19 #include "language/expressions/evaluate.h"
23 #include "libpspp/assertion.h"
24 #include "libpspp/message.h"
25 #include "language/expressions/helpers.h"
26 #include "language/expressions/private.h"
27 #include "language/lexer/value-parser.h"
28 #include "libpspp/pool.h"
33 expr_evaluate (struct expression *e, const struct ccase *c, int case_idx,
36 struct dataset *ds = e->ds;
37 union operation_data *op = e->ops;
39 double *ns = e->number_stack;
40 struct substring *ss = e->string_stack;
42 /* Without a dictionary/dataset, the expression can't refer to variables,
43 and you don't need to specify a case when you evaluate the
44 expression. With a dictionary/dataset, the expression can refer
45 to variables, so you must specify a case when you evaluate the
47 assert ((c != NULL) == (e->ds != NULL));
49 pool_clear (e->eval_pool);
53 assert (op < e->ops + e->op_cnt);
54 switch (op++->operation)
63 const struct substring *s = &op++->string;
64 *ss++ = copy_string (e, s->string, s->length);
68 case OP_return_number:
69 *(double *) result = isfinite (ns[-1]) ? ns[-1] : SYSMIS;
72 case OP_return_string:
73 *(struct substring *) result = ss[-1];
76 #include "evaluate.inc"
85 expr_evaluate_num (struct expression *e, const struct ccase *c, int case_idx)
89 assert (e->type == OP_number || e->type == OP_boolean);
90 expr_evaluate (e, c, case_idx, &d);
95 expr_evaluate_str (struct expression *e, const struct ccase *c, int case_idx,
96 char *dst, size_t dst_size)
100 assert (e->type == OP_string);
101 assert ((dst == NULL) == (dst_size == 0));
102 expr_evaluate (e, c, case_idx, &s);
104 buf_copy_rpad (dst, dst_size, s.string, s.length, ' ');
107 #include "language/lexer/lexer.h"
108 #include "language/command.h"
111 cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
113 bool optimize = true;
114 int retval = CMD_FAILURE;
115 bool dump_postfix = false;
117 struct ccase *c = NULL;
119 struct dataset *ds = NULL;
123 struct expression *expr;
127 struct dictionary *d = NULL;
128 if (lex_match_id (lexer, "NOOPTIMIZE"))
130 else if (lex_match_id (lexer, "POSTFIX"))
132 else if (lex_match (lexer, T_LPAREN))
135 size_t old_value_cnt;
138 if (!lex_force_id (lexer))
140 name = xstrdup (lex_tokcstr (lexer));
143 if (!lex_force_match (lexer, T_EQUALS))
146 if (lex_is_number (lexer))
148 else if (lex_is_string (lexer))
149 width = ss_length (lex_tokss (lexer));
152 lex_error (lexer, _("expecting number or string"));
158 ds = dataset_create ();
159 d = dataset_dict (ds);
162 old_value_cnt = dict_get_next_value_idx (d);
163 v = dict_create_var (d, name, width);
166 msg (SE, _("Duplicate variable name %s."), name);
173 c = case_create (dict_get_proto (d));
175 c = case_unshare_and_resize (c, dict_get_proto (d));
177 if (!parse_value (lexer, case_data_rw (c, v), v))
180 if (!lex_force_match (lexer, T_RPAREN))
186 if (lex_token (lexer) != T_SLASH)
188 lex_force_match (lexer, T_SLASH);
194 expr = expr_parse_any (lexer, ds, optimize);
195 if (!expr || lex_end_of_command (lexer) != CMD_SUCCESS)
204 expr_debug_print_postfix (expr);
210 double d = expr_evaluate_num (expr, c, 0);
214 printf ("%.2f\n", d);
220 double b = expr_evaluate_num (expr, c, 0);
222 b == SYSMIS ? "sysmis" : b == 0.0 ? "false" : "true");
229 expr_evaluate (expr, c, 0, &s);
232 fwrite (s.string, s.length, 1, stdout);
242 retval = CMD_SUCCESS;
245 dataset_destroy (ds);
255 expr_debug_print_postfix (const struct expression *e)
259 for (i = 0; i < e->op_cnt; i++)
261 union operation_data *op = &e->ops[i];
264 switch (e->op_types[i])
267 if (op->operation == OP_return_number)
268 printf ("return_number");
269 else if (op->operation == OP_return_string)
270 printf ("return_string");
271 else if (is_function (op->operation))
272 printf ("%s", operations[op->operation].prototype);
273 else if (is_composite (op->operation))
274 printf ("%s", operations[op->operation].name);
276 printf ("%s:", operations[op->operation].name);
279 if (op->number != SYSMIS)
280 printf ("n<%g>", op->number);
282 printf ("n<SYSMIS>");
286 (int) op->string.length,
287 op->string.string != NULL ? op->string.string : "");
291 char str[FMT_STRING_LEN_MAX + 1];
292 fmt_to_string (op->format, str);
293 printf ("f<%s>", str);
297 printf ("v<%s>", var_get_name (op->variable));
300 printf ("vec<%s>", vector_get_name (op->vector));
303 printf ("i<%d>", op->integer);