home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #4 / amigaacscoverdisc1998-041998.iso / utilities / shareware / dev / ppcsmalleiffel / lib_se / ifthen.e < prev    next >
Encoding:
Text File  |  1998-01-16  |  5.6 KB  |  230 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 IFTHEN
  17. --
  18. -- Note : it is not really a complete Eiffel INSTRUCTION. 
  19. -- It is just a small part of an IFTHENELSE. 
  20. --
  21.  
  22. inherit IF_GLOBALS;
  23.  
  24. creation make
  25.    
  26. feature 
  27.    
  28.    expression: EXPRESSION;
  29.    
  30.    then_compound: COMPOUND;
  31.      -- Not Void if any.
  32.      
  33.    run_compound: COMPOUND;
  34.      -- Not Void when instruction is checked for.
  35.  
  36. feature {NONE}
  37.  
  38.    point2: INTEGER;
  39.      -- To reach the end of IFTHENELSE.
  40.  
  41. feature 
  42.    
  43.    make(exp: like expression; tc: like then_compound) is
  44.       require
  45.      exp /= void;
  46.       do
  47.      expression := exp;
  48.      then_compound := tc;
  49.       ensure      
  50.            expression = exp;
  51.      then_compound = tc;
  52.       end;
  53.  
  54. feature {IFTHENLIST}
  55.  
  56.    afd_check is
  57.       do
  58.      expression.afd_check;
  59.      if then_compound /= Void then
  60.         then_compound.afd_check;
  61.      end;
  62.       end;
  63.  
  64. feature
  65.    
  66.    current_type: TYPE is
  67.       do
  68.      Result := run_compound.current_type;
  69.       end;
  70.    
  71.    compile_to_c(need_else: BOOLEAN): INTEGER is
  72.       local
  73.            trace: BOOLEAN;
  74.       do
  75.      if expression.is_static then
  76.         cpp.incr_static_expression_count;
  77.         if expression.static_value = 1 then
  78.            print_else(need_else); 
  79.            cpp.put_string("{/*AT*/");
  80.            if then_compound /= Void then
  81.           then_compound.compile_to_c;
  82.            end;
  83.            cpp.put_string("}%N");
  84.            Result := static_true;
  85.         else
  86.            cpp.put_string("/*AF*/");
  87.            Result := static_false;
  88.         end;
  89.      else
  90.         Result := non_static;
  91.         trace := not expression.c_simple and then run_control.no_check;
  92.         print_else(need_else); 
  93.         cpp.put_string("if (");
  94.         if trace then
  95.            cpp.trace_boolean_expression(expression);
  96.         else
  97.            expression.compile_to_c;
  98.         end;
  99.         cpp.put_string(") {%N");
  100.         if then_compound /= Void then
  101.            then_compound.compile_to_c;
  102.         end;
  103.         cpp.put_string("}%N");
  104.      end;
  105.       end;
  106.    
  107.    compile_to_jvm: INTEGER is
  108.       local
  109.      point1: INTEGER;
  110.       do
  111.      if expression.is_static then
  112.         jvm.incr_static_expression_count;
  113.         if expression.static_value = 1 then
  114.            if then_compound /= Void then
  115.           then_compound.compile_to_jvm;
  116.            end;
  117.            Result := static_true;
  118.         else
  119.            Result := static_false;
  120.         end;
  121.      else
  122.         Result := non_static;
  123.         point1 := expression.jvm_branch_if_false;
  124.         if then_compound /= Void then
  125.            then_compound.compile_to_jvm;
  126.         end;
  127.         point2 := code_attribute.opcode_goto;
  128.         code_attribute.resolve_u2_branch(point1);
  129.      end;
  130.       ensure
  131.      (<<static_true,static_false,non_static>>).fast_has(Result)
  132.       end;
  133.    
  134.    compile_to_jvm_resolve_branch: INTEGER is
  135.       do
  136.      if expression.is_static then
  137.         if expression.static_value = 1 then
  138.            Result := static_true;
  139.         else
  140.            Result := static_false;
  141.         end;
  142.      else
  143.         Result := non_static;
  144.         if point2 > 0 then
  145.            code_attribute.resolve_u2_branch(point2);
  146.         end;
  147.      end;
  148.       ensure
  149.      (<<static_true,static_false,non_static>>).fast_has(Result)
  150.       end;
  151.  
  152.    use_current: BOOLEAN is
  153.       do
  154.      Result := expression.use_current;
  155.      if not Result and then then_compound /= Void then
  156.         Result := then_compound.use_current;
  157.      end;
  158.       end;
  159.    
  160.    start_position: POSITION is
  161.       do
  162.      Result := expression.start_position;
  163.       end;
  164.       
  165.    pretty_print is
  166.       do
  167.      fmt.level_incr;
  168.      fmt.set_semi_colon_flag(false);
  169.      expression.pretty_print;
  170.      fmt.level_decr;
  171.      fmt.keyword("then");
  172.      fmt.indent;
  173.      if then_compound /= Void then
  174.         then_compound.pretty_print;
  175.      end;
  176.       end;
  177.    
  178.    to_runnable(rc: COMPOUND): like Current is
  179.       local 
  180.      e: like expression;
  181.      tc: like then_compound;
  182.      t: TYPE;
  183.       do
  184.      if run_compound = Void then
  185.         run_compound := rc;
  186.         e := expression.to_runnable(current_type)
  187.         if e = Void then
  188.            error(expression.start_position,
  189.              "Bad BOOLEAN expression.");
  190.         else
  191.            expression := e;
  192.            t := expression.result_type;
  193.            if not t.is_boolean then
  194.           eh.append("Expression of if/elseif must be BOOLEAN. ");
  195.           eh.add_type(expression.result_type,fz_is_not_boolean);
  196.           eh.add_position(expression.start_position);
  197.           eh.print_as_error;
  198.            end;
  199.         end;
  200.         if then_compound /= Void then
  201.            tc := then_compound.to_runnable(current_type);
  202.            if tc /= Void then
  203.           then_compound := tc;
  204.            end;
  205.         end;
  206.         if nb_errors = 0 then
  207.            Result := Current;
  208.         end;
  209.      else
  210.         !!Result.make(expression,then_compound);
  211.         Result := Result.to_runnable(rc);
  212.      end;
  213.       end;
  214.    
  215. feature {NONE}
  216.    
  217.    print_else(need_else: BOOLEAN) is
  218.       do
  219.      if need_else then
  220.         cpp.put_string(" else ");
  221.      end;
  222.       end;
  223.    
  224. invariant
  225.    
  226.    expression /= Void;
  227.    
  228. end -- IFTHEN
  229.  
  230.