4283b4ae694236d6118eb48c9890cfc26b70fd5c
[pspp-builds.git] / src / language / expressions / private.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000 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 #include <libpspp/str.h>
23
24 #include "public.h"
25 #include "operations.h"
26
27 #include <data/format.h>
28
29 enum operation_flags
30   {
31     /* Most operations produce a missing output value if any
32        input value is missing.  Setting this bit indicates that
33        this operation may produce a non-missing result given
34        missing input values (although it is not obliged to do
35        so).  Unless this bit is set, the operation's evaluation
36        function will never be passed a missing argument. */
37     OPF_ABSORB_MISS = 004,
38
39     /* If set, this operation's final operand is an array of one
40        or more elements. */
41     OPF_ARRAY_OPERAND = 001,
42
43     /* If set, the user can specify the minimum number of array
44        elements that must be non-missing for the function result
45        to be non-missing.  The operation must have an array
46        operand and the array must contain `double's.  Both
47        OPF_ABSORB_MISS and OPF_ARRAY_OPERAND must also be set. */
48     OPF_MIN_VALID = 002,
49
50     /* If set, operation is non-optimizable in general.  Unless
51        combined with OPF_ABSORB_MISS, missing input values are
52        still assumed to yield missing results. */
53     OPF_NONOPTIMIZABLE = 010,
54
55     /* If set, this operation is not implemented. */
56     OPF_UNIMPLEMENTED = 020,
57
58     /* If set, this operation is a PSPP extension. */
59     OPF_EXTENSION = 040,
60
61     /* If set, this operation may not occur after TEMPORARY.
62        (Currently this applies only to LAG.) */
63     OPF_PERM_ONLY = 0100,
64
65     /* If set, this operation's name may not be abbreviated. */
66     OPF_NO_ABBREV = 0200
67   };
68
69 #define EXPR_ARG_MAX 4
70 struct operation
71   {
72     const char *name;
73     const char *prototype;
74     enum operation_flags flags;
75     atom_type returns;
76     int arg_cnt;
77     atom_type args[EXPR_ARG_MAX];
78     int array_min_elems;
79     int array_granularity;
80   };
81
82 extern const struct operation operations[];
83
84 /* Tree structured expressions. */
85
86 /* Atoms. */
87 struct number_node
88   {
89     operation_type type;   /* OP_number. */
90     double n;
91   };
92
93 struct string_node
94   {
95     operation_type type;   /* OP_string. */
96     struct substring s;
97   };
98
99 struct variable_node
100   {
101     operation_type type;   /* OP_variable. */
102     const struct variable *v;
103   };
104
105 struct integer_node
106   {
107     operation_type type;   /* OP_integer. */
108     int i;
109   };
110
111 struct vector_node
112   {
113     operation_type type;   /* OP_vector. */
114     const struct vector *v;
115   };
116
117 struct format_node
118   {
119     operation_type type;   /* OP_format. */
120     struct fmt_spec f;
121   };
122
123 /* Any composite node. */
124 struct composite_node
125   {
126     operation_type type;   /* One of OP_*. */
127     size_t arg_cnt;             /* Number of arguments. */
128     union any_node **args;      /* Arguments. */
129     size_t min_valid;           /* Min valid array args to get valid result. */
130   };
131
132 /* Any node. */
133 union any_node
134   {
135     operation_type type;
136     struct number_node number;
137     struct string_node string;
138     struct variable_node variable;
139     struct integer_node integer;
140     struct vector_node vector;
141     struct format_node format;
142     struct composite_node composite;
143   };
144
145 union operation_data
146   {
147     operation_type operation;
148     double number;
149     struct substring string;
150     const struct variable *variable;
151     const struct vector *vector;
152     struct fmt_spec *format;
153     int integer;
154   };
155
156 /* An expression. */
157 struct expression
158   {
159     struct pool *expr_pool;     /* Pool for expression static data. */
160     struct dataset *ds ;        /* The dataset */
161     atom_type type;             /* Type of expression result. */
162
163     union operation_data *ops;  /* Expression data. */
164     operation_type *op_types;   /* ops[] element types (for debugging). */
165     size_t op_cnt, op_cap;      /* Number of ops, amount of allocated space. */
166
167     double *number_stack;       /* Evaluation stack: numerics, Booleans. */
168     struct substring *string_stack; /* Evaluation stack: strings. */
169     struct pool *eval_pool;     /* Pool for evaluation temporaries. */
170   };
171
172 struct expression *expr_parse_any (struct lexer *lexer, struct dataset *,  bool optimize);
173 void expr_debug_print_postfix (const struct expression *);
174
175 union any_node *expr_optimize (union any_node *, struct expression *);
176 void expr_flatten (union any_node *, struct expression *);
177
178 atom_type expr_node_returns (const union any_node *);
179
180 union any_node *expr_allocate_nullary (struct expression *e, operation_type);
181 union any_node *expr_allocate_unary (struct expression *e,
182                                      operation_type, union any_node *);
183 union any_node *expr_allocate_binary (struct expression *e, operation_type,
184                                  union any_node *, union any_node *);
185 union any_node *expr_allocate_composite (struct expression *e, operation_type,
186                                          union any_node **, size_t);
187 union any_node *expr_allocate_number (struct expression *e, double);
188 union any_node *expr_allocate_boolean (struct expression *e, double);
189 union any_node *expr_allocate_integer (struct expression *e, int);
190 union any_node *expr_allocate_pos_int (struct expression *e, int);
191 union any_node *expr_allocate_string_buffer (struct expression *e,
192                                              const char *string, size_t length);
193 union any_node *expr_allocate_string (struct expression *e,
194                                       struct substring);
195 union any_node *expr_allocate_variable (struct expression *e,
196                                         const struct variable *);
197 union any_node *expr_allocate_format (struct expression *e,
198                                  const struct fmt_spec *);
199 union any_node *expr_allocate_vector (struct expression *e,
200                                       const struct vector *);
201
202 #endif /* expressions/private.h */