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);
96 expr_evaluate_str (struct expression *e, const struct ccase *c, int case_idx,
97 char *dst, size_t dst_size)
101 assert (e->type == OP_string);
102 assert ((dst == NULL) == (dst_size == 0));
103 expr_evaluate (e, c, case_idx, &s);
105 buf_copy_rpad (dst, dst_size, s.string, s.length);
108 #include <language/lexer/lexer.h>
109 #include <language/command.h>
112 cmd_debug_evaluate (struct dataset *dsother UNUSED)
114 bool optimize = true;
115 int retval = CMD_FAILURE;
116 bool dump_postfix = false;
118 struct ccase *c = NULL;
120 struct dataset *ds = NULL;
122 struct expression *expr;
126 struct dictionary *d = NULL;
127 if (lex_match_id ("NOOPTIMIZE"))
129 else if (lex_match_id ("POSTFIX"))
131 else if (lex_match ('('))
133 char name[LONG_NAME_LEN + 1];
135 size_t old_value_cnt;
138 if (!lex_force_id ())
140 strcpy (name, tokid);
143 if (!lex_force_match ('='))
146 if (lex_is_number ())
149 fprintf (stderr, "(%s = %.2f)", name, tokval);
151 else if (token == T_STRING)
153 width = ds_length (&tokstr);
154 fprintf (stderr, "(%s = \"%.2s\")", name, ds_cstr (&tokstr));
158 lex_error (_("expecting number or string"));
164 ds = create_dataset ();
165 d = dataset_dict (ds);
168 old_value_cnt = dict_get_next_value_idx (d);
169 v = dict_create_var (d, name, width);
172 msg (SE, _("Duplicate variable name %s."), name);
178 c = xmalloc (sizeof *c);
179 case_create (c, dict_get_next_value_idx (d));
182 case_resize (c, old_value_cnt, dict_get_next_value_idx (d));
184 if (lex_is_number ())
185 case_data_rw (c, v->fv)->f = tokval;
187 memcpy (case_data_rw (c, v->fv)->s, ds_data (&tokstr),
191 if (!lex_force_match (')'))
199 lex_force_match ('/');
204 fprintf(stderr, "; ");
205 fprintf (stderr, "%s => ", lex_rest_of_line (NULL));
208 expr = expr_parse_any (ds, optimize);
209 if (!expr || lex_end_of_command () != CMD_SUCCESS)
213 fprintf (stderr, "error\n");
218 expr_debug_print_postfix (expr);
224 double d = expr_evaluate_num (expr, c, 0);
226 fprintf (stderr, "sysmis\n");
228 fprintf (stderr, "%.2f\n", d);
234 double b = expr_evaluate_num (expr, c, 0);
235 fprintf (stderr, "%s\n",
236 b == SYSMIS ? "sysmis" : b == 0.0 ? "false" : "true");
243 expr_evaluate (expr, c, 0, &s);
246 fwrite (s.string, s.length, 1, stderr);
247 fputs ("\"\n", stderr);
256 retval = CMD_SUCCESS;
260 destroy_dataset (ds);
272 expr_debug_print_postfix (const struct expression *e)
276 for (i = 0; i < e->op_cnt; i++)
278 union operation_data *op = &e->ops[i];
281 switch (e->op_types[i])
284 if (op->operation == OP_return_number)
285 fprintf (stderr, "return_number");
286 else if (op->operation == OP_return_string)
287 fprintf (stderr, "return_string");
288 else if (is_function (op->operation))
289 fprintf (stderr, "%s", operations[op->operation].prototype);
290 else if (is_composite (op->operation))
291 fprintf (stderr, "%s", operations[op->operation].name);
293 fprintf (stderr, "%s:", operations[op->operation].name);
296 if (op->number != SYSMIS)
297 fprintf (stderr, "n<%g>", op->number);
299 fprintf (stderr, "n<SYSMIS>");
302 fprintf (stderr, "s<%.*s>",
303 (int) op->string.length,
304 op->string.string != NULL ? op->string.string : "");
308 char str[FMT_STRING_LEN_MAX + 1];
309 fmt_to_string (op->format, str);
310 fprintf (stderr, "f<%s>", str);
314 fprintf (stderr, "v<%s>", op->variable->name);
317 fprintf (stderr, "vec<%s>", op->vector->name);
320 fprintf (stderr, "i<%d>", op->integer);
326 fprintf (stderr, "\n");