Remove "Written by Ben Pfaff <blp@gnu.org>" lines everywhere.
[pspp-builds.git] / src / language / expressions / evaluate.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    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, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301, USA. */
18
19 #include <config.h>
20 #include "private.h"
21
22 #include <ctype.h>
23 #include <libpspp/alloc.h>
24 #include <libpspp/assertion.h>
25 #include <libpspp/message.h>
26 #include "helpers.h"
27 #include "evaluate.h"
28 #include <libpspp/pool.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 = finite (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   struct expression *expr;
120
121   for (;;) 
122     {
123       struct dictionary *d = NULL;
124       if (lex_match_id (lexer, "NOOPTIMIZE"))
125         optimize = 0;
126       else if (lex_match_id (lexer, "POSTFIX"))
127         dump_postfix = 1;
128       else if (lex_match (lexer, '('))
129         {
130           char name[LONG_NAME_LEN + 1];
131           struct variable *v;
132           size_t old_value_cnt;
133           int width;
134
135           if (!lex_force_id (lexer))
136             goto done;
137           strcpy (name, lex_tokid (lexer));
138
139           lex_get (lexer);
140           if (!lex_force_match (lexer, '='))
141             goto done;
142
143           if (lex_is_number (lexer))
144             {
145               width = 0;
146               fprintf (stderr, "(%s = %.2f)", name, lex_tokval (lexer)); 
147             }
148           else if (lex_token (lexer) == T_STRING) 
149             {
150               width = ds_length (lex_tokstr (lexer));
151               fprintf (stderr, "(%s = \"%.2s\")", name, ds_cstr (lex_tokstr (lexer))); 
152             }
153           else
154             {
155               lex_error (lexer, _("expecting number or string"));
156               goto done;
157             }
158           
159           if  ( ds == NULL ) 
160             {
161               ds = create_dataset ();
162               d = dataset_dict (ds);
163             }
164           
165           old_value_cnt = dict_get_next_value_idx (d);
166           v = dict_create_var (d, name, width);
167           if (v == NULL)
168             {
169               msg (SE, _("Duplicate variable name %s."), name);
170               goto done;
171             }
172
173           if (c == NULL) 
174             {
175               c = xmalloc (sizeof *c);
176               case_create (c, dict_get_next_value_idx (d));
177             }
178           else
179             case_resize (c, old_value_cnt, dict_get_next_value_idx (d));
180
181           if (lex_is_number (lexer))
182             case_data_rw (c, v)->f = lex_tokval (lexer);
183           else
184             memcpy (case_data_rw (c, v)->s, ds_data (lex_tokstr (lexer)),
185                     var_get_width (v));
186           lex_get (lexer);
187
188           if (!lex_force_match (lexer, ')'))
189             goto done;
190         }
191       else 
192         break;
193     }
194   if (lex_token (lexer) != '/') 
195     {
196       lex_force_match (lexer, '/');
197       goto done;
198     }
199
200   if ( ds != NULL ) 
201     fprintf(stderr, "; ");
202   fprintf (stderr, "%s => ", lex_rest_of_line (lexer, NULL));
203   lex_get (lexer);
204
205   expr = expr_parse_any (lexer, ds, optimize);
206   if (!expr || lex_end_of_command (lexer) != CMD_SUCCESS)
207     {
208       if (expr != NULL)
209         expr_free (expr);
210       fprintf (stderr, "error\n");
211       goto done;
212     }
213
214   if (dump_postfix) 
215     expr_debug_print_postfix (expr);
216   else 
217     switch (expr->type) 
218       {
219       case OP_number: 
220         {
221           double d = expr_evaluate_num (expr, c, 0);
222           if (d == SYSMIS)
223             fprintf (stderr, "sysmis\n");
224           else
225             fprintf (stderr, "%.2f\n", d); 
226         }
227         break;
228       
229       case OP_boolean: 
230         {
231           double b = expr_evaluate_num (expr, c, 0);
232           fprintf (stderr, "%s\n",
233                    b == SYSMIS ? "sysmis" : b == 0.0 ? "false" : "true"); 
234         }
235         break;
236
237       case OP_string: 
238         {
239           struct substring s;
240           expr_evaluate (expr, c, 0, &s);
241
242           fputc ('"', stderr);
243           fwrite (s.string, s.length, 1, stderr);
244           fputs ("\"\n", stderr);
245           break; 
246         }
247
248       default:
249         NOT_REACHED ();
250       }
251
252   expr_free (expr);
253   retval = CMD_SUCCESS;
254
255  done:
256   if (ds)
257     destroy_dataset (ds);
258
259   if (c != NULL) 
260     {
261       case_destroy (c);
262       free (c); 
263     }
264
265   return retval;
266 }
267
268 void
269 expr_debug_print_postfix (const struct expression *e) 
270 {
271   size_t i;
272
273   for (i = 0; i < e->op_cnt; i++) 
274     {
275       union operation_data *op = &e->ops[i];
276       if (i > 0)
277         putc (' ', stderr);
278       switch (e->op_types[i]) 
279         {
280         case OP_operation:
281           if (op->operation == OP_return_number)
282             fprintf (stderr, "return_number");
283           else if (op->operation == OP_return_string)
284             fprintf (stderr, "return_string");
285           else if (is_function (op->operation)) 
286             fprintf (stderr, "%s", operations[op->operation].prototype);
287           else if (is_composite (op->operation)) 
288             fprintf (stderr, "%s", operations[op->operation].name);
289           else
290             fprintf (stderr, "%s:", operations[op->operation].name);
291           break;
292         case OP_number:
293           if (op->number != SYSMIS)
294             fprintf (stderr, "n<%g>", op->number);
295           else
296             fprintf (stderr, "n<SYSMIS>");
297           break;
298         case OP_string:
299           fprintf (stderr, "s<%.*s>",
300                    (int) op->string.length,
301                    op->string.string != NULL ? op->string.string : "");
302           break;
303         case OP_format:
304           {
305             char str[FMT_STRING_LEN_MAX + 1];
306             fmt_to_string (op->format, str);
307             fprintf (stderr, "f<%s>", str); 
308           }
309           break;
310         case OP_variable:
311           fprintf (stderr, "v<%s>", var_get_name (op->variable));
312           break;
313         case OP_vector:
314           fprintf (stderr, "vec<%s>", vector_get_name (op->vector));
315           break;
316         case OP_integer:
317           fprintf (stderr, "i<%d>", op->integer);
318           break;
319         default:
320           NOT_REACHED ();
321         } 
322     }
323   fprintf (stderr, "\n");
324 }