1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
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.
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.
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
24 #include "control-stack.h"
25 #include <data/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>
38 #define _(msgid) gettext (msgid)
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.
46 So, the following code:
56 is effectively translated like this:
58 IF a GOTO 1, IF b GOTO 2, ELSE GOTO 3.
68 /* A conditional clause. */
71 struct expression *condition; /* Test expression; NULL for ELSE clause. */
72 int target_index; /* Transformation to jump to if true. */
75 /* DO IF transformation. */
78 struct clause *clauses; /* Clauses. */
79 size_t clause_cnt; /* Number of clauses. */
80 int past_END_IF_index; /* Transformation just past last clause. */
83 static struct ctl_class do_if_class;
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 *);
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);
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;
102 struct do_if_trns *do_if = xmalloc (sizeof *do_if);
103 do_if->clauses = NULL;
104 do_if->clause_cnt = 0;
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);
110 return parse_clause (do_if);
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);
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;
131 return lex_end_of_command ();
138 struct do_if_trns *do_if = ctl_stack_top (&do_if_class);
140 return CMD_CASCADING_FAILURE;
142 ctl_stack_pop (do_if);
144 return lex_end_of_command ();
147 /* Closes out DO_IF, by adding a sentinel ELSE clause if
148 necessary and setting past_END_IF_index. */
150 close_do_if (void *do_if_)
152 struct do_if_trns *do_if = do_if_;
154 if (!has_else (do_if))
156 do_if->past_END_IF_index = next_transformation ();
159 /* Adds an ELSE clause to DO_IF pointing to the next
162 add_else (struct do_if_trns *do_if)
164 assert (!has_else (do_if));
165 add_clause (do_if, NULL, next_transformation ());
168 /* Returns true if DO_IF does not yet have an ELSE clause.
169 Reports an error and returns false if it does already. */
171 must_not_have_else (struct do_if_trns *do_if)
173 if (has_else (do_if))
175 msg (SE, _("This command may not follow ELSE in DO IF...END IF."));
182 /* Returns true if DO_IF already has an ELSE clause,
185 has_else (struct do_if_trns *do_if)
187 return (do_if->clause_cnt != 0
188 && do_if->clauses[do_if->clause_cnt - 1].condition == NULL);
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. */
195 parse_clause (struct do_if_trns *do_if)
197 struct expression *condition;
199 condition = expr_parse (default_dict, EXPR_BOOLEAN);
200 if (condition == NULL)
201 return CMD_CASCADING_FAILURE;
203 add_clause (do_if, condition, next_transformation ());
205 return lex_end_of_command ();
208 /* Adds a clause to DO_IF that tests for the given CONDITION and,
209 if true, jumps to TARGET_INDEX. */
211 add_clause (struct do_if_trns *do_if,
212 struct expression *condition, int target_index)
214 struct clause *clause;
216 if (do_if->clause_cnt > 0)
217 add_transformation (break_trns_proc, NULL, do_if);
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;
226 /* Finalizes DO IF by clearing the control stack, thus ensuring
227 that all open DO IFs are closed. */
229 do_if_finalize_func (void *do_if_ UNUSED)
231 /* This will be called multiple times if multiple DO IFs were
232 executed, which is slightly unclean, but at least it's
237 /* DO IF transformation procedure.
238 Checks each clause and jumps to the appropriate
241 do_if_trns_proc (void *do_if_, struct ccase *c, casenum_t case_num UNUSED)
243 struct do_if_trns *do_if = do_if_;
244 struct clause *clause;
246 for (clause = do_if->clauses; clause < do_if->clauses + do_if->clause_cnt;
249 if (clause->condition != NULL)
251 double boolean = expr_evaluate_num (clause->condition, c, case_num);
253 return clause->target_index;
254 else if (boolean == SYSMIS)
255 return do_if->past_END_IF_index;
258 return clause->target_index;
260 return do_if->past_END_IF_index;
263 /* Frees a DO IF transformation. */
265 do_if_trns_free (void *do_if_)
267 struct do_if_trns *do_if = do_if_;
268 struct clause *clause;
270 for (clause = do_if->clauses; clause < do_if->clauses + do_if->clause_cnt;
272 expr_free (clause->condition);
273 free (do_if->clauses);
278 /* Breaks out of a DO IF construct. */
280 break_trns_proc (void *do_if_, struct ccase *c UNUSED, casenum_t case_num UNUSED)
282 struct do_if_trns *do_if = do_if_;
284 return do_if->past_END_IF_index;
287 /* DO IF control structure class definition. */
288 static struct ctl_class do_if_class =