Subject: | |
From: | |
Reply To: | |
Date: | Tue, 21 Sep 2010 12:45:15 +0100 |
Content-Type: | text/plain |
Parts/Attachments: |
|
|
On 21/09/2010 06:32, Arno Trautmann wrote:
>> Why not just use&& in the first place?
>
> Because of possible catcode-troubles? ;)
> Or maybe such a list might be more useful for a xor operation.
How does the following look? The Boolean expressions are the most basic
versions (NOT takes one argument, others take two), but nesting seems to
work fine. There is definitely less code to this than the current
implementation, so there would be a performance gain.
\documentclass{article}
\usepackage{expl3}
\ExplSyntaxOn
\cs_set_nopar:Npn \bool_if:nT #1 {
\tex_ifodd:D \bool_if_p:n {#1}
\tex_expandafter:D \use:n
\tex_else:D
\tex_expandafter:D \use_none:n
\tex_fi:D
}
\cs_set_nopar:Npn \bool_if:nF #1 {
\tex_ifodd:D \bool_if_p:n {#1}
\tex_expandafter:D \use_none:n
\tex_else:D
\tex_expandafter:D \use:n
\tex_fi:D
}
\cs_set_nopar:Npn \bool_if:nTF #1 {
\tex_ifodd:D \bool_if_p:n {#1}
\tex_expandafter:D \use_i:nn
\tex_else:D
\tex_expandafter:D \use_ii:nn
\tex_fi:D
}
\cs_set_nopar:Npn \bool_if_p:n #1 {
\tex_number:D #1 ~
}
\cs_set_nopar:Npn \bool_not_p:n #1 {
\tex_ifnum:D \tex_number:D #1 = \c_true_bool
\c_false_bool
\tex_else:D
\c_true_bool
\tex_fi:D
}
\cs_set_nopar:Npn \bool_and_p:nn #1#2 {
\tex_ifnum:D \tex_number:D #1 = \c_true_bool
\tex_ifnum:D \tex_number:D #2 = \c_true_bool
\c_true_bool
\tex_else:D
\c_false_bool
\tex_fi:D
\tex_else:D
\c_false_bool
\tex_fi:D
}
\cs_set_nopar:Npn \bool_or_p:nn #1#2 {
\tex_ifnum:D \tex_number:D #1 = \c_true_bool
\c_true_bool
\tex_else:D
\tex_ifnum:D \tex_number:D #2 = \c_true_bool
\c_true_bool
\tex_else:D
\c_false_bool
\tex_fi:D
\tex_fi:D
}
\cs_set_nopar:Npn \bool_xor_p:nn #1#2 {
\tex_ifnum:D \tex_number:D #1 = \c_true_bool
\tex_ifnum:D \tex_number:D #2 = \c_true_bool
\c_false_bool
\tex_else:D
\c_true_bool
\tex_fi:D
\tex_else:D
\tex_ifnum:D \tex_number:D #2 = \c_true_bool
\c_false_bool
\tex_else:D
\c_true_bool
\tex_fi:D
\tex_fi:D
}
\ExplSyntaxOff
\begin{document}
\ExplSyntaxOn
\bool_if:nTF { \c_true_bool } { TRUE } { FALSE } ~
\bool_if:nTF { \bool_not_p:n { \c_true_bool } } { TRUE } { FALSE } ~
\bool_if:nTF
{
\bool_and_p:nn { \c_true_bool }
{
\bool_not_p:n { \bool_or_p:nn { \c_true_bool } { \c_false_bool } }
}
} { TRUE } { FALSE } ~
\bool_if:nTF
{
\bool_and_p:nn { \c_true_bool }
{
\bool_not_p:n { \bool_or_p:nn { \c_true_bool } { \c_false_bool } }
}
} { TRUE } { FALSE } ~
\bool_if:nTF { % LAZY EVALUATION
\bool_and_p:nn
{ \int_compare_p:nNn { 1 } = { 1 } }
{
\bool_and_p:nn
{
\bool_if_p:n
{
\bool_or_p:nn
{ \intexpr_compare_p:nNn { 2 } = { 3 } }
{
\bool_or_p:nn
{ \intexpr_compare_p:nNn { 4 } = { 4 } }
{ \intexpr_compare_p:nNn { 1 } = { \ERROR } } % Skip
}
}
}
{ \int_compare_p:nNn { 2 } = { 2 } }
}
} { TRUE } { FALSE } ~
\ExplSyntaxOff
\end{document}
--
Joseph Wright
|
|
|