1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2009-2012 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/dataset.h"
22 #include "data/transformations.h"
23 #include "language/command.h"
24 #include "language/expressions/public.h"
25 #include "language/lexer/lexer.h"
26 #include "libpspp/compiler.h"
27 #include "libpspp/message.h"
28 #include "libpspp/str.h"
30 #include "gl/xalloc.h"
33 #define _(msgid) gettext (msgid)
35 /* A conditional clause. */
38 struct msg_location *location;
39 struct expression *condition; /* Test expression; NULL for ELSE clause. */
40 struct trns_chain xforms;
43 /* DO IF transformation. */
46 struct clause *clauses; /* Clauses. */
47 size_t n_clauses; /* Number of clauses. */
50 static const struct trns_class do_if_trns_class;
53 start_clause (struct lexer *lexer, struct dataset *ds,
54 bool condition, struct do_if_trns *do_if,
55 size_t *allocated_clauses, bool *ok)
57 if (*ok && do_if->n_clauses > 0
58 && !do_if->clauses[do_if->n_clauses - 1].condition)
61 msg (SE, _("ELSE IF is not allowed following ELSE "
62 "within DO IF...END IF."));
64 msg (SE, _("Only one ELSE is allowed within DO IF...END IF."));
66 msg_at (SN, do_if->clauses[do_if->n_clauses - 1].location,
67 _("This is the location of the previous ELSE clause."));
69 msg_at (SN, do_if->clauses[0].location,
70 _("This is the location of the DO IF command."));
73 if (do_if->n_clauses >= *allocated_clauses)
74 do_if->clauses = x2nrealloc (do_if->clauses, allocated_clauses,
75 sizeof *do_if->clauses);
76 struct clause *clause = &do_if->clauses[do_if->n_clauses++];
78 *clause = (struct clause) { .location = NULL };
81 clause->condition = expr_parse_bool (lexer, ds);
82 if (!clause->condition)
83 lex_discard_rest_of_command (lexer);
85 clause->location = lex_ofs_location (lexer, 0, lex_ofs (lexer));
87 lex_end_of_command (lexer);
90 proc_push_transformations (ds);
94 finish_clause (struct dataset *ds, struct do_if_trns *do_if)
96 struct clause *clause = &do_if->clauses[do_if->n_clauses - 1];
97 proc_pop_transformations (ds, &clause->xforms);
102 cmd_do_if (struct lexer *lexer, struct dataset *ds)
104 struct do_if_trns *do_if = xmalloc (sizeof *do_if);
105 *do_if = (struct do_if_trns) { .n_clauses = 0 };
107 size_t allocated_clauses = 0;
110 start_clause (lexer, ds, true, do_if, &allocated_clauses, &ok);
111 while (!lex_match_phrase (lexer, "END IF"))
113 if (lex_token (lexer) == T_STOP)
115 lex_error (lexer, NULL);
119 else if (lex_match_phrase (lexer, "ELSE IF"))
121 finish_clause (ds, do_if);
122 start_clause (lexer, ds, true, do_if, &allocated_clauses, &ok);
124 else if (lex_match_id (lexer, "ELSE"))
126 finish_clause (ds, do_if);
127 start_clause (lexer, ds, false, do_if, &allocated_clauses, &ok);
130 cmd_parse_in_state (lexer, ds, CMD_STATE_NESTED);
132 finish_clause (ds, do_if);
134 add_transformation (ds, &do_if_trns_class, do_if);
136 return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
139 /* DO IF transformation procedure.
140 Checks each clause and jumps to the appropriate
142 static enum trns_result
143 do_if_trns_proc (void *do_if_, struct ccase **c, casenumber case_num)
145 struct do_if_trns *do_if = do_if_;
147 for (size_t i = 0; i < do_if->n_clauses; i++)
149 const struct clause *clause = &do_if->clauses[i];
150 if (clause->condition != NULL)
152 double boolean = expr_evaluate_num (clause->condition, *c, case_num);
155 else if (boolean == SYSMIS)
156 return TRNS_CONTINUE;
159 return trns_chain_execute (&clause->xforms, case_num, c);
161 return TRNS_CONTINUE;
164 /* Frees a DO IF transformation. */
166 do_if_trns_free (void *do_if_)
168 struct do_if_trns *do_if = do_if_;
170 for (size_t i = 0; i < do_if->n_clauses; i++)
172 struct clause *clause = &do_if->clauses[i];
174 msg_location_destroy (clause->location);
175 expr_free (clause->condition);
177 trns_chain_uninit (&clause->xforms);
179 free (do_if->clauses);
184 static const struct trns_class do_if_trns_class = {
186 .execute = do_if_trns_proc,
187 .destroy = do_if_trns_free,