1 /* PSPP - computes sample statistics.
2 Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3 Written by Ben Pfaff <blp@gnu.org>.
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 #include "algorithm.h"
29 #include "dictionary.h"
42 /* Recursive descent parser in order of increasing precedence. */
43 typedef union any_node *parse_recursively_func (struct expression *);
44 static parse_recursively_func parse_or, parse_and, parse_not;
45 static parse_recursively_func parse_rel, parse_add, parse_mul;
46 static parse_recursively_func parse_neg, parse_exp;
47 static parse_recursively_func parse_primary;
48 static parse_recursively_func parse_vector_element, parse_function;
50 /* Utility functions. */
51 static struct expression *expr_create (struct dictionary *);
52 atom_type expr_node_returns (const union any_node *);
54 static const char *atom_type_name (atom_type);
55 static struct expression *finish_expression (union any_node *,
57 static bool type_check (struct expression *, union any_node **,
58 enum expr_type expected_type);
59 static union any_node *allocate_unary_variable (struct expression *,
62 /* Public functions. */
64 /* Parses an expression of the given TYPE.
65 If DICT is nonnull then variables and vectors within it may be
66 referenced within the expression; otherwise, the expression
67 must not reference any variables or vectors.
68 Returns the new expression if successful or a null pointer
71 expr_parse (struct dictionary *dict, enum expr_type type)
76 assert (type == EXPR_NUMBER || type == EXPR_STRING || type == EXPR_BOOLEAN);
78 e = expr_create (dict);
80 if (n != NULL && type_check (e, &n, type))
81 return finish_expression (expr_optimize (n, e), e);
89 /* Parses and returns an expression of the given TYPE, as
90 expr_parse(), and sets up so that destroying POOL will free
91 the expression as well. */
93 expr_parse_pool (struct pool *pool,
94 struct dictionary *dict, enum expr_type type)
96 struct expression *e = expr_parse (dict, type);
98 pool_add_subpool (pool, e->expr_pool);
102 /* Free expression E. */
104 expr_free (struct expression *e)
107 pool_destroy (e->expr_pool);
111 expr_parse_any (struct dictionary *dict, bool optimize)
114 struct expression *e;
116 e = expr_create (dict);
125 n = expr_optimize (n, e);
126 return finish_expression (n, e);
129 /* Finishing up expression building. */
131 /* Height of an expression's stacks. */
134 int number_height; /* Height of number stack. */
135 int string_height; /* Height of string stack. */
138 /* Stack heights used by different kinds of arguments. */
139 static const struct stack_heights on_number_stack = {1, 0};
140 static const struct stack_heights on_string_stack = {0, 1};
141 static const struct stack_heights not_on_stack = {0, 0};
143 /* Returns the stack heights used by an atom of the given
145 static const struct stack_heights *
146 atom_type_stack (atom_type type)
148 assert (is_atom (type));
154 return &on_number_stack;
157 return &on_string_stack;
167 return ¬_on_stack;
174 /* Measures the stack height needed for node N, supposing that
175 the stack height is initially *HEIGHT and updating *HEIGHT to
176 the final stack height. Updates *MAX, if necessary, to
177 reflect the maximum intermediate or final height. */
179 measure_stack (const union any_node *n,
180 struct stack_heights *height, struct stack_heights *max)
182 const struct stack_heights *return_height;
184 if (is_composite (n->type))
186 struct stack_heights args;
190 for (i = 0; i < n->composite.arg_cnt; i++)
191 measure_stack (n->composite.args[i], &args, max);
193 return_height = atom_type_stack (operations[n->type].returns);
196 return_height = atom_type_stack (n->type);
198 height->number_height += return_height->number_height;
199 height->string_height += return_height->string_height;
201 if (height->number_height > max->number_height)
202 max->number_height = height->number_height;
203 if (height->string_height > max->string_height)
204 max->string_height = height->string_height;
207 /* Allocates stacks within E sufficient for evaluating node N. */
209 allocate_stacks (union any_node *n, struct expression *e)
211 struct stack_heights initial = {0, 0};
212 struct stack_heights max = {0, 0};
214 measure_stack (n, &initial, &max);
215 e->number_stack = pool_alloc (e->expr_pool,
216 sizeof *e->number_stack * max.number_height);
217 e->string_stack = pool_alloc (e->expr_pool,
218 sizeof *e->string_stack * max.string_height);
221 /* Finalizes expression E for evaluating node N. */
222 static struct expression *
223 finish_expression (union any_node *n, struct expression *e)
225 /* Allocate stacks. */
226 allocate_stacks (n, e);
228 /* Output postfix representation. */
231 /* The eval_pool might have been used for allocating strings
232 during optimization. We need to keep those strings around
233 for all subsequent evaluations, so start a new eval_pool. */
234 e->eval_pool = pool_create_subpool (e->expr_pool);
239 /* Verifies that expression E, whose root node is *N, can be
240 converted to type EXPECTED_TYPE, inserting a conversion at *N
241 if necessary. Returns true if successful, false on failure. */
243 type_check (struct expression *e,
244 union any_node **n, enum expr_type expected_type)
246 atom_type actual_type = expr_node_returns (*n);
248 switch (expected_type)
252 if (actual_type != OP_number && actual_type != OP_boolean)
254 msg (SE, _("Type mismatch: expression has %s type, "
255 "but a numeric value is required here."),
256 atom_type_name (actual_type));
259 if (actual_type == OP_number && expected_type == OP_boolean)
260 *n = expr_allocate_unary (e, OP_NUM_TO_BOOLEAN, *n);
264 if (actual_type != OP_string)
266 msg (SE, _("Type mismatch: expression has %s type, "
267 "but a string value is required here."),
268 atom_type_name (actual_type));
280 /* Recursive-descent expression parser. */
282 /* Considers whether *NODE may be coerced to type REQUIRED_TYPE.
283 Returns true if possible, false if disallowed.
285 If DO_COERCION is false, then *NODE is not modified and there
288 If DO_COERCION is true, we perform the coercion if possible,
289 modifying *NODE if necessary. If the coercion is not possible
290 then we free *NODE and set *NODE to a null pointer.
292 This function's interface is somewhat awkward. Use one of the
293 wrapper functions type_coercion(), type_coercion_assert(), or
294 is_coercible() instead. */
296 type_coercion_core (struct expression *e,
297 atom_type required_type,
298 union any_node **node,
299 const char *operator_name,
302 atom_type actual_type;
304 assert (!!do_coercion == (e != NULL));
307 /* Propagate error. Whatever caused the original error
308 already emitted an error message. */
312 actual_type = expr_node_returns (*node);
313 if (actual_type == required_type)
319 switch (required_type)
322 if (actual_type == OP_boolean)
324 /* To enforce strict typing rules, insert Boolean to
325 numeric "conversion". This conversion is a no-op,
326 so it will be removed later. */
328 *node = expr_allocate_unary (e, OP_BOOLEAN_TO_NUM, *node);
334 /* No coercion to string. */
338 if (actual_type == OP_number)
340 /* Convert numeric to boolean. */
342 *node = expr_allocate_unary (e, OP_NUM_TO_BOOLEAN, *node);
351 if ((*node)->type == OP_format
352 && check_input_specifier (&(*node)->format.f, false)
353 && check_specifier_type (&(*node)->format.f, NUMERIC, false))
356 (*node)->type = OP_ni_format;
362 if ((*node)->type == OP_format
363 && check_output_specifier (&(*node)->format.f, false)
364 && check_specifier_type (&(*node)->format.f, NUMERIC, false))
367 (*node)->type = OP_no_format;
373 if ((*node)->type == OP_NUM_VAR)
376 *node = (*node)->composite.args[0];
382 if ((*node)->type == OP_STR_VAR)
385 *node = (*node)->composite.args[0];
391 if ((*node)->type == OP_number
392 && floor ((*node)->number.n) == (*node)->number.n
393 && (*node)->number.n > 0 && (*node)->number.n < INT_MAX)
396 *node = expr_allocate_pos_int (e, (*node)->number.n);
407 msg (SE, _("Type mismatch while applying %s operator: "
408 "cannot convert %s to %s."),
410 atom_type_name (actual_type), atom_type_name (required_type));
416 /* Coerces *NODE to type REQUIRED_TYPE, and returns success. If
417 *NODE cannot be coerced to the desired type then we issue an
418 error message about operator OPERATOR_NAME and free *NODE. */
420 type_coercion (struct expression *e,
421 atom_type required_type, union any_node **node,
422 const char *operator_name)
424 return type_coercion_core (e, required_type, node, operator_name, true);
427 /* Coerces *NODE to type REQUIRED_TYPE.
428 Assert-fails if the coercion is disallowed. */
430 type_coercion_assert (struct expression *e,
431 atom_type required_type, union any_node **node)
433 int success = type_coercion_core (e, required_type, node, NULL, true);
437 /* Returns true if *NODE may be coerced to type REQUIRED_TYPE,
440 is_coercible (atom_type required_type, union any_node *const *node)
442 return type_coercion_core (NULL, required_type,
443 (union any_node **) node, NULL, false);
446 /* How to parse an operator. */
449 int token; /* Token representing operator. */
450 operation_type type; /* Operation type representing operation. */
451 const char *name; /* Name of operator. */
454 /* Attempts to match the current token against the tokens for the
455 OP_CNT operators in OPS[]. If successful, returns true
456 and, if OPERATOR is non-null, sets *OPERATOR to the operator.
457 On failure, returns false and, if OPERATOR is non-null, sets
458 *OPERATOR to a null pointer. */
460 match_operator (const struct operator ops[], size_t op_cnt,
461 const struct operator **operator)
463 const struct operator *op;
465 for (op = ops; op < ops + op_cnt; op++)
467 if (op->token == '-')
468 lex_negative_to_dash ();
469 if (lex_match (op->token))
471 if (operator != NULL)
476 if (operator != NULL)
482 check_operator (const struct operator *op, int arg_cnt, atom_type arg_type)
484 const struct operation *o;
488 o = &operations[op->type];
489 assert (o->arg_cnt == arg_cnt);
490 assert ((o->flags & OPF_ARRAY_OPERAND) == 0);
491 for (i = 0; i < arg_cnt; i++)
492 assert (o->args[i] == arg_type);
497 check_binary_operators (const struct operator ops[], size_t op_cnt,
502 for (i = 0; i < op_cnt; i++)
503 check_operator (&ops[i], 2, arg_type);
508 get_operand_type (const struct operator *op)
510 return operations[op->type].args[0];
513 /* Parses a chain of left-associative operator/operand pairs.
514 There are OP_CNT operators, specified in OPS[]. The
515 operators' operands must all be the same type. The next
516 higher level is parsed by PARSE_NEXT_LEVEL. If CHAIN_WARNING
517 is non-null, then it will be issued as a warning if more than
518 one operator/operand pair is parsed. */
519 static union any_node *
520 parse_binary_operators (struct expression *e, union any_node *node,
521 const struct operator ops[], size_t op_cnt,
522 parse_recursively_func *parse_next_level,
523 const char *chain_warning)
525 atom_type operand_type = get_operand_type (&ops[0]);
527 const struct operator *operator;
529 assert (check_binary_operators (ops, op_cnt, operand_type));
533 for (op_count = 0; match_operator (ops, op_cnt, &operator); op_count++)
537 /* Convert the left-hand side to type OPERAND_TYPE. */
538 if (!type_coercion (e, operand_type, &node, operator->name))
541 /* Parse the right-hand side and coerce to type
543 rhs = parse_next_level (e);
544 if (!type_coercion (e, operand_type, &rhs, operator->name))
546 node = expr_allocate_binary (e, operator->type, node, rhs);
549 if (op_count > 1 && chain_warning != NULL)
550 msg (SW, chain_warning);
555 static union any_node *
556 parse_inverting_unary_operator (struct expression *e,
557 const struct operator *op,
558 parse_recursively_func *parse_next_level)
560 union any_node *node;
563 check_operator (op, 1, get_operand_type (op));
566 while (match_operator (op, 1, NULL))
569 node = parse_next_level (e);
571 && type_coercion (e, get_operand_type (op), &node, op->name)
572 && op_count % 2 != 0)
573 return expr_allocate_unary (e, op->type, node);
578 /* Parses the OR level. */
579 static union any_node *
580 parse_or (struct expression *e)
582 static const struct operator op =
583 { T_OR, OP_OR, "logical disjunction (\"OR\")" };
585 return parse_binary_operators (e, parse_and (e), &op, 1, parse_and, NULL);
588 /* Parses the AND level. */
589 static union any_node *
590 parse_and (struct expression *e)
592 static const struct operator op =
593 { T_AND, OP_AND, "logical conjunction (\"AND\")" };
595 return parse_binary_operators (e, parse_not (e), &op, 1, parse_not, NULL);
598 /* Parses the NOT level. */
599 static union any_node *
600 parse_not (struct expression *e)
602 static const struct operator op
603 = { T_NOT, OP_NOT, "logical negation (\"NOT\")" };
604 return parse_inverting_unary_operator (e, &op, parse_rel);
607 /* Parse relational operators. */
608 static union any_node *
609 parse_rel (struct expression *e)
611 const char *chain_warning =
612 _("Chaining relational operators (e.g. \"a < b < c\") will "
613 "not produce the mathematically expected result. "
614 "Use the AND logical operator to fix the problem "
615 "(e.g. \"a < b AND b < c\"). "
616 "If chaining is really intended, parentheses will disable "
617 "this warning (e.g. \"(a < b) < c\".)");
619 union any_node *node = parse_add (e);
624 switch (expr_node_returns (node))
629 static const struct operator ops[] =
631 { '=', OP_EQ, "numeric equality (\"=\")" },
632 { T_EQ, OP_EQ, "numeric equality (\"EQ\")" },
633 { T_GE, OP_GE, "numeric greater-than-or-equal-to (\">=\")" },
634 { T_GT, OP_GT, "numeric greater than (\">\")" },
635 { T_LE, OP_LE, "numeric less-than-or-equal-to (\"<=\")" },
636 { T_LT, OP_LT, "numeric less than (\"<\")" },
637 { T_NE, OP_NE, "numeric inequality (\"<>\")" },
640 return parse_binary_operators (e, node, ops, sizeof ops / sizeof *ops,
641 parse_add, chain_warning);
646 static const struct operator ops[] =
648 { '=', OP_EQ_STRING, "string equality (\"=\")" },
649 { T_EQ, OP_EQ_STRING, "string equality (\"EQ\")" },
650 { T_GE, OP_GE_STRING, "string greater-than-or-equal-to (\">=\")" },
651 { T_GT, OP_GT_STRING, "string greater than (\">\")" },
652 { T_LE, OP_LE_STRING, "string less-than-or-equal-to (\"<=\")" },
653 { T_LT, OP_LT_STRING, "string less than (\"<\")" },
654 { T_NE, OP_NE_STRING, "string inequality (\"<>\")" },
657 return parse_binary_operators (e, node, ops, sizeof ops / sizeof *ops,
658 parse_add, chain_warning);
666 /* Parses the addition and subtraction level. */
667 static union any_node *
668 parse_add (struct expression *e)
670 static const struct operator ops[] =
672 { '+', OP_ADD, "addition (\"+\")" },
673 { '-', OP_SUB, "subtraction (\"-\")" },
676 return parse_binary_operators (e, parse_mul (e),
677 ops, sizeof ops / sizeof *ops,
681 /* Parses the multiplication and division level. */
682 static union any_node *
683 parse_mul (struct expression *e)
685 static const struct operator ops[] =
687 { '*', OP_MUL, "multiplication (\"*\")" },
688 { '/', OP_DIV, "division (\"/\")" },
691 return parse_binary_operators (e, parse_neg (e),
692 ops, sizeof ops / sizeof *ops,
696 /* Parses the unary minus level. */
697 static union any_node *
698 parse_neg (struct expression *e)
700 static const struct operator op = { '-', OP_NEG, "negation (\"-\")" };
701 return parse_inverting_unary_operator (e, &op, parse_exp);
704 static union any_node *
705 parse_exp (struct expression *e)
707 static const struct operator op =
708 { T_EXP, OP_POW, "exponentiation (\"**\")" };
710 const char *chain_warning =
711 _("The exponentiation operator (\"**\") is left-associative, "
712 "even though right-associative semantics are more useful. "
713 "That is, \"a**b**c\" equals \"(a**b)**c\", not as \"a**(b**c)\". "
714 "To disable this warning, insert parentheses.");
716 return parse_binary_operators (e, parse_primary (e), &op, 1,
717 parse_primary, chain_warning);
720 /* Parses system variables. */
721 static union any_node *
722 parse_sysvar (struct expression *e)
724 if (lex_match_id ("$CASENUM"))
725 return expr_allocate_nullary (e, OP_CASENUM);
726 else if (lex_match_id ("$DATE"))
728 static const char *months[12] =
730 "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
731 "JUL", "AUG", "SEP", "OCT", "NOV", "DEC",
737 time = localtime (&last_vfm_invocation);
738 sprintf (temp_buf, "%02d %s %02d", abs (time->tm_mday) % 100,
739 months[abs (time->tm_mon) % 12], abs (time->tm_year) % 100);
741 return expr_allocate_string_buffer (e, temp_buf, strlen (temp_buf));
743 else if (lex_match_id ("$TRUE"))
744 return expr_allocate_boolean (e, 1.0);
745 else if (lex_match_id ("$FALSE"))
746 return expr_allocate_boolean (e, 0.0);
747 else if (lex_match_id ("$SYSMIS"))
748 return expr_allocate_number (e, SYSMIS);
749 else if (lex_match_id ("$JDATE"))
751 struct tm *time = localtime (&last_vfm_invocation);
752 return expr_allocate_number (e, expr_ymd_to_ofs (time->tm_year + 1900,
756 else if (lex_match_id ("$TIME"))
758 struct tm *time = localtime (&last_vfm_invocation);
759 return expr_allocate_number (e,
760 expr_ymd_to_date (time->tm_year + 1900,
763 + time->tm_hour * 60 * 60.
767 else if (lex_match_id ("$LENGTH"))
768 return expr_allocate_number (e, get_viewlength ());
769 else if (lex_match_id ("$WIDTH"))
770 return expr_allocate_number (e, get_viewwidth ());
773 msg (SE, _("Unknown system variable %s."), tokid);
778 /* Parses numbers, varnames, etc. */
779 static union any_node *
780 parse_primary (struct expression *e)
785 if (lex_look_ahead () == '(')
787 /* An identifier followed by a left parenthesis may be
788 a vector element reference. If not, it's a function
790 if (e->dict != NULL && dict_lookup_vector (e->dict, tokid) != NULL)
791 return parse_vector_element (e);
793 return parse_function (e);
795 else if (tokid[0] == '$')
797 /* $ at the beginning indicates a system variable. */
798 return parse_sysvar (e);
800 else if (e->dict != NULL && dict_lookup_var (e->dict, tokid))
802 /* It looks like a user variable.
803 (It could be a format specifier, but we'll assume
804 it's a variable unless proven otherwise. */
805 return allocate_unary_variable (e, parse_dict_variable (e->dict));
809 /* Try to parse it as a format specifier. */
811 if (parse_format_specifier (&fmt, FMTP_SUPPRESS_ERRORS))
812 return expr_allocate_format (e, &fmt);
814 /* All attempts failed. */
815 msg (SE, _("Unknown identifier %s."), tokid);
823 union any_node *node = expr_allocate_number (e, tokval);
830 union any_node *node = expr_allocate_string_buffer (e, ds_c_str (&tokstr),
831 ds_length (&tokstr));
838 union any_node *node;
841 if (node != NULL && !lex_match (')'))
843 lex_error (_("expecting `)'"));
850 lex_error (_("in expression"));
855 static union any_node *
856 parse_vector_element (struct expression *e)
858 const struct vector *vector;
859 union any_node *element;
861 /* Find vector, skip token.
862 The caller must already have verified that the current token
863 is the name of a vector. */
864 vector = dict_lookup_vector (default_dict, tokid);
865 assert (vector != NULL);
868 /* Skip left parenthesis token.
869 The caller must have verified that the lookahead is a left
871 assert (token == '(');
874 element = parse_or (e);
875 if (!type_coercion (e, OP_number, &element, "vector indexing")
879 return expr_allocate_binary (e, (vector->var[0]->type == NUMERIC
880 ? OP_VEC_ELEM_NUM : OP_VEC_ELEM_STR),
881 element, expr_allocate_vector (e, vector));
884 /* Individual function parsing. */
886 struct operation operations[OP_first + OP_cnt] = {
891 word_matches (const char **test, const char **name)
893 size_t test_len = strcspn (*test, ".");
894 size_t name_len = strcspn (*name, ".");
895 if (test_len == name_len)
897 if (buf_compare_case (*test, *name, test_len))
900 else if (test_len < 3 || test_len > name_len)
904 if (buf_compare_case (*test, *name, test_len))
910 if (**test != **name)
922 compare_names (const char *test, const char *name)
926 if (!word_matches (&test, &name))
928 if (*name == '\0' && *test == '\0')
934 compare_strings (const char *test, const char *name)
936 return strcasecmp (test, name);
940 lookup_function_helper (const char *name,
941 int (*compare) (const char *test, const char *name),
942 const struct operation **first,
943 const struct operation **last)
947 for (f = operations + OP_function_first;
948 f <= operations + OP_function_last; f++)
949 if (!compare (name, f->name))
953 while (f <= operations + OP_function_last && !compare (name, f->name))
964 lookup_function (const char *name,
965 const struct operation **first,
966 const struct operation **last)
968 *first = *last = NULL;
969 return (lookup_function_helper (name, compare_strings, first, last)
970 || lookup_function_helper (name, compare_names, first, last));
974 extract_min_valid (char *s)
976 char *p = strrchr (s, '.');
978 || p[1] < '0' || p[1] > '9'
979 || strspn (p + 1, "0123456789") != strlen (p + 1))
986 function_arg_type (const struct operation *f, size_t arg_idx)
988 assert (arg_idx < f->arg_cnt || (f->flags & OPF_ARRAY_OPERAND));
990 return f->args[arg_idx < f->arg_cnt ? arg_idx : f->arg_cnt - 1];
994 match_function (union any_node **args, int arg_cnt, const struct operation *f)
998 if (arg_cnt < f->arg_cnt
999 || (arg_cnt > f->arg_cnt && (f->flags & OPF_ARRAY_OPERAND) == 0)
1000 || arg_cnt - (f->arg_cnt - 1) < f->array_min_elems)
1003 for (i = 0; i < arg_cnt; i++)
1004 if (!is_coercible (function_arg_type (f, i), &args[i]))
1011 coerce_function_args (struct expression *e, const struct operation *f,
1012 union any_node **args, size_t arg_cnt)
1016 for (i = 0; i < arg_cnt; i++)
1017 type_coercion_assert (e, function_arg_type (f, i), &args[i]);
1021 validate_function_args (const struct operation *f, int arg_cnt, int min_valid)
1023 int array_arg_cnt = arg_cnt - (f->arg_cnt - 1);
1024 if (array_arg_cnt < f->array_min_elems)
1026 msg (SE, _("%s must have at least %d arguments in list."),
1027 f->prototype, f->array_min_elems);
1031 if ((f->flags & OPF_ARRAY_OPERAND)
1032 && array_arg_cnt % f->array_granularity != 0)
1034 if (f->array_granularity == 2)
1035 msg (SE, _("%s must have even number of arguments in list."),
1038 msg (SE, _("%s must have multiple of %d arguments in list."),
1039 f->prototype, f->array_granularity);
1043 if (min_valid != -1)
1045 if (f->array_min_elems == 0)
1047 assert ((f->flags & OPF_MIN_VALID) == 0);
1048 msg (SE, _("%s function does not accept a minimum valid "
1049 "argument count."), f->prototype);
1054 assert (f->flags & OPF_MIN_VALID);
1055 if (array_arg_cnt < f->array_min_elems)
1057 msg (SE, _("%s requires at least %d valid arguments in list."),
1058 f->prototype, f->array_min_elems);
1061 else if (min_valid > array_arg_cnt)
1063 msg (SE, _("With %s, "
1064 "using minimum valid argument count of %d "
1065 "does not make sense when passing only %d "
1066 "arguments in list."),
1067 f->prototype, min_valid, array_arg_cnt);
1077 add_arg (union any_node ***args, int *arg_cnt, int *arg_cap,
1078 union any_node *arg)
1080 if (*arg_cnt >= *arg_cap)
1083 *args = xrealloc (*args, sizeof **args * *arg_cap);
1086 (*args)[(*arg_cnt)++] = arg;
1090 put_invocation (struct string *s,
1091 const char *func_name, union any_node **args, size_t arg_cnt)
1095 ds_printf (s, "%s(", func_name);
1096 for (i = 0; i < arg_cnt; i++)
1100 ds_puts (s, operations[expr_node_returns (args[i])].prototype);
1106 no_match (const char *func_name,
1107 union any_node **args, size_t arg_cnt,
1108 const struct operation *first, const struct operation *last)
1111 const struct operation *f;
1115 if (last - first == 1)
1117 ds_printf (&s, _("Type mismatch invoking %s as "), first->prototype);
1118 put_invocation (&s, func_name, args, arg_cnt);
1122 ds_puts (&s, _("Function invocation "));
1123 put_invocation (&s, func_name, args, arg_cnt);
1124 ds_puts (&s, _(" does not match any known function. Candidates are:"));
1126 for (f = first; f < last; f++)
1127 ds_printf (&s, "\n%s", f->prototype);
1131 msg (SE, "%s", ds_c_str (&s));
1136 static union any_node *
1137 parse_function (struct expression *e)
1140 const struct operation *f, *first, *last;
1142 union any_node **args = NULL;
1146 struct fixed_string func_name;
1150 ls_create (&func_name, ds_c_str (&tokstr));
1151 min_valid = extract_min_valid (ds_c_str (&tokstr));
1152 if (!lookup_function (ds_c_str (&tokstr), &first, &last))
1154 msg (SE, _("No function or vector named %s."), ds_c_str (&tokstr));
1155 ls_destroy (&func_name);
1160 if (!lex_force_match ('('))
1162 ls_destroy (&func_name);
1167 arg_cnt = arg_cap = 0;
1171 if (token == T_ID && lex_look_ahead () == 'T')
1173 struct variable **vars;
1177 if (!parse_variables (default_dict, &vars, &var_cnt, PV_SINGLE))
1179 for (i = 0; i < var_cnt; i++)
1180 add_arg (&args, &arg_cnt, &arg_cap,
1181 allocate_unary_variable (e, vars[i]));
1186 union any_node *arg = parse_or (e);
1190 add_arg (&args, &arg_cnt, &arg_cap, arg);
1192 if (lex_match (')'))
1194 else if (!lex_match (','))
1196 lex_error (_("expecting `,' or `)' invoking %s function"),
1202 for (f = first; f < last; f++)
1203 if (match_function (args, arg_cnt, f))
1207 no_match (ls_c_str (&func_name), args, arg_cnt, first, last);
1211 coerce_function_args (e, f, args, arg_cnt);
1212 if (!validate_function_args (f, arg_cnt, min_valid))
1215 if ((f->flags & OPF_EXTENSION) && get_syntax () == COMPATIBLE)
1216 msg (SW, _("%s is a PSPP extension."), f->prototype);
1217 if (f->flags & OPF_UNIMPLEMENTED)
1219 msg (SE, _("%s is not yet implemented."), f->prototype);
1223 n = expr_allocate_composite (e, f - operations, args, arg_cnt);
1224 n->composite.min_valid = min_valid != -1 ? min_valid : f->array_min_elems;
1226 if (n->type == OP_LAG_Vn || n->type == OP_LAG_Vs)
1231 else if (n->type == OP_LAG_Vnn || n->type == OP_LAG_Vsn)
1234 assert (n->composite.arg_cnt == 2);
1235 assert (n->composite.args[1]->type == OP_pos_int);
1236 n_before = n->composite.args[1]->integer.i;
1237 if (n_lag < n_before)
1242 ls_destroy (&func_name);
1247 ls_destroy (&func_name);
1251 /* Utility functions. */
1253 static struct expression *
1254 expr_create (struct dictionary *dict)
1256 struct pool *pool = pool_create ();
1257 struct expression *e = pool_alloc (pool, sizeof *e);
1258 e->expr_pool = pool;
1260 e->eval_pool = pool_create_subpool (e->expr_pool);
1263 e->op_cnt = e->op_cap = 0;
1268 expr_node_returns (const union any_node *n)
1271 assert (is_operation (n->type));
1272 if (is_atom (n->type))
1274 else if (is_composite (n->type))
1275 return operations[n->type].returns;
1281 atom_type_name (atom_type type)
1283 assert (is_atom (type));
1284 return operations[type].name;
1288 expr_allocate_nullary (struct expression *e, operation_type op)
1290 return expr_allocate_composite (e, op, NULL, 0);
1294 expr_allocate_unary (struct expression *e, operation_type op,
1295 union any_node *arg0)
1297 return expr_allocate_composite (e, op, &arg0, 1);
1301 expr_allocate_binary (struct expression *e, operation_type op,
1302 union any_node *arg0, union any_node *arg1)
1304 union any_node *args[2];
1307 return expr_allocate_composite (e, op, args, 2);
1311 is_valid_node (union any_node *n)
1313 struct operation *op;
1317 assert (is_operation (n->type));
1318 op = &operations[n->type];
1320 if (!is_atom (n->type))
1322 struct composite_node *c = &n->composite;
1324 assert (is_composite (n->type));
1325 assert (c->arg_cnt >= op->arg_cnt);
1326 for (i = 0; i < op->arg_cnt; i++)
1327 assert (expr_node_returns (c->args[i]) == op->args[i]);
1328 if (c->arg_cnt > op->arg_cnt && !is_operator (n->type))
1330 assert (op->flags & OPF_ARRAY_OPERAND);
1331 for (i = 0; i < c->arg_cnt; i++)
1332 assert (operations[c->args[i]->type].returns
1333 == op->args[op->arg_cnt - 1]);
1341 expr_allocate_composite (struct expression *e, operation_type op,
1342 union any_node **args, size_t arg_cnt)
1347 n = pool_alloc (e->expr_pool, sizeof n->composite);
1349 n->composite.arg_cnt = arg_cnt;
1350 n->composite.args = pool_alloc (e->expr_pool,
1351 sizeof *n->composite.args * arg_cnt);
1352 for (i = 0; i < arg_cnt; i++)
1354 if (args[i] == NULL)
1356 n->composite.args[i] = args[i];
1358 memcpy (n->composite.args, args, sizeof *n->composite.args * arg_cnt);
1359 n->composite.min_valid = 0;
1360 assert (is_valid_node (n));
1365 expr_allocate_number (struct expression *e, double d)
1367 union any_node *n = pool_alloc (e->expr_pool, sizeof n->number);
1368 n->type = OP_number;
1374 expr_allocate_boolean (struct expression *e, double b)
1376 union any_node *n = pool_alloc (e->expr_pool, sizeof n->number);
1377 assert (b == 0.0 || b == 1.0 || b == SYSMIS);
1378 n->type = OP_boolean;
1384 expr_allocate_integer (struct expression *e, int i)
1386 union any_node *n = pool_alloc (e->expr_pool, sizeof n->integer);
1387 n->type = OP_integer;
1393 expr_allocate_pos_int (struct expression *e, int i)
1395 union any_node *n = pool_alloc (e->expr_pool, sizeof n->integer);
1397 n->type = OP_pos_int;
1403 expr_allocate_vector (struct expression *e, const struct vector *vector)
1405 union any_node *n = pool_alloc (e->expr_pool, sizeof n->vector);
1406 n->type = OP_vector;
1407 n->vector.v = vector;
1412 expr_allocate_string_buffer (struct expression *e,
1413 const char *string, size_t length)
1415 union any_node *n = pool_alloc (e->expr_pool, sizeof n->string);
1416 n->type = OP_string;
1419 n->string.s = copy_string (e, string, length);
1424 expr_allocate_string (struct expression *e, struct fixed_string s)
1426 union any_node *n = pool_alloc (e->expr_pool, sizeof n->string);
1427 n->type = OP_string;
1433 expr_allocate_variable (struct expression *e, struct variable *v)
1435 union any_node *n = pool_alloc (e->expr_pool, sizeof n->variable);
1436 n->type = v->type == NUMERIC ? OP_num_var : OP_str_var;
1442 expr_allocate_format (struct expression *e, const struct fmt_spec *format)
1444 union any_node *n = pool_alloc (e->expr_pool, sizeof n->format);
1445 n->type = OP_format;
1446 n->format.f = *format;
1450 /* Allocates a unary composite node that represents the value of
1451 variable V in expression E. */
1452 static union any_node *
1453 allocate_unary_variable (struct expression *e, struct variable *v)
1456 return expr_allocate_unary (e, v->type == NUMERIC ? OP_NUM_VAR : OP_STR_VAR,
1457 expr_allocate_variable (e, v));