Back to index

texmacs  1.0.7.15
evaluate_boolean.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : evaluate_boolean.cpp
00004 * DESCRIPTION: boolean operations and common predicates
00005 * COPYRIGHT  : (C) 2006  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #include "evaluate_main.hpp"
00013 
00014 /******************************************************************************
00015 * Boolean operations
00016 ******************************************************************************/
00017 
00018 tree
00019 evaluate_or (tree t) {
00020   if (N(t)<2) return evaluate_error ("bad or");
00021   for (int i=0; i<N(t); i++) {
00022     tree u= evaluate (t[i]);
00023     if (!is_bool (u)) return evaluate_error ("bad or");
00024     if (as_bool (u)) return as_string_bool (true);
00025   }
00026   return as_string_bool (false);
00027 }
00028 
00029 tree
00030 evaluate_xor (tree t) {
00031   if (N(t)!=2) return evaluate_error ("bad xor");
00032   tree t1= evaluate (t[0]);
00033   tree t2= evaluate (t[1]);
00034   if (!is_bool (t1) || !is_bool (t2))
00035     return evaluate_error ("bad xor");
00036   return as_string_bool (as_bool (t1) ^ as_bool (t2));
00037 }
00038 
00039 tree
00040 evaluate_and (tree t) {
00041   if (N(t)<2) return evaluate_error ("bad and");
00042   for (int i=0; i<N(t); i++) {
00043     tree u= evaluate (t[i]);
00044     if (!is_bool (u)) return evaluate_error ("bad and");
00045     if (!as_bool (u)) return as_string_bool (false);
00046   }
00047   return as_string_bool (true);
00048 }
00049 
00050 tree
00051 evaluate_not (tree t) {
00052   if (N(t)!=1) return evaluate_error ("bad not");
00053   tree u= evaluate(t[0]);
00054   if (!is_bool (u)) return evaluate_error ("bad not");
00055   return as_string_bool (!as_bool (u));
00056 }
00057 
00058 /******************************************************************************
00059 * Predicates
00060 ******************************************************************************/
00061 
00062 tree
00063 evaluate_equal (tree t) {
00064   if (N(t)!=2) return evaluate_error ("bad equal");
00065   tree t1= evaluate (t[0]);
00066   tree t2= evaluate (t[1]);
00067   if (is_atomic (t1) && is_atomic (t2) &&
00068       is_length (t1->label) && is_length (t2->label))
00069     return as_string_bool (as_length (t1) == as_length (t2));
00070   return as_string_bool (t1 == t2);
00071 }
00072 
00073 tree
00074 evaluate_unequal (tree t) {
00075   if (N(t)!=2) return evaluate_error ("bad unequal");
00076   tree t1= evaluate (t[0]);
00077   tree t2= evaluate (t[1]);
00078   if (is_atomic (t1) && is_atomic (t2) &&
00079       is_length (t1->label) && is_length (t2->label))
00080     return as_string_bool (as_length (t1) != as_length (t2));
00081   return as_string_bool (t1 != t2);
00082 }
00083 
00084 tree
00085 evaluate_less (tree t) {
00086   if (N(t)!=2) return evaluate_error ("bad less");
00087   tree t1= evaluate (t[0]);
00088   tree t2= evaluate (t[1]);
00089   if (is_compound (t1) || is_compound (t2))
00090     return evaluate_error ("bad less");
00091   string s1= t1->label;
00092   string s2= t2->label;
00093   if (is_double (s1) && is_double (s2))
00094     return as_string_bool (as_double (s1) < as_double (s2));
00095   if (is_length (s1) && is_length (s2))
00096     return as_string_bool (as_length (s1) < as_length (s2));
00097   return evaluate_error ("bad less");
00098 }
00099 
00100 tree
00101 evaluate_lesseq (tree t) {
00102   if (N(t)!=2) return evaluate_error ("bad less or equal");
00103   tree t1= evaluate (t[0]);
00104   tree t2= evaluate (t[1]);
00105   if (is_compound (t1) || is_compound (t2))
00106     return evaluate_error ("bad less or equal");
00107   string s1= t1->label;
00108   string s2= t2->label;
00109   if (is_double (s1) && (is_double (s2)))
00110     return as_string_bool (as_double (s1) <= as_double (s2));
00111   if (is_length (s1) && is_length (s2))
00112     return as_string_bool (as_length (s1) <= as_length (s2));
00113   return evaluate_error ("bad less or equal");
00114 }
00115 
00116 tree
00117 evaluate_greater (tree t) {
00118   if (N(t)!=2) return evaluate_error ("bad greater");
00119   tree t1= evaluate (t[0]);
00120   tree t2= evaluate (t[1]);
00121   if (is_compound (t1) || is_compound (t2))
00122     return evaluate_error ("bad greater");
00123   string s1= t1->label;
00124   string s2= t2->label;
00125   if (is_double (s1) && (is_double (s2)))
00126     return as_string_bool (as_double (s1) > as_double (s2));
00127   if (is_length (s1) && is_length (s2))
00128     return as_string_bool (as_length (s1) > as_length (s2));
00129   return evaluate_error ("bad greater");
00130 }
00131 
00132 tree
00133 evaluate_greatereq (tree t) {
00134   if (N(t)!=2) return evaluate_error ("bad greater or equal");
00135   tree t1= evaluate (t[0]);
00136   tree t2= evaluate (t[1]);
00137   if (is_compound (t1) || is_compound (t2))
00138     return evaluate_error ("bad greater or equal");
00139   string s1= t1->label;
00140   string s2= t2->label;
00141   if (is_double (s1) && (is_double (s2)))
00142     return as_string_bool (as_double (s1) >= as_double (s2));
00143   if (is_length (s1) && is_length (s2))
00144     return as_string_bool (as_length (s1) >= as_length (s2));
00145   return evaluate_error ("bad greater or equal");
00146 }