1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 #include <libpspp/alloc.h>
25 #include <libpspp/assertion.h>
26 #include <libpspp/message.h>
29 #include <libpspp/pool.h>
32 expr_evaluate (struct expression *e, const struct ccase *c, int case_idx,
35 struct dataset *ds = e->ds;
36 union operation_data *op = e->ops;
38 double *ns = e->number_stack;
39 struct substring *ss = e->string_stack;
41 /* Without a dictionary/dataset, the expression can't refer to variables,
42 and you don't need to specify a case when you evaluate the
43 expression. With a dictionary/dataset, the expression can refer
44 to variables, so you must specify a case when you evaluate the
46 assert ((c != NULL) == (e->ds != NULL));
48 pool_clear (e->eval_pool);
52 assert (op < e->ops + e->op_cnt);
53 switch (op++->operation)
62 const struct substring *s = &op++->string;
63 *ss++ = copy_string (e, s->string, s->length);
67 case OP_return_number:
68 *(double *) result = finite (ns[-1]) ? ns[-1] : SYSMIS;
71 case OP_return_string:
72 *(struct substring *) result = ss[-1];
75 #include "evaluate.inc"
84 expr_evaluate_num (struct expression *e, const struct ccase *c, int case_idx)
88 assert (e->type == OP_number || e->type == OP_boolean);
89 expr_evaluate (e, c, case_idx, &d);
94 expr_evaluate_str (struct expression *e, const struct ccase *c, int case_idx,
95 char *dst, size_t dst_size)
99 assert (e->type == OP_string);
100 assert ((dst == NULL) == (dst_size == 0));
101 expr_evaluate (e, c, case_idx, &s);
103 buf_copy_rpad (dst, dst_size, s.string, s.length);
106 #include <language/lexer/lexer.h>
107 #include <language/command.h>
110 cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
112 bool optimize = true;
113 int retval = CMD_FAILURE;
114 bool dump_postfix = false;
116 struct ccase *c = NULL;
118 struct dataset *ds = NULL;
120 struct expression *expr;
124 struct dictionary *d = NULL;
125 if (lex_match_id (lexer, "NOOPTIMIZE"))
127 else if (lex_match_id (lexer, "POSTFIX"))
129 else if (lex_match (lexer, '('))
131 char name[LONG_NAME_LEN + 1];
133 size_t old_value_cnt;
136 if (!lex_force_id (lexer))
138 strcpy (name, lex_tokid (lexer));
141 if (!lex_force_match (lexer, '='))
144 if (lex_is_number (lexer))
147 fprintf (stderr, "(%s = %.2f)", name, lex_tokval (lexer));
149 else if (lex_token (lexer) == T_STRING)
151 width = ds_length (lex_tokstr (lexer));
152 fprintf (stderr, "(%s = \"%.2s\")", name, ds_cstr (lex_tokstr (lexer)));
156 lex_error (lexer, _("expecting number or string"));
162 ds = create_dataset ();
163 d = dataset_dict (ds);
166 old_value_cnt = dict_get_next_value_idx (d);
167 v = dict_create_var (d, name, width);
170 msg (SE, _("Duplicate variable name %s."), name);
176 c = xmalloc (sizeof *c);
177 case_create (c, dict_get_next_value_idx (d));
180 case_resize (c, old_value_cnt, dict_get_next_value_idx (d));
182 if (lex_is_number (lexer))
183 case_data_rw (c, v->fv)->f = lex_tokval (lexer);
185 memcpy (case_data_rw (c, v->fv)->s, ds_data (lex_tokstr (lexer)),
189 if (!lex_force_match (lexer, ')'))
195 if (lex_token (lexer) != '/')
197 lex_force_match (lexer, '/');
202 fprintf(stderr, "; ");
203 fprintf (stderr, "%s => ", lex_rest_of_line (lexer, NULL));
206 expr = expr_parse_any (lexer, ds, optimize);
207 if (!expr || lex_end_of_command (lexer) != CMD_SUCCESS)
211 fprintf (stderr, "error\n");
216 expr_debug_print_postfix (expr);
222 double d = expr_evaluate_num (expr, c, 0);
224 fprintf (stderr, "sysmis\n");
226 fprintf (stderr, "%.2f\n", d);
232 double b = expr_evaluate_num (expr, c, 0);
233 fprintf (stderr, "%s\n",
234 b == SYSMIS ? "sysmis" : b == 0.0 ? "false" : "true");
241 expr_evaluate (expr, c, 0, &s);
244 fwrite (s.string, s.length, 1, stderr);
245 fputs ("\"\n", stderr);
254 retval = CMD_SUCCESS;
258 destroy_dataset (ds);
270 expr_debug_print_postfix (const struct expression *e)
274 for (i = 0; i < e->op_cnt; i++)
276 union operation_data *op = &e->ops[i];
279 switch (e->op_types[i])
282 if (op->operation == OP_return_number)
283 fprintf (stderr, "return_number");
284 else if (op->operation == OP_return_string)
285 fprintf (stderr, "return_string");
286 else if (is_function (op->operation))
287 fprintf (stderr, "%s", operations[op->operation].prototype);
288 else if (is_composite (op->operation))
289 fprintf (stderr, "%s", operations[op->operation].name);
291 fprintf (stderr, "%s:", operations[op->operation].name);
294 if (op->number != SYSMIS)
295 fprintf (stderr, "n<%g>", op->number);
297 fprintf (stderr, "n<SYSMIS>");
300 fprintf (stderr, "s<%.*s>",
301 (int) op->string.length,
302 op->string.string != NULL ? op->string.string : "");
306 char str[FMT_STRING_LEN_MAX + 1];
307 fmt_to_string (op->format, str);
308 fprintf (stderr, "f<%s>", str);
312 fprintf (stderr, "v<%s>", op->variable->name);
315 fprintf (stderr, "vec<%s>", op->vector->name);
318 fprintf (stderr, "i<%d>", op->integer);
324 fprintf (stderr, "\n");