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