checkin of 0.3.0
[pspp-builds.git] / src / exprP.h
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
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.
9
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.
14
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., 59 Temple Place - Suite 330, Boston, MA
18    02111-1307, USA. */
19
20 #if !exprP_h
21 #define exprP_h 1
22
23 #undef DEBUGGING
24 /*#define DEBUGGING 1*/
25 #include "debug-print.h"
26
27 #if GLOBAL_DEBUGGING
28 void debug_print_expr (struct expression *);
29 void debug_print_op (short int *);
30 #endif
31
32 /* Expression types. */
33 enum
34   {
35     EX_ERROR,           /* Error value for propagation. */
36     EX_BOOLEAN,         /* Numeric value that's 0, 1, or SYSMIS. */
37     EX_NUMERIC,         /* Numeric value. */
38     EX_STRING           /* String value. */
39   };
40
41 /* Expression operators.
42    The ordering below is important.  Do not change it. */
43 enum
44   {
45     OP_ERROR,
46
47     /* Basic operators. */
48     OP_PLUS,
49     OP_MUL,
50     OP_POW,
51     OP_AND,
52     OP_OR,
53     OP_NOT,
54
55     /* Numeric relational operators. */
56     OP_EQ,
57     OP_GE,
58     OP_GT,
59     OP_LE,
60     OP_LT,
61     OP_NE,
62
63     /* String relational operators. */
64     OP_STRING_EQ,
65     OP_STRING_GE,
66     OP_STRING_GT,
67     OP_STRING_LE,
68     OP_STRING_LT,
69     OP_STRING_NE,
70
71     /* Unary functions. */
72     OP_NEG,
73     OP_ABS,
74     OP_ARCOS,
75     OP_ARSIN,
76     OP_ARTAN,
77     OP_COS,
78     OP_EXP,
79     OP_LG10,
80     OP_LN,
81     OP_MOD10,
82     OP_RND,
83     OP_SIN,
84     OP_SQRT,
85     OP_TAN,
86     OP_TRUNC,
87
88     /* N-ary numeric functions. */
89     OP_ANY,
90     OP_ANY_STRING,
91     OP_CFVAR,
92     OP_MAX,
93     OP_MEAN,
94     OP_MIN,
95     OP_NMISS,
96     OP_NVALID,
97     OP_RANGE,
98     OP_RANGE_STRING,
99     OP_SD,
100     OP_SUM,
101     OP_VARIANCE,
102
103     /* Time construction & extraction functions. */
104     OP_TIME_HMS,
105
106     /* These never appear in a tree or an expression.
107        They disappear in parse.c:unary_func(). */
108     OP_CTIME_DAYS,
109     OP_CTIME_HOURS,
110     OP_CTIME_MINUTES,
111     OP_CTIME_SECONDS,
112     OP_TIME_DAYS,
113
114     /* Date construction functions. */
115     OP_DATE_DMY,
116     OP_DATE_MDY,
117     OP_DATE_MOYR,
118     OP_DATE_QYR,
119     OP_DATE_WKYR,
120     OP_DATE_YRDAY,
121     OP_YRMODA,
122
123     /* Date extraction functions. */
124     OP_XDATE_DATE,
125     OP_XDATE_HOUR,
126     OP_XDATE_JDAY,
127     OP_XDATE_MDAY,
128     OP_XDATE_MINUTE,
129     OP_XDATE_MONTH,
130     OP_XDATE_QUARTER,
131     OP_XDATE_SECOND,
132     OP_XDATE_TDAY,
133     OP_XDATE_TIME,
134     OP_XDATE_WEEK,
135     OP_XDATE_WKDAY,
136     OP_XDATE_YEAR,
137
138     /* String functions. */
139     OP_CONCAT,
140     OP_INDEX,
141     OP_INDEX_OPT,
142     OP_RINDEX,
143     OP_RINDEX_OPT,
144     OP_LENGTH,
145     OP_LOWER,
146     OP_UPPER,
147     OP_LPAD,
148     OP_LPAD_OPT,
149     OP_RPAD,
150     OP_RPAD_OPT,
151     OP_LTRIM,
152     OP_LTRIM_OPT,
153     OP_RTRIM,
154     OP_RTRIM_OPT,
155     OP_NUMBER,
156     OP_NUMBER_OPT,
157     OP_STRING,
158     OP_SUBSTR,
159     OP_SUBSTR_OPT,
160
161     /* Artificial. */
162     OP_INV,                     /* Reciprocal. */
163     OP_SQUARE,                  /* Squares the argument. */
164     OP_NUM_TO_BOOL,             /* Converts ~0=>0, ~1=>1, SYSMIS=>SYSMIS,
165                                    others=>0 with a warning. */
166
167     /* Weirdness. */
168     OP_MOD,                     /* Modulo function. */
169     OP_NORMAL,                  /* Normally distributed PRNG. */
170     OP_UNIFORM,                 /* Uniformly distributed PRNG. */
171     OP_SYSMIS,                  /* Tests whether for SYSMIS argument. */
172     OP_VEC_ELEM_NUM,            /* Element of a numeric vector. */
173     OP_VEC_ELEM_STR,            /* Element of a string vector. */
174
175     /* Terminals. */
176     OP_TERMINAL,                /* Not a valid type.  Boundary
177                                    between terminals and nonterminals. */
178
179     OP_NUM_CON,                 /* Numeric constant. */
180     OP_STR_CON,                 /* String literal. */
181     OP_NUM_VAR,                 /* Numeric variable reference. */
182     OP_STR_VAR,                 /* String variable reference. */
183     OP_NUM_LAG,                 /* Numeric variable from an earlier case. */
184     OP_STR_LAG,                 /* String variable from an earlier case. */
185     OP_NUM_SYS,                 /* SYSMIS(numvar). */
186     OP_NUM_VAL,                 /* VALUE(numvar). */
187     OP_STR_MIS,                 /* MISSING(strvar). */
188     OP_CASENUM,                 /* $CASENUM. */
189     OP_SENTINEL                 /* Sentinel. */
190   };
191
192 /* Flags that describe operators. */
193 enum
194   {
195     OP_VAR_ARGS = 001,          /* 1=Variable number of args. */
196     OP_MIN_ARGS = 002,          /* 1=Can specific min args with .X. */
197     OP_FMT_SPEC = 004,          /* 1=Includes a format specifier. */
198     OP_ABSORB_MISS = 010,       /* 1=May return other than SYSMIS if
199                                    given a SYSMIS argument. */
200   };
201
202 /* Describes an operator. */
203 struct op_desc
204   {
205 #if GLOBAL_DEBUGGING
206     const char *name;           /* Operator name. */
207 #endif
208     unsigned char flags;        /* Flags. */
209     signed char height;         /* Effect on stack height. */
210     unsigned char skip;         /* Number of operator item arguments. */
211   };
212
213 extern struct op_desc ops[];
214
215 /* Tree structured expressions. */ 
216
217 /* Numeric constant. */
218 struct num_con_node
219   {
220     int type;                   /* Always OP_NUM_CON. */
221     double value;               /* Numeric value. */
222   };
223
224 /* String literal. */
225 struct str_con_node
226   {
227     int type;                   /* Always OP_STR_CON. */
228     int len;                    /* Length of string. */
229     char s[1];                  /* String value. */
230   };
231
232 /* Variable or test for missing values or cancellation of
233    user-missing. */
234 struct var_node
235   {
236     int type;                   /* OP_NUM_VAR, OP_NUM_SYS, OP_NUM_VAL,
237                                    OP_STR_MIS, or OP_STR_VAR. */
238     struct variable *v;         /* Variable. */
239   };
240
241 /* Variable from an earlier case. */
242 struct lag_node
243   {
244     int type;                   /* Always OP_NUM_LAG. */
245     struct variable *v;         /* Relevant variable. */
246     int lag;                    /* Number of cases to lag. */
247   };
248
249 /* $CASENUM. */
250 struct casenum_node
251   {
252     int type;                   /* Always OP_CASENUM. */
253   };
254
255 /* Any nonterminal node. */
256 struct nonterm_node
257   {
258     int type;                   /* Always greater than OP_TERMINAL. */
259     int n;                      /* Number of arguments. */
260     union any_node *arg[1];     /* Arguments. */
261   };
262
263 /* Any node. */
264 union any_node
265   {
266     int type;
267     struct nonterm_node nonterm;
268     struct num_con_node num_con;
269     struct str_con_node str_con;
270     struct var_node var;
271     struct lag_node lag;
272     struct casenum_node casenum;
273   };
274
275 /* An expression. */
276 struct expression
277   {
278     int type;                   /* Type of expression result. */
279     unsigned char *op;          /* Operators. */
280     struct variable **var;      /* Variables. */
281     double *num;                /* Numeric operands. */
282     unsigned char *str;         /* String operands. */
283     union value *stack;         /* Evaluation stack. */
284     unsigned char *str_stack;   /* String evaluation stack. */
285 #if !PAGED_STACK
286     size_t str_size;            /* Size of string eval stack. */
287 #endif
288   };
289
290 struct nonterm_node *optimize_expression (struct nonterm_node *);
291 void dump_expression (union any_node *, struct expression *);
292 void free_node (union any_node *);
293
294 double yrmoda (double year, double month, double day);
295
296 #endif /* exprP.h */