projects
/
pspp
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
toward better summaries
[pspp]
/
src
/
language
/
expressions
/
evaluate.c
diff --git
a/src/language/expressions/evaluate.c
b/src/language/expressions/evaluate.c
index fc5b74a3ab57b7727e96226c785640e5a3e8ee43..09f237e0cd4e3664a0a57353ce2b0f0d2292becf 100644
(file)
--- a/
src/language/expressions/evaluate.c
+++ b/
src/language/expressions/evaluate.c
@@
-1,5
+1,5
@@
/* PSPP - a program for statistical analysis.
/* PSPP - a program for statistical analysis.
- Copyright (C) 1997-9, 2000, 2006, 2007 Free Software Foundation, Inc.
+ Copyright (C) 1997-9, 2000, 2006, 2007
, 2009, 2010, 2011, 2012
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
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
@@
-15,14
+15,18
@@
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <config.h>
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <config.h>
-#include "private.h"
-#include <ctype.h>
-#include <libpspp/assertion.h>
-#include <libpspp/message.h>
-#include "helpers.h"
+#include "language/expressions/private.h"
#include "evaluate.h"
#include "evaluate.h"
-#include <libpspp/pool.h>
+
+#include <ctype.h>
+
+#include "libpspp/assertion.h"
+#include "libpspp/message.h"
+#include "language/expressions/helpers.h"
+#include "language/lexer/value-parser.h"
+#include "libpspp/pool.h"
+#include "output/driver.h"
#include "xalloc.h"
#include "xalloc.h"
@@
-47,7
+51,7
@@
expr_evaluate (struct expression *e, const struct ccase *c, int case_idx,
for (;;)
{
for (;;)
{
- assert (op < e->ops + e->
op_cnt
);
+ assert (op < e->ops + e->
n_ops
);
switch (op++->operation)
{
case OP_number:
switch (op++->operation)
{
case OP_number:
@@
-98,56
+102,73
@@
expr_evaluate_str (struct expression *e, const struct ccase *c, int case_idx,
assert ((dst == NULL) == (dst_size == 0));
expr_evaluate (e, c, case_idx, &s);
assert ((dst == NULL) == (dst_size == 0));
expr_evaluate (e, c, case_idx, &s);
- buf_copy_rpad (dst, dst_size, s.string, s.length);
+ buf_copy_rpad (dst, dst_size, s.string, s.length
, ' '
);
}
\f
}
\f
-#include <language/lexer/lexer.h>
-#include <language/command.h>
+#include "language/lexer/lexer.h"
+#include "language/command.h"
+
+static bool default_optimize = true;
int
cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
{
int
cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
{
- bool optimize =
tru
e;
+ bool optimize =
default_optimiz
e;
int retval = CMD_FAILURE;
bool dump_postfix = false;
int retval = CMD_FAILURE;
bool dump_postfix = false;
+ bool set_defaults = false;
struct ccase *c = NULL;
struct dataset *ds = NULL;
struct ccase *c = NULL;
struct dataset *ds = NULL;
+ char *name = NULL;
+ char *title = NULL;
+
struct expression *expr;
struct expression *expr;
+ struct dictionary *d = NULL;
+
for (;;)
{
for (;;)
{
- struct dictionary *d = NULL;
if (lex_match_id (lexer, "NOOPTIMIZE"))
if (lex_match_id (lexer, "NOOPTIMIZE"))
- optimize = 0;
+ optimize = false;
+ else if (lex_match_id (lexer, "OPTIMIZE"))
+ optimize = true;
else if (lex_match_id (lexer, "POSTFIX"))
dump_postfix = 1;
else if (lex_match_id (lexer, "POSTFIX"))
dump_postfix = 1;
- else if (lex_match (lexer, '('))
+ else if (lex_match_id (lexer, "SET"))
+ set_defaults = true;
+ else if (lex_match (lexer, T_LPAREN))
{
{
- char name[VAR_NAME_LEN + 1];
struct variable *v;
struct variable *v;
- size_t old_value_cnt;
- int width;
if (!lex_force_id (lexer))
goto done;
if (!lex_force_id (lexer))
goto done;
-
strcpy (name, lex_tokid
(lexer));
+
name = xstrdup (lex_tokcstr
(lexer));
lex_get (lexer);
lex_get (lexer);
- if (!lex_force_match (lexer,
'='
))
+ if (!lex_force_match (lexer,
T_EQUALS
))
goto done;
goto done;
+ union value value;
+ int width;
if (lex_is_number (lexer))
{
width = 0;
if (lex_is_number (lexer))
{
width = 0;
- fprintf (stderr, "(%s = %.2f)", name, lex_tokval (lexer));
+ value.f = lex_number (lexer);
+ lex_get (lexer);
+ }
+ else if (lex_match_id (lexer, "SYSMIS"))
+ {
+ width = 0;
+ value.f = SYSMIS;
}
}
- else if (lex_
token (lexer) == T_STRING
)
+ else if (lex_
is_string (lexer)
)
{
{
- width = ds_length (lex_tokstr (lexer));
- fprintf (stderr, "(%s = \"%.2s\")", name, ds_cstr (lex_tokstr (lexer)));
+ width = ss_length (lex_tokss (lexer));
+ value.s = CHAR_CAST (uint8_t *, ss_xstrdup (lex_tokss (lexer)));
+ lex_get (lexer);
}
else
{
}
else
{
@@
-155,58
+176,76
@@
cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
goto done;
}
goto done;
}
- if (
ds == NULL
)
+ if (
ds == NULL
)
{
{
- ds =
create_dataset (
);
+ ds =
dataset_create (NULL, ""
);
d = dataset_dict (ds);
}
d = dataset_dict (ds);
}
- old_value_cnt = dict_get_next_value_idx (d);
v = dict_create_var (d, name, width);
if (v == NULL)
{
msg (SE, _("Duplicate variable name %s."), name);
v = dict_create_var (d, name, width);
if (v == NULL)
{
msg (SE, _("Duplicate variable name %s."), name);
+ value_destroy (&value, width);
goto done;
}
goto done;
}
+ free (name);
+ name = NULL;
- if (
c == NULL
)
+ if (
lex_match_id (lexer, "MISSING")
)
{
{
- c = xmalloc (sizeof *c);
- case_create (c, dict_get_next_value_idx (d));
+ struct missing_values mv;
+ mv_init (&mv, width);
+ mv_add_value (&mv, &value);
+ var_set_missing_values (v, &mv);
+ mv_destroy (&mv);
}
}
- else
- case_resize (c, dict_get_next_value_idx (d));
- if (
lex_is_number (lexer)
)
- c
ase_data_rw (c, v)->f = lex_tokval (lexer
);
+ if (
c == NULL
)
+ c
= case_create (dict_get_proto (d)
);
else
else
- memcpy (case_data_rw (c, v)->s, ds_data (lex_tokstr (lexer)),
-
var_get_width (v)
);
-
lex_get (lexer
);
+ c = case_unshare_and_resize (c, dict_get_proto (d));
+
value_swap (case_data_rw (c, v), &value
);
+
value_destroy (&value, width
);
- if (!lex_force_match (lexer,
')'
))
+ if (!lex_force_match (lexer,
T_RPAREN
))
goto done;
}
goto done;
}
+ else if (lex_match_id (lexer, "VECTOR"))
+ {
+ struct variable **vars;
+ size_t n;
+ dict_get_vars_mutable (d, &vars, &n, 0);
+ dict_create_vector_assert (d, "V", vars, n);
+ free (vars);
+ }
else
break;
}
else
break;
}
- if (lex_token (lexer) != '/')
+
+ if (set_defaults)
{
{
- lex_force_match (lexer, '/');
+ retval = CMD_SUCCESS;
+ default_optimize = optimize;
goto done;
}
goto done;
}
- if ( ds != NULL )
- fprintf(stderr, "; ");
- fprintf (stderr, "%s => ", lex_rest_of_line (lexer));
- lex_get (lexer);
+ if (!lex_force_match (lexer, T_SLASH))
+ goto done;
+
+ for (size_t i = 1; ; i++)
+ if (lex_next_token (lexer, i) == T_ENDCMD)
+ {
+ title = lex_next_representation (lexer, 0, i - 1);
+ break;
+ }
expr = expr_parse_any (lexer, ds, optimize);
if (!expr || lex_end_of_command (lexer) != CMD_SUCCESS)
{
if (expr != NULL)
expr_free (expr);
expr = expr_parse_any (lexer, ds, optimize);
if (!expr || lex_end_of_command (lexer) != CMD_SUCCESS)
{
if (expr != NULL)
expr_free (expr);
-
fprintf (stderr, "error\n"
);
+
output_log ("%s => error", title
);
goto done;
}
goto done;
}
@@
-216,31
+255,29
@@
cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
switch (expr->type)
{
case OP_number:
switch (expr->type)
{
case OP_number:
+ case OP_num_vec_elem:
{
double d = expr_evaluate_num (expr, c, 0);
if (d == SYSMIS)
{
double d = expr_evaluate_num (expr, c, 0);
if (d == SYSMIS)
-
fprintf (stderr, "sysmis\n"
);
+
output_log ("%s => sysmis", title
);
else
else
-
fprintf (stderr, "%.2f\n"
, d);
+
output_log ("%s => %.2f", title
, d);
}
break;
case OP_boolean:
{
double b = expr_evaluate_num (expr, c, 0);
}
break;
case OP_boolean:
{
double b = expr_evaluate_num (expr, c, 0);
-
fprintf (stderr, "%s\n"
,
- b == SYSMIS ? "sysmis" : b == 0.0 ? "false" : "true");
+
output_log ("%s => %s", title
,
+
b == SYSMIS ? "sysmis" : b == 0.0 ? "false" : "true");
}
break;
case OP_string:
{
}
break;
case OP_string:
{
- struct substring s;
- expr_evaluate (expr, c, 0, &s);
-
- fputc ('"', stderr);
- fwrite (s.string, s.length, 1, stderr);
- fputs ("\"\n", stderr);
+ struct substring out;
+ expr_evaluate (expr, c, 0, &out);
+ output_log ("%s => \"%.*s\"", title, (int) out.length, out.string);
break;
}
break;
}
@@
-252,14
+289,12
@@
cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
retval = CMD_SUCCESS;
done:
retval = CMD_SUCCESS;
done:
- if (ds)
- destroy_dataset (ds);
+ dataset_destroy (ds);
- if (c != NULL)
- {
- case_destroy (c);
- free (c);
- }
+ case_unref (c);
+
+ free (name);
+ free (title);
return retval;
}
return retval;
}
@@
-267,57
+302,60
@@
cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
void
expr_debug_print_postfix (const struct expression *e)
{
void
expr_debug_print_postfix (const struct expression *e)
{
- s
ize_t i
;
+ s
truct string s = DS_EMPTY_INITIALIZER
;
- for (
i = 0; i < e->op_cnt
; i++)
+ for (
size_t i = 0; i < e->n_ops
; i++)
{
union operation_data *op = &e->ops[i];
if (i > 0)
{
union operation_data *op = &e->ops[i];
if (i > 0)
-
putc (' ', stderr
);
+
ds_put_byte (&s, ' '
);
switch (e->op_types[i])
{
case OP_operation:
if (op->operation == OP_return_number)
switch (e->op_types[i])
{
case OP_operation:
if (op->operation == OP_return_number)
-
fprintf (stderr
, "return_number");
+
ds_put_cstr (&s
, "return_number");
else if (op->operation == OP_return_string)
else if (op->operation == OP_return_string)
-
fprintf (stderr
, "return_string");
+
ds_put_cstr (&s
, "return_string");
else if (is_function (op->operation))
else if (is_function (op->operation))
-
fprintf (stderr
, "%s", operations[op->operation].prototype);
+
ds_put_format (&s
, "%s", operations[op->operation].prototype);
else if (is_composite (op->operation))
else if (is_composite (op->operation))
-
fprintf (stderr
, "%s", operations[op->operation].name);
+
ds_put_format (&s
, "%s", operations[op->operation].name);
else
else
-
fprintf (stderr
, "%s:", operations[op->operation].name);
+
ds_put_format (&s
, "%s:", operations[op->operation].name);
break;
case OP_number:
if (op->number != SYSMIS)
break;
case OP_number:
if (op->number != SYSMIS)
-
fprintf (stderr
, "n<%g>", op->number);
+
ds_put_format (&s
, "n<%g>", op->number);
else
else
-
fprintf (stderr
, "n<SYSMIS>");
+
ds_put_cstr (&s
, "n<SYSMIS>");
break;
case OP_string:
break;
case OP_string:
- fprintf (stderr, "s<%.*s>",
- (int) op->string.length,
-
op->string.string != NULL ? op->string.string : ""
);
+ ds_put_cstr (&s, "s<");
+ ds_put_substring (&s, op->string);
+
ds_put_byte (&s, '>'
);
break;
case OP_format:
{
char str[FMT_STRING_LEN_MAX + 1];
fmt_to_string (op->format, str);
break;
case OP_format:
{
char str[FMT_STRING_LEN_MAX + 1];
fmt_to_string (op->format, str);
-
fprintf (stderr
, "f<%s>", str);
+
ds_put_format (&s
, "f<%s>", str);
}
break;
case OP_variable:
}
break;
case OP_variable:
-
fprintf (stderr
, "v<%s>", var_get_name (op->variable));
+
ds_put_format (&s
, "v<%s>", var_get_name (op->variable));
break;
case OP_vector:
break;
case OP_vector:
-
fprintf (stderr
, "vec<%s>", vector_get_name (op->vector));
+
ds_put_format (&s
, "vec<%s>", vector_get_name (op->vector));
break;
case OP_integer:
break;
case OP_integer:
- fprintf (stderr, "i<%d>", op->integer);
+ ds_put_format (&s, "i<%d>", op->integer);
+ break;
+ case OP_expr_node:
+ ds_put_cstr (&s, "expr_node");
break;
default:
NOT_REACHED ();
}
}
break;
default:
NOT_REACHED ();
}
}
-
fprintf (stderr, "\n"
);
+
output_log_nocopy (ds_steal_cstr (&s)
);
}
}