1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2007, 2009 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>
23 #include <language/expressions/helpers.h>
24 #include <language/expressions/private.h>
25 #include <language/lexer/value-parser.h>
26 #include <libpspp/pool.h>
31 expr_evaluate (struct expression *e, const struct ccase *c, int case_idx,
34 struct dataset *ds = e->ds;
35 union operation_data *op = e->ops;
37 double *ns = e->number_stack;
38 struct substring *ss = e->string_stack;
40 /* Without a dictionary/dataset, the expression can't refer to variables,
41 and you don't need to specify a case when you evaluate the
42 expression. With a dictionary/dataset, the expression can refer
43 to variables, so you must specify a case when you evaluate the
45 assert ((c != NULL) == (e->ds != NULL));
47 pool_clear (e->eval_pool);
51 assert (op < e->ops + e->op_cnt);
52 switch (op++->operation)
61 const struct substring *s = &op++->string;
62 *ss++ = copy_string (e, s->string, s->length);
66 case OP_return_number:
67 *(double *) result = isfinite (ns[-1]) ? ns[-1] : SYSMIS;
70 case OP_return_string:
71 *(struct substring *) result = ss[-1];
74 #include "evaluate.inc"
83 expr_evaluate_num (struct expression *e, const struct ccase *c, int case_idx)
87 assert (e->type == OP_number || e->type == OP_boolean);
88 expr_evaluate (e, c, case_idx, &d);
93 expr_evaluate_str (struct expression *e, const struct ccase *c, int case_idx,
94 char *dst, size_t dst_size)
98 assert (e->type == OP_string);
99 assert ((dst == NULL) == (dst_size == 0));
100 expr_evaluate (e, c, case_idx, &s);
102 buf_copy_rpad (dst, dst_size, s.string, s.length, ' ');
105 #include <language/lexer/lexer.h>
106 #include <language/command.h>
109 cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
111 bool optimize = true;
112 int retval = CMD_FAILURE;
113 bool dump_postfix = false;
115 struct ccase *c = NULL;
117 struct dataset *ds = NULL;
119 struct expression *expr;
123 struct dictionary *d = NULL;
124 if (lex_match_id (lexer, "NOOPTIMIZE"))
126 else if (lex_match_id (lexer, "POSTFIX"))
128 else if (lex_match (lexer, '('))
130 char name[VAR_NAME_LEN + 1];
132 size_t old_value_cnt;
135 if (!lex_force_id (lexer))
137 strcpy (name, lex_tokid (lexer));
140 if (!lex_force_match (lexer, '='))
143 if (lex_is_number (lexer))
146 fprintf (stderr, "(%s = %.2f)", name, lex_tokval (lexer));
148 else if (lex_token (lexer) == T_STRING)
150 width = ds_length (lex_tokstr (lexer));
151 fprintf (stderr, "(%s = \"%.2s\")", name, ds_cstr (lex_tokstr (lexer)));
155 lex_error (lexer, _("expecting number or string"));
161 ds = create_dataset ();
162 d = dataset_dict (ds);
165 old_value_cnt = dict_get_next_value_idx (d);
166 v = dict_create_var (d, name, width);
169 msg (SE, _("Duplicate variable name %s."), name);
174 c = case_create (dict_get_proto (d));
176 c = case_unshare_and_resize (c, dict_get_proto (d));
178 if (!parse_value (lexer, case_data_rw (c, v), var_get_width (v)))
182 if (!lex_force_match (lexer, ')'))
188 if (lex_token (lexer) != '/')
190 lex_force_match (lexer, '/');
195 fprintf(stderr, "; ");
196 fprintf (stderr, "%s => ", lex_rest_of_line (lexer));
199 expr = expr_parse_any (lexer, ds, optimize);
200 if (!expr || lex_end_of_command (lexer) != CMD_SUCCESS)
204 fprintf (stderr, "error\n");
209 expr_debug_print_postfix (expr);
215 double d = expr_evaluate_num (expr, c, 0);
217 fprintf (stderr, "sysmis\n");
219 fprintf (stderr, "%.2f\n", d);
225 double b = expr_evaluate_num (expr, c, 0);
226 fprintf (stderr, "%s\n",
227 b == SYSMIS ? "sysmis" : b == 0.0 ? "false" : "true");
234 expr_evaluate (expr, c, 0, &s);
237 fwrite (s.string, s.length, 1, stderr);
238 fputs ("\"\n", stderr);
247 retval = CMD_SUCCESS;
251 destroy_dataset (ds);
259 expr_debug_print_postfix (const struct expression *e)
263 for (i = 0; i < e->op_cnt; i++)
265 union operation_data *op = &e->ops[i];
268 switch (e->op_types[i])
271 if (op->operation == OP_return_number)
272 fprintf (stderr, "return_number");
273 else if (op->operation == OP_return_string)
274 fprintf (stderr, "return_string");
275 else if (is_function (op->operation))
276 fprintf (stderr, "%s", operations[op->operation].prototype);
277 else if (is_composite (op->operation))
278 fprintf (stderr, "%s", operations[op->operation].name);
280 fprintf (stderr, "%s:", operations[op->operation].name);
283 if (op->number != SYSMIS)
284 fprintf (stderr, "n<%g>", op->number);
286 fprintf (stderr, "n<SYSMIS>");
289 fprintf (stderr, "s<%.*s>",
290 (int) op->string.length,
291 op->string.string != NULL ? op->string.string : "");
295 char str[FMT_STRING_LEN_MAX + 1];
296 fmt_to_string (op->format, str);
297 fprintf (stderr, "f<%s>", str);
301 fprintf (stderr, "v<%s>", var_get_name (op->variable));
304 fprintf (stderr, "vec<%s>", vector_get_name (op->vector));
307 fprintf (stderr, "i<%d>", op->integer);
313 fprintf (stderr, "\n");