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., 59 Temple Place - Suite 330, Boston, MA
32 #include "debug-print.h"
39 /* Description of DO IF transformations:
41 DO IF has two transformations. One is a conditional jump around
42 a false condition. The second is an unconditional jump around
43 the rest of the code after a true condition. Both of these types
44 have their destinations backpatched in by the next clause (ELSE IF,
47 The characters `^V<>' are meant to represent arrows.
52 V *. Transformations executed when the condition on DO IF is true.
54 V 2. GOTO>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>V
59 V *. Transformations executed when condition on 1st ELSE IF is true. V
61 V 2. GOTO>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>V
66 V *. Transformations executed when condition on 2nd ELSE IF is true. V
68 V 2. GOTO>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>V
70 >>*. Transformations executed when no condition is true. (ELSE) V
72 *. Transformations after DO IF structure.<<<<<<<<<<<<<<<<<<<<<<<<<<<<
77 static struct do_if_trns *parse_do_if (void);
78 static void add_ELSE_IF (struct do_if_trns *);
79 static trns_proc_func goto_trns_proc, do_if_trns_proc;
80 static trns_free_func do_if_trns_free;
88 /* Parse the transformation. */
93 /* Finish up the transformation, add to control stack, add to
94 transformation list. */
96 t->ctl.type = CST_DO_IF;
97 t->ctl.down = ctl_stack;
98 t->ctl.trns = (struct trns_header *) t;
102 add_transformation ((struct trns_header *) t);
111 /* Transformation created. */
112 struct do_if_trns *t;
114 /* Check that we're in a pleasing situation. */
115 if (!ctl_stack || ctl_stack->type != CST_DO_IF)
117 msg (SE, _("There is no DO IF to match with this ELSE IF."));
120 if (((struct do_if_trns *) ctl_stack->trns)->has_else)
122 msg (SE, _("The ELSE command must follow all ELSE IF commands "
123 "in a DO IF structure."));
127 /* Parse the transformation. */
132 /* Stick in the breakout transformation. */
133 t->brk = xmalloc (sizeof *t->brk);
134 t->brk->h.proc = goto_trns_proc;
135 t->brk->h.free = NULL;
137 /* Add to list of transformations, add to string of ELSE IFs. */
138 add_transformation ((struct trns_header *) t->brk);
139 add_transformation ((struct trns_header *) t);
145 msg (SE, _("End of command expected."));
146 return CMD_TRAILING_GARBAGE;
156 struct do_if_trns *t;
158 lex_match_id ("ELSE");
160 /* Check that we're in a pleasing situation. */
161 if (!ctl_stack || ctl_stack->type != CST_DO_IF)
163 msg (SE, _("There is no DO IF to match with this ELSE."));
167 if (((struct do_if_trns *) ctl_stack->trns)->has_else)
169 msg (SE, _("There may be at most one ELSE clause in each DO IF "
170 "structure. It must be the last clause."));
174 /* Note that the ELSE transformation is *not* added to the list of
175 transformations. That's because it doesn't need to do anything.
176 Its goto transformation *is* added, because that's necessary.
177 The main DO IF do_if_trns is the destructor for this ELSE
179 t = xmalloc (sizeof *t);
181 t->brk = xmalloc (sizeof *t->brk);
182 t->brk->h.proc = goto_trns_proc;
183 t->brk->h.free = NULL;
185 add_transformation ((struct trns_header *) t->brk);
186 t->h.index = t->brk->h.index + 1;
188 /* Add to string of ELSE IFs. */
191 return lex_end_of_command ();
199 struct do_if_trns *iter;
203 /* Check that we're in a pleasing situation. */
204 if (!ctl_stack || ctl_stack->type != CST_DO_IF)
206 msg (SE, _("There is no DO IF to match with this END IF."));
210 /* Chain down the list, backpatching destinations for gotos. */
211 iter = (struct do_if_trns *) ctl_stack->trns;
215 iter->brk->dest = n_trns;
216 iter->missing_jump = n_trns;
222 iter->false_jump = n_trns;
224 /* Pop control stack. */
225 ctl_stack = ctl_stack->down;
227 return lex_end_of_command ();
230 /* Adds an ELSE IF or ELSE to the chain of them that hangs off the
233 add_ELSE_IF (struct do_if_trns * t)
236 struct do_if_trns *iter;
238 iter = (struct do_if_trns *) ctl_stack->trns;
241 assert (iter != NULL);
244 iter->false_jump = t->h.index;
247 /* Parses a DO IF or ELSE IF command and returns a pointer to a mostly
248 filled in transformation. */
249 static struct do_if_trns *
252 struct do_if_trns *t;
253 struct expression *e;
257 e = expr_parse (PXP_BOOLEAN);
263 lex_error (_("expecting end of command"));
267 t = xmalloc (sizeof *t);
268 t->h.proc = do_if_trns_proc;
269 t->h.free = do_if_trns_free;
276 /* Executes a goto transformation. */
278 goto_trns_proc (struct trns_header * t, struct ccase * c UNUSED,
281 return ((struct goto_trns *) t)->dest;
285 do_if_trns_proc (struct trns_header * trns, struct ccase * c,
288 struct do_if_trns *t = (struct do_if_trns *) trns;
291 expr_evaluate (t->cond, c, case_num, &bool);
294 debug_printf ((_("DO IF %d: true\n"), t->h.index));
297 else if (bool.f == 0.0)
299 debug_printf ((_("DO IF %d: false\n"), t->h.index));
300 return t->false_jump;
304 debug_printf ((_("DO IF %d: missing\n"), t->h.index));
305 return t->missing_jump;
310 do_if_trns_free (struct trns_header * trns)
312 struct do_if_trns *t = (struct do_if_trns *) trns;
315 /* If brk is NULL then this is the main DO IF; therefore we
316 need to chain down to the ELSE and delete it. */
319 struct do_if_trns *iter = t->next;
324 /* This is the ELSE. */