projects
/
pspp
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
intern: Use UP_CAST macro instad of open-coding it.
[pspp]
/
src
/
language
/
expressions
/
evaluate.c
diff --git
a/src/language/expressions/evaluate.c
b/src/language/expressions/evaluate.c
index d5f5f3871744252ac9f92ad61ec0e78f9a0b73ad..56deb9a5c0665c6f0cd50fb624d7815b20a9c21e 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, 2009 Free Software Foundation, Inc.
+ Copyright (C) 1997-9, 2000, 2006, 2007, 2009
, 2010, 2011
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,17
@@
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 "evaluate.h"
+
+#include "language/expressions/evaluate.h"
#include <ctype.h>
#include <ctype.h>
-#include <libpspp/assertion.h>
-#include <libpspp/message.h>
-#include <language/expressions/helpers.h>
-#include <language/expressions/private.h>
-#include <libpspp/pool.h>
+
+#include "libpspp/assertion.h"
+#include "libpspp/message.h"
+#include "language/expressions/helpers.h"
+#include "language/expressions/private.h"
+#include "language/lexer/value-parser.h"
+#include "libpspp/pool.h"
#include "xalloc.h"
#include "xalloc.h"
@@
-101,8
+104,8
@@
expr_evaluate_str (struct expression *e, const struct ccase *c, int case_idx,
buf_copy_rpad (dst, dst_size, s.string, s.length, ' ');
}
\f
buf_copy_rpad (dst, dst_size, s.string, s.length, ' ');
}
\f
-#include
<language/lexer/lexer.h>
-#include
<language/command.h>
+#include
"language/lexer/lexer.h"
+#include
"language/command.h"
int
cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
int
cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
@@
-115,6
+118,8
@@
cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
struct dataset *ds = NULL;
struct dataset *ds = NULL;
+ char *name = NULL;
+
struct expression *expr;
for (;;)
struct expression *expr;
for (;;)
@@
-124,31
+129,24
@@
cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
optimize = 0;
else if (lex_match_id (lexer, "POSTFIX"))
dump_postfix = 1;
optimize = 0;
else if (lex_match_id (lexer, "POSTFIX"))
dump_postfix = 1;
- else if (lex_match (lexer,
'('
))
+ else if (lex_match (lexer,
T_LPAREN
))
{
{
- char name[VAR_NAME_LEN + 1];
struct variable *v;
size_t old_value_cnt;
int width;
if (!lex_force_id (lexer))
goto done;
struct variable *v;
size_t old_value_cnt;
int width;
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;
if (lex_is_number (lexer))
goto done;
if (lex_is_number (lexer))
- {
- width = 0;
- fprintf (stderr, "(%s = %.2f)", name, lex_tokval (lexer));
- }
- else if (lex_token (lexer) == T_STRING)
- {
- width = ds_length (lex_tokstr (lexer));
- fprintf (stderr, "(%s = \"%.2s\")", name, ds_cstr (lex_tokstr (lexer)));
- }
+ width = 0;
+ else if (lex_is_string (lexer))
+ width = ss_length (lex_tokss (lexer));
else
{
lex_error (lexer, _("expecting number or string"));
else
{
lex_error (lexer, _("expecting number or string"));
@@
-157,7
+155,7
@@
cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
if ( ds == NULL )
{
if ( ds == NULL )
{
- ds =
create_dataset
();
+ ds =
dataset_create
();
d = dataset_dict (ds);
}
d = dataset_dict (ds);
}
@@
-168,34
+166,29
@@
cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
msg (SE, _("Duplicate variable name %s."), name);
goto done;
}
msg (SE, _("Duplicate variable name %s."), name);
goto done;
}
+ free (name);
+ name = NULL;
if (c == NULL)
c = case_create (dict_get_proto (d));
else
c = case_unshare_and_resize (c, dict_get_proto (d));
if (c == NULL)
c = case_create (dict_get_proto (d));
else
c = case_unshare_and_resize (c, dict_get_proto (d));
- if (lex_is_number (lexer))
- case_data_rw (c, v)->f = lex_tokval (lexer);
- else
- memcpy (case_str_rw (c, v), ds_data (lex_tokstr (lexer)),
- var_get_width (v));
- lex_get (lexer);
+ if (!parse_value (lexer, case_data_rw (c, v), v))
+ NOT_REACHED ();
- if (!lex_force_match (lexer,
')'
))
+ if (!lex_force_match (lexer,
T_RPAREN
))
goto done;
}
else
break;
}
goto done;
}
else
break;
}
- if (lex_token (lexer) !=
'/'
)
+ if (lex_token (lexer) !=
T_SLASH
)
{
{
- lex_force_match (lexer,
'/'
);
+ lex_force_match (lexer,
T_SLASH
);
goto done;
}
goto done;
}
- if ( ds != NULL )
- fprintf(stderr, "; ");
- fprintf (stderr, "%s => ", lex_rest_of_line (lexer));
lex_get (lexer);
expr = expr_parse_any (lexer, ds, optimize);
lex_get (lexer);
expr = expr_parse_any (lexer, ds, optimize);
@@
-203,7
+196,7
@@
cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
{
if (expr != NULL)
expr_free (expr);
{
if (expr != NULL)
expr_free (expr);
-
fprintf (stderr,
"error\n");
+
printf (
"error\n");
goto done;
}
goto done;
}
@@
-216,16
+209,16
@@
cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
{
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");
+
printf (
"sysmis\n");
else
else
-
fprintf (stderr,
"%.2f\n", d);
+
printf (
"%.2f\n", 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",
+
printf (
"%s\n",
b == SYSMIS ? "sysmis" : b == 0.0 ? "false" : "true");
}
break;
b == SYSMIS ? "sysmis" : b == 0.0 ? "false" : "true");
}
break;
@@
-235,9
+228,9
@@
cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
struct substring s;
expr_evaluate (expr, c, 0, &s);
struct substring s;
expr_evaluate (expr, c, 0, &s);
-
fputc ('"', stderr
);
- fwrite (s.string, s.length, 1, std
err
);
-
fputs ("\"\n", stderr
);
+
putchar ('"'
);
+ fwrite (s.string, s.length, 1, std
out
);
+
puts ("\""
);
break;
}
break;
}
@@
-249,11
+242,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);
case_unref (c);
case_unref (c);
+ free (name);
+
return retval;
}
return retval;
}
@@
-271,24
+265,24
@@
expr_debug_print_postfix (const struct expression *e)
{
case OP_operation:
if (op->operation == OP_return_number)
{
case OP_operation:
if (op->operation == OP_return_number)
-
fprintf (stderr,
"return_number");
+
printf (
"return_number");
else if (op->operation == OP_return_string)
else if (op->operation == OP_return_string)
-
fprintf (stderr,
"return_string");
+
printf (
"return_string");
else if (is_function (op->operation))
else if (is_function (op->operation))
-
fprintf (stderr,
"%s", operations[op->operation].prototype);
+
printf (
"%s", operations[op->operation].prototype);
else if (is_composite (op->operation))
else if (is_composite (op->operation))
-
fprintf (stderr,
"%s", operations[op->operation].name);
+
printf (
"%s", operations[op->operation].name);
else
else
-
fprintf (stderr,
"%s:", operations[op->operation].name);
+
printf (
"%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);
+
printf (
"n<%g>", op->number);
else
else
-
fprintf (stderr,
"n<SYSMIS>");
+
printf (
"n<SYSMIS>");
break;
case OP_string:
break;
case OP_string:
-
fprintf (stderr,
"s<%.*s>",
+
printf (
"s<%.*s>",
(int) op->string.length,
op->string.string != NULL ? op->string.string : "");
break;
(int) op->string.length,
op->string.string != NULL ? op->string.string : "");
break;
@@
-296,21
+290,21
@@
expr_debug_print_postfix (const struct expression *e)
{
char str[FMT_STRING_LEN_MAX + 1];
fmt_to_string (op->format, str);
{
char str[FMT_STRING_LEN_MAX + 1];
fmt_to_string (op->format, str);
-
fprintf (stderr,
"f<%s>", str);
+
printf (
"f<%s>", str);
}
break;
case OP_variable:
}
break;
case OP_variable:
-
fprintf (stderr,
"v<%s>", var_get_name (op->variable));
+
printf (
"v<%s>", var_get_name (op->variable));
break;
case OP_vector:
break;
case OP_vector:
-
fprintf (stderr,
"vec<%s>", vector_get_name (op->vector));
+
printf (
"vec<%s>", vector_get_name (op->vector));
break;
case OP_integer:
break;
case OP_integer:
-
fprintf (stderr,
"i<%d>", op->integer);
+
printf (
"i<%d>", op->integer);
break;
default:
NOT_REACHED ();
}
}
break;
default:
NOT_REACHED ();
}
}
-
fprintf (stderr,
"\n");
+
printf (
"\n");
}
}