expressions: Major work to improve error messages.
[pspp] / src / language / expressions / private.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2011 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #ifndef EXPRESSIONS_PRIVATE_H
18 #define EXPRESSIONS_PRIVATE_H
19
20 #include <assert.h>
21 #include <stddef.h>
22
23 #include "data/format.h"
24 #include "operations.h"
25 #include "public.h"
26 #include "libpspp/str.h"
27
28 enum operation_flags
29   {
30     /* Most operations produce a missing output value if any
31        input value is missing.  Setting this bit indicates that
32        this operation may produce a non-missing result given
33        missing input values (although it is not obliged to do
34        so).  Unless this bit is set, the operation's evaluation
35        function will never be passed a missing argument. */
36     OPF_ABSORB_MISS = 1 << 0,
37
38     /* If set, this operation's final operand is an array of one
39        or more elements. */
40     OPF_ARRAY_OPERAND = 1 << 1,
41
42     /* If set, the user can specify the minimum number of array
43        elements that must be non-missing for the function result
44        to be non-missing.  The operation must have an array
45        operand and the array must contain `double's.  Both
46        OPF_ABSORB_MISS and OPF_ARRAY_OPERAND must also be set. */
47     OPF_MIN_VALID = 1 << 2,
48
49     /* If set, operation is non-optimizable in general.  Unless
50        combined with OPF_ABSORB_MISS, missing input values are
51        still assumed to yield missing results. */
52     OPF_NONOPTIMIZABLE = 1 << 3,
53
54     /* If set, this operation is not implemented. */
55     OPF_UNIMPLEMENTED = 1 << 4,
56
57     /* If set, this operation is a PSPP extension. */
58     OPF_EXTENSION = 1 << 5,
59
60     /* If set, this operation may not occur after TEMPORARY.
61        (Currently this applies only to LAG.) */
62     OPF_PERM_ONLY = 1 << 6,
63
64     /* If set, this operation's name may not be abbreviated. */
65     OPF_NO_ABBREV = 1 << 7,
66
67     /* If set, this operation needs the "struct expr_node *", for message
68        locations. */
69     OPF_EXPR_NODE = 1 << 8,
70   };
71
72 #define EXPR_ARG_MAX 4
73 struct operation
74   {
75     const char *name;
76     const char *prototype;
77     enum operation_flags flags;
78     atom_type returns;       /* Usually OP_number, OP_string, or OP_boolean. */
79     int n_args;
80     atom_type args[EXPR_ARG_MAX];
81     int array_min_elems;
82     int array_granularity;
83   };
84
85 extern const struct operation operations[];
86
87 /* Expression parse tree. */
88 struct expr_node
89   {
90     operation_type type;
91     struct msg_location *location;
92
93     union
94       {
95         /* OP_number. */
96         double number;
97
98         /* OP_string. */
99         struct substring string;
100
101         /* OP_variable. */
102         const struct variable *variable;
103
104         /* OP_integer. */
105         int integer;
106
107         /* OP_vector. */
108         const struct vector *vector;
109
110         /* OP_format. */
111         struct fmt_spec format;
112
113         /* All the other node types. */
114         struct
115           {
116             size_t n_args;         /* Number of arguments. */
117             struct expr_node **args; /* Arguments. */
118             size_t min_valid;   /* Min valid array args to get valid result. */
119           };
120
121         /* OP_exprnode. */
122         const struct expr_node *expr_node;
123       };
124   };
125
126 union operation_data
127   {
128     operation_type operation;
129     double number;
130     struct substring string;
131     const struct variable *variable;
132     const struct vector *vector;
133     struct fmt_spec *format;
134     const struct expr_node *expr_node;
135     int integer;
136   };
137
138 /* An expression. */
139 struct expression
140   {
141     struct pool *expr_pool;     /* Pool for expression static data. */
142     struct dataset *ds ;        /* The dataset */
143     atom_type type;             /* Type of expression result. */
144
145     union operation_data *ops;  /* Expression data. */
146     operation_type *op_types;   /* ops[] element types (for debugging). */
147     size_t n_ops, allocated_ops; /* Number of ops, amount of allocated space. */
148
149     double *number_stack;       /* Evaluation stack: numerics, Booleans. */
150     struct substring *string_stack; /* Evaluation stack: strings. */
151     struct pool *eval_pool;     /* Pool for evaluation temporaries. */
152   };
153
154 struct expression *expr_parse_any (struct lexer *lexer, struct dataset *,  bool optimize);
155 void expr_debug_print_postfix (const struct expression *);
156
157 struct expr_node *expr_optimize (struct expr_node *, struct expression *);
158 void expr_flatten (struct expr_node *, struct expression *);
159
160 atom_type expr_node_returns (const struct expr_node *);
161
162 struct expr_node *expr_allocate_nullary (struct expression *e, operation_type);
163 struct expr_node *expr_allocate_unary (struct expression *e,
164                                      operation_type, struct expr_node *);
165 struct expr_node *expr_allocate_binary (struct expression *e, operation_type,
166                                  struct expr_node *, struct expr_node *);
167 struct expr_node *expr_allocate_composite (struct expression *e, operation_type,
168                                          struct expr_node **, size_t);
169 struct expr_node *expr_allocate_number (struct expression *e, double);
170 struct expr_node *expr_allocate_boolean (struct expression *e, double);
171 struct expr_node *expr_allocate_integer (struct expression *e, int);
172 struct expr_node *expr_allocate_pos_int (struct expression *e, int);
173 struct expr_node *expr_allocate_string (struct expression *e, struct substring);
174 struct expr_node *expr_allocate_variable (struct expression *e,
175                                         const struct variable *);
176 struct expr_node *expr_allocate_format (struct expression *e,
177                                  const struct fmt_spec *);
178 struct expr_node *expr_allocate_expr_node (struct expression *,
179                                            const struct expr_node *);
180 struct expr_node *expr_allocate_vector (struct expression *e,
181                                       const struct vector *);
182
183 const struct msg_location *expr_location (const struct expression *,
184                                           const struct expr_node *);
185
186 #endif /* expressions/private.h */