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
31 #include "debug-print.h"
38 /* Description of DO IF transformations:
40 DO IF has two transformations. One is a conditional jump around
41 a false condition. The second is an unconditional jump around
42 the rest of the code after a true condition. Both of these types
43 have their destinations backpatched in by the next clause (ELSE IF,
46 The characters `^V<>' are meant to represent arrows.
51 V *. Transformations executed when the condition on DO IF is true.
53 V 2. GOTO>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>V
58 V *. Transformations executed when condition on 1st ELSE IF is true. V
60 V 2. GOTO>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>V
65 V *. Transformations executed when condition on 2nd ELSE IF is true. V
67 V 2. GOTO>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>V
69 >>*. Transformations executed when no condition is true. (ELSE) V
71 *. Transformations after DO IF structure.<<<<<<<<<<<<<<<<<<<<<<<<<<<<
78 static struct do_if_trns *parse_do_if (void);
79 static void add_ELSE_IF (struct do_if_trns *);
80 static int goto_trns_proc (struct trns_header *, struct ccase *);
81 static int do_if_trns_proc (struct trns_header *, struct ccase *);
82 static void do_if_trns_free (struct trns_header *);
90 /* Parse the transformation. */
95 /* Finish up the transformation, add to control stack, add to
96 transformation list. */
98 t->ctl.type = CST_DO_IF;
99 t->ctl.down = ctl_stack;
100 t->ctl.trns = (struct trns_header *) t;
104 add_transformation ((struct trns_header *) t);
113 /* Transformation created. */
114 struct do_if_trns *t;
116 /* Check that we're in a pleasing situation. */
117 if (!ctl_stack || ctl_stack->type != CST_DO_IF)
119 msg (SE, _("There is no DO IF to match with this ELSE IF."));
122 if (((struct do_if_trns *) ctl_stack->trns)->has_else)
124 msg (SE, _("The ELSE command must follow all ELSE IF commands "
125 "in a DO IF structure."));
129 /* Parse the transformation. */
134 /* Stick in the breakout transformation. */
135 t->brk = xmalloc (sizeof *t->brk);
136 t->brk->h.proc = goto_trns_proc;
137 t->brk->h.free = NULL;
139 /* Add to list of transformations, add to string of ELSE IFs. */
140 add_transformation ((struct trns_header *) t->brk);
141 add_transformation ((struct trns_header *) t);
147 msg (SE, _("End of command expected."));
148 return CMD_TRAILING_GARBAGE;
158 struct do_if_trns *t;
160 lex_match_id ("ELSE");
162 /* Check that we're in a pleasing situation. */
163 if (!ctl_stack || ctl_stack->type != CST_DO_IF)
165 msg (SE, _("There is no DO IF to match with this ELSE."));
169 if (((struct do_if_trns *) ctl_stack->trns)->has_else)
171 msg (SE, _("There may be at most one ELSE clause in each DO IF "
172 "structure. It must be the last clause."));
176 /* Note that the ELSE transformation is *not* added to the list of
177 transformations. That's because it doesn't need to do anything.
178 Its goto transformation *is* added, because that's necessary.
179 The main DO IF do_if_trns is the destructor for this ELSE
181 t = xmalloc (sizeof *t);
183 t->brk = xmalloc (sizeof *t->brk);
184 t->brk->h.proc = goto_trns_proc;
185 t->brk->h.free = NULL;
187 add_transformation ((struct trns_header *) t->brk);
188 t->h.index = t->brk->h.index + 1;
190 /* Add to string of ELSE IFs. */
193 return lex_end_of_command ();
201 struct do_if_trns *iter;
205 /* Check that we're in a pleasing situation. */
206 if (!ctl_stack || ctl_stack->type != CST_DO_IF)
208 msg (SE, _("There is no DO IF to match with this END IF."));
212 /* Chain down the list, backpatching destinations for gotos. */
213 iter = (struct do_if_trns *) ctl_stack->trns;
217 iter->brk->dest = n_trns;
218 iter->missing_jump = n_trns;
224 iter->false_jump = n_trns;
226 /* Pop control stack. */
227 ctl_stack = ctl_stack->down;
229 return lex_end_of_command ();
232 /* Adds an ELSE IF or ELSE to the chain of them that hangs off the
235 add_ELSE_IF (struct do_if_trns * t)
238 struct do_if_trns *iter;
240 iter = (struct do_if_trns *) ctl_stack->trns;
243 assert (iter != NULL);
246 iter->false_jump = t->h.index;
249 /* Parses a DO IF or ELSE IF command and returns a pointer to a mostly
250 filled in transformation. */
251 static struct do_if_trns *
254 struct do_if_trns *t;
255 struct expression *e;
259 e = expr_parse (PXP_BOOLEAN);
265 lex_error (_("expecting end of command"));
269 t = xmalloc (sizeof *t);
270 t->h.proc = do_if_trns_proc;
271 t->h.free = do_if_trns_free;
278 /* Executes a goto transformation. */
280 goto_trns_proc (struct trns_header * t, struct ccase * c unused)
282 return ((struct goto_trns *) t)->dest;
286 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, &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. */