efe4667753a387fcb9e0de66d98bf0694f1c0896
[pspp] / src / language / control / do-if.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2009-2012 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include <stdlib.h>
20
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"
29
30 #include "gl/xalloc.h"
31
32 #include "gettext.h"
33 #define _(msgid) gettext (msgid)
34
35 /* A conditional clause. */
36 struct clause
37   {
38     struct msg_location *location;
39     struct expression *condition; /* Test expression; NULL for ELSE clause. */
40     struct trns_chain xforms;
41   };
42
43 /* DO IF transformation. */
44 struct do_if_trns
45   {
46     struct clause *clauses;     /* Clauses. */
47     size_t n_clauses;           /* Number of clauses. */
48   };
49
50 static const struct trns_class do_if_trns_class;
51
52 static void
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)
56 {
57   if (*ok && do_if->n_clauses > 0
58       && !do_if->clauses[do_if->n_clauses - 1].condition)
59     {
60       if (condition)
61         msg (SE, _("ELSE IF is not allowed following ELSE "
62                    "within DO IF...END IF."));
63       else
64         msg (SE, _("Only one ELSE is allowed within DO IF...END IF."));
65
66       msg_at (SN, do_if->clauses[do_if->n_clauses - 1].location,
67               _("This is the location of the previous ELSE clause."));
68
69       msg_at (SN, do_if->clauses[0].location,
70               _("This is the location of the DO IF command."));
71     }
72
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++];
77
78   *clause = (struct clause) { .location = NULL };
79   if (condition)
80     {
81       clause->condition = expr_parse_bool (lexer, ds);
82       if (!clause->condition)
83         lex_discard_rest_of_command (lexer);
84     }
85   clause->location = lex_ofs_location (lexer, 0, lex_ofs (lexer));
86
87   lex_end_of_command (lexer);
88   lex_get (lexer);
89
90   proc_push_transformations (ds);
91 }
92
93 static void
94 finish_clause (struct dataset *ds, struct do_if_trns *do_if)
95 {
96   struct clause *clause = &do_if->clauses[do_if->n_clauses - 1];
97   proc_pop_transformations (ds, &clause->xforms);
98 }
99
100 /* Parse DO IF. */
101 int
102 cmd_do_if (struct lexer *lexer, struct dataset *ds)
103 {
104   struct do_if_trns *do_if = xmalloc (sizeof *do_if);
105   *do_if = (struct do_if_trns) { .n_clauses = 0 };
106
107   size_t allocated_clauses = 0;
108   bool ok = true;
109
110   start_clause (lexer, ds, true, do_if, &allocated_clauses, &ok);
111   while (!lex_match_phrase (lexer, "END IF"))
112     {
113       if (lex_token (lexer) == T_STOP)
114         {
115           lex_error (lexer, NULL);
116           ok = false;
117           break;
118         }
119       else if (lex_match_phrase (lexer, "ELSE IF"))
120         {
121           finish_clause (ds, do_if);
122           start_clause (lexer, ds, true, do_if, &allocated_clauses, &ok);
123         }
124       else if (lex_match_id (lexer, "ELSE"))
125         {
126           finish_clause (ds, do_if);
127           start_clause (lexer, ds, false, do_if, &allocated_clauses, &ok);
128         }
129       else
130         cmd_parse_in_state (lexer, ds, CMD_STATE_NESTED);
131     }
132   finish_clause (ds, do_if);
133
134   add_transformation (ds, &do_if_trns_class, do_if);
135
136   return ok ? CMD_SUCCESS : CMD_CASCADING_FAILURE;
137 }
138
139 /* DO IF transformation procedure.
140    Checks each clause and jumps to the appropriate
141    transformation. */
142 static enum trns_result
143 do_if_trns_proc (void *do_if_, struct ccase **c, casenumber case_num)
144 {
145   struct do_if_trns *do_if = do_if_;
146
147   for (size_t i = 0; i < do_if->n_clauses; i++)
148     {
149       const struct clause *clause = &do_if->clauses[i];
150       if (clause->condition != NULL)
151         {
152           double boolean = expr_evaluate_num (clause->condition, *c, case_num);
153           if (boolean == 0.0)
154             continue;
155           else if (boolean == SYSMIS)
156             return TRNS_CONTINUE;
157         }
158
159       return trns_chain_execute (&clause->xforms, case_num, c);
160     }
161   return TRNS_CONTINUE;
162 }
163
164 /* Frees a DO IF transformation. */
165 static bool
166 do_if_trns_free (void *do_if_)
167 {
168   struct do_if_trns *do_if = do_if_;
169
170   for (size_t i = 0; i < do_if->n_clauses; i++)
171     {
172       struct clause *clause = &do_if->clauses[i];
173
174       msg_location_destroy (clause->location);
175       expr_free (clause->condition);
176
177       trns_chain_uninit (&clause->xforms);
178     }
179   free (do_if->clauses);
180   free (do_if);
181   return true;
182 }
183
184 static const struct trns_class do_if_trns_class = {
185   .name = "DO IF",
186   .execute = do_if_trns_proc,
187   .destroy = do_if_trns_free,
188 };