DEBUG EVALUATE: Eliminate VAR_NAME_LEN limit.
[pspp-builds.git] / src / language / expressions / evaluate.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2006, 2007, 2009, 2010, 2011 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18 #include "evaluate.h"
19
20 #include <ctype.h>
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>
27
28 #include "xalloc.h"
29
30 static void
31 expr_evaluate (struct expression *e, const struct ccase *c, int case_idx,
32                void *result)
33 {
34   struct dataset *ds = e->ds;
35   union operation_data *op = e->ops;
36
37   double *ns = e->number_stack;
38   struct substring *ss = e->string_stack;
39
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
44      expression. */
45   assert ((c != NULL) == (e->ds != NULL));
46
47   pool_clear (e->eval_pool);
48
49   for (;;)
50     {
51       assert (op < e->ops + e->op_cnt);
52       switch (op++->operation)
53         {
54         case OP_number:
55         case OP_boolean:
56           *ns++ = op++->number;
57           break;
58
59         case OP_string:
60           {
61             const struct substring *s = &op++->string;
62             *ss++ = copy_string (e, s->string, s->length);
63           }
64           break;
65
66         case OP_return_number:
67           *(double *) result = isfinite (ns[-1]) ? ns[-1] : SYSMIS;
68           return;
69
70         case OP_return_string:
71           *(struct substring *) result = ss[-1];
72           return;
73
74 #include "evaluate.inc"
75
76         default:
77           NOT_REACHED ();
78         }
79     }
80 }
81
82 double
83 expr_evaluate_num (struct expression *e, const struct ccase *c, int case_idx)
84 {
85   double d;
86
87   assert (e->type == OP_number || e->type == OP_boolean);
88   expr_evaluate (e, c, case_idx, &d);
89   return d;
90 }
91
92 void
93 expr_evaluate_str (struct expression *e, const struct ccase *c, int case_idx,
94                    char *dst, size_t dst_size)
95 {
96   struct substring s;
97
98   assert (e->type == OP_string);
99   assert ((dst == NULL) == (dst_size == 0));
100   expr_evaluate (e, c, case_idx, &s);
101
102   buf_copy_rpad (dst, dst_size, s.string, s.length, ' ');
103 }
104 \f
105 #include <language/lexer/lexer.h>
106 #include <language/command.h>
107
108 int
109 cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
110 {
111   bool optimize = true;
112   int retval = CMD_FAILURE;
113   bool dump_postfix = false;
114
115   struct ccase *c = NULL;
116
117   struct dataset *ds = NULL;
118
119   char *name = NULL;
120
121   struct expression *expr;
122
123   for (;;)
124     {
125       struct dictionary *d = NULL;
126       if (lex_match_id (lexer, "NOOPTIMIZE"))
127         optimize = 0;
128       else if (lex_match_id (lexer, "POSTFIX"))
129         dump_postfix = 1;
130       else if (lex_match (lexer, T_LPAREN))
131         {
132           struct variable *v;
133           size_t old_value_cnt;
134           int width;
135
136           if (!lex_force_id (lexer))
137             goto done;
138           name = xstrdup (lex_tokcstr (lexer));
139
140           lex_get (lexer);
141           if (!lex_force_match (lexer, T_EQUALS))
142             goto done;
143
144           if (lex_is_number (lexer))
145             width = 0;
146           else if (lex_is_string (lexer))
147             width = ss_length (lex_tokss (lexer));
148           else
149             {
150               lex_error (lexer, _("expecting number or string"));
151               goto done;
152             }
153
154           if  ( ds == NULL )
155             {
156               ds = create_dataset ();
157               d = dataset_dict (ds);
158             }
159
160           old_value_cnt = dict_get_next_value_idx (d);
161           v = dict_create_var (d, name, width);
162           if (v == NULL)
163             {
164               msg (SE, _("Duplicate variable name %s."), name);
165               goto done;
166             }
167           free (name);
168           name = NULL;
169
170           if (c == NULL)
171             c = case_create (dict_get_proto (d));
172           else
173             c = case_unshare_and_resize (c, dict_get_proto (d));
174
175           if (!parse_value (lexer, case_data_rw (c, v), var_get_width (v)))
176             NOT_REACHED ();
177
178           if (!lex_force_match (lexer, T_RPAREN))
179             goto done;
180         }
181       else
182         break;
183     }
184   if (lex_token (lexer) != T_SLASH)
185     {
186       lex_force_match (lexer, T_SLASH);
187       goto done;
188     }
189
190   lex_get (lexer);
191
192   expr = expr_parse_any (lexer, ds, optimize);
193   if (!expr || lex_end_of_command (lexer) != CMD_SUCCESS)
194     {
195       if (expr != NULL)
196         expr_free (expr);
197       printf ("error\n");
198       goto done;
199     }
200
201   if (dump_postfix)
202     expr_debug_print_postfix (expr);
203   else
204     switch (expr->type)
205       {
206       case OP_number:
207         {
208           double d = expr_evaluate_num (expr, c, 0);
209           if (d == SYSMIS)
210             printf ("sysmis\n");
211           else
212             printf ("%.2f\n", d);
213         }
214         break;
215
216       case OP_boolean:
217         {
218           double b = expr_evaluate_num (expr, c, 0);
219           printf ("%s\n",
220                    b == SYSMIS ? "sysmis" : b == 0.0 ? "false" : "true");
221         }
222         break;
223
224       case OP_string:
225         {
226           struct substring s;
227           expr_evaluate (expr, c, 0, &s);
228
229           putchar ('"');
230           fwrite (s.string, s.length, 1, stdout);
231           puts ("\"");
232           break;
233         }
234
235       default:
236         NOT_REACHED ();
237       }
238
239   expr_free (expr);
240   retval = CMD_SUCCESS;
241
242  done:
243   if (ds)
244     destroy_dataset (ds);
245
246   case_unref (c);
247
248   free (name);
249
250   return retval;
251 }
252
253 void
254 expr_debug_print_postfix (const struct expression *e)
255 {
256   size_t i;
257
258   for (i = 0; i < e->op_cnt; i++)
259     {
260       union operation_data *op = &e->ops[i];
261       if (i > 0)
262         putc (' ', stderr);
263       switch (e->op_types[i])
264         {
265         case OP_operation:
266           if (op->operation == OP_return_number)
267             printf ("return_number");
268           else if (op->operation == OP_return_string)
269             printf ("return_string");
270           else if (is_function (op->operation))
271             printf ("%s", operations[op->operation].prototype);
272           else if (is_composite (op->operation))
273             printf ("%s", operations[op->operation].name);
274           else
275             printf ("%s:", operations[op->operation].name);
276           break;
277         case OP_number:
278           if (op->number != SYSMIS)
279             printf ("n<%g>", op->number);
280           else
281             printf ("n<SYSMIS>");
282           break;
283         case OP_string:
284           printf ("s<%.*s>",
285                    (int) op->string.length,
286                    op->string.string != NULL ? op->string.string : "");
287           break;
288         case OP_format:
289           {
290             char str[FMT_STRING_LEN_MAX + 1];
291             fmt_to_string (op->format, str);
292             printf ("f<%s>", str);
293           }
294           break;
295         case OP_variable:
296           printf ("v<%s>", var_get_name (op->variable));
297           break;
298         case OP_vector:
299           printf ("vec<%s>", vector_get_name (op->vector));
300           break;
301         case OP_integer:
302           printf ("i<%d>", op->integer);
303           break;
304         default:
305           NOT_REACHED ();
306         }
307     }
308   printf ("\n");
309 }