X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;ds=sidebyside;f=src%2Flanguage%2Fxforms%2Fcompute.c;h=f17798f77722a35cb2597a82f5ecaea4ccfcced7;hb=5e2c79af8a242eb36e62291277805933d5ad5903;hp=a2a7e7d2a430c108cae7170888ebabea2f7fafd8;hpb=f33178b801e1d8e932c2ef411d3985017627c512;p=pspp-builds.git diff --git a/src/language/xforms/compute.c b/src/language/xforms/compute.c index a2a7e7d2..f17798f7 100644 --- a/src/language/xforms/compute.c +++ b/src/language/xforms/compute.c @@ -1,24 +1,22 @@ -/* PSPP - computes sample statistics. +/* PSPP - a program for statistical analysis. Copyright (C) 1997-9, 2000 Free Software Foundation, Inc. - Written by Ben Pfaff . - 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 @@ -26,15 +24,16 @@ #include #include #include +#include #include #include #include -#include -#include #include #include #include +#include "xalloc.h" + #include "gettext.h" #define _(msgid) gettext (msgid) @@ -58,7 +57,6 @@ struct compute_trns /* Variable lvalue, if variable != NULL. */ struct variable *variable; /* Destination variable, if any. */ - int fv; /* `value' index of destination variable. */ int width; /* Lvalue string width; 0=numeric. */ /* Vector lvalue, if vector != NULL. */ @@ -69,8 +67,8 @@ struct compute_trns struct expression *rvalue; /* Rvalue expression. */ }; -static struct expression *parse_rvalue (struct lexer *lexer, - const struct lvalue *, +static struct expression *parse_rvalue (struct lexer *lexer, + const struct lvalue *, struct dataset *); static struct compute_trns *compute_trns_create (void); @@ -119,10 +117,10 @@ 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->fv)->f = expr_evaluate_num (compute->rvalue, c, - case_num); - + || 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); + return TRNS_CONTINUE; } @@ -134,28 +132,30 @@ 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); rindx = floor (index + EPSILON); - if (index == SYSMIS || rindx < 1 || rindx > compute->vector->cnt) + if (index == SYSMIS + || rindx < 1 || rindx > vector_get_var_cnt (compute->vector)) { if (index == SYSMIS) - msg (SW, _("When executing COMPUTE: SYSMIS is not a valid value as " - "an index into vector %s."), compute->vector->name); + msg (SW, _("When executing COMPUTE: SYSMIS is not a valid value " + "as an index into vector %s."), + vector_get_name (compute->vector)); else msg (SW, _("When executing COMPUTE: %g is not a valid value as " "an index into vector %s."), - index, compute->vector->name); + index, vector_get_name (compute->vector)); return TRNS_CONTINUE; } - case_data_rw (c, compute->vector->var[rindx - 1]->fv)->f + case_data_rw (c, vector_get_var (compute->vector, rindx - 1))->f = expr_evaluate_num (compute->rvalue, c, case_num); } - + return TRNS_CONTINUE; } @@ -166,10 +166,10 @@ 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_num (compute->test, c, case_num) == 1.0) expr_evaluate_str (compute->rvalue, c, case_num, - case_data_rw (c, compute->fv)->s, compute->width); - + case_data_rw (c, compute->variable)->s, compute->width); + return TRNS_CONTINUE; } @@ -181,7 +181,7 @@ 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. */ @@ -189,26 +189,27 @@ compute_str_vec (void *compute_, struct ccase *c, casenumber case_num) index = expr_evaluate_num (compute->element, c, case_num); rindx = floor (index + EPSILON); - if (index == SYSMIS) + if (index == SYSMIS) { msg (SW, _("When executing COMPUTE: SYSMIS is not a valid " "value as an index into vector %s."), - compute->vector->name); - return TRNS_CONTINUE; + vector_get_name (compute->vector)); + return TRNS_CONTINUE; } - else if (rindx < 1 || rindx > compute->vector->cnt) + else if (rindx < 1 || rindx > vector_get_var_cnt (compute->vector)) { msg (SW, _("When executing COMPUTE: %g is not a valid value as " "an index into vector %s."), - index, compute->vector->name); + index, vector_get_name (compute->vector)); return TRNS_CONTINUE; } - vr = compute->vector->var[rindx - 1]; + vr = vector_get_var (compute->vector, rindx - 1); expr_evaluate_str (compute->rvalue, c, case_num, - case_data_rw (c, vr->fv)->s, vr->width); + case_data_rw (c, vr)->s, + var_get_width (vr)); } - + return TRNS_CONTINUE; } @@ -255,9 +256,9 @@ cmd_if (struct lexer *lexer, struct dataset *ds) /* Code common to COMPUTE and IF. */ static trns_proc_func * -get_proc_func (const struct lvalue *lvalue) +get_proc_func (const struct lvalue *lvalue) { - bool is_numeric = lvalue_get_type (lvalue) == NUMERIC; + bool is_numeric = lvalue_get_type (lvalue) == VAL_NUMERIC; bool is_vector = lvalue_is_vector (lvalue); return (is_numeric @@ -268,10 +269,10 @@ get_proc_func (const struct lvalue *lvalue) /* Parses and returns an rvalue expression of the same type as LVALUE, or a null pointer on failure. */ static struct expression * -parse_rvalue (struct lexer *lexer, +parse_rvalue (struct lexer *lexer, const struct lvalue *lvalue, struct dataset *ds) { - bool is_numeric = lvalue_get_type (lvalue) == NUMERIC; + bool is_numeric = lvalue_get_type (lvalue) == VAL_NUMERIC; return expr_parse (lexer, ds, is_numeric ? EXPR_NUMBER : EXPR_STRING); } @@ -295,7 +296,7 @@ compute_trns_free (void *compute_) { struct compute_trns *compute = compute_; - if (compute != NULL) + if (compute != NULL) { expr_free (compute->test); expr_free (compute->element); @@ -320,7 +321,7 @@ struct lvalue /* Parses the target variable or vector element into a new `struct lvalue', which is returned. */ static struct lvalue * -lvalue_parse (struct lexer *lexer, struct dataset *ds) +lvalue_parse (struct lexer *lexer, struct dataset *ds) { struct dictionary *dict = dataset_dict (ds); struct lvalue *lvalue; @@ -333,7 +334,7 @@ lvalue_parse (struct lexer *lexer, struct dataset *ds) if (!lex_force_id (lexer)) goto lossage; - + if (lex_look_ahead (lexer) == '(') { /* Vector. */ @@ -359,10 +360,10 @@ lvalue_parse (struct lexer *lexer, struct dataset *ds) /* Variable name. */ const char *var_name = lex_tokid (lexer); lvalue->variable = dict_lookup_var (dict, var_name); - if (lvalue->variable == NULL) + if (lvalue->variable == NULL) { lvalue->variable = dict_create_var_assert (dict, var_name, 0); - lvalue->is_new_variable = true; + lvalue->is_new_variable = true; } lex_get (lexer); } @@ -376,16 +377,16 @@ lvalue_parse (struct lexer *lexer, struct dataset *ds) /* Returns the type (NUMERIC or ALPHA) of the target variable or vector in LVALUE. */ static int -lvalue_get_type (const struct lvalue *lvalue) +lvalue_get_type (const struct lvalue *lvalue) { return (lvalue->variable != NULL - ? lvalue->variable->type - : lvalue->vector->var[0]->type); + ? var_get_type (lvalue->variable) + : vector_get_type (lvalue->vector)); } /* Returns true if LVALUE has a vector as its target. */ static bool -lvalue_is_vector (const struct lvalue *lvalue) +lvalue_is_vector (const struct lvalue *lvalue) { return lvalue->vector != NULL; } @@ -393,24 +394,23 @@ lvalue_is_vector (const struct lvalue *lvalue) /* Finalizes making LVALUE the target of COMPUTE, by creating the target variable if necessary and setting fields in COMPUTE. */ static void -lvalue_finalize (struct lvalue *lvalue, - struct compute_trns *compute, - struct dictionary *dict) +lvalue_finalize (struct lvalue *lvalue, + struct compute_trns *compute, + struct dictionary *dict) { if (lvalue->vector == NULL) { compute->variable = lvalue->variable; - compute->fv = compute->variable->fv; - compute->width = compute->variable->width; + compute->width = var_get_width (compute->variable); /* Goofy behavior, but compatible: Turn off LEAVE. */ - if (dict_class_from_id (compute->variable->name) != DC_SCRATCH) - compute->variable->leave = false; + if (!var_must_leave (compute->variable)) + var_set_leave (compute->variable, false); /* Prevent lvalue_destroy from deleting variable. */ lvalue->is_new_variable = false; } - else + else { compute->vector = lvalue->vector; compute->element = lvalue->element; @@ -421,10 +421,10 @@ lvalue_finalize (struct lvalue *lvalue, } /* Destroys LVALUE. */ -static void -lvalue_destroy (struct lvalue *lvalue, struct dictionary *dict) +static void +lvalue_destroy (struct lvalue *lvalue, struct dictionary *dict) { - if (lvalue == NULL) + if (lvalue == NULL) return; if (lvalue->is_new_variable)