X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flanguage%2Fexpressions%2Fevaluate.c;h=3ff879f2b29b9689628d81089da5477d117df878;hb=244ade48f9c233532cc535d3233fdef53bf9266b;hp=e5f605b797966f7009ca5c48ef0d09694f530e2e;hpb=dcf9b154cbcaa35c3d8459a201b77eec8bcb30bd;p=pspp-builds.git diff --git a/src/language/expressions/evaluate.c b/src/language/expressions/evaluate.c index e5f605b7..3ff879f2 100644 --- a/src/language/expressions/evaluate.c +++ b/src/language/expressions/evaluate.c @@ -21,22 +21,30 @@ #include "private.h" #include -#include "alloc.h" -#include "message.h" +#include +#include +#include #include "helpers.h" #include "evaluate.h" -#include "pool.h" +#include static void expr_evaluate (struct expression *e, const struct ccase *c, int case_idx, void *result) { + struct dataset *ds = e->ds; union operation_data *op = e->ops; double *ns = e->number_stack; - struct fixed_string *ss = e->string_stack; + struct substring *ss = e->string_stack; + + /* Without a dictionary/dataset, the expression can't refer to variables, + and you don't need to specify a case when you evaluate the + expression. With a dictionary/dataset, the expression can refer + to variables, so you must specify a case when you evaluate the + expression. */ + assert ((c != NULL) == (e->ds != NULL)); - assert ((c != NULL) == (e->dict != NULL)); pool_clear (e->eval_pool); for (;;) @@ -51,7 +59,7 @@ expr_evaluate (struct expression *e, const struct ccase *c, int case_idx, case OP_string: { - const struct fixed_string *s = &op++->string; + const struct substring *s = &op++->string; *ss++ = copy_string (e, s->string, s->length); } break; @@ -61,13 +69,13 @@ expr_evaluate (struct expression *e, const struct ccase *c, int case_idx, return; case OP_return_string: - *(struct fixed_string *) result = ss[-1]; + *(struct substring *) result = ss[-1]; return; #include "evaluate.inc" default: - abort (); + NOT_REACHED (); } } } @@ -82,34 +90,40 @@ expr_evaluate_num (struct expression *e, const struct ccase *c, int case_idx) return d; } + + void expr_evaluate_str (struct expression *e, const struct ccase *c, int case_idx, char *dst, size_t dst_size) { - struct fixed_string s; + struct substring s; assert (e->type == OP_string); assert ((dst == NULL) == (dst_size == 0)); expr_evaluate (e, c, case_idx, &s); + buf_copy_rpad (dst, dst_size, s.string, s.length); } -#include "lexer.h" -#include "command.h" +#include +#include int -cmd_debug_evaluate (void) +cmd_debug_evaluate (struct dataset *dsother UNUSED) { bool optimize = true; int retval = CMD_FAILURE; bool dump_postfix = false; - struct dictionary *d = NULL; + struct ccase *c = NULL; + struct dataset *ds = NULL; + struct expression *expr; for (;;) { + struct dictionary *d = NULL; if (lex_match_id ("NOOPTIMIZE")) optimize = 0; else if (lex_match_id ("POSTFIX")) @@ -137,16 +151,19 @@ cmd_debug_evaluate (void) else if (token == T_STRING) { width = ds_length (&tokstr); - fprintf (stderr, "(%s = \"%.2s\")", name, ds_c_str (&tokstr)); + fprintf (stderr, "(%s = \"%.2s\")", name, ds_cstr (&tokstr)); } else { lex_error (_("expecting number or string")); goto done; } - - if (d == NULL) - d = dict_create (); + + if ( ds == NULL ) + { + ds = create_dataset (); + d = dataset_dict (ds); + } old_value_cnt = dict_get_next_value_idx (d); v = dict_create_var (d, name, width); @@ -159,9 +176,10 @@ cmd_debug_evaluate (void) if (c == NULL) { c = xmalloc (sizeof *c); - case_nullify (c); + case_create (c, dict_get_next_value_idx (d)); } - case_resize (c, old_value_cnt, dict_get_next_value_idx (d)); + else + case_resize (c, old_value_cnt, dict_get_next_value_idx (d)); if (lex_is_number ()) case_data_rw (c, v->fv)->f = tokval; @@ -181,12 +199,13 @@ cmd_debug_evaluate (void) lex_force_match ('/'); goto done; } - if (d != NULL) - fprintf (stderr, "; "); + + if ( ds != NULL ) + fprintf(stderr, "; "); fprintf (stderr, "%s => ", lex_rest_of_line (NULL)); lex_get (); - expr = expr_parse_any (d, optimize); + expr = expr_parse_any (ds, optimize); if (!expr || lex_end_of_command () != CMD_SUCCESS) { if (expr != NULL) @@ -220,7 +239,7 @@ cmd_debug_evaluate (void) case OP_string: { - struct fixed_string s; + struct substring s; expr_evaluate (expr, c, 0, &s); fputc ('"', stderr); @@ -230,19 +249,22 @@ cmd_debug_evaluate (void) } default: - assert (0); + NOT_REACHED (); } expr_free (expr); retval = CMD_SUCCESS; done: + if (ds) + destroy_dataset (ds); + if (c != NULL) { case_destroy (c); free (c); } - dict_destroy (d); + return retval; } @@ -296,7 +318,7 @@ expr_debug_print_postfix (const struct expression *e) fprintf (stderr, "i<%d>", op->integer); break; default: - abort (); + NOT_REACHED (); } } fprintf (stderr, "\n");