expressions: Major work to improve error messages.
[pspp] / src / language / expressions / helpers.h
1 /*
2 PSPP - a program for statistical analysis.
3 Copyright (C) 2017 Free Software Foundation, Inc.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU 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, see <http://www.gnu.org/licenses/>.
17 */
18
19 #ifndef EXPRESSIONS_HELPERS_H
20 #define EXPRESSIONS_HELPERS_H
21
22 #include <ctype.h>
23 #include <float.h>
24 #include <gsl/gsl_cdf.h>
25 #include <gsl/gsl_randist.h>
26 #include <gsl/gsl_sf.h>
27 #include <limits.h>
28 #include <math.h>
29 #include <stdbool.h>
30
31 #include "data/calendar.h"
32 #include "data/case.h"
33 #include "data/data-in.h"
34 #include "data/data-out.h"
35 #include "data/dataset.h"
36 #include "data/dictionary.h"
37 #include "data/settings.h"
38 #include "data/value.h"
39 #include "data/variable.h"
40 #include "data/vector.h"
41 #include "language/expressions/public.h"
42 #include "libpspp/assertion.h"
43 #include "libpspp/compiler.h"
44 #include "libpspp/i18n.h"
45 #include "libpspp/message.h"
46 #include "libpspp/misc.h"
47 #include "libpspp/str.h"
48 #include "math/distributions.h"
49 #include "math/moments.h"
50 #include "math/random.h"
51
52 #include "gettext.h"
53 #define _(msgid) gettext (msgid)
54
55 struct expr_node;
56
57 static inline double check_errno (double x)
58 {
59   return errno == 0 ? x : SYSMIS;
60 }
61
62 #define check_errno(EXPRESSION) (errno = 0, check_errno (EXPRESSION))
63
64 #define DAY_S (60. * 60. * 24.)         /* Seconds per day. */
65 #define DAY_H 24.                       /* Hours per day. */
66 #define H_S (60 * 60.)                  /* Seconds per hour. */
67 #define H_MIN 60.                       /* Minutes per hour. */
68 #define MIN_S 60.                       /* Seconds per minute. */
69 #define WEEK_DAY 7.                     /* Days per week. */
70 #define WEEK_S (WEEK_DAY * DAY_S)       /* Seconds per week. */
71
72 extern const struct substring empty_string;
73
74 int compare_string_3way (const struct substring *, const struct substring *);
75
76 double expr_ymd_to_date (int year, int month, int day,
77                          const struct expression *, const struct expr_node *,
78                          int ya, int ma, int da);
79 double expr_ymd_to_ofs (int y, int m, int d,
80                         const struct expression *, const struct expr_node *,
81                         int ya, int ma, int da);
82 double expr_date_difference (double date1, double date2,
83                              struct substring unit, const struct expression *,
84                              const struct expr_node *);
85 double expr_date_sum (double date, double quantity, struct substring unit_name,
86                       struct substring method_name,
87                       const struct expression *, const struct expr_node *);
88 double expr_date_sum_closest (double date, double quantity,
89                               struct substring unit_name,
90                               const struct expression *,
91                               const struct expr_node *);
92
93 struct substring alloc_string (struct expression *, size_t length);
94 struct substring copy_string (struct expression *,
95                               const char *, size_t length);
96
97 static inline bool
98 is_valid (double d)
99 {
100   return isfinite (d) && d != SYSMIS;
101 }
102
103 size_t count_valid (double *, size_t);
104
105 double round_nearest (double x, double mult, double fuzzbits);
106 double round_zero (double x, double mult, double fuzzbits);
107
108 struct substring replace_string (struct expression *,
109                                  struct substring haystack,
110                                  struct substring needle,
111                                  struct substring replacement,
112                                  int n);
113
114 double median (double *, size_t n);
115
116 const struct variable *expr_index_vector (const struct expression *,
117                                           const struct expr_node *,
118                                           const struct vector *, double idx);
119
120 #endif /* expressions/helpers.h */