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