1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 1997-9, 2000, 2006, 2010, 2011, 2014 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/dictionary.h"
23 #include "data/variable.h"
24 #include "data/format.h"
25 #include "language/command.h"
26 #include "language/lexer/format-parser.h"
27 #include "language/lexer/lexer.h"
28 #include "language/lexer/variable-parser.h"
29 #include "libpspp/assertion.h"
30 #include "libpspp/message.h"
31 #include "libpspp/str.h"
34 #define _(msgid) gettext (msgid)
36 /* Parses the NUMERIC command. */
38 cmd_numeric (struct lexer *lexer, struct dataset *ds)
42 /* Names of variables to create. */
48 /* Format spec for variables to create. */
51 if (!parse_DATA_LIST_vars (lexer, dataset_dict (ds),
52 &v, &nv, PV_NO_DUPLICATE))
55 /* Get the optional format specification. */
56 if (lex_match (lexer, T_LPAREN))
58 if (!parse_format_specifier (lexer, &f))
61 if (! fmt_check_output (&f))
64 if (fmt_is_string (f.type))
66 char str[FMT_STRING_LEN_MAX + 1];
67 msg (SE, _("Format type %s may not be used with a numeric "
68 "variable."), fmt_to_string (&f, str));
72 if (!lex_match (lexer, T_RPAREN))
74 lex_error_expecting (lexer, "`)'");
79 f = var_default_formats (0);
81 /* Create each variable. */
82 for (i = 0; i < nv; i++)
84 struct variable *new_var = dict_create_var (dataset_dict (ds), v[i], 0);
86 msg (SE, _("There is already a variable named %s."), v[i]);
88 var_set_both_formats (new_var, &f);
92 for (i = 0; i < nv; i++)
96 while (lex_match (lexer, T_SLASH));
100 /* If we have an error at a point where cleanup is required,
101 flow-of-control comes here. */
103 for (i = 0; i < nv; i++)
109 /* Parses the STRING command. */
111 cmd_string (struct lexer *lexer, struct dataset *ds)
115 /* Names of variables to create. */
119 /* Format spec for variables to create. */
122 /* Width of variables to create. */
127 if (!parse_DATA_LIST_vars (lexer, dataset_dict (ds),
128 &v, &nv, PV_NO_DUPLICATE))
131 if (!lex_force_match (lexer, T_LPAREN)
132 || !parse_format_specifier (lexer, &f)
133 || !lex_force_match (lexer, T_RPAREN))
135 if (!fmt_is_string (f.type))
137 char str[FMT_STRING_LEN_MAX + 1];
138 msg (SE, _("Format type %s may not be used with a string "
139 "variable."), fmt_to_string (&f, str));
142 if (!fmt_check_output (&f))
145 width = fmt_var_width (&f);
147 /* Create each variable. */
148 for (i = 0; i < nv; i++)
150 struct variable *new_var = dict_create_var (dataset_dict (ds), v[i],
153 msg (SE, _("There is already a variable named %s."), v[i]);
155 var_set_both_formats (new_var, &f);
159 for (i = 0; i < nv; i++)
163 while (lex_match (lexer, T_SLASH));
167 /* If we have an error at a point where cleanup is required,
168 flow-of-control comes here. */
170 for (i = 0; i < nv; i++)
176 /* Parses the LEAVE command. */
178 cmd_leave (struct lexer *lexer, struct dataset *ds)
185 if (!parse_variables (lexer, dataset_dict (ds), &v, &nv, PV_NONE))
186 return CMD_CASCADING_FAILURE;
187 for (i = 0; i < nv; i++)
188 var_set_leave (v[i], true);