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