home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #4 / amigaacscoverdisc1998-041998.iso / utilities / shareware / dev / ppcsmalleiffel / lib_se / assertion.e < prev    next >
Encoding:
Text File  |  1998-01-16  |  6.0 KB  |  261 lines

  1. --          This file is part of SmallEiffel The GNU Eiffel Compiler.
  2. --          Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE
  3. --            Dominique COLNET and Suzanne COLLIN - colnet@loria.fr 
  4. --                       http://www.loria.fr/SmallEiffel
  5. -- SmallEiffel is  free  software;  you can  redistribute it and/or modify it 
  6. -- under the terms of the GNU General Public License as published by the Free
  7. -- Software  Foundation;  either  version  2, or (at your option)  any  later 
  8. -- version. SmallEiffel is distributed in the hope that it will be useful,but
  9. -- WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. -- or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU General Public License 
  11. -- for  more  details.  You  should  have  received a copy of the GNU General 
  12. -- Public  License  along  with  SmallEiffel;  see the file COPYING.  If not,
  13. -- write to the  Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  14. -- Boston, MA 02111-1307, USA.
  15. --
  16. class ASSERTION
  17.    -- 
  18.    -- To store one assertion. 
  19.    --
  20.    
  21. inherit GLOBALS;
  22.    
  23. creation make
  24.    
  25. feature 
  26.    
  27.    tag: TAG_NAME;
  28.    
  29.    expression: EXPRESSION;
  30.    
  31.    comment: COMMENT;
  32.    
  33.    current_type: TYPE;
  34.    
  35. feature {NONE}
  36.    
  37.    make(t: like tag; exp: like expression; c: like comment) is
  38.       require
  39.      t /= void or exp /= Void or c /= Void;
  40.       do
  41.      tag := t;
  42.      expression := exp;
  43.          comment := c;
  44.       ensure     
  45.      tag = t;
  46.      expression = exp;
  47.          comment = c;
  48.       end;
  49.    
  50. feature 
  51.    
  52.    start_position: POSITION is
  53.       do
  54.      if tag /= Void then
  55.         Result := tag.start_position;
  56.      elseif expression /= Void then
  57.         Result := expression.start_position;
  58.      else
  59.         Result := comment.start_position;
  60.      end;
  61.       end;
  62.    
  63.    pretty_print is
  64.       require
  65.      fmt.indent_level >= 1;
  66.       do
  67.      if tag /= Void then
  68.         fmt.put_string(tag.to_string);
  69.         fmt.put_string(": ");
  70.      end;
  71.      if expression /= Void then
  72.         expression.pretty_print;
  73.         if fmt.semi_colon_flag then
  74.            fmt.put_string("; ");
  75.         end;
  76.      end;
  77.      if comment /= Void then
  78.         comment.pretty_print;
  79.      end;
  80.       end;
  81.    
  82.    short(h01,r01,h02,r02,h03,r03,h04,r04,h05,r05,h06,r06,h07,r07,
  83.      h08,r08,h09,r09,h10,r10,h11,r11,h12,r12,h13,r13: STRING) is
  84.       do
  85.      short_print.hook_or(h01,r01);
  86.      if tag = Void then
  87.         short_print.hook_or(h02,r02);
  88.      else 
  89.         short_print.hook_or(h03,r03);
  90.         tag.short;
  91.         short_print.hook_or(h04,r04);
  92.      end;
  93.      if expression = Void then
  94.         short_print.hook_or(h05,r05);
  95.      else
  96.         short_print.hook_or(h06,r06);
  97.         expression.short;
  98.         short_print.hook_or(h07,r07);
  99.      end;
  100.      if comment = Void then
  101.         short_print.hook_or(h08,r08);
  102.      else
  103.         short_print.hook_or(h09,r09);
  104.         comment.short(h10,r10,h11,r11);
  105.         short_print.hook_or(h12,r12);
  106.      end;
  107.      short_print.hook_or(h13,r13);
  108.       end;
  109.    
  110.    to_runnable(ct: TYPE): like Current is
  111.       require
  112.      ct.is_run_type;
  113.       local
  114.      e: like expression;
  115.       do
  116.      if current_type = Void then
  117.         current_type := ct;
  118.         Result := Current;
  119.         if expression /= Void then
  120.            e := expression.to_runnable(ct);
  121.            if e = Void then
  122.           error(start_position,fz_bad_assertion);
  123.            else
  124.           expression := e;
  125.           if not expression.result_type.is_boolean then
  126.              eh.add_type(expression.result_type,fz_is_not_boolean);
  127.              error(start_position,fz_bad_assertion);
  128.           end;
  129.            end;
  130.         end;     
  131.      else
  132.         !!Result.make(tag,expression,comment);
  133.         Result := Result.to_runnable(ct);
  134.      end;
  135.       ensure
  136.      nb_errors = 0 implies Result.is_checked;
  137.       end;
  138.    
  139.    is_checked: BOOLEAN is
  140.       do
  141.      Result := current_type /= Void;
  142.       end;
  143.    
  144.    use_current: BOOLEAN is
  145.       do
  146.      if expression /= Void then
  147.         Result := expression.use_current;
  148.      end;
  149.       end;
  150.    
  151.    afd_check is
  152.       require
  153.      is_checked
  154.       do
  155.      if expression /= Void then
  156.         expression.afd_check;
  157.      end;
  158.       end;
  159.  
  160.    compile_to_c is
  161.       require
  162.      is_checked
  163.       do
  164.      if expression /= Void then
  165.         cpp.check_assertion(expression);
  166.      end;
  167.       end;
  168.  
  169.    is_pre_computable: BOOLEAN is
  170.       do
  171.      if expression = Void then
  172.         Result := true;
  173.      else
  174.         Result := expression.is_pre_computable;
  175.      end;
  176.       end;
  177.  
  178.    compile_to_c_old is
  179.       require
  180.      is_checked
  181.       do
  182.      if expression /= Void then
  183.         expression.compile_to_c_old;
  184.      end;
  185.       end;
  186.  
  187.    compile_to_jvm_old is
  188.       require
  189.      is_checked
  190.       do
  191.      if expression /= Void then
  192.         expression.compile_to_jvm_old;
  193.      end;
  194.       end;
  195.    
  196.    compile_to_jvm(last_chance: BOOLEAN) is
  197.      -- According to the value of `last_chance', two kind of code
  198.      -- is produced.
  199.      --
  200.      -- 1/ `last_chance' is true. This means that the assertion 
  201.      -- must be true. The genarated code includes an error 
  202.      -- message to be printed when assertion is false. No result 
  203.      -- value is pushed on the JVM stack for the caller.
  204.      --
  205.      -- 2/ `last_chance' is false. This means that the assertion 
  206.      -- may be false (inherited require). No code is produced for
  207.      -- error messages. The result of the expression is pushed on 
  208.      -- the JVM stack to be used by the caller.
  209.      --
  210.       require
  211.      is_checked
  212.       local
  213.      point1, idx: INTEGER;
  214.      ca: like code_attribute;
  215.       do
  216.      ca := code_attribute;
  217.      if expression = Void then
  218.         if last_chance then
  219.         else
  220.            ca.opcode_iconst_1;
  221.         end;
  222.      else
  223.         expression.compile_to_jvm
  224.         if last_chance then
  225.            point1 := code_attribute.opcode_ifne;
  226.            idx := idx_error_message;
  227.            ca.opcode_system_err_println(idx);
  228.            ca.opcode_aconst_null;
  229.            ca.opcode_athrow;
  230.            ca.resolve_u2_branch(point1);
  231.         end;
  232.      end;
  233.       end;
  234.  
  235. feature {NONE}
  236.  
  237.    idx_error_message: INTEGER is
  238.       local
  239.      sp: POSITION;
  240.       do
  241.      tmp_string.copy(fz_50);
  242.      sp := expression.start_position;
  243.      if sp /= Void then
  244.         tmp_string.extend(' ');
  245.         sp.append_in(tmp_string);
  246.      end;
  247.      Result := constant_pool.idx_string(tmp_string);
  248.       end;
  249.  
  250.    tmp_string: STRING is
  251.       once
  252.      !!Result.make(128);
  253.       end;
  254.  
  255. invariant
  256.    
  257.    tag /= Void or expression /= Void or comment /= Void;
  258.    
  259. end -- ASSERTION
  260.  
  261.