1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2007 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 <libpspp/assertion.h>
22 #include <libpspp/message.h>
25 #include <libpspp/pool.h>
30 expr_evaluate (struct expression *e, const struct ccase *c, int case_idx,
33 struct dataset *ds = e->ds;
34 union operation_data *op = e->ops;
36 double *ns = e->number_stack;
37 struct substring *ss = e->string_stack;
39 /* Without a dictionary/dataset, the expression can't refer to variables,
40 and you don't need to specify a case when you evaluate the
41 expression. With a dictionary/dataset, the expression can refer
42 to variables, so you must specify a case when you evaluate the
44 assert ((c != NULL) == (e->ds != NULL));
46 pool_clear (e->eval_pool);
50 assert (op < e->ops + e->op_cnt);
51 switch (op++->operation)
60 const struct substring *s = &op++->string;
61 *ss++ = copy_string (e, s->string, s->length);
65 case OP_return_number:
66 *(double *) result = finite (ns[-1]) ? ns[-1] : SYSMIS;
69 case OP_return_string:
70 *(struct substring *) result = ss[-1];
73 #include "evaluate.inc"
82 expr_evaluate_num (struct expression *e, const struct ccase *c, int case_idx)
86 assert (e->type == OP_number || e->type == OP_boolean);
87 expr_evaluate (e, c, case_idx, &d);
92 expr_evaluate_str (struct expression *e, const struct ccase *c, int case_idx,
93 char *dst, size_t dst_size)
97 assert (e->type == OP_string);
98 assert ((dst == NULL) == (dst_size == 0));
99 expr_evaluate (e, c, case_idx, &s);
101 buf_copy_rpad (dst, dst_size, s.string, s.length);
104 #include <language/lexer/lexer.h>
105 #include <language/command.h>
108 cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
110 bool optimize = true;
111 int retval = CMD_FAILURE;
112 bool dump_postfix = false;
114 struct ccase *c = NULL;
116 struct dataset *ds = NULL;
118 struct expression *expr;
122 struct dictionary *d = NULL;
123 if (lex_match_id (lexer, "NOOPTIMIZE"))
125 else if (lex_match_id (lexer, "POSTFIX"))
127 else if (lex_match (lexer, '('))
129 char name[LONG_NAME_LEN + 1];
131 size_t old_value_cnt;
134 if (!lex_force_id (lexer))
136 strcpy (name, lex_tokid (lexer));
139 if (!lex_force_match (lexer, '='))
142 if (lex_is_number (lexer))
145 fprintf (stderr, "(%s = %.2f)", name, lex_tokval (lexer));
147 else if (lex_token (lexer) == T_STRING)
149 width = ds_length (lex_tokstr (lexer));
150 fprintf (stderr, "(%s = \"%.2s\")", name, ds_cstr (lex_tokstr (lexer)));
154 lex_error (lexer, _("expecting number or string"));
160 ds = create_dataset ();
161 d = dataset_dict (ds);
164 old_value_cnt = dict_get_next_value_idx (d);
165 v = dict_create_var (d, name, width);
168 msg (SE, _("Duplicate variable name %s."), name);
174 c = xmalloc (sizeof *c);
175 case_create (c, dict_get_next_value_idx (d));
178 case_resize (c, dict_get_next_value_idx (d));
180 if (lex_is_number (lexer))
181 case_data_rw (c, v)->f = lex_tokval (lexer);
183 memcpy (case_data_rw (c, v)->s, ds_data (lex_tokstr (lexer)),
187 if (!lex_force_match (lexer, ')'))
193 if (lex_token (lexer) != '/')
195 lex_force_match (lexer, '/');
200 fprintf(stderr, "; ");
201 fprintf (stderr, "%s => ", lex_rest_of_line (lexer));
204 expr = expr_parse_any (lexer, ds, optimize);
205 if (!expr || lex_end_of_command (lexer) != CMD_SUCCESS)
209 fprintf (stderr, "error\n");
214 expr_debug_print_postfix (expr);
220 double d = expr_evaluate_num (expr, c, 0);
222 fprintf (stderr, "sysmis\n");
224 fprintf (stderr, "%.2f\n", d);
230 double b = expr_evaluate_num (expr, c, 0);
231 fprintf (stderr, "%s\n",
232 b == SYSMIS ? "sysmis" : b == 0.0 ? "false" : "true");
239 expr_evaluate (expr, c, 0, &s);
242 fwrite (s.string, s.length, 1, stderr);
243 fputs ("\"\n", stderr);
252 retval = CMD_SUCCESS;
256 destroy_dataset (ds);
268 expr_debug_print_postfix (const struct expression *e)
272 for (i = 0; i < e->op_cnt; i++)
274 union operation_data *op = &e->ops[i];
277 switch (e->op_types[i])
280 if (op->operation == OP_return_number)
281 fprintf (stderr, "return_number");
282 else if (op->operation == OP_return_string)
283 fprintf (stderr, "return_string");
284 else if (is_function (op->operation))
285 fprintf (stderr, "%s", operations[op->operation].prototype);
286 else if (is_composite (op->operation))
287 fprintf (stderr, "%s", operations[op->operation].name);
289 fprintf (stderr, "%s:", operations[op->operation].name);
292 if (op->number != SYSMIS)
293 fprintf (stderr, "n<%g>", op->number);
295 fprintf (stderr, "n<SYSMIS>");
298 fprintf (stderr, "s<%.*s>",
299 (int) op->string.length,
300 op->string.string != NULL ? op->string.string : "");
304 char str[FMT_STRING_LEN_MAX + 1];
305 fmt_to_string (op->format, str);
306 fprintf (stderr, "f<%s>", str);
310 fprintf (stderr, "v<%s>", var_get_name (op->variable));
313 fprintf (stderr, "vec<%s>", vector_get_name (op->vector));
316 fprintf (stderr, "i<%d>", op->integer);
322 fprintf (stderr, "\n");