1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 #include "control-stack.h"
24 #include <data/case.h>
25 #include <data/procedure.h>
26 #include <data/transformations.h>
27 #include <data/value.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 dataset *ds; /* The dataset */
79 struct clause *clauses; /* Clauses. */
80 size_t clause_cnt; /* Number of clauses. */
81 int past_END_IF_index; /* Transformation just past last clause. */
84 static const struct ctl_class do_if_class;
86 static int parse_clause (struct lexer *, struct do_if_trns *, struct dataset *ds);
87 static void add_clause (struct do_if_trns *,
88 struct expression *condition, int target_index);
89 static void add_else (struct do_if_trns *);
91 static bool has_else (struct do_if_trns *);
92 static bool must_not_have_else (struct do_if_trns *);
93 static void close_do_if (void *do_if);
95 static trns_finalize_func do_if_finalize_func;
96 static trns_proc_func do_if_trns_proc, break_trns_proc;
97 static trns_free_func do_if_trns_free;
101 cmd_do_if (struct lexer *lexer, struct dataset *ds)
103 struct do_if_trns *do_if = xmalloc (sizeof *do_if);
104 do_if->clauses = NULL;
105 do_if->clause_cnt = 0;
108 ctl_stack_push (&do_if_class, do_if);
109 add_transformation_with_finalizer (ds, do_if_finalize_func,
110 do_if_trns_proc, do_if_trns_free, do_if);
112 return parse_clause (lexer, do_if, ds);
117 cmd_else_if (struct lexer *lexer, struct dataset *ds)
119 struct do_if_trns *do_if = ctl_stack_top (&do_if_class);
120 if (do_if == NULL || !must_not_have_else (do_if))
121 return CMD_CASCADING_FAILURE;
122 return parse_clause (lexer, do_if, ds);
127 cmd_else (struct lexer *lexer, struct dataset *ds)
129 struct do_if_trns *do_if = ctl_stack_top (&do_if_class);
130 assert (ds == do_if->ds);
131 if (do_if == NULL || !must_not_have_else (do_if))
132 return CMD_CASCADING_FAILURE;
134 return lex_end_of_command (lexer);
139 cmd_end_if (struct lexer *lexer, struct dataset *ds)
141 struct do_if_trns *do_if = ctl_stack_top (&do_if_class);
142 assert (ds == do_if->ds);
145 return CMD_CASCADING_FAILURE;
147 ctl_stack_pop (do_if);
149 return lex_end_of_command (lexer);
152 /* Closes out DO_IF, by adding a sentinel ELSE clause if
153 necessary and setting past_END_IF_index. */
155 close_do_if (void *do_if_)
157 struct do_if_trns *do_if = do_if_;
159 if (!has_else (do_if))
161 do_if->past_END_IF_index = next_transformation (do_if->ds);
164 /* Adds an ELSE clause to DO_IF pointing to the next
167 add_else (struct do_if_trns *do_if)
169 assert (!has_else (do_if));
170 add_clause (do_if, NULL, next_transformation (do_if->ds));
173 /* Returns true if DO_IF does not yet have an ELSE clause.
174 Reports an error and returns false if it does already. */
176 must_not_have_else (struct do_if_trns *do_if)
178 if (has_else (do_if))
180 msg (SE, _("This command may not follow ELSE in DO IF...END IF."));
187 /* Returns true if DO_IF already has an ELSE clause,
190 has_else (struct do_if_trns *do_if)
192 return (do_if->clause_cnt != 0
193 && do_if->clauses[do_if->clause_cnt - 1].condition == NULL);
196 /* Parses a DO IF or ELSE IF expression and appends the
197 corresponding clause to DO_IF. Checks for end of command and
198 returns a command return code. */
200 parse_clause (struct lexer *lexer, struct do_if_trns *do_if, struct dataset *ds)
202 struct expression *condition;
204 condition = expr_parse (lexer, ds, EXPR_BOOLEAN);
205 if (condition == NULL)
206 return CMD_CASCADING_FAILURE;
208 add_clause (do_if, condition, next_transformation (ds));
210 return lex_end_of_command (lexer);
213 /* Adds a clause to DO_IF that tests for the given CONDITION and,
214 if true, jumps to TARGET_INDEX. */
216 add_clause (struct do_if_trns *do_if,
217 struct expression *condition, int target_index)
219 struct clause *clause;
221 if (do_if->clause_cnt > 0)
222 add_transformation (do_if->ds, break_trns_proc, NULL, do_if);
224 do_if->clauses = xnrealloc (do_if->clauses,
225 do_if->clause_cnt + 1, sizeof *do_if->clauses);
226 clause = &do_if->clauses[do_if->clause_cnt++];
227 clause->condition = condition;
228 clause->target_index = target_index;
231 /* Finalizes DO IF by clearing the control stack, thus ensuring
232 that all open DO IFs are closed. */
234 do_if_finalize_func (void *do_if_ UNUSED)
236 /* This will be called multiple times if multiple DO IFs were
237 executed, which is slightly unclean, but at least it's
242 /* DO IF transformation procedure.
243 Checks each clause and jumps to the appropriate
246 do_if_trns_proc (void *do_if_, struct ccase *c, casenumber case_num UNUSED)
248 struct do_if_trns *do_if = do_if_;
249 struct clause *clause;
251 for (clause = do_if->clauses; clause < do_if->clauses + do_if->clause_cnt;
254 if (clause->condition != NULL)
256 double boolean = expr_evaluate_num (clause->condition, c, case_num);
258 return clause->target_index;
259 else if (boolean == SYSMIS)
260 return do_if->past_END_IF_index;
263 return clause->target_index;
265 return do_if->past_END_IF_index;
268 /* Frees a DO IF transformation. */
270 do_if_trns_free (void *do_if_)
272 struct do_if_trns *do_if = do_if_;
273 struct clause *clause;
275 for (clause = do_if->clauses; clause < do_if->clauses + do_if->clause_cnt;
277 expr_free (clause->condition);
278 free (do_if->clauses);
283 /* Breaks out of a DO IF construct. */
285 break_trns_proc (void *do_if_, struct ccase *c UNUSED, casenumber case_num UNUSED)
287 struct do_if_trns *do_if = do_if_;
289 return do_if->past_END_IF_index;
292 /* DO IF control structure class definition. */
293 static const struct ctl_class do_if_class =