X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;ds=sidebyside;f=src%2Fcompute.c;h=e6afe0b62d7e76383f54f524cfbec587a5058a66;hb=1d985886f778e35f8d89c4e3c897b79fde8de6ed;hp=d26bf1ed2dd441d8f1406353b75b29b4105d227b;hpb=d4d9866bb2ec1797b8fb103e7144d0e9ffd1abff;p=pspp-builds.git diff --git a/src/compute.c b/src/compute.c index d26bf1ed..e6afe0b6 100644 --- a/src/compute.c +++ b/src/compute.c @@ -18,12 +18,14 @@ 02111-1307, USA. */ #include -#include +#include "error.h" #include #include "alloc.h" +#include "case.h" #include "command.h" +#include "dictionary.h" #include "error.h" -#include "expr.h" +#include "expressions/public.h" #include "lexer.h" #include "misc.h" #include "str.h" @@ -75,8 +77,6 @@ cmd_compute (void) struct lvalue *lvalue = NULL; struct compute_trns *compute = NULL; - lex_match_id ("COMPUTE"); - lvalue = lvalue_parse (); if (lvalue == NULL) goto fail; @@ -104,111 +104,105 @@ cmd_compute (void) /* Transformation functions. */ +/* Handle COMPUTE or IF with numeric target variable. */ static int -compute_num (struct trns_header *compute_, struct ccase *c) +compute_num (struct trns_header *compute_, struct ccase *c, + int case_num) { struct compute_trns *compute = (struct compute_trns *) compute_; if (compute->test == NULL - || expr_evaluate (compute->test, c, NULL) == 1.0) - { - expr_evaluate (compute->rvalue, c, &c->data[compute->fv]); - } + || 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); return -1; } +/* Handle COMPUTE or IF with numeric vector element target + variable. */ static int -compute_num_vec (struct trns_header *compute_, struct ccase *c) +compute_num_vec (struct trns_header *compute_, struct ccase *c, + int case_num) { struct compute_trns *compute = (struct compute_trns *) compute_; if (compute->test == NULL - || expr_evaluate (compute->test, c, NULL) == 1.0) + || expr_evaluate_num (compute->test, c, case_num) == 1.0) { - /* Index into the vector. */ - union value index; - - /* Rounded index value. */ - int rindx; + double index; /* Index into the vector. */ + int rindx; /* Rounded index value. */ - expr_evaluate (compute->element, c, &index); - rindx = floor (index.f + EPSILON); - if (index.f == SYSMIS || rindx < 1 || rindx > compute->vector->cnt) + index = expr_evaluate_num (compute->element, c, case_num); + rindx = floor (index + EPSILON); + if (index == SYSMIS || rindx < 1 || rindx > compute->vector->cnt) { - if (index.f == SYSMIS) + if (index == SYSMIS) msg (SW, _("When executing COMPUTE: SYSMIS is not a valid value as " "an index into vector %s."), compute->vector->name); else msg (SW, _("When executing COMPUTE: %g is not a valid value as " "an index into vector %s."), - index.f, compute->vector->name); + index, compute->vector->name); return -1; } - expr_evaluate (compute->rvalue, c, - &c->data[compute->vector->var[rindx - 1]->fv]); + case_data_rw (c, compute->vector->var[rindx - 1]->fv)->f + = expr_evaluate_num (compute->rvalue, c, case_num); } return -1; } +/* Handle COMPUTE or IF with string target variable. */ static int -compute_str (struct trns_header *compute_, struct ccase *c) +compute_str (struct trns_header *compute_, struct ccase *c, + int case_num) { struct compute_trns *compute = (struct compute_trns *) compute_; if (compute->test == NULL - || expr_evaluate (compute->test, c, NULL) == 1.0) - { - /* Temporary storage for string expression return value. */ - union value v; - - expr_evaluate (compute->rvalue, c, &v); - st_bare_pad_len_copy (c->data[compute->fv].s, &v.c[1], compute->width, - v.c[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); return -1; } +/* Handle COMPUTE or IF with string vector element target + variable. */ static int -compute_str_vec (struct trns_header *compute_, struct ccase *c) +compute_str_vec (struct trns_header *compute_, struct ccase *c, + int case_num) { struct compute_trns *compute = (struct compute_trns *) compute_; if (compute->test == NULL - || expr_evaluate (compute->test, c, NULL) == 1.0) + || expr_evaluate_num (compute->test, c, case_num) == 1.0) { - /* Temporary storage for string expression return value. */ - union value v; - - /* Index into the vector. */ - union value index; + double index; /* Index into the vector. */ + int rindx; /* Rounded index value. */ + struct variable *vr; /* Variable reference by indexed vector. */ - /* Rounded index value. */ - int rindx; - - /* Variable reference by indexed vector. */ - struct variable *vr; - - expr_evaluate (compute->element, c, &index); - rindx = floor (index.f + EPSILON); - if (index.f == SYSMIS || rindx < 1 || rindx > compute->vector->cnt) + index = expr_evaluate_num (compute->element, c, case_num); + rindx = floor (index + EPSILON); + if (index == SYSMIS) { - if (index.f == SYSMIS) - msg (SW, _("When executing COMPUTE: SYSMIS is not a valid " - "value as an index into vector %s."), - compute->vector->name); - else - msg (SW, _("When executing COMPUTE: %g is not a valid value as " - "an index into vector %s."), - index.f, compute->vector->name); + msg (SW, _("When executing COMPUTE: SYSMIS is not a valid " + "value as an index into vector %s."), + compute->vector->name); + return -1; + } + else if (rindx < 1 || rindx > compute->vector->cnt) + { + msg (SW, _("When executing COMPUTE: %g is not a valid value as " + "an index into vector %s."), + index, compute->vector->name); return -1; } - expr_evaluate (compute->rvalue, c, &v); vr = compute->vector->var[rindx - 1]; - st_bare_pad_len_copy (c->data[vr->fv].s, &v.c[1], vr->width, v.c[0]); + expr_evaluate_str (compute->rvalue, c, case_num, + case_data_rw (c, vr->fv)->s, vr->width); } return -1; @@ -222,11 +216,10 @@ cmd_if (void) struct compute_trns *compute = NULL; struct lvalue *lvalue = NULL; - lex_match_id ("IF"); compute = compute_trns_create (); /* Test expression. */ - compute->test = expr_parse (PXP_BOOLEAN); + compute->test = expr_parse (default_dict, EXPR_BOOLEAN); if (compute->test == NULL) goto fail; @@ -269,7 +262,8 @@ parse_rvalue_expression (struct compute_trns *compute, assert (type == NUMERIC || type == ALPHA); - compute->rvalue = expr_parse (type == ALPHA ? PXP_STRING : PXP_NUMERIC); + compute->rvalue = expr_parse (default_dict, + type == ALPHA ? EXPR_STRING : EXPR_NUMBER); if (compute->rvalue == NULL) return 0; @@ -313,6 +307,7 @@ compute_trns_free (struct trns_header *compute_) expr_free (compute->rvalue); } +/* COMPUTE or IF target variable or vector element. */ struct lvalue { char var_name[9]; /* Destination variable name, or "". */ @@ -320,6 +315,8 @@ struct lvalue struct expression *element; /* Destination vector element, or NULL. */ }; +/* Parses the target variable or vector elector into a new + `struct lvalue', which is returned. */ static struct lvalue * lvalue_parse (void) { @@ -347,7 +344,7 @@ lvalue_parse (void) lex_get (); if (!lex_force_match ('(')) goto lossage; - lvalue->element = expr_parse (PXP_NUMERIC); + lvalue->element = expr_parse (default_dict, EXPR_NUMBER); if (lvalue->element == NULL) goto lossage; if (!lex_force_match (')')) @@ -367,6 +364,8 @@ lvalue_parse (void) return NULL; } +/* Returns the type (NUMERIC or ALPHA) of the target variable or + vector in LVALUE. */ static int lvalue_get_type (const struct lvalue *lvalue) { @@ -383,12 +382,15 @@ lvalue_get_type (const struct lvalue *lvalue) return lvalue->vector->var[0]->type; } +/* Returns nonzero if LVALUE has a vector as its target. */ static int lvalue_is_vector (const struct lvalue *lvalue) { return lvalue->vector != NULL; } +/* 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) @@ -397,20 +399,12 @@ lvalue_finalize (struct lvalue *lvalue, { compute->variable = dict_lookup_var (default_dict, lvalue->var_name); if (compute->variable == NULL) - { - struct fmt_spec input_spec = { 0,8,2 }; compute->variable = dict_create_var_assert (default_dict, lvalue->var_name, 0); - convert_fmt_ItoO (&input_spec, &compute->variable->print); - compute->variable->write = compute->variable->print; - } - compute->fv = compute->variable->fv; compute->width = compute->variable->width; - - /* Goofy behavior, but compatible: Turn off LEAVE. */ if (dict_class_from_id (compute->variable->name) != DC_SCRATCH) compute->variable->reinit = 1; @@ -425,31 +419,13 @@ lvalue_finalize (struct lvalue *lvalue, lvalue_destroy (lvalue); } +/* Destroys LVALUE. */ static void lvalue_destroy (struct lvalue *lvalue) { + if ( ! lvalue ) + return ; + expr_free (lvalue->element); free (lvalue); } - -/* EVALUATE. */ - -int -cmd_evaluate (void) -{ - struct expression *expr; - - lex_match_id ("EVALUATE"); - expr = expr_parse (PXP_DUMP); - if (!expr) - return CMD_FAILURE; - - expr_free (expr); - if (token != '.') - { - msg (SE, _("Extra characters after expression.")); - return CMD_FAILURE; - } - - return CMD_SUCCESS; -}