build: add `patch` as a bootstrap dependency
[coreutils/ericb.git] / src / expr.c
blobdbeaeba89b7a7609a56cea2445bb9d306b85777e
1 /* expr -- evaluate expressions.
2 Copyright (C) 1986, 1991-1997, 1999-2010 Free Software Foundation, Inc.
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.
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.
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/>. */
17 /* Author: Mike Parker.
18 Modified for arbitrary-precision calculation by James Youngman.
20 This program evaluates expressions. Each token (operator, operand,
21 parenthesis) of the expression must be a seperate argument. The
22 parser used is a reasonably general one, though any incarnation of
23 it is language-specific. It is especially nice for expressions.
25 No parse tree is needed; a new node is evaluated immediately.
26 One function can handle multiple operators all of equal precedence,
27 provided they all associate ((x op x) op x).
29 Define EVAL_TRACE to print an evaluation trace. */
31 #include <config.h>
32 #include <stdio.h>
33 #include <sys/types.h>
34 #include "system.h"
36 #include <regex.h>
37 #include "error.h"
38 #include "long-options.h"
39 #include "quotearg.h"
40 #include "strnumcmp.h"
41 #include "xstrtol.h"
43 /* Various parts of this code assume size_t fits into unsigned long
44 int, the widest unsigned type that GMP supports. */
45 verify (SIZE_MAX <= ULONG_MAX);
47 static void integer_overflow (char) ATTRIBUTE_NORETURN;
49 #ifndef HAVE_GMP
50 # define HAVE_GMP 0
51 #endif
53 #if HAVE_GMP
54 # include <gmp.h>
55 #else
56 /* Approximate gmp.h well enough for expr.c's purposes. */
57 typedef intmax_t mpz_t[1];
58 static void mpz_clear (mpz_t z) { (void) z; }
59 static void mpz_init_set_ui (mpz_t z, unsigned long int i) { z[0] = i; }
60 static int
61 mpz_init_set_str (mpz_t z, char *s, int base)
63 return xstrtoimax (s, NULL, base, z, NULL) == LONGINT_OK ? 0 : -1;
65 static void
66 mpz_add (mpz_t r, mpz_t a0, mpz_t b0)
68 intmax_t a = a0[0];
69 intmax_t b = b0[0];
70 intmax_t val = a + b;
71 if ((val < a) != (b < 0))
72 integer_overflow ('+');
73 r[0] = val;
75 static void
76 mpz_sub (mpz_t r, mpz_t a0, mpz_t b0)
78 intmax_t a = a0[0];
79 intmax_t b = b0[0];
80 intmax_t val = a - b;
81 if ((a < val) != (b < 0))
82 integer_overflow ('-');
83 r[0] = val;
85 static void
86 mpz_mul (mpz_t r, mpz_t a0, mpz_t b0)
88 intmax_t a = a0[0];
89 intmax_t b = b0[0];
90 intmax_t val = a * b;
91 if (! (a == 0 || b == 0
92 || ((val < 0) == ((a < 0) ^ (b < 0)) && val / a == b)))
93 integer_overflow ('*');
94 r[0] = val;
96 static void
97 mpz_tdiv_q (mpz_t r, mpz_t a0, mpz_t b0)
99 intmax_t a = a0[0];
100 intmax_t b = b0[0];
102 /* Some x86-style hosts raise an exception for INT_MIN / -1. */
103 if (a < - INTMAX_MAX && b == -1)
104 integer_overflow ('/');
105 r[0] = a / b;
107 static void
108 mpz_tdiv_r (mpz_t r, mpz_t a0, mpz_t b0)
110 intmax_t a = a0[0];
111 intmax_t b = b0[0];
113 /* Some x86-style hosts raise an exception for INT_MIN % -1. */
114 r[0] = a < - INTMAX_MAX && b == -1 ? 0 : a % b;
116 static char *
117 mpz_get_str (char const *str, int base, mpz_t z)
119 (void) str; (void) base;
120 char buf[INT_BUFSIZE_BOUND (intmax_t)];
121 return xstrdup (imaxtostr (z[0], buf));
123 static int
124 mpz_sgn (mpz_t z)
126 return z[0] < 0 ? -1 : 0 < z[0];
128 static int
129 mpz_fits_ulong_p (mpz_t z)
131 return 0 <= z[0] && z[0] <= ULONG_MAX;
133 static unsigned long int
134 mpz_get_ui (mpz_t z)
136 return z[0];
138 static int
139 mpz_out_str (FILE *stream, int base, mpz_t z)
141 (void) base;
142 char buf[INT_BUFSIZE_BOUND (intmax_t)];
143 return fputs (imaxtostr (z[0], buf), stream) != EOF;
145 #endif
147 /* The official name of this program (e.g., no `g' prefix). */
148 #define PROGRAM_NAME "expr"
150 #define AUTHORS \
151 proper_name ("Mike Parker"), \
152 proper_name ("James Youngman"), \
153 proper_name ("Paul Eggert")
155 /* Exit statuses. */
156 enum
158 /* Invalid expression: e.g., its form does not conform to the
159 grammar for expressions. Our grammar is an extension of the
160 POSIX grammar. */
161 EXPR_INVALID = 2,
163 /* An internal error occurred, e.g., arithmetic overflow, storage
164 exhaustion. */
165 EXPR_FAILURE
168 /* The kinds of value we can have. */
169 enum valtype
171 integer,
172 string
174 typedef enum valtype TYPE;
176 /* A value is.... */
177 struct valinfo
179 TYPE type; /* Which kind. */
180 union
181 { /* The value itself. */
182 mpz_t i;
183 char *s;
184 } u;
186 typedef struct valinfo VALUE;
188 /* The arguments given to the program, minus the program name. */
189 static char **args;
191 static VALUE *eval (bool);
192 static bool nomoreargs (void);
193 static bool null (VALUE *v);
194 static void printv (VALUE *v);
196 void
197 usage (int status)
199 if (status != EXIT_SUCCESS)
200 fprintf (stderr, _("Try `%s --help' for more information.\n"),
201 program_name);
202 else
204 printf (_("\
205 Usage: %s EXPRESSION\n\
206 or: %s OPTION\n\
208 program_name, program_name);
209 putchar ('\n');
210 fputs (HELP_OPTION_DESCRIPTION, stdout);
211 fputs (VERSION_OPTION_DESCRIPTION, stdout);
212 fputs (_("\
214 Print the value of EXPRESSION to standard output. A blank line below\n\
215 separates increasing precedence groups. EXPRESSION may be:\n\
217 ARG1 | ARG2 ARG1 if it is neither null nor 0, otherwise ARG2\n\
219 ARG1 & ARG2 ARG1 if neither argument is null or 0, otherwise 0\n\
220 "), stdout);
221 fputs (_("\
223 ARG1 < ARG2 ARG1 is less than ARG2\n\
224 ARG1 <= ARG2 ARG1 is less than or equal to ARG2\n\
225 ARG1 = ARG2 ARG1 is equal to ARG2\n\
226 ARG1 != ARG2 ARG1 is unequal to ARG2\n\
227 ARG1 >= ARG2 ARG1 is greater than or equal to ARG2\n\
228 ARG1 > ARG2 ARG1 is greater than ARG2\n\
229 "), stdout);
230 fputs (_("\
232 ARG1 + ARG2 arithmetic sum of ARG1 and ARG2\n\
233 ARG1 - ARG2 arithmetic difference of ARG1 and ARG2\n\
234 "), stdout);
235 /* Tell xgettext that the "% A" below is not a printf-style
236 format string: xgettext:no-c-format */
237 fputs (_("\
239 ARG1 * ARG2 arithmetic product of ARG1 and ARG2\n\
240 ARG1 / ARG2 arithmetic quotient of ARG1 divided by ARG2\n\
241 ARG1 % ARG2 arithmetic remainder of ARG1 divided by ARG2\n\
242 "), stdout);
243 fputs (_("\
245 STRING : REGEXP anchored pattern match of REGEXP in STRING\n\
247 match STRING REGEXP same as STRING : REGEXP\n\
248 substr STRING POS LENGTH substring of STRING, POS counted from 1\n\
249 index STRING CHARS index in STRING where any CHARS is found, or 0\n\
250 length STRING length of STRING\n\
251 "), stdout);
252 fputs (_("\
253 + TOKEN interpret TOKEN as a string, even if it is a\n\
254 keyword like `match' or an operator like `/'\n\
256 ( EXPRESSION ) value of EXPRESSION\n\
257 "), stdout);
258 fputs (_("\
260 Beware that many operators need to be escaped or quoted for shells.\n\
261 Comparisons are arithmetic if both ARGs are numbers, else lexicographical.\n\
262 Pattern matches return the string matched between \\( and \\) or null; if\n\
263 \\( and \\) are not used, they return the number of characters matched or 0.\n\
264 "), stdout);
265 fputs (_("\
267 Exit status is 0 if EXPRESSION is neither null nor 0, 1 if EXPRESSION is null\n\
268 or 0, 2 if EXPRESSION is syntactically invalid, and 3 if an error occurred.\n\
269 "), stdout);
270 emit_ancillary_info ();
272 exit (status);
275 /* Report a syntax error and exit. */
276 static void
277 syntax_error (void)
279 error (EXPR_INVALID, 0, _("syntax error"));
282 /* Report an integer overflow for operation OP and exit. */
283 static void
284 integer_overflow (char op)
286 error (EXPR_FAILURE, ERANGE, "%c", op);
287 abort (); /* notreached */
290 static void die (int errno_val, char const *msg)
291 ATTRIBUTE_NORETURN;
292 static void
293 die (int errno_val, char const *msg)
295 error (EXPR_FAILURE, errno_val, "%s", msg);
296 abort (); /* notreached */
300 main (int argc, char **argv)
302 VALUE *v;
304 initialize_main (&argc, &argv);
305 set_program_name (argv[0]);
306 setlocale (LC_ALL, "");
307 bindtextdomain (PACKAGE, LOCALEDIR);
308 textdomain (PACKAGE);
310 initialize_exit_failure (EXPR_FAILURE);
311 atexit (close_stdout);
313 parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, VERSION,
314 usage, AUTHORS, (char const *) NULL);
315 /* The above handles --help and --version.
316 Since there is no other invocation of getopt, handle `--' here. */
317 if (argc > 1 && STREQ (argv[1], "--"))
319 --argc;
320 ++argv;
323 if (argc <= 1)
325 error (0, 0, _("missing operand"));
326 usage (EXPR_INVALID);
329 args = argv + 1;
331 v = eval (true);
332 if (!nomoreargs ())
333 syntax_error ();
334 printv (v);
336 exit (null (v));
339 /* Return a VALUE for I. */
341 static VALUE *
342 int_value (unsigned long int i)
344 VALUE *v = xmalloc (sizeof *v);
345 v->type = integer;
346 mpz_init_set_ui (v->u.i, i);
347 return v;
350 /* Return a VALUE for S. */
352 static VALUE *
353 str_value (char const *s)
355 VALUE *v = xmalloc (sizeof *v);
356 v->type = string;
357 v->u.s = xstrdup (s);
358 return v;
361 /* Free VALUE V, including structure components. */
363 static void
364 freev (VALUE *v)
366 if (v->type == string)
367 free (v->u.s);
368 else
369 mpz_clear (v->u.i);
370 free (v);
373 /* Print VALUE V. */
375 static void
376 printv (VALUE *v)
378 switch (v->type)
380 case integer:
381 mpz_out_str (stdout, 10, v->u.i);
382 putchar ('\n');
383 break;
384 case string:
385 puts (v->u.s);
386 break;
387 default:
388 abort ();
392 /* Return true if V is a null-string or zero-number. */
394 static bool
395 null (VALUE *v)
397 switch (v->type)
399 case integer:
400 return mpz_sgn (v->u.i) == 0;
401 case string:
403 char const *cp = v->u.s;
404 if (*cp == '\0')
405 return true;
407 cp += (*cp == '-');
411 if (*cp != '0')
412 return false;
414 while (*++cp);
416 return true;
418 default:
419 abort ();
423 /* Return true if CP takes the form of an integer. */
425 static bool
426 looks_like_integer (char const *cp)
428 cp += (*cp == '-');
431 if (! ISDIGIT (*cp))
432 return false;
433 while (*++cp);
435 return true;
438 /* Coerce V to a string value (can't fail). */
440 static void
441 tostring (VALUE *v)
443 switch (v->type)
445 case integer:
447 char *s = mpz_get_str (NULL, 10, v->u.i);
448 mpz_clear (v->u.i);
449 v->u.s = s;
450 v->type = string;
452 break;
453 case string:
454 break;
455 default:
456 abort ();
460 /* Coerce V to an integer value. Return true on success, false on failure. */
462 static bool
463 toarith (VALUE *v)
465 switch (v->type)
467 case integer:
468 return true;
469 case string:
471 char *s = v->u.s;
473 if (! looks_like_integer (s))
474 return false;
475 if (mpz_init_set_str (v->u.i, s, 10) != 0 && !HAVE_GMP)
476 error (EXPR_FAILURE, ERANGE, "%s", s);
477 free (s);
478 v->type = integer;
479 return true;
481 default:
482 abort ();
486 /* Extract a size_t value from a integer value I.
487 If the value is negative, return SIZE_MAX.
488 If the value is too large, return SIZE_MAX - 1. */
489 static size_t
490 getsize (mpz_t i)
492 if (mpz_sgn (i) < 0)
493 return SIZE_MAX;
494 if (mpz_fits_ulong_p (i))
496 unsigned long int ul = mpz_get_ui (i);
497 if (ul < SIZE_MAX)
498 return ul;
500 return SIZE_MAX - 1;
503 /* Return true and advance if the next token matches STR exactly.
504 STR must not be NULL. */
506 static bool
507 nextarg (char const *str)
509 if (*args == NULL)
510 return false;
511 else
513 bool r = STREQ (*args, str);
514 args += r;
515 return r;
519 /* Return true if there no more tokens. */
521 static bool
522 nomoreargs (void)
524 return *args == 0;
527 #ifdef EVAL_TRACE
528 /* Print evaluation trace and args remaining. */
530 static void
531 trace (fxn)
532 char *fxn;
534 char **a;
536 printf ("%s:", fxn);
537 for (a = args; *a; a++)
538 printf (" %s", *a);
539 putchar ('\n');
541 #endif
543 /* Do the : operator.
544 SV is the VALUE for the lhs (the string),
545 PV is the VALUE for the rhs (the pattern). */
547 static VALUE *
548 docolon (VALUE *sv, VALUE *pv)
550 VALUE *v IF_LINT ( = NULL);
551 const char *errmsg;
552 struct re_pattern_buffer re_buffer;
553 char fastmap[UCHAR_MAX + 1];
554 struct re_registers re_regs;
555 regoff_t matchlen;
557 tostring (sv);
558 tostring (pv);
560 re_regs.num_regs = 0;
561 re_regs.start = NULL;
562 re_regs.end = NULL;
564 re_buffer.buffer = NULL;
565 re_buffer.allocated = 0;
566 re_buffer.fastmap = fastmap;
567 re_buffer.translate = NULL;
568 re_syntax_options =
569 RE_SYNTAX_POSIX_BASIC & ~RE_CONTEXT_INVALID_DUP & ~RE_NO_EMPTY_RANGES;
570 errmsg = re_compile_pattern (pv->u.s, strlen (pv->u.s), &re_buffer);
571 if (errmsg)
572 error (EXPR_INVALID, 0, "%s", errmsg);
573 re_buffer.newline_anchor = 0;
575 matchlen = re_match (&re_buffer, sv->u.s, strlen (sv->u.s), 0, &re_regs);
576 if (0 <= matchlen)
578 /* Were \(...\) used? */
579 if (re_buffer.re_nsub > 0)
581 sv->u.s[re_regs.end[1]] = '\0';
582 v = str_value (sv->u.s + re_regs.start[1]);
584 else
585 v = int_value (matchlen);
587 else if (matchlen == -1)
589 /* Match failed -- return the right kind of null. */
590 if (re_buffer.re_nsub > 0)
591 v = str_value ("");
592 else
593 v = int_value (0);
595 else
596 error (EXPR_FAILURE,
597 (matchlen == -2 ? errno : EOVERFLOW),
598 _("error in regular expression matcher"));
600 if (0 < re_regs.num_regs)
602 free (re_regs.start);
603 free (re_regs.end);
605 re_buffer.fastmap = NULL;
606 regfree (&re_buffer);
607 return v;
610 /* Handle bare operands and ( expr ) syntax. */
612 static VALUE *
613 eval7 (bool evaluate)
615 VALUE *v;
617 #ifdef EVAL_TRACE
618 trace ("eval7");
619 #endif
620 if (nomoreargs ())
621 syntax_error ();
623 if (nextarg ("("))
625 v = eval (evaluate);
626 if (!nextarg (")"))
627 syntax_error ();
628 return v;
631 if (nextarg (")"))
632 syntax_error ();
634 return str_value (*args++);
637 /* Handle match, substr, index, and length keywords, and quoting "+". */
639 static VALUE *
640 eval6 (bool evaluate)
642 VALUE *l;
643 VALUE *r;
644 VALUE *v;
645 VALUE *i1;
646 VALUE *i2;
648 #ifdef EVAL_TRACE
649 trace ("eval6");
650 #endif
651 if (nextarg ("+"))
653 if (nomoreargs ())
654 syntax_error ();
655 return str_value (*args++);
657 else if (nextarg ("length"))
659 r = eval6 (evaluate);
660 tostring (r);
661 v = int_value (strlen (r->u.s));
662 freev (r);
663 return v;
665 else if (nextarg ("match"))
667 l = eval6 (evaluate);
668 r = eval6 (evaluate);
669 if (evaluate)
671 v = docolon (l, r);
672 freev (l);
674 else
675 v = l;
676 freev (r);
677 return v;
679 else if (nextarg ("index"))
681 size_t pos;
683 l = eval6 (evaluate);
684 r = eval6 (evaluate);
685 tostring (l);
686 tostring (r);
687 pos = strcspn (l->u.s, r->u.s);
688 v = int_value (l->u.s[pos] ? pos + 1 : 0);
689 freev (l);
690 freev (r);
691 return v;
693 else if (nextarg ("substr"))
695 size_t llen;
696 l = eval6 (evaluate);
697 i1 = eval6 (evaluate);
698 i2 = eval6 (evaluate);
699 tostring (l);
700 llen = strlen (l->u.s);
702 if (!toarith (i1) || !toarith (i2))
703 v = str_value ("");
704 else
706 size_t pos = getsize (i1->u.i);
707 size_t len = getsize (i2->u.i);
709 if (llen < pos || pos == 0 || len == 0 || len == SIZE_MAX)
710 v = str_value ("");
711 else
713 size_t vlen = MIN (len, llen - pos + 1);
714 char *vlim;
715 v = xmalloc (sizeof *v);
716 v->type = string;
717 v->u.s = xmalloc (vlen + 1);
718 vlim = mempcpy (v->u.s, l->u.s + pos - 1, vlen);
719 *vlim = '\0';
722 freev (l);
723 freev (i1);
724 freev (i2);
725 return v;
727 else
728 return eval7 (evaluate);
731 /* Handle : operator (pattern matching).
732 Calls docolon to do the real work. */
734 static VALUE *
735 eval5 (bool evaluate)
737 VALUE *l;
738 VALUE *r;
739 VALUE *v;
741 #ifdef EVAL_TRACE
742 trace ("eval5");
743 #endif
744 l = eval6 (evaluate);
745 while (1)
747 if (nextarg (":"))
749 r = eval6 (evaluate);
750 if (evaluate)
752 v = docolon (l, r);
753 freev (l);
754 l = v;
756 freev (r);
758 else
759 return l;
763 /* Handle *, /, % operators. */
765 static VALUE *
766 eval4 (bool evaluate)
768 VALUE *l;
769 VALUE *r;
770 enum { multiply, divide, mod } fxn;
772 #ifdef EVAL_TRACE
773 trace ("eval4");
774 #endif
775 l = eval5 (evaluate);
776 while (1)
778 if (nextarg ("*"))
779 fxn = multiply;
780 else if (nextarg ("/"))
781 fxn = divide;
782 else if (nextarg ("%"))
783 fxn = mod;
784 else
785 return l;
786 r = eval5 (evaluate);
787 if (evaluate)
789 if (!toarith (l) || !toarith (r))
790 error (EXPR_INVALID, 0, _("non-integer argument"));
791 if (fxn != multiply && mpz_sgn (r->u.i) == 0)
792 error (EXPR_INVALID, 0, _("division by zero"));
793 ((fxn == multiply ? mpz_mul
794 : fxn == divide ? mpz_tdiv_q
795 : mpz_tdiv_r)
796 (l->u.i, l->u.i, r->u.i));
798 freev (r);
802 /* Handle +, - operators. */
804 static VALUE *
805 eval3 (bool evaluate)
807 VALUE *l;
808 VALUE *r;
809 enum { plus, minus } fxn;
811 #ifdef EVAL_TRACE
812 trace ("eval3");
813 #endif
814 l = eval4 (evaluate);
815 while (1)
817 if (nextarg ("+"))
818 fxn = plus;
819 else if (nextarg ("-"))
820 fxn = minus;
821 else
822 return l;
823 r = eval4 (evaluate);
824 if (evaluate)
826 if (!toarith (l) || !toarith (r))
827 error (EXPR_INVALID, 0, _("non-integer argument"));
828 (fxn == plus ? mpz_add : mpz_sub) (l->u.i, l->u.i, r->u.i);
830 freev (r);
834 /* Handle comparisons. */
836 static VALUE *
837 eval2 (bool evaluate)
839 VALUE *l;
841 #ifdef EVAL_TRACE
842 trace ("eval2");
843 #endif
844 l = eval3 (evaluate);
845 while (1)
847 VALUE *r;
848 enum
850 less_than, less_equal, equal, not_equal, greater_equal, greater_than
851 } fxn;
852 bool val = false;
854 if (nextarg ("<"))
855 fxn = less_than;
856 else if (nextarg ("<="))
857 fxn = less_equal;
858 else if (nextarg ("=") || nextarg ("=="))
859 fxn = equal;
860 else if (nextarg ("!="))
861 fxn = not_equal;
862 else if (nextarg (">="))
863 fxn = greater_equal;
864 else if (nextarg (">"))
865 fxn = greater_than;
866 else
867 return l;
868 r = eval3 (evaluate);
870 if (evaluate)
872 int cmp;
873 tostring (l);
874 tostring (r);
876 if (looks_like_integer (l->u.s) && looks_like_integer (r->u.s))
877 cmp = strintcmp (l->u.s, r->u.s);
878 else
880 errno = 0;
881 cmp = strcoll (l->u.s, r->u.s);
883 if (errno)
885 error (0, errno, _("string comparison failed"));
886 error (0, 0, _("set LC_ALL='C' to work around the problem"));
887 error (EXPR_INVALID, 0,
888 _("the strings compared were %s and %s"),
889 quotearg_n_style (0, locale_quoting_style, l->u.s),
890 quotearg_n_style (1, locale_quoting_style, r->u.s));
894 switch (fxn)
896 case less_than: val = (cmp < 0); break;
897 case less_equal: val = (cmp <= 0); break;
898 case equal: val = (cmp == 0); break;
899 case not_equal: val = (cmp != 0); break;
900 case greater_equal: val = (cmp >= 0); break;
901 case greater_than: val = (cmp > 0); break;
902 default: abort ();
906 freev (l);
907 freev (r);
908 l = int_value (val);
912 /* Handle &. */
914 static VALUE *
915 eval1 (bool evaluate)
917 VALUE *l;
918 VALUE *r;
920 #ifdef EVAL_TRACE
921 trace ("eval1");
922 #endif
923 l = eval2 (evaluate);
924 while (1)
926 if (nextarg ("&"))
928 r = eval2 (evaluate && !null (l));
929 if (null (l) || null (r))
931 freev (l);
932 freev (r);
933 l = int_value (0);
935 else
936 freev (r);
938 else
939 return l;
943 /* Handle |. */
945 static VALUE *
946 eval (bool evaluate)
948 VALUE *l;
949 VALUE *r;
951 #ifdef EVAL_TRACE
952 trace ("eval");
953 #endif
954 l = eval1 (evaluate);
955 while (1)
957 if (nextarg ("|"))
959 r = eval1 (evaluate && null (l));
960 if (null (l))
962 freev (l);
963 l = r;
964 if (null (l))
966 freev (l);
967 l = int_value (0);
970 else
971 freev (r);
973 else
974 return l;