1
0
mirror of https://git.FreeBSD.org/src.git synced 2024-10-19 02:29:40 +00:00

Implement gawk multiple-arg extension to and, or, and xor.

gawk allows multiple arguemnts to bit-wiste and, or and xor
functions. Implement an arbitrary number of arguments for these
functions. Also, use NULL in preference to 0 to match rest of file.

Sponsored by: Netflix
Differential Revision: https://reviews.freebsd.org/D12361
This commit is contained in:
Warner Losh 2017-09-14 05:48:23 +00:00
parent 69679fc10f
commit 73f7ff91b2
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=323577
2 changed files with 39 additions and 24 deletions

View File

@ -1476,7 +1476,7 @@ Cell *bltin(Node **a, int n) /* builtin functions. a[0] is type, a[1] is arg lis
{
Cell *x, *y;
Awkfloat u;
int t;
int t, i;
Awkfloat tmp;
char *p, *buf;
Node *nextarg;
@ -1520,40 +1520,52 @@ Cell *bltin(Node **a, int n) /* builtin functions. a[0] is type, a[1] is arg lis
u = ~((int)getfval(x));
break;
case FAND:
if (nextarg == 0) {
if (nextarg == NULL) {
WARNING("and requires two arguments; returning 0");
u = 0;
break;
}
y = execute(a[1]->nnext);
u = ((int)getfval(x)) & ((int)getfval(y));
tempfree(y);
nextarg = nextarg->nnext;
i = ((int)getfval(x));
while (nextarg != NULL) {
y = execute(nextarg);
i &= (int)getfval(y);
tempfree(y);
nextarg = nextarg->nnext;
}
u = i;
break;
case FFOR:
if (nextarg == 0) {
if (nextarg == NULL) {
WARNING("or requires two arguments; returning 0");
u = 0;
break;
}
y = execute(a[1]->nnext);
u = ((int)getfval(x)) | ((int)getfval(y));
tempfree(y);
nextarg = nextarg->nnext;
i = ((int)getfval(x));
while (nextarg != NULL) {
y = execute(nextarg);
i |= (int)getfval(y);
tempfree(y);
nextarg = nextarg->nnext;
}
u = i;
break;
case FXOR:
if (nextarg == 0) {
if (nextarg == NULL) {
WARNING("xor requires two arguments; returning 0");
u = 0;
break;
}
y = execute(a[1]->nnext);
u = ((int)getfval(x)) ^ ((int)getfval(y));
tempfree(y);
nextarg = nextarg->nnext;
i = ((int)getfval(x));
while (nextarg != NULL) {
y = execute(nextarg);
i ^= (int)getfval(y);
tempfree(y);
nextarg = nextarg->nnext;
}
u = i;
break;
case FLSHIFT:
if (nextarg == 0) {
if (nextarg == NULL) {
WARNING("lshift requires two arguments; returning 0");
u = 0;
break;
@ -1564,7 +1576,7 @@ Cell *bltin(Node **a, int n) /* builtin functions. a[0] is type, a[1] is arg lis
nextarg = nextarg->nnext;
break;
case FRSHIFT:
if (nextarg == 0) {
if (nextarg == NULL) {
WARNING("rshift requires two arguments; returning 0");
u = 0;
break;

View File

@ -690,12 +690,15 @@ and returns its exit status.
.Bl -tag -width "lshift(a, b)"
.It Fn compl x
Returns the bitwise complement of integer argument x.
.It Fn and x y
Performs a bitwise AND on integer arguments x and y.
.It Fn or x y
Performs a bitwise OR on integer arguments x and y.
.It Fn xor x y
Performs a bitwise Exclusive-OR on integer arguments x and y.
.It Fn and v1 v2 ...
Performs a bitwise AND on all arguments provided, as integers.
There must be at least two values.
.It Fn or v1 v2 ...
Performs a bitwise OR on all arguments provided, as integers.
There must be at least two values.
.It Fn xor v1 v2 ...
Performs a bitwise Exclusive-OR on all arguments provided, as integers.
There must be at least two values.
.It Fn lshift x n
Returns integer argument x shifted by n bits to the left.
.It Fn rshift x n