X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flanguage%2Fexpressions%2Fevaluate.c;h=56deb9a5c0665c6f0cd50fb624d7815b20a9c21e;hb=993af59a283dbad8b26a390535f76cdeb00a9108;hp=92f0a0e347c6ee3acf966bbfffd7b2044e855025;hpb=8021cf8974a46fe82af7b8952e448c0ea6858a48;p=pspp
diff --git a/src/language/expressions/evaluate.c b/src/language/expressions/evaluate.c
index 92f0a0e347..56deb9a5c0 100644
--- a/src/language/expressions/evaluate.c
+++ b/src/language/expressions/evaluate.c
@@ -1,31 +1,33 @@
-/* PSPP - computes sample statistics.
- Copyright (C) 1997-9, 2000, 2006, 2007 Free Software Foundation, Inc.
+/* PSPP - a program for statistical analysis.
+ 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 the Free Software Foundation; either version 2 of the
- License, or (at your option) any later version.
+ 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
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
- This program is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- 02110-1301, USA. */
+ along with this program. If not, see . */
#include
-#include "private.h"
+
+#include "language/expressions/evaluate.h"
#include
-#include
-#include
-#include
-#include "helpers.h"
-#include "evaluate.h"
-#include
+
+#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"
static void
expr_evaluate (struct expression *e, const struct ccase *c, int case_idx,
@@ -37,7 +39,7 @@ expr_evaluate (struct expression *e, const struct ccase *c, int case_idx,
double *ns = e->number_stack;
struct substring *ss = e->string_stack;
- /* Without a dictionary/dataset, the expression can't refer to variables,
+ /* 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
@@ -64,7 +66,7 @@ expr_evaluate (struct expression *e, const struct ccase *c, int case_idx,
break;
case OP_return_number:
- *(double *) result = finite (ns[-1]) ? ns[-1] : SYSMIS;
+ *(double *) result = isfinite (ns[-1]) ? ns[-1] : SYSMIS;
return;
case OP_return_string:
@@ -72,7 +74,7 @@ expr_evaluate (struct expression *e, const struct ccase *c, int case_idx,
return;
#include "evaluate.inc"
-
+
default:
NOT_REACHED ();
}
@@ -91,19 +93,19 @@ expr_evaluate_num (struct expression *e, const struct ccase *c, int case_idx)
void
expr_evaluate_str (struct expression *e, const struct ccase *c, int case_idx,
- char *dst, size_t dst_size)
+ char *dst, size_t dst_size)
{
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);
+
+ buf_copy_rpad (dst, dst_size, s.string, s.length, ' ');
}
-#include
-#include
+#include "language/lexer/lexer.h"
+#include "language/command.h"
int
cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
@@ -116,40 +118,35 @@ cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
struct dataset *ds = NULL;
+ char *name = NULL;
+
struct expression *expr;
- for (;;)
+ for (;;)
{
struct dictionary *d = NULL;
if (lex_match_id (lexer, "NOOPTIMIZE"))
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[LONG_NAME_LEN + 1];
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);
- if (!lex_force_match (lexer, '='))
+ if (!lex_force_match (lexer, T_EQUALS))
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"));
@@ -158,7 +155,7 @@ cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
if ( ds == NULL )
{
- ds = create_dataset (NULL, NULL, NULL);
+ ds = dataset_create ();
d = dataset_dict (ds);
}
@@ -169,37 +166,29 @@ cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
msg (SE, _("Duplicate variable name %s."), name);
goto done;
}
+ free (name);
+ name = NULL;
- if (c == NULL)
- {
- c = xmalloc (sizeof *c);
- case_create (c, dict_get_next_value_idx (d));
- }
+ if (c == NULL)
+ c = case_create (dict_get_proto (d));
else
- case_resize (c, dict_get_next_value_idx (d));
+ 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_data_rw (c, v)->s, 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
+ else
break;
}
- if (lex_token (lexer) != '/')
+ if (lex_token (lexer) != T_SLASH)
{
- lex_force_match (lexer, '/');
+ lex_force_match (lexer, T_SLASH);
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);
@@ -207,42 +196,42 @@ cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
{
if (expr != NULL)
expr_free (expr);
- fprintf (stderr, "error\n");
+ printf ("error\n");
goto done;
}
- if (dump_postfix)
+ if (dump_postfix)
expr_debug_print_postfix (expr);
- else
- switch (expr->type)
+ else
+ switch (expr->type)
{
- case OP_number:
+ case OP_number:
{
double d = expr_evaluate_num (expr, c, 0);
if (d == SYSMIS)
- fprintf (stderr, "sysmis\n");
+ printf ("sysmis\n");
else
- fprintf (stderr, "%.2f\n", d);
+ printf ("%.2f\n", d);
}
break;
-
- case OP_boolean:
+
+ case OP_boolean:
{
double b = expr_evaluate_num (expr, c, 0);
- fprintf (stderr, "%s\n",
- b == SYSMIS ? "sysmis" : b == 0.0 ? "false" : "true");
+ printf ("%s\n",
+ b == SYSMIS ? "sysmis" : b == 0.0 ? "false" : "true");
}
break;
- case OP_string:
+ case OP_string:
{
struct substring s;
expr_evaluate (expr, c, 0, &s);
- fputc ('"', stderr);
- fwrite (s.string, s.length, 1, stderr);
- fputs ("\"\n", stderr);
- break;
+ putchar ('"');
+ fwrite (s.string, s.length, 1, stdout);
+ puts ("\"");
+ break;
}
default:
@@ -253,50 +242,47 @@ cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
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);
return retval;
}
void
-expr_debug_print_postfix (const struct expression *e)
+expr_debug_print_postfix (const struct expression *e)
{
size_t i;
- for (i = 0; i < e->op_cnt; i++)
+ for (i = 0; i < e->op_cnt; i++)
{
union operation_data *op = &e->ops[i];
if (i > 0)
putc (' ', stderr);
- switch (e->op_types[i])
+ switch (e->op_types[i])
{
case OP_operation:
if (op->operation == OP_return_number)
- fprintf (stderr, "return_number");
+ printf ("return_number");
else if (op->operation == OP_return_string)
- fprintf (stderr, "return_string");
- else if (is_function (op->operation))
- fprintf (stderr, "%s", operations[op->operation].prototype);
- else if (is_composite (op->operation))
- fprintf (stderr, "%s", operations[op->operation].name);
+ printf ("return_string");
+ else if (is_function (op->operation))
+ printf ("%s", operations[op->operation].prototype);
+ else if (is_composite (op->operation))
+ printf ("%s", operations[op->operation].name);
else
- fprintf (stderr, "%s:", operations[op->operation].name);
+ printf ("%s:", operations[op->operation].name);
break;
case OP_number:
if (op->number != SYSMIS)
- fprintf (stderr, "n<%g>", op->number);
+ printf ("n<%g>", op->number);
else
- fprintf (stderr, "n");
+ printf ("n");
break;
case OP_string:
- fprintf (stderr, "s<%.*s>",
+ printf ("s<%.*s>",
(int) op->string.length,
op->string.string != NULL ? op->string.string : "");
break;
@@ -304,21 +290,21 @@ expr_debug_print_postfix (const struct expression *e)
{
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:
- fprintf (stderr, "v<%s>", var_get_name (op->variable));
+ printf ("v<%s>", var_get_name (op->variable));
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:
- fprintf (stderr, "i<%d>", op->integer);
+ printf ("i<%d>", op->integer);
break;
default:
NOT_REACHED ();
- }
+ }
}
- fprintf (stderr, "\n");
+ printf ("\n");
}