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
21 #include "control-stack.h"
28 #include "expressions/public.h"
34 #define _(msgid) gettext (msgid)
36 /* DO IF, ELSE IF, and ELSE are translated as a single
37 transformation that evaluates each condition and jumps to the
38 start of the appropriate block of transformations. Each block
39 of transformations (except for the last) ends with a
40 transformation that jumps past the remaining blocks.
42 So, the following code:
52 is effectively translated like this:
54 IF a GOTO 1, IF b GOTO 2, ELSE GOTO 3.
64 /* A conditional clause. */
67 struct expression *condition; /* Test expression; NULL for ELSE clause. */
68 int target_index; /* Transformation to jump to if true. */
71 /* DO IF transformation. */
74 struct clause *clauses; /* Clauses. */
75 size_t clause_cnt; /* Number of clauses. */
76 int past_END_IF_index; /* Transformation just past last clause. */
79 static struct ctl_class do_if_class;
81 static int parse_clause (struct do_if_trns *);
82 static void add_clause (struct do_if_trns *,
83 struct expression *condition, int target_index);
84 static void add_else (struct do_if_trns *);
86 static bool has_else (struct do_if_trns *);
87 static bool must_not_have_else (struct do_if_trns *);
88 static void close_do_if (void *do_if);
90 static trns_proc_func do_if_trns_proc, break_trns_proc;
91 static trns_free_func do_if_trns_free;
97 struct do_if_trns *do_if = xmalloc (sizeof *do_if);
98 do_if->clauses = NULL;
99 do_if->clause_cnt = 0;
101 ctl_stack_push (&do_if_class, do_if);
102 add_transformation (do_if_trns_proc, do_if_trns_free, do_if);
104 return parse_clause (do_if);
111 struct do_if_trns *do_if = ctl_stack_top (&do_if_class);
112 if (do_if == NULL || !must_not_have_else (do_if))
113 return CMD_CASCADING_FAILURE;
114 return parse_clause (do_if);
121 struct do_if_trns *do_if = ctl_stack_top (&do_if_class);
122 if (do_if == NULL || !must_not_have_else (do_if))
123 return CMD_CASCADING_FAILURE;
125 return lex_end_of_command ();
132 struct do_if_trns *do_if = ctl_stack_top (&do_if_class);
134 return CMD_CASCADING_FAILURE;
136 ctl_stack_pop (do_if);
138 return lex_end_of_command ();
141 /* Closes out DO_IF, by adding a sentinel ELSE clause if
142 necessary and setting past_END_IF_index. */
144 close_do_if (void *do_if_)
146 struct do_if_trns *do_if = do_if_;
148 if (!has_else (do_if))
150 do_if->past_END_IF_index = next_transformation ();
153 /* Adds an ELSE clause to DO_IF pointing to the next
156 add_else (struct do_if_trns *do_if)
158 assert (!has_else (do_if));
159 add_clause (do_if, NULL, next_transformation ());
162 /* Returns true if DO_IF does not yet have an ELSE clause.
163 Reports an error and returns false if it does already. */
165 must_not_have_else (struct do_if_trns *do_if)
167 if (has_else (do_if))
169 msg (SE, _("This command may not follow ELSE in DO IF...END IF."));
176 /* Returns true if DO_IF already has an ELSE clause,
179 has_else (struct do_if_trns *do_if)
181 return (do_if->clause_cnt != 0
182 && do_if->clauses[do_if->clause_cnt - 1].condition == NULL);
185 /* Parses a DO IF or ELSE IF expression and appends the
186 corresponding clause to DO_IF. Checks for end of command and
187 returns a command return code. */
189 parse_clause (struct do_if_trns *do_if)
191 struct expression *condition;
193 condition = expr_parse (default_dict, EXPR_BOOLEAN);
194 if (condition == NULL)
195 return CMD_CASCADING_FAILURE;
197 add_clause (do_if, condition, next_transformation ());
199 return lex_end_of_command ();
202 /* Adds a clause to DO_IF that tests for the given CONDITION and,
203 if true, jumps to TARGET_INDEX. */
205 add_clause (struct do_if_trns *do_if,
206 struct expression *condition, int target_index)
208 struct clause *clause;
210 if (do_if->clause_cnt > 0)
211 add_transformation (break_trns_proc, NULL, do_if);
213 do_if->clauses = xnrealloc (do_if->clauses,
214 do_if->clause_cnt + 1, sizeof *do_if->clauses);
215 clause = &do_if->clauses[do_if->clause_cnt++];
216 clause->condition = condition;
217 clause->target_index = target_index;
220 /* DO IF transformation procedure.
221 Checks each clause and jumps to the appropriate
224 do_if_trns_proc (void *do_if_, struct ccase *c, int case_num UNUSED)
226 struct do_if_trns *do_if = do_if_;
227 struct clause *clause;
229 for (clause = do_if->clauses; clause < do_if->clauses + do_if->clause_cnt;
232 if (clause->condition != NULL)
234 double boolean = expr_evaluate_num (clause->condition, c, case_num);
236 return clause->target_index;
237 else if (boolean == SYSMIS)
238 return do_if->past_END_IF_index;
241 return clause->target_index;
243 return do_if->past_END_IF_index;
246 /* Frees a DO IF transformation. */
248 do_if_trns_free (void *do_if_)
250 struct do_if_trns *do_if = do_if_;
251 struct clause *clause;
253 for (clause = do_if->clauses; clause < do_if->clauses + do_if->clause_cnt;
255 expr_free (clause->condition);
256 free (do_if->clauses);
261 /* Breaks out of a DO IF construct. */
263 break_trns_proc (void *do_if_, struct ccase *c UNUSED, int case_num UNUSED)
265 struct do_if_trns *do_if = do_if_;
267 return do_if->past_END_IF_index;
270 /* DO IF control structure class definition. */
271 static struct ctl_class do_if_class =