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., 59 Temple Place - Suite 330, Boston, MA
23 #if TIME_WITH_SYS_TIME
42 expr_evaluate (struct expression *e, const struct ccase *c, int case_idx,
45 union operation_data *op = e->ops;
47 double *ns = e->number_stack;
48 struct fixed_string *ss = e->string_stack;
50 assert ((c != NULL) == (e->dict != NULL));
51 pool_clear (e->eval_pool);
55 assert (op < e->ops + e->op_cnt);
56 switch (op++->operation)
65 const struct fixed_string *s = &op++->string;
66 *ss++ = copy_string (e, s->string, s->length);
70 case OP_return_number:
71 *(double *) result = finite (ns[-1]) ? ns[-1] : SYSMIS;
74 case OP_return_string:
75 *(struct fixed_string *) result = ss[-1];
78 #include "evaluate.inc"
87 expr_evaluate_num (struct expression *e, const struct ccase *c, int case_idx)
91 assert (e->type == OP_number || e->type == OP_boolean);
92 expr_evaluate (e, c, case_idx, &d);
97 expr_evaluate_str (struct expression *e, const struct ccase *c, int case_idx,
98 char *dst, size_t dst_size)
100 struct fixed_string s;
102 assert (e->type == OP_string);
103 assert ((dst == NULL) == (dst_size == 0));
104 expr_evaluate (e, c, case_idx, &s);
105 st_bare_pad_len_copy (dst, s.string, dst_size, s.length);
112 cmd_debug_evaluate (void)
114 bool optimize = true;
115 int retval = CMD_FAILURE;
116 bool dump_postfix = false;
117 struct dictionary *d = NULL;
118 struct ccase *c = NULL;
120 struct expression *expr;
124 if (lex_match_id ("NOOPTIMIZE"))
126 else if (lex_match_id ("POSTFIX"))
128 else if (lex_match ('('))
130 char name[MAX_VAR_NAME_LEN + 1];
132 size_t old_value_cnt;
135 if (!lex_force_id ())
137 strcpy (name, tokid);
140 if (!lex_force_match ('='))
143 if (lex_is_number ())
146 fprintf (stderr, "(%s = %.2f)", name, tokval);
148 else if (token == T_STRING)
150 width = ds_length (&tokstr);
151 fprintf (stderr, "(%s = \"%.2s\")", name, ds_c_str (&tokstr));
155 lex_error (_("expecting number or string"));
162 old_value_cnt = dict_get_next_value_idx (d);
163 v = dict_create_var (d, name, width);
166 msg (SE, _("Duplicate variable name %s."), name);
172 c = xmalloc (sizeof *c);
175 case_resize (c, old_value_cnt, dict_get_next_value_idx (d));
177 if (lex_is_number ())
178 case_data_rw (c, v->fv)->f = tokval;
180 memcpy (case_data_rw (c, v->fv)->s, ds_data (&tokstr),
184 if (!lex_force_match (')'))
192 lex_force_match ('/');
196 fprintf (stderr, "; ");
197 fprintf (stderr, "%s => ", lex_rest_of_line (NULL));
200 expr = expr_parse_any (d, optimize);
201 if (!expr || lex_end_of_command () != CMD_SUCCESS)
205 fprintf (stderr, "error\n");
210 expr_debug_print_postfix (expr);
216 double d = expr_evaluate_num (expr, c, 0);
218 fprintf (stderr, "sysmis\n");
220 fprintf (stderr, "%.2f\n", d);
226 double b = expr_evaluate_num (expr, c, 0);
227 fprintf (stderr, "%s\n",
228 b == SYSMIS ? "sysmis" : b == 0.0 ? "false" : "true");
234 struct fixed_string s;
235 expr_evaluate (expr, c, 0, &s);
238 fwrite (s.string, s.length, 1, stderr);
239 fputs ("\"\n", stderr);
248 retval = CMD_SUCCESS;
257 expr_debug_print_postfix (const struct expression *e)
261 for (i = 0; i < e->op_cnt; i++)
263 union operation_data *op = &e->ops[i];
266 switch (e->op_types[i])
269 if (op->operation == OP_return_number)
270 fprintf (stderr, "return_number");
271 else if (op->operation == OP_return_string)
272 fprintf (stderr, "return_string");
273 else if (is_function (op->operation))
274 fprintf (stderr, "%s", operations[op->operation].prototype);
275 else if (is_composite (op->operation))
276 fprintf (stderr, "%s", operations[op->operation].name);
278 fprintf (stderr, "%s:", operations[op->operation].name);
281 if (op->number != SYSMIS)
282 fprintf (stderr, "n<%g>", op->number);
284 fprintf (stderr, "n<SYSMIS>");
287 fprintf (stderr, "s<%.*s>",
288 (int) op->string.length, op->string.string);
291 fprintf (stderr, "f<%s%d.%d>",
292 formats[op->format->type].name,
293 op->format->w, op->format->d);
296 fprintf (stderr, "v<%s>", op->variable->name);
299 fprintf (stderr, "vec<%s>", op->vector->name);
302 fprintf (stderr, "i<%d>", op->integer);
308 fprintf (stderr, "\n");