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
27 #include "expressions/public.h"
33 #define _(msgid) gettext (msgid)
35 #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.<<<<<<<<<<<<<<<<<<<<<<<<<<<<
76 static struct do_if_trns *parse_do_if (void);
77 static void add_ELSE_IF (struct do_if_trns *);
78 static trns_proc_func goto_trns_proc, do_if_trns_proc;
79 static trns_free_func do_if_trns_free;
87 /* Parse the transformation. */
92 /* Finish up the transformation, add to control stack, add to
93 transformation list. */
95 t->ctl.type = CST_DO_IF;
96 t->ctl.down = ctl_stack;
97 t->ctl.trns = (struct trns_header *) t;
101 add_transformation ((struct trns_header *) t);
110 /* Transformation created. */
111 struct do_if_trns *t;
113 /* Check that we're in a pleasing situation. */
114 if (!ctl_stack || ctl_stack->type != CST_DO_IF)
116 msg (SE, _("There is no DO IF to match with this ELSE IF."));
119 if (((struct do_if_trns *) ctl_stack->trns)->has_else)
121 msg (SE, _("The ELSE command must follow all ELSE IF commands "
122 "in a DO IF structure."));
126 /* Parse the transformation. */
131 /* Stick in the breakout transformation. */
132 t->brk = xmalloc (sizeof *t->brk);
133 t->brk->h.proc = goto_trns_proc;
134 t->brk->h.free = NULL;
136 /* Add to list of transformations, add to string of ELSE IFs. */
137 add_transformation ((struct trns_header *) t->brk);
138 add_transformation ((struct trns_header *) t);
144 msg (SE, _("End of command expected."));
145 return CMD_TRAILING_GARBAGE;
155 struct do_if_trns *t;
157 /* Check that we're in a pleasing situation. */
158 if (!ctl_stack || ctl_stack->type != CST_DO_IF)
160 msg (SE, _("There is no DO IF to match with this ELSE."));
164 if (((struct do_if_trns *) ctl_stack->trns)->has_else)
166 msg (SE, _("There may be at most one ELSE clause in each DO IF "
167 "structure. It must be the last clause."));
171 /* Note that the ELSE transformation is *not* added to the list of
172 transformations. That's because it doesn't need to do anything.
173 Its goto transformation *is* added, because that's necessary.
174 The main DO IF do_if_trns is the destructor for this ELSE
176 t = xmalloc (sizeof *t);
178 t->brk = xmalloc (sizeof *t->brk);
179 t->brk->h.proc = goto_trns_proc;
180 t->brk->h.free = NULL;
182 add_transformation ((struct trns_header *) t->brk);
183 t->h.index = t->brk->h.index + 1;
185 /* Add to string of ELSE IFs. */
188 return lex_end_of_command ();
196 struct do_if_trns *iter;
198 /* Check that we're in a pleasing situation. */
199 if (!ctl_stack || ctl_stack->type != CST_DO_IF)
201 msg (SE, _("There is no DO IF to match with this END IF."));
205 /* Chain down the list, backpatching destinations for gotos. */
206 iter = (struct do_if_trns *) ctl_stack->trns;
210 iter->brk->dest = n_trns;
211 iter->missing_jump = n_trns;
217 iter->false_jump = n_trns;
219 /* Pop control stack. */
220 ctl_stack = ctl_stack->down;
222 return lex_end_of_command ();
225 /* Adds an ELSE IF or ELSE to the chain of them that hangs off the
228 add_ELSE_IF (struct do_if_trns * t)
231 struct do_if_trns *iter;
233 iter = (struct do_if_trns *) ctl_stack->trns;
236 assert (iter != NULL);
239 iter->false_jump = t->h.index;
242 /* Parses a DO IF or ELSE IF command and returns a pointer to a mostly
243 filled in transformation. */
244 static struct do_if_trns *
247 struct do_if_trns *t;
248 struct expression *e;
250 e = expr_parse (default_dict, EXPR_BOOLEAN);
256 lex_error (_("expecting end of command"));
260 t = xmalloc (sizeof *t);
261 t->h.proc = do_if_trns_proc;
262 t->h.free = do_if_trns_free;
269 /* Executes a goto transformation. */
271 goto_trns_proc (struct trns_header * t, struct ccase * c UNUSED,
274 return ((struct goto_trns *) t)->dest;
278 do_if_trns_proc (struct trns_header * trns, struct ccase * c,
281 struct do_if_trns *t = (struct do_if_trns *) trns;
284 boolean = expr_evaluate_num (t->cond, c, case_num);
287 debug_printf ((_("DO IF %d: true\n"), t->h.index));
290 else if (boolean == 0.0)
292 debug_printf ((_("DO IF %d: false\n"), t->h.index));
293 return t->false_jump;
297 debug_printf ((_("DO IF %d: missing\n"), t->h.index));
298 return t->missing_jump;
303 do_if_trns_free (struct trns_header * trns)
305 struct do_if_trns *t = (struct do_if_trns *) trns;
308 /* If brk is NULL then this is the main DO IF; therefore we
309 need to chain down to the ELSE and delete it. */
312 struct do_if_trns *iter = t->next;
317 /* This is the ELSE. */