#' Generation of character infix operators: `&`, `&&`, `|`, `||`
#'
#' @details
#' This function generates infix operators: `&`, `&&`, `|`, `||`
#' in the global environment, \cr
#' which are almost equivalent to, resp, `\%&\%`, `\%&&\%`, `\%|\%`, `\%||\%` .
#' \cr
#' The difference is that `&` has precedence over `|`  but `\%&\%` does NOT.
#' \cr For example: \cr\cr
#' \code{"a" | "b" & "c"} produces \code{"(a) | ((b) & (c))"} \cr
#' but \cr
#' \code{"a" \%|\% "b" \%&\% "c"} produces \code{"((a) | (b)) & (c)"} .
#' \cr\cr
#' See the examples below.
#' \cr\cr
#' \code{ "a" & "b" } is equivalent to  \code{And( "a", "b" )}
#' and generates \code{"(a) & (b)"}.\cr
#' \code{"a" && "b"} is equivalent to  \code{And( "a", "b", oper="&&" )}
#' and generates \code{"(a) && (b)"}.\cr
#' \code{ "a" | "b" } is equivalent to \code{Or( "a", "b" )}
#' and generates \code{"(a) | (b)"}.\cr
#' \code{ "a" || "b"} is equivalent to  \code{Or( "a", "b", oper="||" )}
#' and generates \code{"(a) || (b)"}.\cr
#' \cr
#' Use \code{eval( parse(text=condition) )} to evaluate the expression.
#' \cr
#' \cr
#' To remove these operators: \cr
#'  \code{ rm(  `&`, `&&`, `|`, `||` ) }
#'
#' @return
#' Generates four infix operators in the global environment.
#'
#' @examples
#' generate_AndOr()
#'
#' "a" & "b"
#' "a" %&% "b"
#' And( "a", "b" )
#' "a" | "b"
#' "a" %|% "b"
#' Or( "a", "b" )
#'
#' # precedences
#' "a" | "b" & "c"
#' "a" %|% "b" %&% "c"
#'
#' "a" & "b" | "c"
#' "a" %&% "b" %|% "c"
#'
#'
#' @export
#'

generate_AndOr <- function(){
 # generate infix operators: `%&%`, `%&&%`, `%|%`, `%||%`
 # Shin-ichi Mayekawa
 # 20171006,07
 # roxygen2 error fixed: 20191129dc
 #

 percent=0

 if( percent == 1 ){

  `%&%` <<- function( A, B=NULL ){
   # & operator
   # Shin-ichi Mayekawa
   # 20171006

   if( is.character(A) | is.character(B) ){
    if( is.null(B) ) return( And(TRUE,A))
    else return( And(A,B) )
   }
   else{
    if( is.null(B) )  return(  .Primitive("&")(TRUE,A) )
    else return(  .Primitive("&")(A,B) )
   }
  } # end of &

  `%|%` <<- function( A, B=NULL ){
   # | operator
   # Shin-ichi Mayekawa
   # 20171006

   if( is.character(A) | is.character(B) ){
    if( is.null(B) ) return( Or(TRUE,A))
    else return( Or(A,B) )
   }
   else{
    if( is.null(B) )  return(  .Primitive("|")(FALSE,A) )
    else return(  .Primitive("|")(A,B) )
   }
  } # end of |

  `%&&%` <<- function( A, B=NULL ){
   # & operator
   # Shin-ichi Mayekawa
   # 20171006

   if( is.character(A) | is.character(B) ){
    if( is.null(B) ) return( And(TRUE,A, oper=" && "))
    else return( And(A,B, oper=" && ") )
   }
   else{
    if( is.null(B) )  return(  .Primitive("&")(TRUE,A) )
    else return(  .Primitive("&")(A,B) )
   }
  } # end of &&

  `%||%` <<- function( A, B=NULL ){
   # | operator
   # Shin-ichi Mayekawa
   # 20171006

   if( is.character(A) | is.character(B) ){
    if( is.null(B) ) return( Or(TRUE,A, oper=" || "))
    else return( Or(A,B, oper=" || ") )
   }
   else{
    if( is.null(B) )  return(  .Primitive("|")(FALSE,A) )
    else return(  .Primitive("|")(A,B) )
   }
  } # end of ||

 }
 else{

  .GlobalEnv$`&` <- function( A, B=NULL ){
   # & operator
   # Shin-ichi Mayekawa
   # 20171006

   if( .Primitive("|")(is.character(A), is.character(B)) ){
    if( is.null(B) ) return( And(TRUE,A))
    else return( And(A,B) )
   }
   else{
    if( is.null(B) )  return(  .Primitive("&")(TRUE,A) )
    else return(  .Primitive("&")(A,B) )
   }
  } # end of &

  .GlobalEnv$`|` <- function( A, B=NULL ){
   # | operator
   # Shin-ichi Mayekawa
   # 20171006

   if( .Primitive("|")(is.character(A), is.character(B)) ){
    if( is.null(B) ) return( Or(TRUE,A))
    else return( Or(A,B) )
   }
   else{
    if( is.null(B) )  return(  .Primitive("|")(FALSE,A) )
    else return(  .Primitive("|")(A,B) )
   }
  } # end of |

  .GlobalEnv$`&&` <- function( A, B=NULL ){
   # & operator
   # Shin-ichi Mayekawa
   # 20171006

   if( .Primitive("|")(is.character(A), is.character(B)) ){
    if( is.null(B) ) return( And(TRUE,A, oper=" && "))
    else return( And(A,B, oper=" && ") )
   }
   else{
    if( is.null(B) )  return(  .Primitive("&")(TRUE,A) )
    else return(  .Primitive("&")(A,B) )
   }
  } # end of &&

  .GlobalEnv$`||` <- function( A, B=NULL ){
   # | operator
   # Shin-ichi Mayekawa
   # 20171006

   if( .Primitive("|")(is.character(A), is.character(B)) ){
    if( is.null(B) ) return( Or(TRUE,A, oper=" || "))
    else return( Or(A,B, oper=" || ") )
   }
   else{
    if( is.null(B) )  return(  .Primitive("|")(FALSE,A) )
    else return(  .Primitive("|")(A,B) )
   }
  } # end of ||

  cat( "\n\n ** String Infix Operators ( `&`, `&&`, `|`, `||`  ) are"
      , " now available in the global environment. \n" )
  cat( "    To remove these operators, submit the following code:"
      , "  rm(  `&`, `&&`, `|`, `||` ) .     **\n\n" )

 }


} # end of generate_AndOr


