X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;ds=sidebyside;f=src%2Flanguage%2Fxforms%2Fcompute.c;h=00f7c5ad4706b4eec18f51872eb78640c32df110;hb=fa1e2f50efc0b7b8a9436b40568eadf03feae9dc;hp=600a048002f506991bdbe48da3b0a1ef3c59a2a8;hpb=f5c108becd49d78f4898cab11352291f5689d24e;p=pspp
diff --git a/src/language/xforms/compute.c b/src/language/xforms/compute.c
index 600a048002..00f7c5ad47 100644
--- a/src/language/xforms/compute.c
+++ b/src/language/xforms/compute.c
@@ -1,39 +1,39 @@
-/* PSPP - computes sample statistics.
- Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
+/* PSPP - a program for statistical analysis.
+ Copyright (C) 1997-9, 2000, 2009, 2010, 2011, 2014 Free Software Foundation, Inc.
- This program is free software; you can redistribute it and/or
- modify it under the terms of the GNU General Public License as
- published by the Free Software Foundation; either version 2 of the
- License, or (at your option) any later version.
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
- This program is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- 02110-1301, USA. */
+ along with this program. If not, see . */
#include
+#include
+#include
#include
-#include
-#include
-#include
-#include
-#include
-#include
-#include
-#include
-#include
-#include
-#include
-#include
-#include
-#include
+#include "data/case.h"
+#include "data/dataset.h"
+#include "data/dictionary.h"
+#include "data/transformations.h"
+#include "data/variable.h"
+#include "data/vector.h"
+#include "language/command.h"
+#include "language/expressions/public.h"
+#include "language/lexer/lexer.h"
+#include "libpspp/message.h"
+#include "libpspp/misc.h"
+#include "libpspp/str.h"
+
+#include "gl/xalloc.h"
#include "gettext.h"
#define _(msgid) gettext (msgid)
@@ -91,7 +91,7 @@ cmd_compute (struct lexer *lexer, struct dataset *ds)
if (lvalue == NULL)
goto fail;
- if (!lex_force_match (lexer, '='))
+ if (!lex_force_match (lexer, T_EQUALS))
goto fail;
compute->rvalue = parse_rvalue (lexer, lvalue, ds);
if (compute->rvalue == NULL)
@@ -101,7 +101,7 @@ cmd_compute (struct lexer *lexer, struct dataset *ds)
lvalue_finalize (lvalue, compute, dict);
- return lex_end_of_command (lexer);
+ return CMD_SUCCESS;
fail:
lvalue_destroy (lvalue, dict);
@@ -113,14 +113,17 @@ cmd_compute (struct lexer *lexer, struct dataset *ds)
/* Handle COMPUTE or IF with numeric target variable. */
static int
-compute_num (void *compute_, struct ccase *c, casenumber case_num)
+compute_num (void *compute_, struct ccase **c, casenumber case_num)
{
struct compute_trns *compute = compute_;
if (compute->test == NULL
- || expr_evaluate_num (compute->test, c, case_num) == 1.0)
- case_data_rw (c, compute->variable)->f
- = expr_evaluate_num (compute->rvalue, c, case_num);
+ || expr_evaluate_num (compute->test, *c, case_num) == 1.0)
+ {
+ *c = case_unshare (*c);
+ case_data_rw (*c, compute->variable)->f
+ = expr_evaluate_num (compute->rvalue, *c, case_num);
+ }
return TRNS_CONTINUE;
}
@@ -128,17 +131,17 @@ compute_num (void *compute_, struct ccase *c, casenumber case_num)
/* Handle COMPUTE or IF with numeric vector element target
variable. */
static int
-compute_num_vec (void *compute_, struct ccase *c, casenumber case_num)
+compute_num_vec (void *compute_, struct ccase **c, casenumber case_num)
{
struct compute_trns *compute = compute_;
if (compute->test == NULL
- || expr_evaluate_num (compute->test, c, case_num) == 1.0)
+ || expr_evaluate_num (compute->test, *c, case_num) == 1.0)
{
double index; /* Index into the vector. */
int rindx; /* Rounded index value. */
- index = expr_evaluate_num (compute->element, c, case_num);
+ index = expr_evaluate_num (compute->element, *c, case_num);
rindx = floor (index + EPSILON);
if (index == SYSMIS
|| rindx < 1 || rindx > vector_get_var_cnt (compute->vector))
@@ -148,13 +151,15 @@ compute_num_vec (void *compute_, struct ccase *c, casenumber case_num)
"as an index into vector %s."),
vector_get_name (compute->vector));
else
- msg (SW, _("When executing COMPUTE: %g is not a valid value as "
+ msg (SW, _("When executing COMPUTE: %.*g is not a valid value as "
"an index into vector %s."),
- index, vector_get_name (compute->vector));
+ DBL_DIG + 1, index, vector_get_name (compute->vector));
return TRNS_CONTINUE;
}
- case_data_rw (c, vector_get_var (compute->vector, rindx - 1))->f
- = expr_evaluate_num (compute->rvalue, c, case_num);
+
+ *c = case_unshare (*c);
+ case_data_rw (*c, vector_get_var (compute->vector, rindx - 1))->f
+ = expr_evaluate_num (compute->rvalue, *c, case_num);
}
return TRNS_CONTINUE;
@@ -162,14 +167,19 @@ compute_num_vec (void *compute_, struct ccase *c, casenumber case_num)
/* Handle COMPUTE or IF with string target variable. */
static int
-compute_str (void *compute_, struct ccase *c, casenumber case_num)
+compute_str (void *compute_, struct ccase **c, casenumber case_num)
{
struct compute_trns *compute = compute_;
if (compute->test == NULL
- || expr_evaluate_num (compute->test, c, case_num) == 1.0)
- expr_evaluate_str (compute->rvalue, c, case_num,
- case_data_rw (c, compute->variable)->s, compute->width);
+ || expr_evaluate_num (compute->test, *c, case_num) == 1.0)
+ {
+ char *s;
+
+ *c = case_unshare (*c);
+ s = CHAR_CAST_BUG (char *, case_str_rw (*c, compute->variable));
+ expr_evaluate_str (compute->rvalue, *c, case_num, s, compute->width);
+ }
return TRNS_CONTINUE;
}
@@ -177,18 +187,18 @@ compute_str (void *compute_, struct ccase *c, casenumber case_num)
/* Handle COMPUTE or IF with string vector element target
variable. */
static int
-compute_str_vec (void *compute_, struct ccase *c, casenumber case_num)
+compute_str_vec (void *compute_, struct ccase **c, casenumber case_num)
{
struct compute_trns *compute = compute_;
if (compute->test == NULL
- || expr_evaluate_num (compute->test, c, case_num) == 1.0)
+ || expr_evaluate_num (compute->test, *c, case_num) == 1.0)
{
double index; /* Index into the vector. */
int rindx; /* Rounded index value. */
struct variable *vr; /* Variable reference by indexed vector. */
- index = expr_evaluate_num (compute->element, c, case_num);
+ index = expr_evaluate_num (compute->element, *c, case_num);
rindx = floor (index + EPSILON);
if (index == SYSMIS)
{
@@ -199,15 +209,16 @@ compute_str_vec (void *compute_, struct ccase *c, casenumber case_num)
}
else if (rindx < 1 || rindx > vector_get_var_cnt (compute->vector))
{
- msg (SW, _("When executing COMPUTE: %g is not a valid value as "
+ msg (SW, _("When executing COMPUTE: %.*g is not a valid value as "
"an index into vector %s."),
- index, vector_get_name (compute->vector));
+ DBL_DIG + 1, index, vector_get_name (compute->vector));
return TRNS_CONTINUE;
}
vr = vector_get_var (compute->vector, rindx - 1);
- expr_evaluate_str (compute->rvalue, c, case_num,
- case_data_rw (c, vr)->s,
+ *c = case_unshare (*c);
+ expr_evaluate_str (compute->rvalue, *c, case_num,
+ CHAR_CAST_BUG (char *, case_str_rw (*c, vr)),
var_get_width (vr));
}
@@ -236,7 +247,7 @@ cmd_if (struct lexer *lexer, struct dataset *ds)
goto fail;
/* Rvalue expression. */
- if (!lex_force_match (lexer, '='))
+ if (!lex_force_match (lexer, T_EQUALS))
goto fail;
compute->rvalue = parse_rvalue (lexer, lvalue, ds);
if (compute->rvalue == NULL)
@@ -246,7 +257,7 @@ cmd_if (struct lexer *lexer, struct dataset *ds)
lvalue_finalize (lvalue, compute, dict);
- return lex_end_of_command (lexer);
+ return CMD_SUCCESS;
fail:
lvalue_destroy (lvalue, dict);
@@ -259,7 +270,7 @@ cmd_if (struct lexer *lexer, struct dataset *ds)
static trns_proc_func *
get_proc_func (const struct lvalue *lvalue)
{
- bool is_numeric = lvalue_get_type (lvalue) == VAR_NUMERIC;
+ bool is_numeric = lvalue_get_type (lvalue) == VAL_NUMERIC;
bool is_vector = lvalue_is_vector (lvalue);
return (is_numeric
@@ -273,7 +284,7 @@ static struct expression *
parse_rvalue (struct lexer *lexer,
const struct lvalue *lvalue, struct dataset *ds)
{
- bool is_numeric = lvalue_get_type (lvalue) == VAR_NUMERIC;
+ bool is_numeric = lvalue_get_type (lvalue) == VAL_NUMERIC;
return expr_parse (lexer, ds, is_numeric ? EXPR_NUMBER : EXPR_STRING);
}
@@ -336,30 +347,30 @@ lvalue_parse (struct lexer *lexer, struct dataset *ds)
if (!lex_force_id (lexer))
goto lossage;
- if (lex_look_ahead (lexer) == '(')
+ if (lex_next_token (lexer, 1) == T_LPAREN)
{
/* Vector. */
- lvalue->vector = dict_lookup_vector (dict, lex_tokid (lexer));
+ lvalue->vector = dict_lookup_vector (dict, lex_tokcstr (lexer));
if (lvalue->vector == NULL)
{
- msg (SE, _("There is no vector named %s."), lex_tokid (lexer));
+ msg (SE, _("There is no vector named %s."), lex_tokcstr (lexer));
goto lossage;
}
/* Vector element. */
lex_get (lexer);
- if (!lex_force_match (lexer, '('))
+ if (!lex_force_match (lexer, T_LPAREN))
goto lossage;
lvalue->element = expr_parse (lexer, ds, EXPR_NUMBER);
if (lvalue->element == NULL)
goto lossage;
- if (!lex_force_match (lexer, ')'))
+ if (!lex_force_match (lexer, T_RPAREN))
goto lossage;
}
else
{
/* Variable name. */
- const char *var_name = lex_tokid (lexer);
+ const char *var_name = lex_tokcstr (lexer);
lvalue->variable = dict_lookup_var (dict, var_name);
if (lvalue->variable == NULL)
{