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/procedure.h>
25 #include <data/transformations.h>
26 #include <data/value.h>
27 #include <language/command.h>
28 #include <language/expressions/public.h>
29 #include <language/lexer/lexer.h>
30 #include <libpspp/alloc.h>
31 #include <libpspp/compiler.h>
32 #include <libpspp/message.h>
33 #include <libpspp/message.h>
34 #include <libpspp/str.h>
37 #define _(msgid) gettext (msgid)
39 /* DO IF, ELSE IF, and ELSE are translated as a single
40 transformation that evaluates each condition and jumps to the
41 start of the appropriate block of transformations. Each block
42 of transformations (except for the last) ends with a
43 transformation that jumps past the remaining blocks.
45 So, the following code:
55 is effectively translated like this:
57 IF a GOTO 1, IF b GOTO 2, ELSE GOTO 3.
67 /* A conditional clause. */
70 struct expression *condition; /* Test expression; NULL for ELSE clause. */
71 int target_index; /* Transformation to jump to if true. */
74 /* DO IF transformation. */
77 struct dataset *ds; /* The dataset */
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 const struct ctl_class do_if_class;
85 static int parse_clause (struct lexer *, struct do_if_trns *, struct dataset *ds);
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;
100 cmd_do_if (struct lexer *lexer, struct dataset *ds)
102 struct do_if_trns *do_if = xmalloc (sizeof *do_if);
103 do_if->clauses = NULL;
104 do_if->clause_cnt = 0;
107 ctl_stack_push (&do_if_class, do_if);
108 add_transformation_with_finalizer (ds, do_if_finalize_func,
109 do_if_trns_proc, do_if_trns_free, do_if);
111 return parse_clause (lexer, do_if, ds);
116 cmd_else_if (struct lexer *lexer, struct dataset *ds)
118 struct do_if_trns *do_if = ctl_stack_top (&do_if_class);
119 if (do_if == NULL || !must_not_have_else (do_if))
120 return CMD_CASCADING_FAILURE;
121 return parse_clause (lexer, do_if, ds);
126 cmd_else (struct lexer *lexer, struct dataset *ds)
128 struct do_if_trns *do_if = ctl_stack_top (&do_if_class);
129 assert (ds == do_if->ds);
130 if (do_if == NULL || !must_not_have_else (do_if))
131 return CMD_CASCADING_FAILURE;
133 return lex_end_of_command (lexer);
138 cmd_end_if (struct lexer *lexer, struct dataset *ds)
140 struct do_if_trns *do_if = ctl_stack_top (&do_if_class);
141 assert (ds == do_if->ds);
144 return CMD_CASCADING_FAILURE;
146 ctl_stack_pop (do_if);
148 return lex_end_of_command (lexer);
151 /* Closes out DO_IF, by adding a sentinel ELSE clause if
152 necessary and setting past_END_IF_index. */
154 close_do_if (void *do_if_)
156 struct do_if_trns *do_if = do_if_;
158 if (!has_else (do_if))
160 do_if->past_END_IF_index = next_transformation (do_if->ds);
163 /* Adds an ELSE clause to DO_IF pointing to the next
166 add_else (struct do_if_trns *do_if)
168 assert (!has_else (do_if));
169 add_clause (do_if, NULL, next_transformation (do_if->ds));
172 /* Returns true if DO_IF does not yet have an ELSE clause.
173 Reports an error and returns false if it does already. */
175 must_not_have_else (struct do_if_trns *do_if)
177 if (has_else (do_if))
179 msg (SE, _("This command may not follow ELSE in DO IF...END IF."));
186 /* Returns true if DO_IF already has an ELSE clause,
189 has_else (struct do_if_trns *do_if)
191 return (do_if->clause_cnt != 0
192 && do_if->clauses[do_if->clause_cnt - 1].condition == NULL);
195 /* Parses a DO IF or ELSE IF expression and appends the
196 corresponding clause to DO_IF. Checks for end of command and
197 returns a command return code. */
199 parse_clause (struct lexer *lexer, struct do_if_trns *do_if, struct dataset *ds)
201 struct expression *condition;
203 condition = expr_parse (lexer, ds, EXPR_BOOLEAN);
204 if (condition == NULL)
205 return CMD_CASCADING_FAILURE;
207 add_clause (do_if, condition, next_transformation (ds));
209 return lex_end_of_command (lexer);
212 /* Adds a clause to DO_IF that tests for the given CONDITION and,
213 if true, jumps to TARGET_INDEX. */
215 add_clause (struct do_if_trns *do_if,
216 struct expression *condition, int target_index)
218 struct clause *clause;
220 if (do_if->clause_cnt > 0)
221 add_transformation (do_if->ds, break_trns_proc, NULL, do_if);
223 do_if->clauses = xnrealloc (do_if->clauses,
224 do_if->clause_cnt + 1, sizeof *do_if->clauses);
225 clause = &do_if->clauses[do_if->clause_cnt++];
226 clause->condition = condition;
227 clause->target_index = target_index;
230 /* Finalizes DO IF by clearing the control stack, thus ensuring
231 that all open DO IFs are closed. */
233 do_if_finalize_func (void *do_if_ UNUSED)
235 /* This will be called multiple times if multiple DO IFs were
236 executed, which is slightly unclean, but at least it's
241 /* DO IF transformation procedure.
242 Checks each clause and jumps to the appropriate
245 do_if_trns_proc (void *do_if_, struct ccase *c, casenumber case_num UNUSED)
247 struct do_if_trns *do_if = do_if_;
248 struct clause *clause;
250 for (clause = do_if->clauses; clause < do_if->clauses + do_if->clause_cnt;
253 if (clause->condition != NULL)
255 double boolean = expr_evaluate_num (clause->condition, c, case_num);
257 return clause->target_index;
258 else if (boolean == SYSMIS)
259 return do_if->past_END_IF_index;
262 return clause->target_index;
264 return do_if->past_END_IF_index;
267 /* Frees a DO IF transformation. */
269 do_if_trns_free (void *do_if_)
271 struct do_if_trns *do_if = do_if_;
272 struct clause *clause;
274 for (clause = do_if->clauses; clause < do_if->clauses + do_if->clause_cnt;
276 expr_free (clause->condition);
277 free (do_if->clauses);
282 /* Breaks out of a DO IF construct. */
284 break_trns_proc (void *do_if_, struct ccase *c UNUSED, casenumber case_num UNUSED)
286 struct do_if_trns *do_if = do_if_;
288 return do_if->past_END_IF_index;
291 /* DO IF control structure class definition. */
292 static const struct ctl_class do_if_class =