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 "ctl-stack.h"
27 #include "expressions/public.h"
33 #define _(msgid) gettext (msgid)
35 /* DO IF, ELSE IF, and ELSE are translated as a single
36 transformation that evaluates each condition and jumps to the
37 start of the appropriate block of transformations. Each block
38 of transformations (except for the last) ends with a
39 transformation that jumps past the remaining blocks.
41 So, the following code:
51 is effectively translated like this:
53 IF a GOTO 1, IF b GOTO 2, ELSE GOTO 3.
63 /* A conditional clause. */
66 struct expression *condition; /* Test expression; NULL for ELSE clause. */
67 int target_index; /* Transformation to jump to if true. */
70 /* DO IF transformation. */
73 struct clause *clauses; /* Clauses. */
74 size_t clause_cnt; /* Number of clauses. */
75 int past_END_IF_index; /* Transformation just past last clause. */
78 static struct ctl_class do_if_class;
80 static int parse_clause (struct do_if_trns *);
81 static void add_clause (struct do_if_trns *,
82 struct expression *condition, int target_index);
83 static void add_else (struct do_if_trns *);
85 static bool has_else (struct do_if_trns *);
86 static bool must_not_have_else (struct do_if_trns *);
87 static void close_do_if (void *do_if);
89 static trns_proc_func do_if_trns_proc, break_trns_proc;
90 static trns_free_func do_if_trns_free;
96 struct do_if_trns *do_if = xmalloc (sizeof *do_if);
97 do_if->clauses = NULL;
98 do_if->clause_cnt = 0;
100 ctl_stack_push (&do_if_class, do_if);
101 add_transformation (do_if_trns_proc, do_if_trns_free, do_if);
103 return parse_clause (do_if);
110 struct do_if_trns *do_if = ctl_stack_top (&do_if_class);
111 if (do_if == NULL || !must_not_have_else (do_if))
113 return parse_clause (do_if);
120 struct do_if_trns *do_if = ctl_stack_top (&do_if_class);
121 if (do_if == NULL || !must_not_have_else (do_if))
124 return lex_end_of_command ();
131 struct do_if_trns *do_if = ctl_stack_top (&do_if_class);
135 ctl_stack_pop (do_if);
137 return lex_end_of_command ();
140 /* Closes out DO_IF, by adding a sentinel ELSE clause if
141 necessary and setting past_END_IF_index. */
143 close_do_if (void *do_if_)
145 struct do_if_trns *do_if = do_if_;
147 if (!has_else (do_if))
149 do_if->past_END_IF_index = next_transformation ();
152 /* Adds an ELSE clause to DO_IF pointing to the next
155 add_else (struct do_if_trns *do_if)
157 assert (!has_else (do_if));
158 add_clause (do_if, NULL, next_transformation ());
161 /* Returns true if DO_IF does not yet have an ELSE clause.
162 Reports an error and returns false if it does already. */
164 must_not_have_else (struct do_if_trns *do_if)
166 if (has_else (do_if))
168 msg (SE, _("This command may not follow ELSE in DO IF...END IF."));
175 /* Returns true if DO_IF already has an ELSE clause,
178 has_else (struct do_if_trns *do_if)
180 return (do_if->clause_cnt != 0
181 && do_if->clauses[do_if->clause_cnt - 1].condition == NULL);
184 /* Parses a DO IF or ELSE IF expression and appends the
185 corresponding clause to DO_IF. Checks for end of command and
186 returns a command return code. */
188 parse_clause (struct do_if_trns *do_if)
190 struct expression *condition;
192 condition = expr_parse (default_dict, EXPR_BOOLEAN);
193 if (condition == NULL)
196 add_clause (do_if, condition, next_transformation ());
198 return lex_end_of_command ();
201 /* Adds a clause to DO_IF that tests for the given CONDITION and,
202 if true, jumps to TARGET_INDEX. */
204 add_clause (struct do_if_trns *do_if,
205 struct expression *condition, int target_index)
207 struct clause *clause;
209 if (do_if->clause_cnt > 0)
210 add_transformation (break_trns_proc, NULL, do_if);
212 do_if->clauses = xnrealloc (do_if->clauses,
213 do_if->clause_cnt + 1, sizeof *do_if->clauses);
214 clause = &do_if->clauses[do_if->clause_cnt++];
215 clause->condition = condition;
216 clause->target_index = target_index;
219 /* DO IF transformation procedure.
220 Checks each clause and jumps to the appropriate
223 do_if_trns_proc (void *do_if_, struct ccase *c, int case_num UNUSED)
225 struct do_if_trns *do_if = do_if_;
226 struct clause *clause;
228 for (clause = do_if->clauses; clause < do_if->clauses + do_if->clause_cnt;
231 if (clause->condition != NULL)
233 double boolean = expr_evaluate_num (clause->condition, c, case_num);
235 return clause->target_index;
236 else if (boolean == SYSMIS)
237 return do_if->past_END_IF_index;
240 return clause->target_index;
242 return do_if->past_END_IF_index;
245 /* Frees a DO IF transformation. */
247 do_if_trns_free (void *do_if_)
249 struct do_if_trns *do_if = do_if_;
250 struct clause *clause;
252 for (clause = do_if->clauses; clause < do_if->clauses + do_if->clause_cnt;
254 expr_free (clause->condition);
255 free (do_if->clauses);
259 /* Breaks out of a DO IF construct. */
261 break_trns_proc (void *do_if_, struct ccase *c UNUSED, int case_num UNUSED)
263 struct do_if_trns *do_if = do_if_;
265 return do_if->past_END_IF_index;
268 /* DO IF control structure class definition. */
269 static struct ctl_class do_if_class =