Remove "Written by Ben Pfaff <blp@gnu.org>" lines everywhere.
[pspp-builds.git] / src / language / control / do-if.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 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
21 #include <stdlib.h>
22
23 #include "control-stack.h"
24 #include <data/procedure.h>
25 #include <data/transformations.h>
26 #include <data/value.h>
27 #include <language/command.h>
28 #include <language/expressions/public.h>
29 #include <language/lexer/lexer.h>
30 #include <libpspp/alloc.h>
31 #include <libpspp/compiler.h>
32 #include <libpspp/message.h>
33 #include <libpspp/message.h>
34 #include <libpspp/str.h>
35
36 #include "gettext.h"
37 #define _(msgid) gettext (msgid)
38
39 /* DO IF, ELSE IF, and ELSE are translated as a single
40    transformation that evaluates each condition and jumps to the
41    start of the appropriate block of transformations.  Each block
42    of transformations (except for the last) ends with a
43    transformation that jumps past the remaining blocks.
44
45    So, the following code:
46
47        DO IF a.             
48        ...block 1...
49        ELSE IF b.
50        ...block 2...
51        ELSE.
52        ...block 3...
53        END IF.
54
55    is effectively translated like this:
56
57        IF a GOTO 1, IF b GOTO 2, ELSE GOTO 3.
58        1: ...block 1...
59           GOTO 4
60        2: ...block 2...
61           GOTO 4
62        3: ...block 3...
63        4:
64
65 */
66
67 /* A conditional clause. */
68 struct clause 
69   {
70     struct expression *condition; /* Test expression; NULL for ELSE clause. */
71     int target_index;           /* Transformation to jump to if true. */
72   };
73
74 /* DO IF transformation. */
75 struct do_if_trns
76   {
77     struct dataset *ds;         /* The dataset */
78     struct clause *clauses;     /* Clauses. */
79     size_t clause_cnt;          /* Number of clauses. */
80     int past_END_IF_index;      /* Transformation just past last clause. */
81   };
82
83 static const struct ctl_class do_if_class;
84
85 static int parse_clause (struct lexer *, struct do_if_trns *, struct dataset *ds);
86 static void add_clause (struct do_if_trns *,
87                         struct expression *condition, int target_index);
88 static void add_else (struct do_if_trns *);
89
90 static bool has_else (struct do_if_trns *);
91 static bool must_not_have_else (struct do_if_trns *);
92 static void close_do_if (void *do_if);
93
94 static trns_finalize_func do_if_finalize_func;
95 static trns_proc_func do_if_trns_proc, break_trns_proc;
96 static trns_free_func do_if_trns_free;
97
98 /* Parse DO IF. */
99 int
100 cmd_do_if (struct lexer *lexer, struct dataset *ds)
101 {
102   struct do_if_trns *do_if = xmalloc (sizeof *do_if);
103   do_if->clauses = NULL;
104   do_if->clause_cnt = 0;
105   do_if->ds = ds;
106
107   ctl_stack_push (&do_if_class, do_if);
108   add_transformation_with_finalizer (ds, do_if_finalize_func,
109                                      do_if_trns_proc, do_if_trns_free, do_if);
110
111   return parse_clause (lexer, do_if, ds);
112 }
113
114 /* Parse ELSE IF. */
115 int
116 cmd_else_if (struct lexer *lexer, struct dataset *ds)
117 {
118   struct do_if_trns *do_if = ctl_stack_top (&do_if_class);
119   if (do_if == NULL || !must_not_have_else (do_if))
120     return CMD_CASCADING_FAILURE;
121   return parse_clause (lexer, do_if, ds);
122 }
123
124 /* Parse ELSE. */
125 int
126 cmd_else (struct lexer *lexer, struct dataset *ds)
127 {
128   struct do_if_trns *do_if = ctl_stack_top (&do_if_class);
129   assert (ds == do_if->ds);
130   if (do_if == NULL || !must_not_have_else (do_if))
131     return CMD_CASCADING_FAILURE;
132   add_else (do_if);
133   return lex_end_of_command (lexer);
134 }
135
136 /* Parse END IF. */
137 int
138 cmd_end_if (struct lexer *lexer, struct dataset *ds)
139 {
140   struct do_if_trns *do_if = ctl_stack_top (&do_if_class);
141   assert (ds == do_if->ds);
142
143   if (do_if == NULL)
144     return CMD_CASCADING_FAILURE;
145
146   ctl_stack_pop (do_if);
147
148   return lex_end_of_command (lexer);
149 }
150
151 /* Closes out DO_IF, by adding a sentinel ELSE clause if
152    necessary and setting past_END_IF_index. */
153 static void
154 close_do_if (void *do_if_)
155 {
156   struct do_if_trns *do_if = do_if_;
157   
158   if (!has_else (do_if)) 
159     add_else (do_if);
160   do_if->past_END_IF_index = next_transformation (do_if->ds);
161 }
162
163 /* Adds an ELSE clause to DO_IF pointing to the next
164    transformation. */
165 static void
166 add_else (struct do_if_trns *do_if) 
167 {
168   assert (!has_else (do_if));
169   add_clause (do_if, NULL, next_transformation (do_if->ds));
170 }
171
172 /* Returns true if DO_IF does not yet have an ELSE clause.
173    Reports an error and returns false if it does already. */
174 static bool
175 must_not_have_else (struct do_if_trns *do_if) 
176 {
177   if (has_else (do_if))
178     {
179       msg (SE, _("This command may not follow ELSE in DO IF...END IF."));
180       return false;
181     }
182   else
183     return true;
184 }
185
186 /* Returns true if DO_IF already has an ELSE clause,
187    false otherwise. */
188 static bool
189 has_else (struct do_if_trns *do_if) 
190 {
191   return (do_if->clause_cnt != 0
192           && do_if->clauses[do_if->clause_cnt - 1].condition == NULL);
193 }
194
195 /* Parses a DO IF or ELSE IF expression and appends the
196    corresponding clause to DO_IF.  Checks for end of command and
197    returns a command return code. */
198 static int
199 parse_clause (struct lexer *lexer, struct do_if_trns *do_if, struct dataset *ds)
200 {
201   struct expression *condition;
202
203   condition = expr_parse (lexer, ds, EXPR_BOOLEAN);
204   if (condition == NULL)
205     return CMD_CASCADING_FAILURE;
206
207   add_clause (do_if, condition, next_transformation (ds));
208
209   return lex_end_of_command (lexer);
210 }
211
212 /* Adds a clause to DO_IF that tests for the given CONDITION and,
213    if true, jumps to TARGET_INDEX. */
214 static void
215 add_clause (struct do_if_trns *do_if,
216             struct expression *condition, int target_index) 
217 {
218   struct clause *clause;
219
220   if (do_if->clause_cnt > 0)
221     add_transformation (do_if->ds, break_trns_proc, NULL, do_if);
222
223   do_if->clauses = xnrealloc (do_if->clauses,
224                               do_if->clause_cnt + 1, sizeof *do_if->clauses);
225   clause = &do_if->clauses[do_if->clause_cnt++];
226   clause->condition = condition;
227   clause->target_index = target_index;
228 }
229
230 /* Finalizes DO IF by clearing the control stack, thus ensuring
231    that all open DO IFs are closed. */ 
232 static void
233 do_if_finalize_func (void *do_if_ UNUSED) 
234 {
235   /* This will be called multiple times if multiple DO IFs were
236      executed, which is slightly unclean, but at least it's
237      idempotent. */
238   ctl_stack_clear ();
239 }
240
241 /* DO IF transformation procedure.
242    Checks each clause and jumps to the appropriate
243    transformation. */
244 static int 
245 do_if_trns_proc (void *do_if_, struct ccase *c, casenumber case_num UNUSED)
246 {
247   struct do_if_trns *do_if = do_if_;
248   struct clause *clause;
249
250   for (clause = do_if->clauses; clause < do_if->clauses + do_if->clause_cnt;
251        clause++) 
252     {
253       if (clause->condition != NULL)
254         {
255           double boolean = expr_evaluate_num (clause->condition, c, case_num);
256           if (boolean == 1.0)
257             return clause->target_index;
258           else if (boolean == SYSMIS)
259             return do_if->past_END_IF_index;
260         }
261       else 
262         return clause->target_index;
263     }
264   return do_if->past_END_IF_index;
265 }
266
267 /* Frees a DO IF transformation. */
268 static bool
269 do_if_trns_free (void *do_if_)
270 {
271   struct do_if_trns *do_if = do_if_;
272   struct clause *clause;
273
274   for (clause = do_if->clauses; clause < do_if->clauses + do_if->clause_cnt;
275        clause++)
276     expr_free (clause->condition);
277   free (do_if->clauses);
278   free (do_if);
279   return true;
280 }
281
282 /* Breaks out of a DO IF construct. */
283 static int 
284 break_trns_proc (void *do_if_, struct ccase *c UNUSED, casenumber case_num UNUSED)
285 {
286   struct do_if_trns *do_if = do_if_;
287
288   return do_if->past_END_IF_index;
289 }
290
291 /* DO IF control structure class definition. */
292 static const struct ctl_class do_if_class = 
293   {
294     "DO IF",
295     "END IF",
296     close_do_if,
297   };