X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flanguage%2Fxforms%2Fcompute.c;h=bcede8a378ff58ef31f7e54c8bdf1c8c843297bc;hb=92c09e564002d356d20fc1e2e131027ef89f6748;hp=0bc6e4d3fd231581fcc9448792f89d494cd964f7;hpb=81fff61a96bece351e381ad3fef8ab1248a952ba;p=pspp-builds.git diff --git a/src/language/xforms/compute.c b/src/language/xforms/compute.c index 0bc6e4d3..bcede8a3 100644 --- a/src/language/xforms/compute.c +++ b/src/language/xforms/compute.c @@ -1,6 +1,5 @@ /* PSPP - computes sample statistics. 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 @@ -26,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -43,12 +43,12 @@ struct lvalue; /* Target of a COMPUTE or IF assignment, either a variable or a vector element. */ -static struct lvalue *lvalue_parse (void); +static struct lvalue *lvalue_parse (struct lexer *lexer, struct dataset *); static int lvalue_get_type (const struct lvalue *); static bool lvalue_is_vector (const struct lvalue *); static void lvalue_finalize (struct lvalue *, - struct compute_trns *); -static void lvalue_destroy (struct lvalue *); + struct compute_trns *, struct dictionary *); +static void lvalue_destroy (struct lvalue *, struct dictionary *); /* COMPUTE and IF transformation. */ struct compute_trns @@ -58,7 +58,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,7 +68,10 @@ struct compute_trns struct expression *rvalue; /* Rvalue expression. */ }; -static struct expression *parse_rvalue (const struct lvalue *); +static struct expression *parse_rvalue (struct lexer *lexer, + const struct lvalue *, + struct dataset *); + static struct compute_trns *compute_trns_create (void); static trns_proc_func *get_proc_func (const struct lvalue *); static trns_free_func compute_trns_free; @@ -77,31 +79,32 @@ static trns_free_func compute_trns_free; /* COMPUTE. */ int -cmd_compute (void) +cmd_compute (struct lexer *lexer, struct dataset *ds) { + struct dictionary *dict = dataset_dict (ds); struct lvalue *lvalue = NULL; struct compute_trns *compute = NULL; compute = compute_trns_create (); - lvalue = lvalue_parse (); + lvalue = lvalue_parse (lexer, ds); if (lvalue == NULL) goto fail; - if (!lex_force_match ('=')) + if (!lex_force_match (lexer, '=')) goto fail; - compute->rvalue = parse_rvalue (lvalue); + compute->rvalue = parse_rvalue (lexer, lvalue, ds); if (compute->rvalue == NULL) goto fail; - add_transformation (get_proc_func (lvalue), compute_trns_free, compute); + add_transformation (ds, get_proc_func (lvalue), compute_trns_free, compute); - lvalue_finalize (lvalue, compute); + lvalue_finalize (lvalue, compute, dict); - return lex_end_of_command (); + return lex_end_of_command (lexer); fail: - lvalue_destroy (lvalue); + lvalue_destroy (lvalue, dict); compute_trns_free (compute); return CMD_CASCADING_FAILURE; } @@ -110,14 +113,14 @@ cmd_compute (void) /* Handle COMPUTE or IF with numeric target variable. */ static int -compute_num (void *compute_, struct ccase *c, int 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->fv)->f = expr_evaluate_num (compute->rvalue, c, - case_num); + case_data_rw (c, compute->variable)->f + = expr_evaluate_num (compute->rvalue, c, case_num); return TRNS_CONTINUE; } @@ -125,7 +128,7 @@ compute_num (void *compute_, struct ccase *c, int case_num) /* Handle COMPUTE or IF with numeric vector element target variable. */ static int -compute_num_vec (void *compute_, struct ccase *c, int case_num) +compute_num_vec (void *compute_, struct ccase *c, casenumber case_num) { struct compute_trns *compute = compute_; @@ -137,18 +140,20 @@ compute_num_vec (void *compute_, struct ccase *c, int case_num) 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); } @@ -157,14 +162,14 @@ compute_num_vec (void *compute_, struct ccase *c, int case_num) /* Handle COMPUTE or IF with string target variable. */ static int -compute_str (void *compute_, struct ccase *c, int 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->fv)->s, compute->width); + case_data_rw (c, compute->variable)->s, compute->width); return TRNS_CONTINUE; } @@ -172,7 +177,7 @@ compute_str (void *compute_, struct ccase *c, int case_num) /* Handle COMPUTE or IF with string vector element target variable. */ static int -compute_str_vec (void *compute_, struct ccase *c, int case_num) +compute_str_vec (void *compute_, struct ccase *c, casenumber case_num) { struct compute_trns *compute = compute_; @@ -189,20 +194,21 @@ compute_str_vec (void *compute_, struct ccase *c, int case_num) { msg (SW, _("When executing COMPUTE: SYSMIS is not a valid " "value as an index into vector %s."), - compute->vector->name); + 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; @@ -211,38 +217,39 @@ compute_str_vec (void *compute_, struct ccase *c, int case_num) /* IF. */ int -cmd_if (void) +cmd_if (struct lexer *lexer, struct dataset *ds) { + struct dictionary *dict = dataset_dict (ds); struct compute_trns *compute = NULL; struct lvalue *lvalue = NULL; compute = compute_trns_create (); /* Test expression. */ - compute->test = expr_parse (default_dict, EXPR_BOOLEAN); + compute->test = expr_parse (lexer, ds, EXPR_BOOLEAN); if (compute->test == NULL) goto fail; /* Lvalue variable. */ - lvalue = lvalue_parse (); + lvalue = lvalue_parse (lexer, ds); if (lvalue == NULL) goto fail; /* Rvalue expression. */ - if (!lex_force_match ('=')) + if (!lex_force_match (lexer, '=')) goto fail; - compute->rvalue = parse_rvalue (lvalue); + compute->rvalue = parse_rvalue (lexer, lvalue, ds); if (compute->rvalue == NULL) goto fail; - add_transformation (get_proc_func (lvalue), compute_trns_free, compute); + add_transformation (ds, get_proc_func (lvalue), compute_trns_free, compute); - lvalue_finalize (lvalue, compute); + lvalue_finalize (lvalue, compute, dict); - return lex_end_of_command (); + return lex_end_of_command (lexer); fail: - lvalue_destroy (lvalue); + lvalue_destroy (lvalue, dict); compute_trns_free (compute); return CMD_CASCADING_FAILURE; } @@ -252,7 +259,7 @@ cmd_if (void) static trns_proc_func * get_proc_func (const struct lvalue *lvalue) { - bool is_numeric = lvalue_get_type (lvalue) == NUMERIC; + bool is_numeric = lvalue_get_type (lvalue) == VAR_NUMERIC; bool is_vector = lvalue_is_vector (lvalue); return (is_numeric @@ -263,11 +270,12 @@ 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 (const struct lvalue *lvalue) +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) == VAR_NUMERIC; - return expr_parse (default_dict, is_numeric ? EXPR_NUMBER : EXPR_STRING); + return expr_parse (lexer, ds, is_numeric ? EXPR_NUMBER : EXPR_STRING); } /* Returns a new struct compute_trns after initializing its fields. */ @@ -299,10 +307,14 @@ compute_trns_free (void *compute_) return true; } -/* COMPUTE or IF target variable or vector element. */ +/* COMPUTE or IF target variable or vector element. + For a variable, the `variable' member is non-null. + For a vector element, the `vector' member is non-null. */ struct lvalue { - char var_name[LONG_NAME_LEN + 1]; /* Destination variable name, or "". */ + struct variable *variable; /* Destination variable. */ + bool is_new_variable; /* Did we create the variable? */ + const struct vector *vector; /* Destination vector, if any, or NULL. */ struct expression *element; /* Destination vector element, or NULL. */ }; @@ -310,48 +322,56 @@ struct lvalue /* Parses the target variable or vector element into a new `struct lvalue', which is returned. */ static struct lvalue * -lvalue_parse (void) +lvalue_parse (struct lexer *lexer, struct dataset *ds) { + struct dictionary *dict = dataset_dict (ds); struct lvalue *lvalue; lvalue = xmalloc (sizeof *lvalue); - lvalue->var_name[0] = '\0'; + lvalue->variable = NULL; + lvalue->is_new_variable = false; lvalue->vector = NULL; lvalue->element = NULL; - if (!lex_force_id ()) + if (!lex_force_id (lexer)) goto lossage; - if (lex_look_ahead () == '(') + if (lex_look_ahead (lexer) == '(') { /* Vector. */ - lvalue->vector = dict_lookup_vector (default_dict, tokid); + lvalue->vector = dict_lookup_vector (dict, lex_tokid (lexer)); if (lvalue->vector == NULL) { - msg (SE, _("There is no vector named %s."), tokid); + msg (SE, _("There is no vector named %s."), lex_tokid (lexer)); goto lossage; } /* Vector element. */ - lex_get (); - if (!lex_force_match ('(')) + lex_get (lexer); + if (!lex_force_match (lexer, '(')) goto lossage; - lvalue->element = expr_parse (default_dict, EXPR_NUMBER); + lvalue->element = expr_parse (lexer, ds, EXPR_NUMBER); if (lvalue->element == NULL) goto lossage; - if (!lex_force_match (')')) + if (!lex_force_match (lexer, ')')) goto lossage; } else { /* Variable name. */ - str_copy_trunc (lvalue->var_name, sizeof lvalue->var_name, tokid); - lex_get (); + const char *var_name = lex_tokid (lexer); + lvalue->variable = dict_lookup_var (dict, var_name); + if (lvalue->variable == NULL) + { + lvalue->variable = dict_create_var_assert (dict, var_name, 0); + lvalue->is_new_variable = true; + } + lex_get (lexer); } return lvalue; lossage: - lvalue_destroy (lvalue); + lvalue_destroy (lvalue, dict); return NULL; } @@ -360,19 +380,12 @@ lvalue_parse (void) static int lvalue_get_type (const struct lvalue *lvalue) { - if (lvalue->vector == NULL) - { - struct variable *var = dict_lookup_var (default_dict, lvalue->var_name); - if (var == NULL) - return NUMERIC; - else - return var->type; - } - else - return lvalue->vector->var[0]->type; + return (lvalue->variable != NULL + ? var_get_type (lvalue->variable) + : vector_get_type (lvalue->vector)); } -/* Returns nonzero if LVALUE has a vector as its target. */ +/* Returns true if LVALUE has a vector as its target. */ static bool lvalue_is_vector (const struct lvalue *lvalue) { @@ -382,21 +395,21 @@ 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) +lvalue_finalize (struct lvalue *lvalue, + struct compute_trns *compute, + struct dictionary *dict) { if (lvalue->vector == NULL) { - compute->variable = dict_lookup_var (default_dict, lvalue->var_name); - if (compute->variable == NULL) - compute->variable = dict_create_var_assert (default_dict, - lvalue->var_name, 0); - - compute->fv = compute->variable->fv; - compute->width = compute->variable->width; + compute->variable = lvalue->variable; + 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 { @@ -405,16 +418,18 @@ lvalue_finalize (struct lvalue *lvalue, struct compute_trns *compute) lvalue->element = NULL; } - lvalue_destroy (lvalue); + lvalue_destroy (lvalue, dict); } /* Destroys LVALUE. */ static void -lvalue_destroy (struct lvalue *lvalue) +lvalue_destroy (struct lvalue *lvalue, struct dictionary *dict) { if (lvalue == NULL) return; + if (lvalue->is_new_variable) + dict_delete_var (dict, lvalue->variable); expr_free (lvalue->element); free (lvalue); }