home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #4 / amigaacscoverdisc1998-041998.iso / utilities / shareware / dev / ppcsmalleiffel / lib_se / ifthenelse.e < prev    next >
Encoding:
Text File  |  1998-01-16  |  5.0 KB  |  223 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 IFTHENELSE
  17. --
  18. -- The conditionnal instruction : "if ... then ... elseif ... else ... end".
  19. --
  20.  
  21. inherit 
  22.    INSTRUCTION 
  23.       redefine copy, is_equal 
  24.       select fill_tagged_out_memory
  25.       end;
  26.    IF_GLOBALS
  27.       undefine fill_tagged_out_memory
  28.       end;
  29.    
  30. creation make
  31.  
  32. feature 
  33.    
  34.    start_position: POSITION;
  35.      -- Of keyword "if".
  36.    
  37.    ifthenlist: IFTHENLIST;
  38.    
  39.    else_compound: COMPOUND;
  40.      -- Not Void if any.
  41.  
  42. feature {NONE}
  43.    
  44.    make(sp: like start_position) is
  45.       do
  46.      start_position := sp;
  47.       end;
  48.    
  49. feature
  50.  
  51.    is_pre_computable: BOOLEAN is false;
  52.  
  53.    end_mark_comment: BOOLEAN is true;
  54.    
  55. feature
  56.  
  57.    use_current: BOOLEAN is
  58.       do
  59.      if ifthenlist.use_current then
  60.         Result := true;
  61.      elseif else_compound /= Void then
  62.         Result := else_compound.use_current;
  63.      end;
  64.       end;
  65.  
  66.    afd_check is
  67.       do
  68.      ifthenlist.afd_check;
  69.      if else_compound /= Void then
  70.         else_compound.afd_check;
  71.      end;
  72.       end;
  73.  
  74.    compile_to_c is   
  75.       local
  76.      static_value: INTEGER;
  77.       do
  78.      check
  79.         ifthenlist.count > 0
  80.      end;
  81.      cpp.put_string("/*IF*/");
  82.      static_value := ifthenlist.compile_to_c;
  83.      inspect
  84.         static_value
  85.      when static_false then
  86.         cpp.put_string("/*AE*/%N");
  87.         if else_compound /= Void then
  88.            else_compound.compile_to_c;
  89.         end;
  90.      when static_true then
  91.      when non_static then
  92.         if else_compound /= Void then
  93.            cpp.put_string("else {%N");
  94.            else_compound.compile_to_c;
  95.            cpp.put_string("}%N");
  96.         end;
  97.      end;
  98.      cpp.put_string("/*FI*/");
  99.       end;
  100.    
  101.    compile_to_jvm is
  102.       local
  103.      static_value: INTEGER;
  104.       do
  105.      check
  106.         ifthenlist.count > 0
  107.      end;
  108.      static_value := ifthenlist.compile_to_jvm;
  109.      inspect
  110.         static_value
  111.      when static_false then
  112.         -- Always else :
  113.         if else_compound /= Void then
  114.            else_compound.compile_to_jvm;
  115.         end;
  116.      when static_true then
  117.         -- Never else :
  118.         ifthenlist.compile_to_jvm_resolve_branch;
  119.      when non_static then
  120.         -- Else is possible :
  121.         if else_compound /= Void then
  122.            else_compound.compile_to_jvm;
  123.         end;
  124.         ifthenlist.compile_to_jvm_resolve_branch;
  125.      end;
  126.       end;
  127.    
  128.    to_runnable(rc: like run_compound): like Current is
  129.       local
  130.      ne: INTEGER;
  131.      itl: like ifthenlist;
  132.      ec: like else_compound;
  133.       do
  134.      ne := nb_errors;
  135.      if run_compound = Void then
  136.         run_compound := rc;
  137.         itl := ifthenlist.to_runnable(rc);
  138.         if itl = Void then
  139.            check
  140.           nb_errors - ne > 0
  141.            end;
  142.         else
  143.            ifthenlist := itl;
  144.         end;
  145.         if nb_errors - ne = 0 and then else_compound /= Void then
  146.            ec := else_compound.to_runnable(current_type);
  147.            if ec = Void then
  148.           check
  149.              nb_errors - ne > 0
  150.           end;
  151.            else
  152.           else_compound := ec;
  153.            end;
  154.         end;
  155.         if itl /= Void then
  156.            Result := Current
  157.         end;
  158.      else
  159.         Result := twin.to_runnable(rc);
  160.      end;
  161.       end;
  162.    
  163.    copy(other: like Current) is
  164.       do
  165.      start_position := other.start_position;
  166.      ifthenlist := other.ifthenlist;
  167.      else_compound := other.else_compound;
  168.       end;
  169.    
  170.    is_equal(other: like Current): BOOLEAN is
  171.       do
  172.      if Current = other then
  173.         Result := true;
  174.      else
  175.         Result := start_position = other.start_position;
  176.      end;
  177.       end;
  178.       
  179.    add_if_then(expression: EXPRESSION; then_compound: COMPOUND) is
  180.       require
  181.      expression /= void;
  182.       local
  183.      ifthen: IFTHEN;
  184.       do
  185.      !!ifthen.make(expression,then_compound);
  186.      if ifthenlist = Void then
  187.         !!ifthenlist.make(<<ifthen>>);
  188.      else
  189.         ifthenlist.add_last(ifthen);
  190.      end;
  191.       end;
  192.  
  193.    pretty_print is
  194.       do
  195.      check
  196.         ifthenlist.count > 0;
  197.      end;
  198.      fmt.keyword("if");
  199.      ifthenlist.pretty_print;
  200.      if else_compound /= Void then
  201.         fmt.indent;
  202.         fmt.keyword("else");
  203.         else_compound.pretty_print;
  204.      end;
  205.      fmt.indent;
  206.      fmt.keyword("end;");
  207.      if fmt.print_end_if then
  208.         fmt.put_end("if");
  209.      end;
  210.       end;
  211.  
  212. feature {EIFFEL_PARSER}
  213.    
  214.    set_else_compound(ec: like else_compound) is
  215.       do
  216.      else_compound := ec;
  217.       ensure
  218.      else_compound = ec;
  219.       end;
  220.    
  221. end -- IFTHENELSE
  222.  
  223.