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