home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sa104os2.zip / SATHR104.ZIP / SATHER / LIBRARY / BOOL.SA < prev    next >
Text File  |  1995-02-05  |  7KB  |  178 lines

  1. -- Copyright (C) International Computer Science Institute, 1994.  COPYRIGHT  --
  2. -- NOTICE: This code is provided "AS IS" WITHOUT ANY WARRANTY and is subject --
  3. -- to the terms of the SATHER LIBRARY GENERAL PUBLIC LICENSE contained in    --
  4. -- the file "Doc/License" of the Sather distribution.  The license is also   --
  5. -- available from ICSI, 1947 Center St., Suite 600, Berkeley CA 94704, USA.  --
  6. --------> Please email comments to "sather-bugs@icsi.berkeley.edu". <----------
  7.  
  8. -- bool.sa: Boolean values.
  9. -------------------------------------------------------------------
  10. value class BOOL is
  11.    -- BOOL objects represent boolean values and are either equal to 
  12.    -- `true' or `false'. The boolean operators `and' and `or' are 
  13.    -- part of the Sather language. This class defines several 
  14.    -- additional operators.
  15.  
  16.    not:SAME is
  17.       -- The complement of self.
  18.       -- if self then return false else return true end 
  19.       raise "BOOL::not:SAME undefined." end;
  20.    
  21.    xnor(b:SAME):SAME is
  22.       -- True if self and `b' have the same value (same as "is_eq").
  23.       return self=b end;   
  24.    
  25.    is_eq(b:SAME):SAME is
  26.       -- True if self and `b' have the same value (same as "xnor").
  27.       raise "BOOL::is_eq(BOOL):BOOL undefined"; end;
  28.    
  29.    xor(b:SAME):SAME is
  30.       -- Self exclusive ored with `b'.  Same as "is_neq".
  31.       return self/=b end;
  32.    
  33.    is_neq(b:SAME):SAME is
  34.       -- True if self and `b' have the same value (same as "xor").
  35.       raise "BOOL::is_neq(BOOL):BOOL undefined"; end;
  36.    
  37.    nand(b:SAME):SAME is
  38.       -- The complement of self anded with `b'.
  39.       return ~(self and b) end;
  40.    
  41.    nor(b:SAME):SAME is
  42.       -- The complement of self ored with `b'.
  43.       return ~(self or b) end;
  44.    
  45.    implies(b:SAME):SAME is
  46.       -- True iff self implies `b'. Same as "nand_not".
  47.       return not or b end;
  48.    
  49.    and_rout(b:SAME):SAME is
  50.       -- A routine version of "self and `b'". (Useful for making
  51.       -- bound routines.)
  52.       return self and b end;
  53.    
  54.    or_rout(b:SAME):SAME is
  55.       -- A routine version of "self or `b'". (Useful for making
  56.       -- bound routines.)
  57.       return self or b end;   
  58.  
  59.    and_not(b:SAME):SAME is
  60.       -- Computes self and the complement of `b'.
  61.       return self and ~b end;
  62.  
  63.    or_not(b:SAME):SAME is
  64.       -- Computes self or the complement of `b'.
  65.       return self or ~b end;
  66.    
  67.    nand_not(b:SAME):SAME is
  68.       -- Computes self nand the complement of `b'. This is the same as
  69.       -- the complement of self or `b'.
  70.       return not or b end;
  71.  
  72.    nor_not(b:SAME):SAME is
  73.       -- Computes self nor the complement of `b'. This is the same as
  74.       -- the complement of self and `b'.
  75.       return not and b end;
  76.    
  77.    str:STR is
  78.       -- The string representation of self.
  79. --    if self then return "true" else return "false" end end;                   -- NLP
  80.       if self then return "true"; end; return "false"; end;                     -- NLP
  81.  
  82.    from_str(s:STR):SAME is
  83.        case s
  84.        when "true","t","True","T","TRUE" then return true;
  85.        when "false","f","False","F","FALSE" then return false;
  86.        else raise "Can't interpret bool value: "+s;
  87.        end;
  88.        return false;                                                            -- NLP
  89.    end;
  90.  
  91. end; -- value class BOOL
  92.  
  93. -------------------------------------------------------------------
  94. class TEST_BOOL is
  95.    -- Test of BOOL.
  96.    include TEST;
  97.    
  98.    main is
  99.       -- Test of BOOL.
  100.       class_name("BOOL");
  101.       
  102.       test("false.not", false.not.str, "true");
  103.       test("true.not", true.not.str, "false");
  104.       
  105.       test("false and false", (false and false).str, "false");
  106.       test("false and true", (false and true).str, "false");
  107.       test("true and false", (true and false).str, "false");
  108.       test("true and true", (true and true).str, "true");
  109.       
  110.       test("false or false", (false or false).str, "false");
  111.       test("false or true", (false or true).str, "true");
  112.       test("true or false", (true or false).str, "true");
  113.       test("true or true", (true or true).str, "true");
  114.       
  115.       test("false.xor(false)", (false.xor(false)).str, "false");
  116.       test("false.xor(true)", (false.xor(true)).str, "true");
  117.       test("true.xor(false)", (true.xor(false)).str, "true");
  118.       test("true.xor(true)", (true.xor(true)).str, "false");
  119.       
  120.       test("false.xnor(false)", (false.xnor(false)).str, "true");
  121.       test("false.xnor(true)", (false.xnor(true)).str, "false");
  122.       test("true.xnor(false)", (true.xnor(false)).str, "false");
  123.       test("true.xnor(true)", (true.xnor(true)).str, "true");
  124.  
  125.       test("false.nand(false)", (false.nand(false)).str, "true");
  126.       test("false.nand(true)", (false.nand(true)).str, "true");
  127.       test("true.nand(false)", (true.nand(false)).str, "true");
  128.       test("true.nand(true)", (true.nand(true)).str, "false");
  129.  
  130.       test("false.nor(false)", (false.nor(false)).str, "true");
  131.       test("false.nor(true)", (false.nor(true)).str, "false");
  132.       test("true.nor(false)", (true.nor(false)).str, "false");
  133.       test("true.nor(true)", (true.nor(true)).str, "false");
  134.  
  135.       test("false.implies(false)", (false.implies(false)).str, "true");
  136.       test("false.implies(true)", (false.implies(true)).str, "true");
  137.       test("true.implies(false)", (true.implies(false)).str, "false");
  138.       test("true.implies(true)", (true.implies(true)).str, "true");
  139.       
  140.       test("false.and_rout(false)", false.and_rout(false).str, "false");
  141.       test("false.and_rout(true)", false.and_rout(true).str, "false");
  142.       test("true.and_rout(false)", true.and_rout(false).str, "false");
  143.       test("true.and_rout(true)", true.and_rout(true).str, "true");
  144.       
  145.       test("false.or_rout(false)", false.or_rout(false).str, "false");
  146.       test("false.or_rout(true)", false.or_rout(true).str, "true");
  147.       test("true.or_rout(false)", true.or_rout(false).str, "true");
  148.       test("true.or_rout(true)", true.or_rout(true).str, "true");
  149.       
  150.       test("false.nor_not(false)", false.nor_not(false).str, "false");
  151.       test("false.nor_not(true)", false.nor_not(true).str, "true");
  152.       test("true.nor_not(false)", true.nor_not(false).str, "false");
  153.       test("true.nor_not(true)", true.nor_not(true).str, "false");
  154.       
  155.       test("false.and_not(false)", false.and_not(false).str, "false");
  156.       test("false.and_not(true)", false.and_not(true).str, "false");
  157.       test("true.and_not(false)", true.and_not(false).str, "true");
  158.       test("true.and_not(true)", true.and_not(true).str, "false");
  159.  
  160.       test("false.nand_not(false)", false.nand_not(false).str, "true");
  161.       test("false.nand_not(true)", false.nand_not(true).str, "true");
  162.       test("true.nand_not(false)", true.nand_not(false).str, "false");
  163.       test("true.nand_not(true)", true.nand_not(true).str, "true");
  164.       
  165.       test("false.or_not(false)", false.or_not(false).str, "true");
  166.       test("false.or_not(true)", false.or_not(true).str, "false");
  167.       test("true.or_not(false)", true.or_not(false).str, "true");
  168.       test("true.or_not(true)", true.or_not(true).str, "true");
  169.       
  170.       test("true.str", true.str, "true");
  171.       test("false.str", false.str, "false");
  172.       
  173.       finish end;
  174.  
  175. end; 
  176.  
  177. -------------------------------------------------------------------
  178.