Add bitwise logical functions

This commit is contained in:
Glen Whitney 2019-07-29 13:22:15 -04:00
parent 1114df2e13
commit a5bf64b061
2 changed files with 114 additions and 5 deletions

View file

@ -511,6 +511,42 @@ static Token z_func(int argc, const Token argv[])
}
/*}}}*/
typedef enum { LOG_AND, LOG_OR } LogicalFunction;
static Token bitwise_func(int argc, const Token argv[], LogicalFunction lop)
{
Token result;
int accum;
if (lop == LOG_AND) accum = -1;
else accum = 0;
for (size_t i = 0; i < argc; ++i) {
if (!INTPATIBLE(argv[i]))
{
result.type = EEK;
result.u.err =
mystrmalloc(_("Bitwise functions operate only on integers."));
return result;
}
int val = 0;
if (argv[i].type == INT) val = argv[i].u.integer;
if (lop == LOG_AND) accum = accum & val;
else accum = accum | val;
}
result.type = INT;
result.u.integer = accum;
return result;
}
static Token bitand_func(int argc, const Token argv[])
{
return bitwise_func(argc, argv, LOG_AND);
}
static Token bitor_func(int argc, const Token argv[])
{
return bitwise_func(argc, argv, LOG_OR);
}
/* e */ /*{{{*/
static Token e_func(int argc, const Token argv[])
{
@ -1451,6 +1487,8 @@ Tfunc tfunc[]=
{ "substr", substr_func },
{ "strptime", strptime_func },
{ "time", time_func },
{ "bitand", bitand_func },
{ "bitor", bitor_func },
{ "", (Token (*)(int, const Token[]))0 }
};
/*}}}*/