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 <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>
36 #define _(msgid) gettext (msgid)
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.
44 So, the following code:
54 is effectively translated like this:
56 IF a GOTO 1, IF b GOTO 2, ELSE GOTO 3.
66 /* A conditional clause. */
69 struct expression *condition; /* Test expression; NULL for ELSE clause. */
70 int target_index; /* Transformation to jump to if true. */
73 /* DO IF transformation. */
76 struct clause *clauses; /* Clauses. */
77 size_t clause_cnt; /* Number of clauses. */
78 int past_END_IF_index; /* Transformation just past last clause. */
81 static struct ctl_class do_if_class;
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 *);
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);
92 static trns_proc_func do_if_trns_proc, break_trns_proc;
93 static trns_free_func do_if_trns_free;
99 struct do_if_trns *do_if = xmalloc (sizeof *do_if);
100 do_if->clauses = NULL;
101 do_if->clause_cnt = 0;
103 ctl_stack_push (&do_if_class, do_if);
104 add_transformation (do_if_trns_proc, do_if_trns_free, do_if);
106 return parse_clause (do_if);
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);
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;
127 return lex_end_of_command ();
134 struct do_if_trns *do_if = ctl_stack_top (&do_if_class);
136 return CMD_CASCADING_FAILURE;
138 ctl_stack_pop (do_if);
140 return lex_end_of_command ();
143 /* Closes out DO_IF, by adding a sentinel ELSE clause if
144 necessary and setting past_END_IF_index. */
146 close_do_if (void *do_if_)
148 struct do_if_trns *do_if = do_if_;
150 if (!has_else (do_if))
152 do_if->past_END_IF_index = next_transformation ();
155 /* Adds an ELSE clause to DO_IF pointing to the next
158 add_else (struct do_if_trns *do_if)
160 assert (!has_else (do_if));
161 add_clause (do_if, NULL, next_transformation ());
164 /* Returns true if DO_IF does not yet have an ELSE clause.
165 Reports an error and returns false if it does already. */
167 must_not_have_else (struct do_if_trns *do_if)
169 if (has_else (do_if))
171 msg (SE, _("This command may not follow ELSE in DO IF...END IF."));
178 /* Returns true if DO_IF already has an ELSE clause,
181 has_else (struct do_if_trns *do_if)
183 return (do_if->clause_cnt != 0
184 && do_if->clauses[do_if->clause_cnt - 1].condition == NULL);
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. */
191 parse_clause (struct do_if_trns *do_if)
193 struct expression *condition;
195 condition = expr_parse (default_dict, EXPR_BOOLEAN);
196 if (condition == NULL)
197 return CMD_CASCADING_FAILURE;
199 add_clause (do_if, condition, next_transformation ());
201 return lex_end_of_command ();
204 /* Adds a clause to DO_IF that tests for the given CONDITION and,
205 if true, jumps to TARGET_INDEX. */
207 add_clause (struct do_if_trns *do_if,
208 struct expression *condition, int target_index)
210 struct clause *clause;
212 if (do_if->clause_cnt > 0)
213 add_transformation (break_trns_proc, NULL, do_if);
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;
222 /* DO IF transformation procedure.
223 Checks each clause and jumps to the appropriate
226 do_if_trns_proc (void *do_if_, struct ccase *c, int case_num UNUSED)
228 struct do_if_trns *do_if = do_if_;
229 struct clause *clause;
231 for (clause = do_if->clauses; clause < do_if->clauses + do_if->clause_cnt;
234 if (clause->condition != NULL)
236 double boolean = expr_evaluate_num (clause->condition, c, case_num);
238 return clause->target_index;
239 else if (boolean == SYSMIS)
240 return do_if->past_END_IF_index;
243 return clause->target_index;
245 return do_if->past_END_IF_index;
248 /* Frees a DO IF transformation. */
250 do_if_trns_free (void *do_if_)
252 struct do_if_trns *do_if = do_if_;
253 struct clause *clause;
255 for (clause = do_if->clauses; clause < do_if->clauses + do_if->clause_cnt;
257 expr_free (clause->condition);
258 free (do_if->clauses);
263 /* Breaks out of a DO IF construct. */
265 break_trns_proc (void *do_if_, struct ccase *c UNUSED, int case_num UNUSED)
267 struct do_if_trns *do_if = do_if_;
269 return do_if->past_END_IF_index;
272 /* DO IF control structure class definition. */
273 static struct ctl_class do_if_class =