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/>. */
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))
137 if (!lex_force_id (lexer))
139 name = xstrdup (lex_tokcstr (lexer));
142 if (!lex_force_match (lexer, T_EQUALS))
145 if (lex_is_number (lexer))
147 else if (lex_is_string (lexer))
148 width = ss_length (lex_tokss (lexer));
151 lex_error (lexer, _("expecting number or string"));
157 ds = dataset_create (NULL, "");
158 d = dataset_dict (ds);
161 v = dict_create_var (d, name, width);
164 msg (SE, _("Duplicate variable name %s."), name);
171 c = case_create (dict_get_proto (d));
173 c = case_unshare_and_resize (c, dict_get_proto (d));
175 if (!parse_value (lexer, case_data_rw (c, v), v))
178 if (!lex_force_match (lexer, T_RPAREN))
184 if (lex_token (lexer) != T_SLASH)
186 lex_force_match (lexer, T_SLASH);
192 expr = expr_parse_any (lexer, ds, optimize);
193 if (!expr || lex_end_of_command (lexer) != CMD_SUCCESS)
202 expr_debug_print_postfix (expr);
208 double d = expr_evaluate_num (expr, c, 0);
212 printf ("%.2f\n", d);
218 double b = expr_evaluate_num (expr, c, 0);
220 b == SYSMIS ? "sysmis" : b == 0.0 ? "false" : "true");
227 expr_evaluate (expr, c, 0, &s);
230 fwrite (s.string, s.length, 1, stdout);
240 retval = CMD_SUCCESS;
243 dataset_destroy (ds);
253 expr_debug_print_postfix (const struct expression *e)
257 for (i = 0; i < e->op_cnt; i++)
259 union operation_data *op = &e->ops[i];
262 switch (e->op_types[i])
265 if (op->operation == OP_return_number)
266 printf ("return_number");
267 else if (op->operation == OP_return_string)
268 printf ("return_string");
269 else if (is_function (op->operation))
270 printf ("%s", operations[op->operation].prototype);
271 else if (is_composite (op->operation))
272 printf ("%s", operations[op->operation].name);
274 printf ("%s:", operations[op->operation].name);
277 if (op->number != SYSMIS)
278 printf ("n<%g>", op->number);
280 printf ("n<SYSMIS>");
284 (int) op->string.length,
285 op->string.string != NULL ? op->string.string : "");
289 char str[FMT_STRING_LEN_MAX + 1];
290 fmt_to_string (op->format, str);
291 printf ("f<%s>", str);
295 printf ("v<%s>", var_get_name (op->variable));
298 printf ("vec<%s>", vector_get_name (op->vector));
301 printf ("i<%d>", op->integer);