1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 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 union operation_data *op = e->ops;
37 double *ns = e->number_stack;
38 struct substring *ss = e->string_stack;
40 assert ((c != NULL) == (e->dict != NULL));
41 pool_clear (e->eval_pool);
45 assert (op < e->ops + e->op_cnt);
46 switch (op++->operation)
55 const struct substring *s = &op++->string;
56 *ss++ = copy_string (e, s->string, s->length);
60 case OP_return_number:
61 *(double *) result = finite (ns[-1]) ? ns[-1] : SYSMIS;
64 case OP_return_string:
65 *(struct substring *) result = ss[-1];
68 #include "evaluate.inc"
77 expr_evaluate_num (struct expression *e, const struct ccase *c, int case_idx)
81 assert (e->type == OP_number || e->type == OP_boolean);
82 expr_evaluate (e, c, case_idx, &d);
89 expr_evaluate_str (struct expression *e, const struct ccase *c, int case_idx,
90 char *dst, size_t dst_size)
94 assert (e->type == OP_string);
95 assert ((dst == NULL) == (dst_size == 0));
96 expr_evaluate (e, c, case_idx, &s);
98 buf_copy_rpad (dst, dst_size, s.string, s.length);
101 #include <language/lexer/lexer.h>
102 #include <language/command.h>
105 cmd_debug_evaluate (void)
107 bool optimize = true;
108 int retval = CMD_FAILURE;
109 bool dump_postfix = false;
110 struct dictionary *d = NULL;
111 struct ccase *c = NULL;
113 struct expression *expr;
117 if (lex_match_id ("NOOPTIMIZE"))
119 else if (lex_match_id ("POSTFIX"))
121 else if (lex_match ('('))
123 char name[LONG_NAME_LEN + 1];
125 size_t old_value_cnt;
128 if (!lex_force_id ())
130 strcpy (name, tokid);
133 if (!lex_force_match ('='))
136 if (lex_is_number ())
139 fprintf (stderr, "(%s = %.2f)", name, tokval);
141 else if (token == T_STRING)
143 width = ds_length (&tokstr);
144 fprintf (stderr, "(%s = \"%.2s\")", name, ds_cstr (&tokstr));
148 lex_error (_("expecting number or string"));
155 old_value_cnt = dict_get_next_value_idx (d);
156 v = dict_create_var (d, name, width);
159 msg (SE, _("Duplicate variable name %s."), name);
165 c = xmalloc (sizeof *c);
168 case_resize (c, old_value_cnt, dict_get_next_value_idx (d));
170 if (lex_is_number ())
171 case_data_rw (c, v->fv)->f = tokval;
173 memcpy (case_data_rw (c, v->fv)->s, ds_data (&tokstr),
177 if (!lex_force_match (')'))
185 lex_force_match ('/');
189 fprintf (stderr, "; ");
190 fprintf (stderr, "%s => ", lex_rest_of_line (NULL));
193 expr = expr_parse_any (d, optimize);
194 if (!expr || lex_end_of_command () != CMD_SUCCESS)
198 fprintf (stderr, "error\n");
203 expr_debug_print_postfix (expr);
209 double d = expr_evaluate_num (expr, c, 0);
211 fprintf (stderr, "sysmis\n");
213 fprintf (stderr, "%.2f\n", d);
219 double b = expr_evaluate_num (expr, c, 0);
220 fprintf (stderr, "%s\n",
221 b == SYSMIS ? "sysmis" : b == 0.0 ? "false" : "true");
228 expr_evaluate (expr, c, 0, &s);
231 fwrite (s.string, s.length, 1, stderr);
232 fputs ("\"\n", stderr);
241 retval = CMD_SUCCESS;
254 expr_debug_print_postfix (const struct expression *e)
258 for (i = 0; i < e->op_cnt; i++)
260 union operation_data *op = &e->ops[i];
263 switch (e->op_types[i])
266 if (op->operation == OP_return_number)
267 fprintf (stderr, "return_number");
268 else if (op->operation == OP_return_string)
269 fprintf (stderr, "return_string");
270 else if (is_function (op->operation))
271 fprintf (stderr, "%s", operations[op->operation].prototype);
272 else if (is_composite (op->operation))
273 fprintf (stderr, "%s", operations[op->operation].name);
275 fprintf (stderr, "%s:", operations[op->operation].name);
278 if (op->number != SYSMIS)
279 fprintf (stderr, "n<%g>", op->number);
281 fprintf (stderr, "n<SYSMIS>");
284 fprintf (stderr, "s<%.*s>",
285 (int) op->string.length,
286 op->string.string != NULL ? op->string.string : "");
289 fprintf (stderr, "f<%s%d.%d>",
290 formats[op->format->type].name,
291 op->format->w, op->format->d);
294 fprintf (stderr, "v<%s>", op->variable->name);
297 fprintf (stderr, "vec<%s>", op->vector->name);
300 fprintf (stderr, "i<%d>", op->integer);
306 fprintf (stderr, "\n");