blob: b7e33de49379174a75bc0dbef6a5254b4668f9b9 [file] [log] [blame]
Eric Andersen1b355eb2000-09-05 17:37:48 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini expr implementation for busybox
4 *
5 * based on GNU expr Mike Parker.
6 * Copyright (C) 86, 1991-1997, 1999 Free Software Foundation, Inc.
7 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00008 * Busybox modifications
Eric Andersen1b355eb2000-09-05 17:37:48 +00009 * Copyright (c) 2000 Edward Betts <edward@debian.org>.
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000010 * Copyright (C) 2003-2005 Vladimir Oleynik <dzo@simtreas.ru>
11 * - reduced 464 bytes.
12 * - 64 math support
Eric Andersen1b355eb2000-09-05 17:37:48 +000013 *
"Robert P. J. Day"801ab142006-07-12 07:56:04 +000014 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersen1b355eb2000-09-05 17:37:48 +000015 */
16
17/* This program evaluates expressions. Each token (operator, operand,
Eric Andersenaff114c2004-04-14 17:51:38 +000018 * parenthesis) of the expression must be a separate argument. The
Eric Andersen1b355eb2000-09-05 17:37:48 +000019 * parser used is a reasonably general one, though any incarnation of
20 * it is language-specific. It is especially nice for expressions.
21 *
22 * No parse tree is needed; a new node is evaluated immediately.
23 * One function can handle multiple operators all of equal precedence,
24 * provided they all associate ((x op x) op x). */
25
Mark Whitley827e45c2001-03-09 23:59:51 +000026/* no getopt needed */
27
Eric Andersencbe31da2001-02-20 06:14:08 +000028#include "busybox.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +000029#include "xregex.h"
Eric Andersen1b355eb2000-09-05 17:37:48 +000030
31/* The kinds of value we can have. */
32enum valtype {
33 integer,
34 string
35};
36typedef enum valtype TYPE;
37
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000038#if ENABLE_EXPR_MATH_SUPPORT_64
39typedef int64_t arith_t;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000040
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000041#define PF_REZ "ll"
Eric Andersen5e678872006-01-30 19:48:23 +000042#define PF_REZ_TYPE (long long)
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000043#define STRTOL(s, e, b) strtoll(s, e, b)
44#else
45typedef long arith_t;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000046
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000047#define PF_REZ "l"
Eric Andersen5e678872006-01-30 19:48:23 +000048#define PF_REZ_TYPE (long)
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000049#define STRTOL(s, e, b) strtol(s, e, b)
50#endif
51
Denis Vlasenkod686a042006-11-27 14:43:21 +000052/* TODO: use bb_strtol[l]? It's easier to check for errors... */
53
Eric Andersen1b355eb2000-09-05 17:37:48 +000054/* A value is.... */
55struct valinfo {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000056 TYPE type; /* Which kind. */
57 union { /* The value itself. */
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000058 arith_t i;
Eric Andersen1b355eb2000-09-05 17:37:48 +000059 char *s;
60 } u;
61};
62typedef struct valinfo VALUE;
63
64/* The arguments given to the program, minus the program name. */
65static char **args;
66
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000067static VALUE *docolon(VALUE * sv, VALUE * pv);
68static VALUE *eval(void);
69static VALUE *int_value(arith_t i);
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +000070static VALUE *str_value(const char *s);
71static int nextarg(const char *str);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000072static int null(VALUE * v);
73static int toarith(VALUE * v);
74static void freev(VALUE * v);
75static void tostring(VALUE * v);
Eric Andersen1b355eb2000-09-05 17:37:48 +000076
Denis Vlasenko06af2162007-02-03 17:28:39 +000077int expr_main(int argc, char **argv);
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000078int expr_main(int argc, char **argv)
Eric Andersen1b355eb2000-09-05 17:37:48 +000079{
80 VALUE *v;
81
82 if (argc == 1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000083 bb_error_msg_and_die("too few arguments");
Eric Andersen1b355eb2000-09-05 17:37:48 +000084 }
85
86 args = argv + 1;
87
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000088 v = eval();
Eric Andersen1b355eb2000-09-05 17:37:48 +000089 if (*args)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000090 bb_error_msg_and_die("syntax error");
Eric Andersen1b355eb2000-09-05 17:37:48 +000091
92 if (v->type == integer)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000093 printf("%" PF_REZ "d\n", PF_REZ_TYPE v->u.i);
Matt Kraai59df6f72001-05-16 14:21:09 +000094 else
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000095 puts(v->u.s);
Eric Andersen1b355eb2000-09-05 17:37:48 +000096
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000097 fflush_stdout_and_exit(null(v));
Eric Andersen1b355eb2000-09-05 17:37:48 +000098}
99
100/* Return a VALUE for I. */
101
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000102static VALUE *int_value(arith_t i)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000103{
104 VALUE *v;
105
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000106 v = xmalloc(sizeof(VALUE));
Eric Andersen1b355eb2000-09-05 17:37:48 +0000107 v->type = integer;
108 v->u.i = i;
109 return v;
110}
111
112/* Return a VALUE for S. */
113
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000114static VALUE *str_value(const char *s)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000115{
116 VALUE *v;
117
Rob Landleyd921b2e2006-08-03 15:41:12 +0000118 v = xmalloc(sizeof(VALUE));
Eric Andersen1b355eb2000-09-05 17:37:48 +0000119 v->type = string;
Rob Landleyd921b2e2006-08-03 15:41:12 +0000120 v->u.s = xstrdup(s);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000121 return v;
122}
123
124/* Free VALUE V, including structure components. */
125
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000126static void freev(VALUE * v)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000127{
128 if (v->type == string)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000129 free(v->u.s);
130 free(v);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000131}
132
133/* Return nonzero if V is a null-string or zero-number. */
134
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000135static int null(VALUE * v)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000136{
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000137 if (v->type == integer)
138 return v->u.i == 0;
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000139 /* string: */
140 return v->u.s[0] == '\0' || LONE_CHAR(v->u.s, '0');
Eric Andersen1b355eb2000-09-05 17:37:48 +0000141}
142
143/* Coerce V to a string value (can't fail). */
144
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000145static void tostring(VALUE * v)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000146{
Eric Andersen1b355eb2000-09-05 17:37:48 +0000147 if (v->type == integer) {
Rob Landleyd921b2e2006-08-03 15:41:12 +0000148 v->u.s = xasprintf("%" PF_REZ "d", PF_REZ_TYPE v->u.i);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000149 v->type = string;
150 }
151}
152
153/* Coerce V to an integer value. Return 1 on success, 0 on failure. */
154
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000155static int toarith(VALUE * v)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000156{
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000157 if (v->type == string) {
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000158 arith_t i;
Manuel Novoa III 70183852004-01-25 19:47:10 +0000159 char *e;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000160
Manuel Novoa III 70183852004-01-25 19:47:10 +0000161 /* Don't interpret the empty string as an integer. */
162 /* Currently does not worry about overflow or int/long differences. */
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000163 i = STRTOL(v->u.s, &e, 10);
Manuel Novoa III 70183852004-01-25 19:47:10 +0000164 if ((v->u.s == e) || *e)
165 return 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000166 free(v->u.s);
Manuel Novoa III 70183852004-01-25 19:47:10 +0000167 v->u.i = i;
168 v->type = integer;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000169 }
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000170 return 1;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000171}
172
173/* Return nonzero if the next token matches STR exactly.
174 STR must not be NULL. */
175
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000176static int nextarg(const char *str)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000177{
178 if (*args == NULL)
179 return 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000180 return strcmp(*args, str) == 0;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000181}
182
183/* The comparison operator handling functions. */
184
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000185static int cmp_common(VALUE * l, VALUE * r, int op)
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000186{
187 int cmpval;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000188
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000189 if (l->type == string || r->type == string) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000190 tostring(l);
191 tostring(r);
192 cmpval = strcmp(l->u.s, r->u.s);
193 } else
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000194 cmpval = l->u.i - r->u.i;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000195 if (op == '<')
196 return cmpval < 0;
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000197 if (op == ('L' + 'E'))
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000198 return cmpval <= 0;
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000199 if (op == '=')
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000200 return cmpval == 0;
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000201 if (op == '!')
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000202 return cmpval != 0;
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000203 if (op == '>')
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000204 return cmpval > 0;
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000205 /* >= */
206 return cmpval >= 0;
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000207}
Eric Andersen1b355eb2000-09-05 17:37:48 +0000208
209/* The arithmetic operator handling functions. */
210
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000211static arith_t arithmetic_common(VALUE * l, VALUE * r, int op)
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000212{
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000213 arith_t li, ri;
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000214
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000215 if (!toarith(l) || !toarith(r))
216 bb_error_msg_and_die("non-numeric argument");
217 li = l->u.i;
218 ri = r->u.i;
219 if ((op == '/' || op == '%') && ri == 0)
220 bb_error_msg_and_die("division by zero");
221 if (op == '+')
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000222 return li + ri;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000223 else if (op == '-')
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000224 return li - ri;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000225 else if (op == '*')
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000226 return li * ri;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000227 else if (op == '/')
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000228 return li / ri;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000229 else
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000230 return li % ri;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000231}
232
Eric Andersen1b355eb2000-09-05 17:37:48 +0000233/* Do the : operator.
234 SV is the VALUE for the lhs (the string),
235 PV is the VALUE for the rhs (the pattern). */
236
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000237static VALUE *docolon(VALUE * sv, VALUE * pv)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000238{
239 VALUE *v;
Rob Landley540d3f62005-05-09 21:42:42 +0000240 regex_t re_buffer;
241 const int NMATCH = 2;
242 regmatch_t re_regs[NMATCH];
Eric Andersen1b355eb2000-09-05 17:37:48 +0000243
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000244 tostring(sv);
245 tostring(pv);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000246
247 if (pv->u.s[0] == '^') {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000248 fprintf(stderr, "\
Eric Andersen1b355eb2000-09-05 17:37:48 +0000249warning: unportable BRE: `%s': using `^' as the first character\n\
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000250of a basic regular expression is not portable; it is being ignored", pv->u.s);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000251 }
252
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000253 memset(&re_buffer, 0, sizeof(re_buffer));
254 memset(re_regs, 0, sizeof(*re_regs));
255 if (regcomp(&re_buffer, pv->u.s, 0) != 0)
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000256 bb_error_msg_and_die("invalid regular expression");
Eric Andersen1b355eb2000-09-05 17:37:48 +0000257
Rob Landley540d3f62005-05-09 21:42:42 +0000258 /* expr uses an anchored pattern match, so check that there was a
259 * match and that the match starts at offset 0. */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000260 if (regexec(&re_buffer, sv->u.s, NMATCH, re_regs, 0) != REG_NOMATCH &&
261 re_regs[0].rm_so == 0) {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000262 /* Were \(...\) used? */
Rob Landley540d3f62005-05-09 21:42:42 +0000263 if (re_buffer.re_nsub > 0) {
264 sv->u.s[re_regs[1].rm_eo] = '\0';
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000265 v = str_value(sv->u.s + re_regs[1].rm_so);
266 } else
267 v = int_value(re_regs[0].rm_eo);
268 } else {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000269 /* Match failed -- return the right kind of null. */
270 if (re_buffer.re_nsub > 0)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000271 v = str_value("");
Eric Andersen1b355eb2000-09-05 17:37:48 +0000272 else
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000273 v = int_value(0);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000274 }
Eric Andersen1b355eb2000-09-05 17:37:48 +0000275 return v;
276}
277
278/* Handle bare operands and ( expr ) syntax. */
279
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000280static VALUE *eval7(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000281{
282 VALUE *v;
283
284 if (!*args)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000285 bb_error_msg_and_die("syntax error");
Eric Andersen1b355eb2000-09-05 17:37:48 +0000286
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000287 if (nextarg("(")) {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000288 args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000289 v = eval();
290 if (!nextarg(")"))
291 bb_error_msg_and_die("syntax error");
292 args++;
293 return v;
294 }
Eric Andersen1b355eb2000-09-05 17:37:48 +0000295
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000296 if (nextarg(")"))
297 bb_error_msg_and_die("syntax error");
Eric Andersen1b355eb2000-09-05 17:37:48 +0000298
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000299 return str_value(*args++);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000300}
301
302/* Handle match, substr, index, length, and quote keywords. */
303
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000304static VALUE *eval6(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000305{
306 VALUE *l, *r, *v, *i1, *i2;
307
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000308 if (nextarg("quote")) {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000309 args++;
310 if (!*args)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000311 bb_error_msg_and_die("syntax error");
312 return str_value(*args++);
313 } else if (nextarg("length")) {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000314 args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000315 r = eval6();
316 tostring(r);
317 v = int_value(strlen(r->u.s));
318 freev(r);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000319 return v;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000320 } else if (nextarg("match")) {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000321 args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000322 l = eval6();
323 r = eval6();
324 v = docolon(l, r);
325 freev(l);
326 freev(r);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000327 return v;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000328 } else if (nextarg("index")) {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000329 args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000330 l = eval6();
331 r = eval6();
332 tostring(l);
333 tostring(r);
334 v = int_value(strcspn(l->u.s, r->u.s) + 1);
335 if (v->u.i == (arith_t) strlen(l->u.s) + 1)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000336 v->u.i = 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000337 freev(l);
338 freev(r);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000339 return v;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000340 } else if (nextarg("substr")) {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000341 args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000342 l = eval6();
343 i1 = eval6();
344 i2 = eval6();
345 tostring(l);
346 if (!toarith(i1) || !toarith(i2)
347 || i1->u.i > (arith_t) strlen(l->u.s)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000348 || i1->u.i <= 0 || i2->u.i <= 0)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000349 v = str_value("");
Eric Andersen1b355eb2000-09-05 17:37:48 +0000350 else {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000351 v = xmalloc(sizeof(VALUE));
Eric Andersen1b355eb2000-09-05 17:37:48 +0000352 v->type = string;
Rob Landleyd921b2e2006-08-03 15:41:12 +0000353 v->u.s = xstrndup(l->u.s + i1->u.i - 1, i2->u.i);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000354 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000355 freev(l);
356 freev(i1);
357 freev(i2);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000358 return v;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000359 } else
360 return eval7();
Eric Andersen1b355eb2000-09-05 17:37:48 +0000361}
362
363/* Handle : operator (pattern matching).
364 Calls docolon to do the real work. */
365
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000366static VALUE *eval5(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000367{
368 VALUE *l, *r, *v;
369
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000370 l = eval6();
371 while (nextarg(":")) {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000372 args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000373 r = eval6();
374 v = docolon(l, r);
375 freev(l);
376 freev(r);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000377 l = v;
378 }
379 return l;
380}
381
382/* Handle *, /, % operators. */
383
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000384static VALUE *eval4(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000385{
386 VALUE *l, *r;
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000387 int op;
388 arith_t val;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000389
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000390 l = eval5();
Eric Andersen1b355eb2000-09-05 17:37:48 +0000391 while (1) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000392 if (nextarg("*"))
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000393 op = '*';
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000394 else if (nextarg("/"))
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000395 op = '/';
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000396 else if (nextarg("%"))
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000397 op = '%';
Eric Andersen1b355eb2000-09-05 17:37:48 +0000398 else
399 return l;
400 args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000401 r = eval5();
402 val = arithmetic_common(l, r, op);
403 freev(l);
404 freev(r);
405 l = int_value(val);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000406 }
407}
408
409/* Handle +, - operators. */
410
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000411static VALUE *eval3(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000412{
413 VALUE *l, *r;
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000414 int op;
415 arith_t val;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000416
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000417 l = eval4();
Eric Andersen1b355eb2000-09-05 17:37:48 +0000418 while (1) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000419 if (nextarg("+"))
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000420 op = '+';
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000421 else if (nextarg("-"))
Glenn L McGrath07f6b952003-09-08 23:19:12 +0000422 op = '-';
Eric Andersen1b355eb2000-09-05 17:37:48 +0000423 else
424 return l;
425 args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000426 r = eval4();
427 val = arithmetic_common(l, r, op);
428 freev(l);
429 freev(r);
430 l = int_value(val);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000431 }
432}
433
434/* Handle comparisons. */
435
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000436static VALUE *eval2(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000437{
438 VALUE *l, *r;
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000439 int op;
440 arith_t val;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000441
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000442 l = eval3();
Eric Andersen1b355eb2000-09-05 17:37:48 +0000443 while (1) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000444 if (nextarg("<"))
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000445 op = '<';
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000446 else if (nextarg("<="))
447 op = 'L' + 'E';
448 else if (nextarg("=") || nextarg("=="))
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000449 op = '=';
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000450 else if (nextarg("!="))
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000451 op = '!';
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000452 else if (nextarg(">="))
453 op = 'G' + 'E';
454 else if (nextarg(">"))
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000455 op = '>';
Eric Andersen1b355eb2000-09-05 17:37:48 +0000456 else
457 return l;
458 args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000459 r = eval3();
460 toarith(l);
461 toarith(r);
462 val = cmp_common(l, r, op);
463 freev(l);
464 freev(r);
465 l = int_value(val);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000466 }
467}
468
469/* Handle &. */
470
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000471static VALUE *eval1(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000472{
473 VALUE *l, *r;
474
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000475 l = eval2();
476 while (nextarg("&")) {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000477 args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000478 r = eval2();
479 if (null(l) || null(r)) {
480 freev(l);
481 freev(r);
482 l = int_value(0);
483 } else
484 freev(r);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000485 }
486 return l;
487}
488
489/* Handle |. */
490
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000491static VALUE *eval(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000492{
493 VALUE *l, *r;
494
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000495 l = eval1();
496 while (nextarg("|")) {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000497 args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000498 r = eval1();
499 if (null(l)) {
500 freev(l);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000501 l = r;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000502 } else
503 freev(r);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000504 }
505 return l;
506}