2000-09-05 23:07:48 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Mini expr implementation for busybox
|
|
|
|
*
|
|
|
|
* based on GNU expr Mike Parker.
|
|
|
|
* Copyright (C) 86, 1991-1997, 1999 Free Software Foundation, Inc.
|
|
|
|
*
|
2004-03-15 13:59:22 +05:30
|
|
|
* Busybox modifications
|
2000-09-05 23:07:48 +05:30
|
|
|
* Copyright (c) 2000 Edward Betts <edward@debian.org>.
|
2006-01-25 19:26:03 +05:30
|
|
|
* Copyright (C) 2003-2005 Vladimir Oleynik <dzo@simtreas.ru>
|
|
|
|
* - reduced 464 bytes.
|
|
|
|
* - 64 math support
|
2000-09-05 23:07:48 +05:30
|
|
|
*
|
2006-07-12 13:26:04 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
2000-09-05 23:07:48 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
/* This program evaluates expressions. Each token (operator, operand,
|
2004-04-14 23:21:38 +05:30
|
|
|
* parenthesis) of the expression must be a separate argument. The
|
2000-09-05 23:07:48 +05:30
|
|
|
* parser used is a reasonably general one, though any incarnation of
|
|
|
|
* it is language-specific. It is especially nice for expressions.
|
|
|
|
*
|
|
|
|
* No parse tree is needed; a new node is evaluated immediately.
|
|
|
|
* One function can handle multiple operators all of equal precedence,
|
|
|
|
* provided they all associate ((x op x) op x). */
|
|
|
|
|
2001-03-10 05:29:51 +05:30
|
|
|
/* no getopt needed */
|
|
|
|
|
2001-02-20 11:44:08 +05:30
|
|
|
#include "busybox.h"
|
2006-08-03 21:11:12 +05:30
|
|
|
#include "xregex.h"
|
2000-09-05 23:07:48 +05:30
|
|
|
|
|
|
|
/* The kinds of value we can have. */
|
|
|
|
enum valtype {
|
|
|
|
integer,
|
|
|
|
string
|
|
|
|
};
|
|
|
|
typedef enum valtype TYPE;
|
|
|
|
|
2006-01-25 19:26:03 +05:30
|
|
|
#if ENABLE_EXPR_MATH_SUPPORT_64
|
|
|
|
typedef int64_t arith_t;
|
2006-08-29 05:01:54 +05:30
|
|
|
|
2006-01-25 19:26:03 +05:30
|
|
|
#define PF_REZ "ll"
|
2006-01-31 01:18:23 +05:30
|
|
|
#define PF_REZ_TYPE (long long)
|
2006-01-25 19:26:03 +05:30
|
|
|
#define STRTOL(s, e, b) strtoll(s, e, b)
|
|
|
|
#else
|
|
|
|
typedef long arith_t;
|
2006-08-29 05:01:54 +05:30
|
|
|
|
2006-01-25 19:26:03 +05:30
|
|
|
#define PF_REZ "l"
|
2006-01-31 01:18:23 +05:30
|
|
|
#define PF_REZ_TYPE (long)
|
2006-01-25 19:26:03 +05:30
|
|
|
#define STRTOL(s, e, b) strtol(s, e, b)
|
|
|
|
#endif
|
|
|
|
|
2000-09-05 23:07:48 +05:30
|
|
|
/* A value is.... */
|
|
|
|
struct valinfo {
|
2006-08-29 05:01:54 +05:30
|
|
|
TYPE type; /* Which kind. */
|
|
|
|
union { /* The value itself. */
|
2006-01-25 19:26:03 +05:30
|
|
|
arith_t i;
|
2000-09-05 23:07:48 +05:30
|
|
|
char *s;
|
|
|
|
} u;
|
|
|
|
};
|
|
|
|
typedef struct valinfo VALUE;
|
|
|
|
|
|
|
|
/* The arguments given to the program, minus the program name. */
|
|
|
|
static char **args;
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
static VALUE *docolon(VALUE * sv, VALUE * pv);
|
|
|
|
static VALUE *eval(void);
|
|
|
|
static VALUE *int_value(arith_t i);
|
|
|
|
static VALUE *str_value(char *s);
|
|
|
|
static int nextarg(char *str);
|
|
|
|
static int null(VALUE * v);
|
|
|
|
static int toarith(VALUE * v);
|
|
|
|
static void freev(VALUE * v);
|
|
|
|
static void tostring(VALUE * v);
|
|
|
|
|
|
|
|
int expr_main(int argc, char **argv)
|
2000-09-05 23:07:48 +05:30
|
|
|
{
|
|
|
|
VALUE *v;
|
|
|
|
|
|
|
|
if (argc == 1) {
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_error_msg_and_die("too few arguments");
|
2000-09-05 23:07:48 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
args = argv + 1;
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
v = eval();
|
2000-09-05 23:07:48 +05:30
|
|
|
if (*args)
|
2006-08-29 05:01:54 +05:30
|
|
|
bb_error_msg_and_die("syntax error");
|
2000-09-05 23:07:48 +05:30
|
|
|
|
|
|
|
if (v->type == integer)
|
2006-08-29 05:01:54 +05:30
|
|
|
bb_printf("%" PF_REZ "d\n", PF_REZ_TYPE v->u.i);
|
2001-05-16 19:51:09 +05:30
|
|
|
else
|
2006-08-29 05:01:54 +05:30
|
|
|
puts(v->u.s);
|
2000-09-05 23:07:48 +05:30
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
exit(null(v));
|
2000-09-05 23:07:48 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Return a VALUE for I. */
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
static VALUE *int_value(arith_t i)
|
2000-09-05 23:07:48 +05:30
|
|
|
{
|
|
|
|
VALUE *v;
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
v = xmalloc(sizeof(VALUE));
|
2000-09-05 23:07:48 +05:30
|
|
|
v->type = integer;
|
|
|
|
v->u.i = i;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return a VALUE for S. */
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
static VALUE *str_value(char *s)
|
2000-09-05 23:07:48 +05:30
|
|
|
{
|
|
|
|
VALUE *v;
|
|
|
|
|
2006-08-03 21:11:12 +05:30
|
|
|
v = xmalloc(sizeof(VALUE));
|
2000-09-05 23:07:48 +05:30
|
|
|
v->type = string;
|
2006-08-03 21:11:12 +05:30
|
|
|
v->u.s = xstrdup(s);
|
2000-09-05 23:07:48 +05:30
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free VALUE V, including structure components. */
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
static void freev(VALUE * v)
|
2000-09-05 23:07:48 +05:30
|
|
|
{
|
|
|
|
if (v->type == string)
|
2006-08-29 05:01:54 +05:30
|
|
|
free(v->u.s);
|
|
|
|
free(v);
|
2000-09-05 23:07:48 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Return nonzero if V is a null-string or zero-number. */
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
static int null(VALUE * v)
|
2000-09-05 23:07:48 +05:30
|
|
|
{
|
2006-08-29 05:01:54 +05:30
|
|
|
if (v->type == integer)
|
|
|
|
return v->u.i == 0;
|
|
|
|
else /* string: */
|
|
|
|
return v->u.s[0] == '\0' || strcmp(v->u.s, "0") == 0;
|
2000-09-05 23:07:48 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Coerce V to a string value (can't fail). */
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
static void tostring(VALUE * v)
|
2000-09-05 23:07:48 +05:30
|
|
|
{
|
|
|
|
if (v->type == integer) {
|
2006-08-03 21:11:12 +05:30
|
|
|
v->u.s = xasprintf("%" PF_REZ "d", PF_REZ_TYPE v->u.i);
|
2000-09-05 23:07:48 +05:30
|
|
|
v->type = string;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Coerce V to an integer value. Return 1 on success, 0 on failure. */
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
static int toarith(VALUE * v)
|
2000-09-05 23:07:48 +05:30
|
|
|
{
|
2006-08-29 05:01:54 +05:30
|
|
|
if (v->type == string) {
|
2006-01-25 19:26:03 +05:30
|
|
|
arith_t i;
|
2004-01-26 01:17:10 +05:30
|
|
|
char *e;
|
|
|
|
|
|
|
|
/* Don't interpret the empty string as an integer. */
|
|
|
|
/* Currently does not worry about overflow or int/long differences. */
|
2006-01-25 19:26:03 +05:30
|
|
|
i = STRTOL(v->u.s, &e, 10);
|
2004-01-26 01:17:10 +05:30
|
|
|
if ((v->u.s == e) || *e)
|
|
|
|
return 0;
|
2006-08-29 05:01:54 +05:30
|
|
|
free(v->u.s);
|
2004-01-26 01:17:10 +05:30
|
|
|
v->u.i = i;
|
|
|
|
v->type = integer;
|
2000-09-05 23:07:48 +05:30
|
|
|
}
|
2003-08-29 12:59:30 +05:30
|
|
|
return 1;
|
2000-09-05 23:07:48 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Return nonzero if the next token matches STR exactly.
|
|
|
|
STR must not be NULL. */
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
static int nextarg(char *str)
|
2000-09-05 23:07:48 +05:30
|
|
|
{
|
|
|
|
if (*args == NULL)
|
|
|
|
return 0;
|
2006-08-29 05:01:54 +05:30
|
|
|
return strcmp(*args, str) == 0;
|
2000-09-05 23:07:48 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* The comparison operator handling functions. */
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
static int cmp_common(VALUE * l, VALUE * r, int op)
|
2003-08-29 12:59:30 +05:30
|
|
|
{
|
|
|
|
int cmpval;
|
2000-09-05 23:07:48 +05:30
|
|
|
|
2003-08-29 12:59:30 +05:30
|
|
|
if (l->type == string || r->type == string) {
|
2006-08-29 05:01:54 +05:30
|
|
|
tostring(l);
|
|
|
|
tostring(r);
|
|
|
|
cmpval = strcmp(l->u.s, r->u.s);
|
|
|
|
} else
|
2003-08-29 12:59:30 +05:30
|
|
|
cmpval = l->u.i - r->u.i;
|
2006-08-29 05:01:54 +05:30
|
|
|
if (op == '<')
|
|
|
|
return cmpval < 0;
|
|
|
|
else if (op == ('L' + 'E'))
|
|
|
|
return cmpval <= 0;
|
|
|
|
else if (op == '=')
|
|
|
|
return cmpval == 0;
|
|
|
|
else if (op == '!')
|
|
|
|
return cmpval != 0;
|
|
|
|
else if (op == '>')
|
|
|
|
return cmpval > 0;
|
|
|
|
else /* >= */
|
|
|
|
return cmpval >= 0;
|
2003-08-29 12:59:30 +05:30
|
|
|
}
|
2000-09-05 23:07:48 +05:30
|
|
|
|
|
|
|
/* The arithmetic operator handling functions. */
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
static arith_t arithmetic_common(VALUE * l, VALUE * r, int op)
|
2003-08-29 12:59:30 +05:30
|
|
|
{
|
2006-08-29 05:01:54 +05:30
|
|
|
arith_t li, ri;
|
|
|
|
|
|
|
|
if (!toarith(l) || !toarith(r))
|
|
|
|
bb_error_msg_and_die("non-numeric argument");
|
|
|
|
li = l->u.i;
|
|
|
|
ri = r->u.i;
|
|
|
|
if ((op == '/' || op == '%') && ri == 0)
|
|
|
|
bb_error_msg_and_die("division by zero");
|
|
|
|
if (op == '+')
|
2003-08-29 12:59:30 +05:30
|
|
|
return li + ri;
|
2006-08-29 05:01:54 +05:30
|
|
|
else if (op == '-')
|
2003-08-29 12:59:30 +05:30
|
|
|
return li - ri;
|
2006-08-29 05:01:54 +05:30
|
|
|
else if (op == '*')
|
2003-08-29 12:59:30 +05:30
|
|
|
return li * ri;
|
2006-08-29 05:01:54 +05:30
|
|
|
else if (op == '/')
|
2003-08-29 12:59:30 +05:30
|
|
|
return li / ri;
|
2006-08-29 05:01:54 +05:30
|
|
|
else
|
2003-08-29 12:59:30 +05:30
|
|
|
return li % ri;
|
2000-09-05 23:07:48 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Do the : operator.
|
|
|
|
SV is the VALUE for the lhs (the string),
|
|
|
|
PV is the VALUE for the rhs (the pattern). */
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
static VALUE *docolon(VALUE * sv, VALUE * pv)
|
2000-09-05 23:07:48 +05:30
|
|
|
{
|
|
|
|
VALUE *v;
|
2005-05-10 03:12:42 +05:30
|
|
|
regex_t re_buffer;
|
|
|
|
const int NMATCH = 2;
|
|
|
|
regmatch_t re_regs[NMATCH];
|
2000-09-05 23:07:48 +05:30
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
tostring(sv);
|
|
|
|
tostring(pv);
|
2000-09-05 23:07:48 +05:30
|
|
|
|
|
|
|
if (pv->u.s[0] == '^') {
|
2006-08-29 05:01:54 +05:30
|
|
|
fprintf(stderr, "\
|
2000-09-05 23:07:48 +05:30
|
|
|
warning: unportable BRE: `%s': using `^' as the first character\n\
|
2006-08-29 05:01:54 +05:30
|
|
|
of a basic regular expression is not portable; it is being ignored", pv->u.s);
|
2000-09-05 23:07:48 +05:30
|
|
|
}
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
memset(&re_buffer, 0, sizeof(re_buffer));
|
|
|
|
memset(re_regs, 0, sizeof(*re_regs));
|
|
|
|
if (regcomp(&re_buffer, pv->u.s, 0) != 0)
|
2006-10-20 18:58:22 +05:30
|
|
|
bb_error_msg_and_die("invalid regular expression");
|
2005-05-10 03:12:42 +05:30
|
|
|
|
|
|
|
/* expr uses an anchored pattern match, so check that there was a
|
|
|
|
* match and that the match starts at offset 0. */
|
2006-08-29 05:01:54 +05:30
|
|
|
if (regexec(&re_buffer, sv->u.s, NMATCH, re_regs, 0) != REG_NOMATCH &&
|
|
|
|
re_regs[0].rm_so == 0) {
|
2000-09-05 23:07:48 +05:30
|
|
|
/* Were \(...\) used? */
|
2005-05-10 03:12:42 +05:30
|
|
|
if (re_buffer.re_nsub > 0) {
|
|
|
|
sv->u.s[re_regs[1].rm_eo] = '\0';
|
2006-08-29 05:01:54 +05:30
|
|
|
v = str_value(sv->u.s + re_regs[1].rm_so);
|
|
|
|
} else
|
|
|
|
v = int_value(re_regs[0].rm_eo);
|
|
|
|
} else {
|
2000-09-05 23:07:48 +05:30
|
|
|
/* Match failed -- return the right kind of null. */
|
|
|
|
if (re_buffer.re_nsub > 0)
|
2006-08-29 05:01:54 +05:30
|
|
|
v = str_value("");
|
2000-09-05 23:07:48 +05:30
|
|
|
else
|
2006-08-29 05:01:54 +05:30
|
|
|
v = int_value(0);
|
2000-09-05 23:07:48 +05:30
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle bare operands and ( expr ) syntax. */
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
static VALUE *eval7(void)
|
2000-09-05 23:07:48 +05:30
|
|
|
{
|
|
|
|
VALUE *v;
|
|
|
|
|
|
|
|
if (!*args)
|
2006-08-29 05:01:54 +05:30
|
|
|
bb_error_msg_and_die("syntax error");
|
2000-09-05 23:07:48 +05:30
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
if (nextarg("(")) {
|
2000-09-05 23:07:48 +05:30
|
|
|
args++;
|
2006-08-29 05:01:54 +05:30
|
|
|
v = eval();
|
|
|
|
if (!nextarg(")"))
|
|
|
|
bb_error_msg_and_die("syntax error");
|
|
|
|
args++;
|
|
|
|
return v;
|
|
|
|
}
|
2000-09-05 23:07:48 +05:30
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
if (nextarg(")"))
|
|
|
|
bb_error_msg_and_die("syntax error");
|
2000-09-05 23:07:48 +05:30
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
return str_value(*args++);
|
2000-09-05 23:07:48 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle match, substr, index, length, and quote keywords. */
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
static VALUE *eval6(void)
|
2000-09-05 23:07:48 +05:30
|
|
|
{
|
|
|
|
VALUE *l, *r, *v, *i1, *i2;
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
if (nextarg("quote")) {
|
2000-09-05 23:07:48 +05:30
|
|
|
args++;
|
|
|
|
if (!*args)
|
2006-08-29 05:01:54 +05:30
|
|
|
bb_error_msg_and_die("syntax error");
|
|
|
|
return str_value(*args++);
|
|
|
|
} else if (nextarg("length")) {
|
2000-09-05 23:07:48 +05:30
|
|
|
args++;
|
2006-08-29 05:01:54 +05:30
|
|
|
r = eval6();
|
|
|
|
tostring(r);
|
|
|
|
v = int_value(strlen(r->u.s));
|
|
|
|
freev(r);
|
2000-09-05 23:07:48 +05:30
|
|
|
return v;
|
2006-08-29 05:01:54 +05:30
|
|
|
} else if (nextarg("match")) {
|
2000-09-05 23:07:48 +05:30
|
|
|
args++;
|
2006-08-29 05:01:54 +05:30
|
|
|
l = eval6();
|
|
|
|
r = eval6();
|
|
|
|
v = docolon(l, r);
|
|
|
|
freev(l);
|
|
|
|
freev(r);
|
2000-09-05 23:07:48 +05:30
|
|
|
return v;
|
2006-08-29 05:01:54 +05:30
|
|
|
} else if (nextarg("index")) {
|
2000-09-05 23:07:48 +05:30
|
|
|
args++;
|
2006-08-29 05:01:54 +05:30
|
|
|
l = eval6();
|
|
|
|
r = eval6();
|
|
|
|
tostring(l);
|
|
|
|
tostring(r);
|
|
|
|
v = int_value(strcspn(l->u.s, r->u.s) + 1);
|
|
|
|
if (v->u.i == (arith_t) strlen(l->u.s) + 1)
|
2000-09-05 23:07:48 +05:30
|
|
|
v->u.i = 0;
|
2006-08-29 05:01:54 +05:30
|
|
|
freev(l);
|
|
|
|
freev(r);
|
2000-09-05 23:07:48 +05:30
|
|
|
return v;
|
2006-08-29 05:01:54 +05:30
|
|
|
} else if (nextarg("substr")) {
|
2000-09-05 23:07:48 +05:30
|
|
|
args++;
|
2006-08-29 05:01:54 +05:30
|
|
|
l = eval6();
|
|
|
|
i1 = eval6();
|
|
|
|
i2 = eval6();
|
|
|
|
tostring(l);
|
|
|
|
if (!toarith(i1) || !toarith(i2)
|
|
|
|
|| i1->u.i > (arith_t) strlen(l->u.s)
|
2000-09-05 23:07:48 +05:30
|
|
|
|| i1->u.i <= 0 || i2->u.i <= 0)
|
2006-08-29 05:01:54 +05:30
|
|
|
v = str_value("");
|
2000-09-05 23:07:48 +05:30
|
|
|
else {
|
2006-08-29 05:01:54 +05:30
|
|
|
v = xmalloc(sizeof(VALUE));
|
2000-09-05 23:07:48 +05:30
|
|
|
v->type = string;
|
2006-08-03 21:11:12 +05:30
|
|
|
v->u.s = xstrndup(l->u.s + i1->u.i - 1, i2->u.i);
|
2000-09-05 23:07:48 +05:30
|
|
|
}
|
2006-08-29 05:01:54 +05:30
|
|
|
freev(l);
|
|
|
|
freev(i1);
|
|
|
|
freev(i2);
|
2000-09-05 23:07:48 +05:30
|
|
|
return v;
|
2006-08-29 05:01:54 +05:30
|
|
|
} else
|
|
|
|
return eval7();
|
2000-09-05 23:07:48 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle : operator (pattern matching).
|
|
|
|
Calls docolon to do the real work. */
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
static VALUE *eval5(void)
|
2000-09-05 23:07:48 +05:30
|
|
|
{
|
|
|
|
VALUE *l, *r, *v;
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
l = eval6();
|
|
|
|
while (nextarg(":")) {
|
2000-09-05 23:07:48 +05:30
|
|
|
args++;
|
2006-08-29 05:01:54 +05:30
|
|
|
r = eval6();
|
|
|
|
v = docolon(l, r);
|
|
|
|
freev(l);
|
|
|
|
freev(r);
|
2000-09-05 23:07:48 +05:30
|
|
|
l = v;
|
|
|
|
}
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle *, /, % operators. */
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
static VALUE *eval4(void)
|
2000-09-05 23:07:48 +05:30
|
|
|
{
|
|
|
|
VALUE *l, *r;
|
2006-01-25 19:26:03 +05:30
|
|
|
int op;
|
|
|
|
arith_t val;
|
2000-09-05 23:07:48 +05:30
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
l = eval5();
|
2000-09-05 23:07:48 +05:30
|
|
|
while (1) {
|
2006-08-29 05:01:54 +05:30
|
|
|
if (nextarg("*"))
|
2003-08-29 12:59:30 +05:30
|
|
|
op = '*';
|
2006-08-29 05:01:54 +05:30
|
|
|
else if (nextarg("/"))
|
2003-08-29 12:59:30 +05:30
|
|
|
op = '/';
|
2006-08-29 05:01:54 +05:30
|
|
|
else if (nextarg("%"))
|
2003-08-29 12:59:30 +05:30
|
|
|
op = '%';
|
2000-09-05 23:07:48 +05:30
|
|
|
else
|
|
|
|
return l;
|
|
|
|
args++;
|
2006-08-29 05:01:54 +05:30
|
|
|
r = eval5();
|
|
|
|
val = arithmetic_common(l, r, op);
|
|
|
|
freev(l);
|
|
|
|
freev(r);
|
|
|
|
l = int_value(val);
|
2000-09-05 23:07:48 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle +, - operators. */
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
static VALUE *eval3(void)
|
2000-09-05 23:07:48 +05:30
|
|
|
{
|
|
|
|
VALUE *l, *r;
|
2006-01-25 19:26:03 +05:30
|
|
|
int op;
|
|
|
|
arith_t val;
|
2000-09-05 23:07:48 +05:30
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
l = eval4();
|
2000-09-05 23:07:48 +05:30
|
|
|
while (1) {
|
2006-08-29 05:01:54 +05:30
|
|
|
if (nextarg("+"))
|
2003-08-29 12:59:30 +05:30
|
|
|
op = '+';
|
2006-08-29 05:01:54 +05:30
|
|
|
else if (nextarg("-"))
|
2003-09-09 04:49:12 +05:30
|
|
|
op = '-';
|
2000-09-05 23:07:48 +05:30
|
|
|
else
|
|
|
|
return l;
|
|
|
|
args++;
|
2006-08-29 05:01:54 +05:30
|
|
|
r = eval4();
|
|
|
|
val = arithmetic_common(l, r, op);
|
|
|
|
freev(l);
|
|
|
|
freev(r);
|
|
|
|
l = int_value(val);
|
2000-09-05 23:07:48 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle comparisons. */
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
static VALUE *eval2(void)
|
2000-09-05 23:07:48 +05:30
|
|
|
{
|
|
|
|
VALUE *l, *r;
|
2006-01-25 19:26:03 +05:30
|
|
|
int op;
|
|
|
|
arith_t val;
|
2000-09-05 23:07:48 +05:30
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
l = eval3();
|
2000-09-05 23:07:48 +05:30
|
|
|
while (1) {
|
2006-08-29 05:01:54 +05:30
|
|
|
if (nextarg("<"))
|
2003-08-29 12:59:30 +05:30
|
|
|
op = '<';
|
2006-08-29 05:01:54 +05:30
|
|
|
else if (nextarg("<="))
|
|
|
|
op = 'L' + 'E';
|
|
|
|
else if (nextarg("=") || nextarg("=="))
|
2003-08-29 12:59:30 +05:30
|
|
|
op = '=';
|
2006-08-29 05:01:54 +05:30
|
|
|
else if (nextarg("!="))
|
2003-08-29 12:59:30 +05:30
|
|
|
op = '!';
|
2006-08-29 05:01:54 +05:30
|
|
|
else if (nextarg(">="))
|
|
|
|
op = 'G' + 'E';
|
|
|
|
else if (nextarg(">"))
|
2003-08-29 12:59:30 +05:30
|
|
|
op = '>';
|
2000-09-05 23:07:48 +05:30
|
|
|
else
|
|
|
|
return l;
|
|
|
|
args++;
|
2006-08-29 05:01:54 +05:30
|
|
|
r = eval3();
|
|
|
|
toarith(l);
|
|
|
|
toarith(r);
|
|
|
|
val = cmp_common(l, r, op);
|
|
|
|
freev(l);
|
|
|
|
freev(r);
|
|
|
|
l = int_value(val);
|
2000-09-05 23:07:48 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle &. */
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
static VALUE *eval1(void)
|
2000-09-05 23:07:48 +05:30
|
|
|
{
|
|
|
|
VALUE *l, *r;
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
l = eval2();
|
|
|
|
while (nextarg("&")) {
|
2000-09-05 23:07:48 +05:30
|
|
|
args++;
|
2006-08-29 05:01:54 +05:30
|
|
|
r = eval2();
|
|
|
|
if (null(l) || null(r)) {
|
|
|
|
freev(l);
|
|
|
|
freev(r);
|
|
|
|
l = int_value(0);
|
|
|
|
} else
|
|
|
|
freev(r);
|
2000-09-05 23:07:48 +05:30
|
|
|
}
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle |. */
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
static VALUE *eval(void)
|
2000-09-05 23:07:48 +05:30
|
|
|
{
|
|
|
|
VALUE *l, *r;
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
l = eval1();
|
|
|
|
while (nextarg("|")) {
|
2000-09-05 23:07:48 +05:30
|
|
|
args++;
|
2006-08-29 05:01:54 +05:30
|
|
|
r = eval1();
|
|
|
|
if (null(l)) {
|
|
|
|
freev(l);
|
2000-09-05 23:07:48 +05:30
|
|
|
l = r;
|
2006-08-29 05:01:54 +05:30
|
|
|
} else
|
|
|
|
freev(r);
|
2000-09-05 23:07:48 +05:30
|
|
|
}
|
|
|
|
return l;
|
|
|
|
}
|