more tests
[pspp] / src / language / expressions / operations.def
1 // -*- c -*-
2 //
3 // PSPP - a program for statistical analysis.
4 // Copyright (C) 2005, 2006, 2009, 2010, 2011, 2012, 2015, 2016 Free Software Foundation, Inc.
5 //
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 operator NEG (x) = -x;
20
21 operator ADD (a, b) = a + b;
22 operator SUB (a, b) = a - b;
23
24 absorb_miss operator MUL (a, b)
25 = (a == 0. || b == 0. ? 0.
26    : a == SYSMIS || b == SYSMIS ? SYSMIS
27    : a * b);
28
29 absorb_miss operator DIV (a, b)
30 = (a == 0. ? 0.
31    : a == SYSMIS || b == SYSMIS ? SYSMIS
32    : a / b);
33
34 absorb_miss operator POW (a, b)
35 = (a == SYSMIS ? (b == 0. ? 1. : a)
36    : b == SYSMIS ? (a == 0. ? 0. : SYSMIS)
37    : a == 0. && b <= 0. ? SYSMIS
38    : pow (a, b));
39
40 absorb_miss boolean operator AND (boolean a, boolean b)
41 = (a == 0. ? 0.
42    : b == 0. ? 0.
43    : b == SYSMIS ? SYSMIS
44    : a);
45
46 absorb_miss boolean operator OR (boolean a, boolean b)
47 = (a == 1. ? 1.
48    : b == 1. ? 1.
49    : b == SYSMIS ? SYSMIS
50    : a);
51
52 boolean operator NOT (boolean a)
53 = (a == 0. ? 1.
54    : a == 1. ? 0.
55    : SYSMIS);
56
57 // Numeric relational operators.
58 boolean operator EQ (a, b) = a == b;
59 boolean operator GE (a, b) = a >= b;
60 boolean operator GT (a, b) = a > b;
61 boolean operator LE (a, b) = a <= b;
62 boolean operator LT (a, b) = a < b;
63 boolean operator NE (a, b) = a != b;
64
65 // String relational operators.
66 boolean operator EQ_STRING (string a, string b) = compare_string_3way (&a, &b) == 0;
67 boolean operator GE_STRING (string a, string b) = compare_string_3way (&a, &b) >= 0;
68 boolean operator GT_STRING (string a, string b) = compare_string_3way (&a, &b) > 0;
69 boolean operator LE_STRING (string a, string b) = compare_string_3way (&a, &b) <= 0;
70 boolean operator LT_STRING (string a, string b) = compare_string_3way (&a, &b) < 0;
71 boolean operator NE_STRING (string a, string b) = compare_string_3way (&a, &b) != 0;
72
73 // Unary functions.
74 function ABS (x) = fabs (x);
75 extension function ACOS (x >= -1 && x <= 1) = acos (x);
76 function ASIN (x >= -1 && x <= 1) = asin (x);
77 function ATAN (x) = atan (x);
78 extension function ARCOS (x >= -1 && x <= 1) = acos (x);
79 function ARSIN (x >= -1 && x <= 1) = asin (x);
80 function ARTAN (x) = atan (x);
81 function COS (x) = cos (x);
82 function EXP (x) = check_errno (exp (x));
83 function LG10(x) = check_errno (log10 (x));
84 function LN (x) = check_errno (log (x));
85 function LNGAMMA (x >= 0) = gsl_sf_lngamma (x);
86 function MOD10 (x) = fmod (x, 10);
87 function RND (x) = round_nearest (x, 1, 0);
88 function RND (x, mult != 0) = round_nearest (x, mult, 0);
89 function RND (x, mult != 0, fuzzbits >= 0) = round_nearest (x, mult, fuzzbits);
90 function SIN (x) = sin (x);
91 function SQRT (x >= 0) = sqrt (x);
92 function TAN (x) = check_errno (tan (x));
93 function TRUNC (x) = round_zero (x, 1, 0);
94 function TRUNC (x, mult != 0) = round_zero (x, mult, 0);
95 function TRUNC (x, mult != 0, fuzzbits >= 0) = round_zero (x, mult, fuzzbits);
96
97 absorb_miss function MOD (n, d)
98 {
99   if (d != SYSMIS)
100     return n != SYSMIS ? fmod (n, d) : SYSMIS;
101   else
102     return n != 0. ? SYSMIS : 0.;
103 }
104
105 // N-ary numeric functions.
106 absorb_miss boolean function ANY (x != SYSMIS, a[n])
107 {
108   int sysmis = 0;
109   size_t i;
110
111   for (i = 0; i < n; i++)
112     if (a[i] == x)
113       return 1.;
114     else if (a[i] == SYSMIS)
115       sysmis = 1;
116
117   return sysmis ? SYSMIS : 0.;
118 }
119
120 boolean function ANY (string x, string a[n])
121 {
122   size_t i;
123
124   for (i = 0; i < n; i++)
125     if (!compare_string_3way (&x, &a[i]))
126       return 1.;
127   return 0.;
128 }
129
130 function CFVAR.2 (a[n])
131 {
132   double mean, variance;
133
134   moments_of_doubles (a, n, NULL, &mean, &variance, NULL, NULL);
135
136   if (mean == SYSMIS || mean == 0 || variance == SYSMIS)
137     return SYSMIS;
138   else
139     return sqrt (variance) / mean;
140 }
141
142 function MAX.1 (a[n])
143 {
144   double max;
145   size_t i;
146
147   max = -DBL_MAX;
148   for (i = 0; i < n; i++)
149     if (a[i] != SYSMIS && a[i] > max)
150       max = a[i];
151   return max;
152 }
153
154 string function MAX (string a[n])
155 {
156   struct substring *max;
157   size_t i;
158
159   max = &a[0];
160   for (i = 1; i < n; i++)
161     if (compare_string_3way (&a[i], max) > 0)
162       max = &a[i];
163   return *max;
164 }
165
166 function MEAN.1 (a[n])
167 {
168   double mean;
169   moments_of_doubles (a, n, NULL, &mean, NULL, NULL, NULL);
170   return mean;
171 }
172
173 function MEDIAN.1 (a[n])
174 {
175   return median (a, n);
176 }
177
178 function MIN.1 (a[n])
179 {
180   double min;
181   size_t i;
182
183   min = DBL_MAX;
184   for (i = 0; i < n; i++)
185     if (a[i] != SYSMIS && a[i] < min)
186       min = a[i];
187   return min;
188 }
189
190 string function MIN (string a[n])
191 {
192   struct substring *min;
193   size_t i;
194
195   min = &a[0];
196   for (i = 1; i < n; i++)
197     if (compare_string_3way (&a[i], min) < 0)
198       min = &a[i];
199   return *min;
200 }
201
202 absorb_miss function NMISS (a[n])
203 {
204   size_t n_missings = 0;
205   for (size_t i = 0; i < n; i++)
206     n_missings += a[i] == SYSMIS;
207   return n_missings;
208 }
209
210 absorb_miss function NVALID (a[n])
211 {
212   size_t n_valids = 0;
213   for (size_t i = 0; i < n; i++)
214     n_valids += a[i] != SYSMIS;
215   return n_valids;
216 }
217
218 absorb_miss boolean function RANGE (x != SYSMIS, a[n*2])
219 {
220   bool found = false;
221   bool valid = false;
222   for (size_t i = 0; i < n; i++)
223     {
224       double w = a[2 * i];
225       double y = a[2 * i + 1];
226       if (w != SYSMIS && y != SYSMIS)
227         {
228           if (w <= x && x <= y)
229             found = true;
230           else if (w <= y)
231             valid = true;
232           else
233             return SYSMIS;
234         }
235     }
236   return found ? true : valid ? false : SYSMIS;
237 }
238
239 boolean function RANGE (string x, string a[n*2])
240 {
241   bool found = false;
242   for (size_t i = 0; i < n; i++)
243     {
244       struct substring *w = &a[2 * i];
245       struct substring *y = &a[2 * i + 1];
246       if (compare_string_3way (w, &x) <= 0 && compare_string_3way (&x, y) <= 0)
247         found = true;
248       else if (compare_string_3way (w, y) > 0)
249         return SYSMIS;
250     }
251   return found;
252 }
253
254 function SD.2 (a[n])
255 {
256   double variance;
257   moments_of_doubles (a, n, NULL, NULL, &variance, NULL, NULL);
258   return sqrt (variance);
259 }
260
261 function SUM.1 (a[n])
262 {
263   double sum;
264   size_t i;
265
266   sum = 0.;
267   for (i = 0; i < n; i++)
268     if (a[i] != SYSMIS)
269       sum += a[i];
270   return sum;
271 }
272
273 function VARIANCE.2 (a[n])
274 {
275   double variance;
276   moments_of_doubles (a, n, NULL, NULL, &variance, NULL, NULL);
277   return variance;
278 }
279
280 // Time construction & extraction functions.
281 function TIME.HMS (h, m, s)
282   expression e;
283   expr_node n;
284 {
285   if ((h > 0. || m > 0. || s > 0.) && (h < 0. || m < 0. || s < 0.))
286     {
287       msg_at (SW, expr_location (e, n),
288               _("TIME.HMS cannot accept a mix of positive and negative "
289                 "arguments."));
290       double args[] = { h, m, s };
291       for (size_t i = 0; i < 3; i++)
292         if (args[i] > 0)
293           msg_at (SN, expr_location (e, n->args[i]),
294                   _("This argument has positive value %g."), args[i]);
295         else if (args[i] < 0)
296           msg_at (SN, expr_location (e, n->args[i]),
297                   _("This argument has negative value %g."), args[i]);
298       return SYSMIS;
299     }
300   else
301     return H_S * h + MIN_S * m + s;
302 }
303 function TIME.DAYS (days) = days * DAY_S;
304 function CTIME.DAYS (time) = time / DAY_S;
305 function CTIME.HOURS (time) = time / H_S;
306 function CTIME.MINUTES (time) = time / MIN_S;
307 function CTIME.SECONDS (time) = time;
308
309 // Date construction functions.
310 function DATE.DMY (integer d, integer m, integer y)
311   expression e;
312   expr_node n;
313 = expr_ymd_to_date (y, m, d, e, n, 3, 2, 1);
314
315 function DATE.MDY (integer m, integer d, integer y)
316   expression e;
317   expr_node n;
318 = expr_ymd_to_date (y, m, d, e, n, 3, 1, 2);
319
320 function DATE.MOYR (integer m, integer y)
321   expression e;
322   expr_node n;
323 = expr_ymd_to_date (y, m, 1, e, n, 2, 1, 0);
324
325 function DATE.QYR (integer q, integer y)
326   expression e;
327   expr_node n;
328 {
329   if (q < 1 || q > 4)
330     {
331       msg_at (SW, expr_location (e, n->args[0]),
332               _("Argument 1 to DATE.QYR must be 1, 2, 3, or 4 (not %d)."), q);
333       return SYSMIS;
334     }
335   return expr_ymd_to_date (y, q * 3 - 2, 1, e, n, 2, 0, 0);
336 }
337
338 function DATE.WKYR (integer w, integer y)
339   expression e;
340   expr_node n;
341 {
342   if (w < 1 || w > 53)
343     {
344       msg_at (SE, expr_location (e, n->args[0]),
345               _("The week argument to DATE.WKYR is outside the acceptable "
346                 "range of 1 to 53.  The result will be system-missing."));
347       return SYSMIS;
348     }
349   else
350     {
351       double yr_1_1 = expr_ymd_to_ofs (y, 1, 1, e, n, 2, 0, 0);
352       if (yr_1_1 != SYSMIS)
353         return DAY_S * (yr_1_1 + WEEK_DAY * (w - 1));
354       else
355         return SYSMIS;
356     }
357 }
358
359 function DATE.YRDAY (integer y, integer yd)
360   expression e;
361   expr_node n;
362 {
363   if (yd < 1 || yd > 366)
364     {
365       msg_at (SE, expr_location (e, n->args[1]),
366               _("DATE.YRDAY day argument %d is outside the acceptable "
367                 "range of 1 to 366.  The result will be system-missing."), yd);
368       return SYSMIS;
369     }
370   else
371     {
372       double yr_1_1 = expr_ymd_to_ofs (y, 1, 1, e, n, 1, 0, 0);
373       if (yr_1_1 != SYSMIS)
374         return DAY_S * (yr_1_1 + yd - 1.);
375       else
376         return SYSMIS;
377     }
378 }
379
380 function YRMODA (integer y, integer m, integer d)
381   expression e;
382   expr_node n;
383 {
384   if (y >= 0 && y <= 99)
385     y += 1900;
386   else if (y > 47516)
387     {
388       msg_at (SE, expr_location (e, n->args[0]),
389               _("The year argument to YRMODA is greater than 47516.  "
390                 "The result will be system-missing."));
391       return SYSMIS;
392     }
393
394   return expr_ymd_to_ofs (y, m, d, e, n, 1, 2, 3);
395 }
396
397 // Date extraction functions.
398 function XDATE.TDAY (date) = floor (date / DAY_S);
399 function XDATE.HOUR (date) = fmod (floor (date / H_S), DAY_H);
400 function XDATE.MINUTE (date) = fmod (floor (date / H_MIN), H_MIN);
401 function XDATE.SECOND (date) = fmod (date, MIN_S);
402 function XDATE.DATE (date) = floor (date / DAY_S) * DAY_S;
403 function XDATE.TIME (date) = fmod (date, DAY_S);
404
405 function XDATE.JDAY (date >= DAY_S) = calendar_offset_to_yday (date / DAY_S);
406 function XDATE.MDAY (date >= DAY_S) = calendar_offset_to_mday (date / DAY_S);
407 function XDATE.MONTH (date >= DAY_S)
408      = calendar_offset_to_month (date / DAY_S);
409 function XDATE.QUARTER (date >= DAY_S)
410     = (calendar_offset_to_month (date / DAY_S) - 1) / 3 + 1;
411 function XDATE.WEEK (date >= DAY_S)
412     = (calendar_offset_to_yday (date / DAY_S) - 1) / 7 + 1;
413 function XDATE.WKDAY (date >= DAY_S) = calendar_offset_to_wday (date / DAY_S);
414 function XDATE.YEAR (date >= DAY_S) = calendar_offset_to_year (date / DAY_S);
415
416 // Date arithmetic functions.
417 no_abbrev function DATEDIFF (date2 >= DAY_S, date1 >= DAY_S, string unit)
418   expression e;
419   expr_node n;
420 = expr_date_difference (date1, date2, unit, e, n);
421
422 no_abbrev function DATESUM (date, quantity, string unit)
423   expression e;
424   expr_node n;
425 = expr_date_sum_closest (date, quantity, unit, e, n);
426 no_abbrev function DATESUM (date, quantity, string unit, string method)
427   expression e;
428   expr_node n;
429 = expr_date_sum (date, quantity, unit, method, e, n);
430
431
432 // String functions.
433 string function CONCAT (string a[n])
434      expression e;
435 {
436   struct substring dst;
437   size_t i;
438
439   dst = alloc_string (e, MAX_STRING);
440   dst.length = 0;
441   for (i = 0; i < n; i++)
442     {
443       struct substring *src = &a[i];
444       size_t copy_len;
445
446       copy_len = src->length;
447       if (dst.length + copy_len > MAX_STRING)
448         copy_len = MAX_STRING - dst.length;
449       memcpy (&dst.string[dst.length], src->string, copy_len);
450       dst.length += copy_len;
451     }
452
453   return dst;
454 }
455
456 function INDEX (string haystack, string needle)
457 {
458   if (haystack.length >= needle.length)
459     {
460       size_t limit = haystack.length - needle.length + 1;
461       for (size_t i = 1; i <= limit; i++)
462         if (!memcmp (&haystack.string[i - 1], needle.string, needle.length))
463           return i;
464     }
465   return 0;
466 }
467
468 function INDEX (string haystack, string needles, integer needle_len)
469   expression e;
470   expr_node n;
471 {
472   if (needle_len <= 0 || needles.length % needle_len != 0)
473     {
474       msg_at (SE, expr_location (e, n),
475               _("INDEX needle length argument must evenly divide the "
476                 "length of the needles argument."));
477       msg_at (SN, expr_location (e, n->args[1]),
478               _("The needles argument has length %zu."), needles.length);
479       msg_at (SN, expr_location (e, n->args[2]),
480               _("The needle length argument has value %d."), needle_len);
481       return SYSMIS;
482     }
483
484   if (haystack.length >= needle_len)
485     {
486       size_t limit = haystack.length - needle_len + 1;
487       for (size_t i = 1; i <= limit; i++)
488         for (size_t j = 0; j < needles.length; j += needle_len)
489           if (!memcmp (&haystack.string[i - 1], &needles.string[j], needle_len))
490             return i;
491     }
492
493   return 0;
494 }
495
496 function RINDEX (string haystack, string needle)
497 {
498   if (haystack.length >= needle.length)
499     {
500       size_t limit = haystack.length - needle.length + 1;
501       for (size_t i = limit; i >= 1; i--)
502         if (!memcmp (&haystack.string[i - 1], needle.string, needle.length))
503           return i;
504     }
505
506   return 0;
507 }
508
509 function RINDEX (string haystack, string needles, integer needle_len)
510   expression e;
511   expr_node n;
512 {
513   if (needle_len <= 0 || needles.length % needle_len != 0)
514     {
515       msg_at (SE, expr_location (e, n),
516               _("RINDEX needle length argument must evenly divide the "
517                 "length of the needles argument."));
518       msg_at (SN, expr_location (e, n->args[1]),
519               _("The needles argument has length %zu."), needles.length);
520       msg_at (SN, expr_location (e, n->args[2]),
521               _("The needle length argument has value %d."), needle_len);
522       return SYSMIS;
523     }
524
525   if (haystack.length >= needle_len)
526     {
527       size_t limit = haystack.length - needle_len + 1;
528       for (size_t i = limit; i >= 1; i--)
529         for (size_t j = 0; j < needles.length; j += needle_len)
530           if (!memcmp (&haystack.string[i - 1], &needles.string[j], needle_len))
531             return i;
532     }
533
534   return 0;
535 }
536
537 function LENGTH (string s)
538 {
539   return s.length;
540 }
541
542 string function LOWER (string s)
543 {
544   int i;
545
546   for (i = 0; i < s.length; i++)
547     s.string[i] = tolower ((unsigned char) s.string[i]);
548   return s;
549 }
550
551 function MBLEN.BYTE (string s, idx)
552 {
553   if (idx < 0 || idx >= s.length || (int) idx != idx)
554     return SYSMIS;
555   else
556     return 1;
557 }
558
559 string function UPCASE (string s)
560 {
561   int i;
562
563   for (i = 0; i < s.length; i++)
564     s.string[i] = toupper ((unsigned char) s.string[i]);
565   return s;
566 }
567
568 absorb_miss string function LPAD (string s, integer n)
569      expression e;
570      expr_node node;
571 {
572   if (n < 0 || n > MAX_STRING)
573     {
574       if (n != INT_MIN)
575         {
576           msg_at (SE, expr_location (e, node),
577                   _("The length argument to LPAD must be between 0 and %d."),
578                   MAX_STRING);
579           msg_at (SN, expr_location (e, node->args[1]),
580                   _("The length argument is %d."), n);
581         }
582
583       return s;
584     }
585   else if (s.length >= n)
586     return s;
587   else
588     {
589       struct substring t = alloc_string (e, n);
590       size_t pad = n - s.length;
591       memset (t.string, ' ', pad);
592       memcpy (&t.string[pad], s.string, s.length);
593       return t;
594     }
595 }
596
597 absorb_miss string function LPAD (string s, integer n, string c)
598      expression e;
599      expr_node node;
600 {
601   if (n < 0 || n > MAX_STRING)
602     {
603       if (n != INT_MIN)
604         {
605           msg_at (SE, expr_location (e, node),
606                   _("The length argument to LPAD must be between 0 and %d."),
607                   MAX_STRING);
608           msg_at (SN, expr_location (e, node->args[1]),
609                   _("The length argument is %d."), n);
610         }
611
612       return s;
613     }
614   else if (s.length >= n)
615     return s;
616   else if (c.length == 0)
617     {
618       msg_at (SE, expr_location (e, node),
619               _("The padding argument to LPAD must not be an empty string."));
620       return s;
621     }
622   else
623     {
624       size_t n_pad = (n - s.length) / c.length;
625       if (!n_pad)
626         return s;
627
628       struct substring t = alloc_string (e, n);
629       t.length = 0;
630       for (size_t i = 0; i < n_pad; i++)
631         {
632           memcpy (t.string + t.length, c.string, c.length);
633           t.length += c.length;
634         }
635       memcpy (t.string + t.length, s.string, s.length);
636       t.length += s.length;
637       return t;
638     }
639 }
640
641 string function REPLACE (string haystack, string needle, string replacement)
642     expression e;
643   = replace_string (e, haystack, needle, replacement, INT_MAX);
644
645 absorb_miss string function REPLACE (string haystack, string needle,
646                          string replacement, integer n)
647     expression e;
648   = replace_string (e, haystack, needle, replacement, n);
649
650 absorb_miss string function RPAD (string s, integer n)
651      expression e;
652      expr_node node;
653 {
654   if (n < 0 || n > MAX_STRING)
655     {
656       if (n != INT_MIN)
657         {
658           msg_at (SE, expr_location (e, node),
659                   _("The length argument to RPAD must be between 0 and %d."),
660                   MAX_STRING);
661           msg_at (SN, expr_location (e, node->args[1]),
662                   _("The length argument is %d."), n);
663         }
664
665       return s;
666     }
667   else if (s.length >= n)
668     return s;
669   else
670     {
671       struct substring t = alloc_string (e, n);
672       size_t pad = n - s.length;
673       memcpy (t.string, s.string, s.length);
674       memset (t.string + s.length, ' ', pad);
675       return t;
676     }
677 }
678
679 absorb_miss string function RPAD (string s, integer n, string c)
680      expression e;
681      expr_node node;
682 {
683   if (n < 0 || n > MAX_STRING)
684     {
685       if (n != INT_MIN)
686         {
687           msg_at (SE, expr_location (e, node),
688                   _("The length argument to RPAD must be between 0 and %d."),
689                   MAX_STRING);
690           msg_at (SN, expr_location (e, node->args[1]),
691                   _("The length argument is %d."), n);
692         }
693
694       return s;
695     }
696   else if (s.length >= n)
697     return s;
698   else if (c.length == 0)
699     {
700       msg_at (SE, expr_location (e, node),
701               _("The padding argument to RPAD must not be an empty string."));
702       return s;
703     }
704   else
705     {
706       size_t n_pad = (n - s.length) / c.length;
707       if (!n_pad)
708         return s;
709
710       struct substring t = alloc_string (e, n);
711       memcpy (t.string, s.string, s.length);
712       t.length = s.length;
713       for (size_t i = 0; i < n_pad; i++)
714         {
715           memcpy (t.string + t.length, c.string, c.length);
716           t.length += c.length;
717         }
718       return t;
719     }
720 }
721
722 string function LTRIM (string s)
723 {
724   while (s.length > 0 && s.string[0] == ' ')
725     {
726       s.length--;
727       s.string++;
728     }
729   return s;
730 }
731
732 string function LTRIM (string s, string c)
733 {
734   if (c.length > 0)
735     while (s.length >= c.length && !memcmp (s.string, c.string, c.length))
736       {
737         s.length -= c.length;
738         s.string += c.length;
739       }
740   return s;
741 }
742
743 string function RTRIM (string s)
744 {
745   while (s.length > 0 && s.string[s.length - 1] == ' ')
746     s.length--;
747   return s;
748 }
749
750 string function RTRIM (string s, string c)
751 {
752   if (c.length > 0)
753     while (s.length >= c.length
754            && !memcmp (&s.string[s.length - c.length], c.string, c.length))
755       s.length -= c.length;
756   return s;
757 }
758
759 function NUMBER (string s, ni_format f)
760   expression e;
761   expr_node n;
762 {
763   if (s.length > f->w)
764     s.length = f->w;
765
766   union value out;
767   char *error = data_in (s, C_ENCODING, f->type, settings_get_fmt_settings (),
768                          &out, 0, NULL);
769   if (error == NULL)
770     data_in_imply_decimals (s, C_ENCODING, f->type, f->d,
771                             settings_get_fmt_settings (), &out);
772   else
773     {
774       msg_at (SE, expr_location (e, n->args[0]),
775               _("Cannot parse \"%.*s\" as format %s: %s"),
776               (int) s.length, s.string, fmt_name (f->type), error);
777       free (error);
778     }
779   return out.f;
780 }
781
782 absorb_miss string function STRING (x, no_format f)
783      expression e;
784 {
785   union value v;
786   struct substring dst;
787   char *s;
788
789   v.f = x;
790
791   assert (!fmt_is_string (f->type));
792   s = data_out (&v, C_ENCODING, f, settings_get_fmt_settings ());
793   dst = alloc_string (e, strlen (s));
794   strcpy (dst.string, s);
795   free (s);
796   return dst;
797 }
798
799 absorb_miss string function STRUNC (string s, integer n)
800 {
801   if (n < 1)
802     return n == INT_MIN ? s : empty_string;
803
804   if (n < s.length)
805     s.length = n;
806   while (s.length > 0 && s.string[s.length - 1] == ' ')
807     s.length--;
808   return s;
809 }
810
811 absorb_miss string function SUBSTR (string s, ofs)
812      expression e;
813 {
814   if (ofs >= 1 && ofs <= s.length && (int) ofs == ofs)
815     return copy_string (e, &s.string[(int) ofs - 1], s.length - ofs + 1);
816   else
817     return empty_string;
818 }
819
820 absorb_miss string function SUBSTR (string s, ofs, cnt)
821      expression e;
822 {
823   if (ofs >= 1 && ofs <= s.length && (int) ofs == ofs
824       && cnt >= 1 && cnt <= INT_MAX && (int) cnt == cnt)
825     {
826       int cnt_max = s.length - (int) ofs + 1;
827       return copy_string (e, &s.string[(int) ofs - 1],
828                           cnt <= cnt_max ? cnt : cnt_max);
829     }
830   else
831     return empty_string;
832 }
833
834 absorb_miss no_opt no_abbrev string function VALUELABEL (var v)
835      expression e;
836      case c;
837 {
838   const char *label = var_lookup_value_label (v, case_data (c, v));
839   if (label != NULL)
840     return copy_string (e, label, strlen (label));
841   else
842     return empty_string;
843 }
844
845 // Artificial.
846 operator SQUARE (x) = x * x;
847
848 absorb_miss boolean operator OPERAND_TO_BOOLEAN (x, expr_node parent)
849   expression e;
850   expr_node n;
851 {
852   if (x == 0. || x == 1. || x == SYSMIS)
853     return x;
854
855   switch (parent->n_args)
856     {
857     case 2:
858       msg_at (SE, expr_location (e, parent),
859               /* TRANSLATORS: There are exactly two operands. */
860               _("The operands of %s must have value 0 or 1."),
861               operations[parent->type].name);
862       break;
863
864     case 1:
865       msg_at (SE, expr_location (e, parent),
866               _("The operand of %s must have value 0 or 1."),
867               operations[parent->type].name);
868       break;
869
870     default:
871       NOT_REACHED ();
872     }
873
874   msg_at (SN, expr_location (e, n),
875           _("This operand with unexpected value %g will be treated as 0."), x);
876   return 0.;
877 }
878
879 absorb_miss boolean operator EXPR_TO_BOOLEAN (x)
880   expression e;
881   expr_node n;
882 {
883   if (x == 0. || x == 1. || x == SYSMIS)
884     return x;
885
886   msg_at (SE, expr_location (e, n),
887           _("This expression, which must be 0 or 1, evaluated to %g.  "
888             "It will be treated as 0."), x);
889   return 0.;
890 }
891
892 operator NUM_TO_INTEGER (x)
893   expression e;
894   expr_node n;
895 {
896   if (x == floor (x) && x > INT_MIN && x <= INT_MAX)
897     return x;
898
899   msg_at (SE, expr_location (e, n),
900           _("Treating unexpected non-integer value %g as missing."), x);
901   return SYSMIS;
902 }
903
904 operator BOOLEAN_TO_NUM (boolean x) = x;
905
906 // Beta distribution.
907 function PDF.BETA (x >= 0 && x <= 1, a > 0, b > 0)
908      = gsl_ran_beta_pdf (x, a, b);
909 function CDF.BETA (x >= 0 && x <= 1, a > 0, b > 0) = gsl_cdf_beta_P (x, a, b);
910 function IDF.BETA (P >= 0 && P <= 1, a > 0, b > 0)
911      = gsl_cdf_beta_Pinv (P, a, b);
912 no_opt function RV.BETA (a > 0, b > 0) = gsl_ran_beta (get_rng (), a, b);
913 function NCDF.BETA (x >= 0, a > 0, b > 0, lambda > 0)
914      = ncdf_beta (x, a, b, lambda);
915 function NPDF.BETA (x >= 0, a > 0, b > 0, lambda > 0)
916      = npdf_beta (x, a, b, lambda);
917
918 // Bivariate normal distribution.
919 function CDF.BVNOR (x0, x1, r >= -1 && r <= 1) = cdf_bvnor (x0, x1, r);
920 function PDF.BVNOR (x0, x1, r >= -1 && r <= 1)
921      = gsl_ran_bivariate_gaussian_pdf (x0, x1, 1, 1, r);
922
923 // Cauchy distribution.
924 function CDF.CAUCHY (x, a, b > 0) = gsl_cdf_cauchy_P ((x - a) / b, 1);
925 function IDF.CAUCHY (P > 0 && P < 1, a, b > 0)
926      = a + b * gsl_cdf_cauchy_Pinv (P, 1);
927 function PDF.CAUCHY (x, a, b > 0) = gsl_ran_cauchy_pdf ((x - a) / b, 1) / b;
928 no_opt function RV.CAUCHY (a, b > 0) = a + b * gsl_ran_cauchy (get_rng (), 1);
929
930 // Chi-square distribution.
931 function CDF.CHISQ (x >= 0, df > 0) = gsl_cdf_chisq_P (x, df);
932 function IDF.CHISQ (P >= 0 && P < 1, df > 0) = gsl_cdf_chisq_Pinv (P, df);
933 function PDF.CHISQ (x >= 0, df > 0) = gsl_ran_chisq_pdf (x, df);
934 no_opt function RV.CHISQ (df > 0) = gsl_ran_chisq (get_rng (), df);
935 function NCDF.CHISQ (x >= 0, df > 0, c) = unimplemented;
936 function NPDF.CHISQ (x >= 0, df > 0, c) = unimplemented;
937 function SIG.CHISQ (x >= 0, df > 0) = gsl_cdf_chisq_Q (x, df);
938
939 // Exponential distribution.
940 function CDF.EXP (x >= 0, a > 0) = gsl_cdf_exponential_P (x, 1. / a);
941 function IDF.EXP (P >= 0 && P < 1, a > 0)
942      = gsl_cdf_exponential_Pinv (P, 1. / a);
943 function PDF.EXP (x >= 0, a > 0) = gsl_ran_exponential_pdf (x, 1. / a);
944 no_opt function RV.EXP (a > 0) = gsl_ran_exponential (get_rng (), 1. / a);
945
946 // Exponential power distribution.
947 extension function PDF.XPOWER (x, a > 0, b >= 0)
948      = gsl_ran_exppow_pdf (x, a, b);
949 no_opt extension function RV.XPOWER (a > 0, b >= 0)
950      = gsl_ran_exppow (get_rng (), a, b);
951
952 // F distribution.
953 function CDF.F (x >= 0, df1 > 0, df2 > 0) = gsl_cdf_fdist_P (x, df1, df2);
954 function IDF.F (P >= 0 && P < 1, df1 > 0, df2 > 0) = idf_fdist (P, df1, df2);
955 function PDF.F (x >= 0, df1 > 0, df2 > 0) = gsl_ran_fdist_pdf (x, df1, df2);
956 no_opt function RV.F (df1 > 0, df2 > 0) = gsl_ran_fdist (get_rng (), df1, df2);
957 function NCDF.F (x >= 0, df1 > 0, df2 > 0, lambda >= 0) = unimplemented;
958 function NPDF.F (x >= 0, df1 > 0, df2 > 0, lmabda >= 0) = unimplemented;
959 function SIG.F (x >= 0, df1 > 0, df2 > 0) = gsl_cdf_fdist_Q (x, df1, df2);
960
961 // Gamma distribution.
962 function CDF.GAMMA (x >= 0, a > 0, b > 0) = gsl_cdf_gamma_P (x, a, 1. / b);
963 function IDF.GAMMA (P >= 0 && P <= 1, a > 0, b > 0)
964      = gsl_cdf_gamma_Pinv (P, a, 1. / b);
965 function PDF.GAMMA (x >= 0, a > 0, b > 0) = gsl_ran_gamma_pdf (x, a, 1. / b);
966 no_opt function RV.GAMMA (a > 0, b > 0)
967      = gsl_ran_gamma (get_rng (), a, 1. / b);
968
969 // Half-normal distribution.
970 function CDF.HALFNRM (x, a, b > 0) = unimplemented;
971 function IDF.HALFNRM (P > 0 && P < 1, a, b > 0) = unimplemented;
972 function PDF.HALFNRM (x, a, b > 0) = unimplemented;
973 no_opt function RV.HALFNRM (a, b > 0) = unimplemented;
974
975 // Inverse Gaussian distribution.
976 function CDF.IGAUSS (x > 0, a > 0, b > 0) = unimplemented;
977 function IDF.IGAUSS (P >= 0 && P < 1, a > 0, b > 0) = unimplemented;
978 function PDF.IGAUSS (x > 0, a > 0, b > 0) = unimplemented;
979 no_opt function RV.IGAUSS (a > 0, b > 0) = unimplemented;
980
981 // Landau distribution.
982 extension function PDF.LANDAU (x) = gsl_ran_landau_pdf (x);
983 no_opt extension function RV.LANDAU () = gsl_ran_landau (get_rng ());
984
985 // Laplace distribution.
986 function CDF.LAPLACE (x, a, b > 0) = gsl_cdf_laplace_P ((x - a) / b, 1);
987 function IDF.LAPLACE (P > 0 && P < 1, a, b > 0)
988      = a + b * gsl_cdf_laplace_Pinv (P, 1);
989 function PDF.LAPLACE (x, a, b > 0) = gsl_ran_laplace_pdf ((x - a) / b, 1) / b;
990 no_opt function RV.LAPLACE (a, b > 0)
991      = a + b * gsl_ran_laplace (get_rng (), 1);
992
993 // Levy alpha-stable distribution.
994 no_opt extension function RV.LEVY (c, alpha > 0 && alpha <= 2)
995      = gsl_ran_levy (get_rng (), c, alpha);
996
997 // Levy skew alpha-stable distribution.
998 no_opt extension function RV.LVSKEW (c, alpha > 0 && alpha <= 2,
999                                      beta >= -1 && beta <= 1)
1000      = gsl_ran_levy_skew (get_rng (), c, alpha, beta);
1001
1002 // Logistic distribution.
1003 function CDF.LOGISTIC (x, a, b > 0) = gsl_cdf_logistic_P ((x - a) / b, 1);
1004 function IDF.LOGISTIC (P > 0 && P < 1, a, b > 0)
1005      = a + b * gsl_cdf_logistic_Pinv (P, 1);
1006 function PDF.LOGISTIC (x, a, b > 0)
1007      = gsl_ran_logistic_pdf ((x - a) / b, 1) / b;
1008 no_opt function RV.LOGISTIC (a, b > 0)
1009      = a + b * gsl_ran_logistic (get_rng (), 1);
1010
1011 // Lognormal distribution.
1012 function CDF.LNORMAL (x >= 0, m > 0, s > 0)
1013      = gsl_cdf_lognormal_P (x, log (m), s);
1014 function IDF.LNORMAL (P >= 0 && P < 1, m > 0, s > 0)
1015      = gsl_cdf_lognormal_Pinv (P, log (m), s);
1016 function PDF.LNORMAL (x >= 0, m > 0, s > 0)
1017      = gsl_ran_lognormal_pdf (x, log (m), s);
1018 no_opt function RV.LNORMAL (m > 0, s > 0)
1019      = gsl_ran_lognormal (get_rng (), log (m), s);
1020
1021 // Normal distribution.
1022 function CDF.NORMAL (x, u, s > 0) = gsl_cdf_gaussian_P (x - u, s);
1023 function IDF.NORMAL (P > 0 && P < 1, u, s > 0)
1024      = u + gsl_cdf_gaussian_Pinv (P, s);
1025 function PDF.NORMAL (x, u, s > 0) = gsl_ran_gaussian_pdf ((x - u) / s, 1) / s;
1026 no_opt function RV.NORMAL (u, s > 0) = u + gsl_ran_gaussian (get_rng (), s);
1027 function CDFNORM (x) = gsl_cdf_ugaussian_P (x);
1028 function PROBIT (P > 0 && P < 1) = gsl_cdf_ugaussian_Pinv (P);
1029 no_opt function NORMAL (s > 0) = gsl_ran_gaussian (get_rng (), s);
1030
1031 // Normal tail distribution.
1032 function PDF.NTAIL (x, a > 0, sigma > 0)
1033      = gsl_ran_gaussian_tail_pdf (x, a, sigma);
1034 no_opt function RV.NTAIL (a > 0, sigma > 0)
1035      = gsl_ran_gaussian_tail (get_rng (), a, sigma);
1036
1037 // Pareto distribution.
1038 function CDF.PARETO (x >= a, a > 0, b > 0) = gsl_cdf_pareto_P (x, b, a);
1039 function IDF.PARETO (P >= 0 && P < 1, a > 0, b > 0)
1040      = gsl_cdf_pareto_Pinv (P, b, a);
1041 function PDF.PARETO (x >= a, a > 0, b > 0) = gsl_ran_pareto_pdf (x, b, a);
1042 no_opt function RV.PARETO (a > 0, b > 0) = gsl_ran_pareto (get_rng (), b, a);
1043
1044 // Rayleigh distribution.
1045 extension function CDF.RAYLEIGH (x, sigma > 0) = gsl_cdf_rayleigh_P (x, sigma);
1046 extension function IDF.RAYLEIGH (P >= 0 && P <= 1, sigma > 0)
1047      = gsl_cdf_rayleigh_Pinv (P, sigma);
1048 extension function PDF.RAYLEIGH (x, sigma > 0)
1049      = gsl_ran_rayleigh_pdf (x, sigma);
1050 no_opt extension function RV.RAYLEIGH (sigma > 0)
1051      = gsl_ran_rayleigh (get_rng (), sigma);
1052
1053 // Rayleigh tail distribution.
1054 extension function PDF.RTAIL (x, a, sigma)
1055      = gsl_ran_rayleigh_tail_pdf (x, a, sigma);
1056 no_opt extension function RV.RTAIL (a, sigma)
1057      = gsl_ran_rayleigh_tail (get_rng (), a, sigma);
1058
1059 // Studentized maximum modulus distribution.
1060 function CDF.SMOD (x > 0, a >= 1, b >= 1) = unimplemented;
1061 function IDF.SMOD (P >= 0 && P < 1, a >= 1, b >= 1) = unimplemented;
1062
1063 // Studentized range distribution.
1064 function CDF.SRANGE (x > 0, a >= 1, b >= 1) = unimplemented;
1065 function IDF.SRANGE (P >= 0 && P < 1, a >= 1, b >= 1) = unimplemented;
1066
1067 // Student t distribution.
1068 function CDF.T (x, df > 0) = gsl_cdf_tdist_P (x, df);
1069 function IDF.T (P > 0 && P < 1, df > 0) = gsl_cdf_tdist_Pinv (P, df);
1070 function PDF.T (x, df > 0) = gsl_ran_tdist_pdf (x, df);
1071 no_opt function RV.T (df > 0) = gsl_ran_tdist (get_rng (), df);
1072 function NCDF.T (x, df > 0, nc) = unimplemented;
1073 function NPDF.T (x, df > 0, nc) = unimplemented;
1074
1075 // Type-1 Gumbel distribution.
1076 extension function CDF.T1G (x, a, b) = gsl_cdf_gumbel1_P (x, a, b);
1077 extension function IDF.T1G (P >= 0 && P <= 1, a, b)
1078      = gsl_cdf_gumbel1_Pinv (P, a, b);
1079 extension function PDF.T1G (x, a, b) = gsl_ran_gumbel1_pdf (x, a, b);
1080 no_opt extension function RV.T1G (a, b) = gsl_ran_gumbel1 (get_rng (), a, b);
1081
1082 // Type-2 Gumbel distribution.
1083 extension function CDF.T2G (x, a, b) = gsl_cdf_gumbel2_P (x, a, b);
1084 extension function IDF.T2G (P >= 0 && P <= 1, a, b)
1085      = gsl_cdf_gumbel2_Pinv (P, a, b);
1086 extension function PDF.T2G (x, a, b) = gsl_ran_gumbel2_pdf (x, a, b);
1087 no_opt extension function RV.T2G (a, b) = gsl_ran_gumbel2 (get_rng (), a, b);
1088
1089 // Uniform distribution.
1090 function CDF.UNIFORM (x <= b, a <= x, b) = gsl_cdf_flat_P (x, a, b);
1091 function IDF.UNIFORM (P >= 0 && P <= 1, a <= b, b)
1092      = gsl_cdf_flat_Pinv (P, a, b);
1093 function PDF.UNIFORM (x <= b, a <= x, b) = gsl_ran_flat_pdf (x, a, b);
1094 no_opt function RV.UNIFORM (a <= b, b) = gsl_ran_flat (get_rng (), a, b);
1095 no_opt function UNIFORM (b >= 0) = gsl_ran_flat (get_rng (), 0, b);
1096
1097 // Weibull distribution.
1098 function CDF.WEIBULL (x >= 0, a > 0, b > 0) = gsl_cdf_weibull_P (x, a, b);
1099 function IDF.WEIBULL (P >= 0 && P < 1, a > 0, b > 0)
1100      = gsl_cdf_weibull_Pinv (P, a, b);
1101 function PDF.WEIBULL (x >= 0, a > 0, b > 0) = gsl_ran_weibull_pdf (x, a, b);
1102 no_opt function RV.WEIBULL (a > 0, b > 0) = gsl_ran_weibull (get_rng (), a, b);
1103
1104 // Bernoulli distribution.
1105 function CDF.BERNOULLI (k == 0 || k == 1, p >= 0 && p <= 1)
1106      = k ? 1 : 1 - p;
1107 function PDF.BERNOULLI (k == 0 || k == 1, p >= 0 && p <= 1)
1108      = gsl_ran_bernoulli_pdf (k, p);
1109 no_opt function RV.BERNOULLI (p >= 0 && p <= 1)
1110      = gsl_ran_bernoulli (get_rng (), p);
1111
1112 // Binomial distribution.
1113 function CDF.BINOM (k, n > 0 && n == floor (n), p >= 0 && p <= 1)
1114      = gsl_cdf_binomial_P (k, p, n);
1115 function PDF.BINOM (k >= 0 && k == floor (k) && k <= n,
1116                     n > 0 && n == floor (n),
1117                     p >= 0 && p <= 1)
1118      = gsl_ran_binomial_pdf (k, p, n);
1119 no_opt function RV.BINOM (p > 0 && p == floor (p), n >= 0 && n <= 1)
1120      = gsl_ran_binomial (get_rng (), p, n);
1121
1122 // Geometric distribution.
1123 function CDF.GEOM (k >= 1 && k == floor (k), p >= 0 && p <= 1)
1124      = gsl_cdf_geometric_P (k, p);
1125 function PDF.GEOM (k >= 1 && k == floor (k),
1126                    p >= 0 && p <= 1)
1127      = gsl_ran_geometric_pdf (k, p);
1128 no_opt function RV.GEOM (p >= 0 && p <= 1) = gsl_ran_geometric (get_rng (), p);
1129
1130 // Hypergeometric distribution.
1131 function CDF.HYPER (k >= 0 && k == floor (k) && k <= c,
1132                     a > 0 && a == floor (a),
1133                     b > 0 && b == floor (b) && b <= a,
1134                     c > 0 && c == floor (c) && c <= a)
1135      = gsl_cdf_hypergeometric_P (k, c, a - c, b);
1136 function PDF.HYPER (k >= 0 && k == floor (k) && k <= c,
1137                     a > 0 && a == floor (a),
1138                     b > 0 && b == floor (b) && b <= a,
1139                     c > 0 && c == floor (c) && c <= a)
1140      = gsl_ran_hypergeometric_pdf (k, c, a - c, b);
1141 no_opt function RV.HYPER (a > 0 && a == floor (a),
1142                           b > 0 && b == floor (b) && b <= a,
1143                           c > 0 && c == floor (c) && c <= a)
1144      = gsl_ran_hypergeometric (get_rng (), c, a - c, b);
1145
1146 // Logarithmic distribution.
1147 extension function PDF.LOG (k >= 1, p > 0 && p <= 1)
1148      = gsl_ran_logarithmic_pdf (k, p);
1149 no_opt extension function RV.LOG (p > 0 && p <= 1)
1150      = gsl_ran_logarithmic (get_rng (), p);
1151
1152 // Negative binomial distribution.
1153 function CDF.NEGBIN (k >= 1, n == floor (n), p > 0 && p <= 1)
1154      = gsl_cdf_negative_binomial_P (k, p, n);
1155 function PDF.NEGBIN (k >= 1, n == floor (n), p > 0 && p <= 1)
1156      = gsl_ran_negative_binomial_pdf (k, p, n);
1157 no_opt function RV.NEGBIN (n == floor (n), p > 0 && p <= 1)
1158      = gsl_ran_negative_binomial (get_rng (), p, n);
1159
1160 // Poisson distribution.
1161 function CDF.POISSON (k >= 0 && k == floor (k), mu > 0)
1162      = gsl_cdf_poisson_P (k, mu);
1163 function PDF.POISSON (k >= 0 && k == floor (k), mu > 0)
1164      = gsl_ran_poisson_pdf (k, mu);
1165 no_opt function RV.POISSON (mu > 0) = gsl_ran_poisson (get_rng (), mu);
1166
1167 // Weirdness.
1168 absorb_miss boolean function MISSING (x) = x == SYSMIS || !isfinite (x);
1169 absorb_miss boolean function SYSMIS (x) = x == SYSMIS || !isfinite (x);
1170 no_opt boolean function SYSMIS (num_var v)
1171      case c;
1172 {
1173   return case_num (c, v) == SYSMIS;
1174 }
1175 no_opt function VALUE (num_var v)
1176      case c;
1177 {
1178   return case_num (c, v);
1179 }
1180 no_opt function VALUE (num_vec_elem v)
1181 {
1182   return v;
1183 }
1184
1185 // A numeric vector element used in a "normal" context, in which a user-missing
1186 // value becomes system-missing.
1187 absorb_miss no_opt operator VEC_ELEM_NUM (idx)
1188      vector v;
1189      case c;
1190      expression e;
1191      expr_node n;
1192 {
1193   const struct variable *var = expr_index_vector (e, n, v, idx);
1194   if (var)
1195     {
1196       double d = case_num (c, var);
1197       if (!var_is_num_missing (var, d, MV_USER))
1198         return d;
1199     }
1200   return SYSMIS;
1201 }
1202
1203 // A numeric vector element used as the argument to the VALUE() function, in
1204 // which a user-missing value retains its value.
1205 //
1206 // All numeric vector elements are initially parsed this way.  In most contexts
1207 // they then get coerced into numbers.
1208 absorb_miss no_opt num_vec_elem operator VEC_ELEM_NUM_RAW (idx)
1209      vector v;
1210      case c;
1211      expression e;
1212      expr_node n;
1213 {
1214   const struct variable *var = expr_index_vector (e, n, v, idx);
1215   return var ? case_num (c, var) : SYSMIS;
1216 }
1217
1218 absorb_miss no_opt string operator VEC_ELEM_STR (idx)
1219      expression e;
1220      vector v;
1221      case c;
1222      expr_node n;
1223 {
1224   const struct variable *var = expr_index_vector (e, n, v, idx);
1225   return (var
1226           ? copy_string (e, CHAR_CAST_BUG (char *, case_str (c, var)),
1227                          var_get_width (var))
1228           : empty_string);
1229 }
1230
1231 // Terminals.
1232
1233 no_opt operator NUM_VAR ()
1234      case c;
1235      num_var v;
1236 {
1237   double d = case_num (c, v);
1238   return !var_is_num_missing (v, d, MV_USER) ? d : SYSMIS;
1239 }
1240
1241 no_opt string operator STR_VAR ()
1242      case c;
1243      expression e;
1244      str_var v;
1245 {
1246   struct substring s = alloc_string (e, var_get_width (v));
1247   memcpy (s.string, case_str (c, v), var_get_width (v));
1248   return s;
1249 }
1250
1251 no_opt perm_only function LAG (num_var v, pos_int n_before)
1252     dataset ds;
1253 {
1254   const struct ccase *c = lagged_case (ds, n_before);
1255   if (c != NULL)
1256     {
1257       double x = case_num (c, v);
1258       return !var_is_num_missing (v, x, MV_USER) ? x : SYSMIS;
1259     }
1260   else
1261     return SYSMIS;
1262 }
1263
1264 no_opt perm_only function LAG (num_var v)
1265     dataset ds;
1266 {
1267   const struct ccase *c = lagged_case (ds, 1);
1268   if (c != NULL)
1269     {
1270       double x = case_num (c, v);
1271       return !var_is_num_missing (v, x, MV_USER) ? x : SYSMIS;
1272     }
1273   else
1274     return SYSMIS;
1275 }
1276
1277 no_opt perm_only string function LAG (str_var v, pos_int n_before)
1278      expression e;
1279      dataset ds;
1280 {
1281   const struct ccase *c = lagged_case (ds, n_before);
1282   if (c != NULL)
1283     return copy_string (e, CHAR_CAST_BUG (char *, case_str (c, v)),
1284                         var_get_width (v));
1285   else
1286     return empty_string;
1287 }
1288
1289 no_opt perm_only string function LAG (str_var v)
1290      expression e;
1291      dataset ds;
1292 {
1293   const struct ccase *c = lagged_case (ds, 1);
1294   if (c != NULL)
1295     return copy_string (e, CHAR_CAST_BUG (char *, case_str (c, v)),
1296                         var_get_width (v));
1297   else
1298     return empty_string;
1299 }
1300
1301 no_opt operator NUM_SYS ()
1302      case c;
1303      num_var v;
1304 {
1305   return case_num (c, v) == SYSMIS;
1306 }
1307
1308 no_opt operator NUM_VAL ()
1309      case c;
1310      num_var v;
1311 {
1312   return case_num (c, v);
1313 }
1314
1315 no_opt operator CASENUM ()
1316      case_idx idx;
1317 {
1318   return idx;
1319 }