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
27 #include "expressions/public.h"
32 #include "debug-print.h"
35 /* Description of DO IF transformations:
37 DO IF has two transformations. One is a conditional jump around
38 a false condition. The second is an unconditional jump around
39 the rest of the code after a true condition. Both of these types
40 have their destinations backpatched in by the next clause (ELSE IF,
43 The characters `^V<>' are meant to represent arrows.
48 V *. Transformations executed when the condition on DO IF is true.
50 V 2. GOTO>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>V
55 V *. Transformations executed when condition on 1st ELSE IF is true. V
57 V 2. GOTO>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>V
62 V *. Transformations executed when condition on 2nd ELSE IF is true. V
64 V 2. GOTO>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>V
66 >>*. Transformations executed when no condition is true. (ELSE) V
68 *. Transformations after DO IF structure.<<<<<<<<<<<<<<<<<<<<<<<<<<<<
73 static struct do_if_trns *parse_do_if (void);
74 static void add_ELSE_IF (struct do_if_trns *);
75 static trns_proc_func goto_trns_proc, do_if_trns_proc;
76 static trns_free_func do_if_trns_free;
84 /* Parse the transformation. */
89 /* Finish up the transformation, add to control stack, add to
90 transformation list. */
92 t->ctl.type = CST_DO_IF;
93 t->ctl.down = ctl_stack;
94 t->ctl.trns = (struct trns_header *) t;
98 add_transformation ((struct trns_header *) t);
107 /* Transformation created. */
108 struct do_if_trns *t;
110 /* Check that we're in a pleasing situation. */
111 if (!ctl_stack || ctl_stack->type != CST_DO_IF)
113 msg (SE, _("There is no DO IF to match with this ELSE IF."));
116 if (((struct do_if_trns *) ctl_stack->trns)->has_else)
118 msg (SE, _("The ELSE command must follow all ELSE IF commands "
119 "in a DO IF structure."));
123 /* Parse the transformation. */
128 /* Stick in the breakout transformation. */
129 t->brk = xmalloc (sizeof *t->brk);
130 t->brk->h.proc = goto_trns_proc;
131 t->brk->h.free = NULL;
133 /* Add to list of transformations, add to string of ELSE IFs. */
134 add_transformation ((struct trns_header *) t->brk);
135 add_transformation ((struct trns_header *) t);
141 msg (SE, _("End of command expected."));
142 return CMD_TRAILING_GARBAGE;
152 struct do_if_trns *t;
154 /* Check that we're in a pleasing situation. */
155 if (!ctl_stack || ctl_stack->type != CST_DO_IF)
157 msg (SE, _("There is no DO IF to match with this ELSE."));
161 if (((struct do_if_trns *) ctl_stack->trns)->has_else)
163 msg (SE, _("There may be at most one ELSE clause in each DO IF "
164 "structure. It must be the last clause."));
168 /* Note that the ELSE transformation is *not* added to the list of
169 transformations. That's because it doesn't need to do anything.
170 Its goto transformation *is* added, because that's necessary.
171 The main DO IF do_if_trns is the destructor for this ELSE
173 t = xmalloc (sizeof *t);
175 t->brk = xmalloc (sizeof *t->brk);
176 t->brk->h.proc = goto_trns_proc;
177 t->brk->h.free = NULL;
179 add_transformation ((struct trns_header *) t->brk);
180 t->h.index = t->brk->h.index + 1;
182 /* Add to string of ELSE IFs. */
185 return lex_end_of_command ();
193 struct do_if_trns *iter;
195 /* Check that we're in a pleasing situation. */
196 if (!ctl_stack || ctl_stack->type != CST_DO_IF)
198 msg (SE, _("There is no DO IF to match with this END IF."));
202 /* Chain down the list, backpatching destinations for gotos. */
203 iter = (struct do_if_trns *) ctl_stack->trns;
207 iter->brk->dest = n_trns;
208 iter->missing_jump = n_trns;
214 iter->false_jump = n_trns;
216 /* Pop control stack. */
217 ctl_stack = ctl_stack->down;
219 return lex_end_of_command ();
222 /* Adds an ELSE IF or ELSE to the chain of them that hangs off the
225 add_ELSE_IF (struct do_if_trns * t)
228 struct do_if_trns *iter;
230 iter = (struct do_if_trns *) ctl_stack->trns;
233 assert (iter != NULL);
236 iter->false_jump = t->h.index;
239 /* Parses a DO IF or ELSE IF command and returns a pointer to a mostly
240 filled in transformation. */
241 static struct do_if_trns *
244 struct do_if_trns *t;
245 struct expression *e;
247 e = expr_parse (default_dict, EXPR_BOOLEAN);
253 lex_error (_("expecting end of command"));
257 t = xmalloc (sizeof *t);
258 t->h.proc = do_if_trns_proc;
259 t->h.free = do_if_trns_free;
266 /* Executes a goto transformation. */
268 goto_trns_proc (struct trns_header * t, struct ccase * c UNUSED,
271 return ((struct goto_trns *) t)->dest;
275 do_if_trns_proc (struct trns_header * trns, struct ccase * c,
278 struct do_if_trns *t = (struct do_if_trns *) trns;
281 boolean = expr_evaluate_num (t->cond, c, case_num);
284 debug_printf ((_("DO IF %d: true\n"), t->h.index));
287 else if (boolean == 0.0)
289 debug_printf ((_("DO IF %d: false\n"), t->h.index));
290 return t->false_jump;
294 debug_printf ((_("DO IF %d: missing\n"), t->h.index));
295 return t->missing_jump;
300 do_if_trns_free (struct trns_header * trns)
302 struct do_if_trns *t = (struct do_if_trns *) trns;
305 /* If brk is NULL then this is the main DO IF; therefore we
306 need to chain down to the ELSE and delete it. */
309 struct do_if_trns *iter = t->next;
314 /* This is the ELSE. */