1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2009-2011 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "data/case.h"
22 #include "data/dataset.h"
23 #include "data/dictionary.h"
24 #include "data/settings.h"
25 #include "data/transformations.h"
26 #include "data/variable.h"
27 #include "language/command.h"
28 #include "language/expressions/public.h"
29 #include "language/lexer/lexer.h"
30 #include "libpspp/compiler.h"
31 #include "libpspp/message.h"
32 #include "libpspp/misc.h"
33 #include "libpspp/pool.h"
34 #include "libpspp/str.h"
36 #include "gl/xalloc.h"
39 #define _(msgid) gettext (msgid)
43 /* a=a TO b [BY c]. */
44 struct variable *index_var; /* Index variable. */
45 struct expression *first; /* Starting index. */
46 struct expression *by; /* Index increment (or NULL). */
47 struct expression *last; /* Terminal index. */
49 /* IF condition for LOOP or END LOOP. */
50 struct expression *loop_condition;
51 struct expression *end_loop_condition;
53 /* Inner transformations. */
54 struct transformation *xforms;
58 static trns_proc_func loop_trns_proc;
59 static trns_free_func loop_trns_free;
61 static bool parse_if_clause (struct lexer *, struct dataset *,
62 struct expression **);
63 static bool parse_index_clause (struct dataset *, struct lexer *,
70 cmd_loop (struct lexer *lexer, struct dataset *ds)
72 struct loop_trns *loop = xmalloc (sizeof *loop);
73 *loop = (struct loop_trns) { .index_var = NULL };
76 while (lex_token (lexer) != T_ENDCMD && ok)
78 if (lex_match_id (lexer, "IF"))
79 ok = parse_if_clause (lexer, ds, &loop->loop_condition);
81 ok = parse_index_clause (ds, lexer, loop);
83 lex_end_of_command (lexer);
85 proc_push_transformations (ds);
88 if (lex_token (lexer) == T_STOP)
90 lex_error (lexer, NULL);
94 else if (lex_match_phrase (lexer, "END LOOP"))
96 if (lex_match_id (lexer, "IF"))
97 ok = parse_if_clause (lexer, ds, &loop->end_loop_condition) && ok;
101 cmd_parse_in_state (lexer, ds, CMD_STATE_NESTED);
103 proc_pop_transformations (ds, &loop->xforms, &loop->n_xforms);
105 add_transformation (ds, loop_trns_proc, loop_trns_free, loop);
107 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
112 cmd_break (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
114 //add_transformation (ds, break_trns_proc, NULL, NULL);
119 /* Parses an IF clause for LOOP or END LOOP and stores the
120 resulting expression to *CONDITION.
121 Returns true if successful, false on failure. */
123 parse_if_clause (struct lexer *lexer, struct dataset *ds,
124 struct expression **condition)
126 if (*condition != NULL)
128 lex_sbc_only_once ("IF");
132 *condition = expr_parse_bool (lexer, ds);
133 return *condition != NULL;
136 /* Parses an indexing clause into LOOP. Returns true if successful, false on
139 parse_index_clause (struct dataset *ds, struct lexer *lexer,
140 struct loop_trns *loop)
142 if (loop->index_var != NULL)
144 msg (SE, _("Only one index clause may be specified."));
148 if (lex_token (lexer) != T_ID)
150 lex_error (lexer, NULL);
154 loop->index_var = dict_lookup_var (dataset_dict (ds), lex_tokcstr (lexer));
155 if (!loop->index_var)
156 loop->index_var = dict_create_var_assert (dataset_dict (ds),
157 lex_tokcstr (lexer), 0);
160 if (!lex_force_match (lexer, T_EQUALS))
163 loop->first = expr_parse (lexer, ds, VAL_NUMERIC);
164 if (loop->first == NULL)
169 struct expression **e;
170 if (lex_match (lexer, T_TO))
172 else if (lex_match (lexer, T_BY))
179 lex_sbc_only_once (e == &loop->last ? "TO" : "BY");
182 *e = expr_parse (lexer, ds, VAL_NUMERIC);
186 if (loop->last == NULL)
188 lex_sbc_missing ("TO");
195 /* Sets up LOOP for the first pass. */
196 static enum trns_result
197 loop_trns_proc (void *loop_, struct ccase **c, casenumber case_num)
199 struct loop_trns *loop = loop_;
201 double cur, by, last;
204 /* Evaluate loop index expressions. */
205 cur = expr_evaluate_num (loop->first, *c, case_num);
206 by = loop->by ? expr_evaluate_num (loop->by, *c, case_num) : 1.0;
207 last = expr_evaluate_num (loop->last, *c, case_num);
209 /* Even if the loop is never entered, set the index
210 variable to the initial value. */
211 *c = case_unshare (*c);
212 *case_num_rw (*c, loop->index_var) = cur;
214 /* Throw out pathological cases. */
215 if (!isfinite (cur) || !isfinite (by) || !isfinite (last)
217 || (by > 0.0 && cur > last)
218 || (by < 0.0 && cur < last))
219 return TRNS_CONTINUE;
222 cur = by = last = 0.0;
224 int max_pass = loop->index_var ? settings_get_mxloops () : -1;
225 for (int i = 0; max_pass < 0 || i < max_pass; i++)
227 if (loop->loop_condition
228 && expr_evaluate_num (loop->loop_condition, *c, case_num) != 1.0)
231 enum trns_result r = transformations_execute (
232 loop->xforms, loop->n_xforms, c, case_num);
233 if (r != TRNS_CONTINUE)
234 return r == TRNS_BREAK ? TRNS_CONTINUE : r;
236 if (loop->end_loop_condition != NULL
237 && expr_evaluate_num (loop->end_loop_condition, *c, case_num) != 0.0)
243 if (by > 0.0 ? cur > last : cur < last)
246 *c = case_unshare (*c);
247 *case_num_rw (*c, loop->index_var) = cur;
250 return TRNS_CONTINUE;
255 loop_trns_free (void *loop_)
257 struct loop_trns *loop = loop_;
259 expr_free (loop->first);
260 expr_free (loop->by);
261 expr_free (loop->last);
263 expr_free (loop->loop_condition);
264 expr_free (loop->end_loop_condition);
266 transformations_destroy (loop->xforms, loop->n_xforms);