From a5bf64b061d73dddf7f2393e4042715553be0a3a Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Mon, 29 Jul 2019 13:22:15 -0400 Subject: [PATCH] Add bitwise logical functions --- doc/teapot.lyx | 81 ++++++++++++++++++++++++++++++++++++++++++++--- src/common/func.c | 38 ++++++++++++++++++++++ 2 files changed, 114 insertions(+), 5 deletions(-) diff --git a/doc/teapot.lyx b/doc/teapot.lyx index 694bdf7..86b5361 100644 --- a/doc/teapot.lyx +++ b/doc/teapot.lyx @@ -5546,7 +5546,7 @@ abs x \emph default -) +) \series default \end_layout @@ -5775,7 +5775,78 @@ x x \emph default is given in radians. - +\end_layout + +\begin_layout Description + +\series medium +int +\begin_inset space ~ +\end_inset + + +\series default +bitand +\series medium +(int +\emph on + +\begin_inset space ~ +\end_inset + +v1, +\begin_inset space ~ +\end_inset + +... +\emph default +) evaluates to the bitwise +\begin_inset Quotes eld +\end_inset + +and +\begin_inset Quotes erd +\end_inset + + of all the supplied values. +\end_layout + +\begin_layout Description + +\series medium +int +\begin_inset space ~ +\end_inset + + +\series default +bitor +\series medium +(int +\emph on + +\begin_inset space ~ +\end_inset + +v +\emph default +1, +\emph on + +\begin_inset space ~ +\end_inset + + +\emph default +...) evaluates to the bitwise +\begin_inset Quotes eld +\end_inset + +or +\begin_inset Quotes erd +\end_inset + + of all the supplied values. \end_layout \begin_layout Description @@ -6788,7 +6859,7 @@ string x \emph default -) +) \series default \end_layout @@ -7084,7 +7155,7 @@ x l \emph default -]) +]) \series default \end_layout @@ -7108,7 +7179,7 @@ y l \emph default -]) +]) \series default \end_layout diff --git a/src/common/func.c b/src/common/func.c index 8621a9f..5b35490 100644 --- a/src/common/func.c +++ b/src/common/func.c @@ -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 } }; /*}}}*/