home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Programming / sofa / archive / SmallEiffel.lha / SmallEiffel / lib_se / ifthen.e < prev    next >
Text File  |  1999-06-05  |  7KB  |  232 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 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. feature {NONE}
  34.  
  35.    current_type: TYPE;
  36.          -- Not Void when instruction is checked for.
  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.    collect_c_tmp is
  65.       do
  66.          expression.collect_c_tmp;
  67.       end;
  68.  
  69. feature
  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(fz_12);
  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(fz_if);
  94.             cpp.put_character('(');
  95.             if trace then
  96.                cpp.trace_boolean_expression(expression);
  97.             else
  98.                expression.compile_to_c;
  99.             end;
  100.             cpp.put_character(')');
  101.             cpp.put_string(fz_11);
  102.             if then_compound /= Void then
  103.                then_compound.compile_to_c;
  104.             end;
  105.             cpp.put_string(fz_12);
  106.          end;
  107.       end;
  108.  
  109.    compile_to_jvm: INTEGER is
  110.       local
  111.          point1: INTEGER;
  112.       do
  113.          if expression.is_static then
  114.             jvm.incr_static_expression_count;
  115.             if expression.static_value = 1 then
  116.                if then_compound /= Void then
  117.                   then_compound.compile_to_jvm;
  118.                end;
  119.                Result := static_true;
  120.             else
  121.                Result := static_false;
  122.             end;
  123.          else
  124.             Result := non_static;
  125.             point1 := expression.jvm_branch_if_false;
  126.             if then_compound /= Void then
  127.                then_compound.compile_to_jvm;
  128.             end;
  129.             point2 := code_attribute.opcode_goto;
  130.             code_attribute.resolve_u2_branch(point1);
  131.          end;
  132.       ensure
  133.          (<<static_true,static_false,non_static>>).fast_has(Result)
  134.       end;
  135.  
  136.    compile_to_jvm_resolve_branch: INTEGER is
  137.       do
  138.          if expression.is_static then
  139.             if expression.static_value = 1 then
  140.                Result := static_true;
  141.             else
  142.                Result := static_false;
  143.             end;
  144.          else
  145.             Result := non_static;
  146.             if point2 > 0 then
  147.                code_attribute.resolve_u2_branch(point2);
  148.             end;
  149.          end;
  150.       ensure
  151.          (<<static_true,static_false,non_static>>).fast_has(Result)
  152.       end;
  153.  
  154.    use_current: BOOLEAN is
  155.       do
  156.          Result := expression.use_current;
  157.          if not Result and then then_compound /= Void then
  158.             Result := then_compound.use_current;
  159.          end;
  160.       end;
  161.  
  162.    start_position: POSITION is
  163.       do
  164.          Result := expression.start_position;
  165.       end;
  166.  
  167.    pretty_print is
  168.       do
  169.          fmt.level_incr;
  170.          fmt.set_semi_colon_flag(false);
  171.          expression.pretty_print;
  172.          fmt.level_decr;
  173.          fmt.keyword("then");
  174.          fmt.indent;
  175.          if then_compound /= Void then
  176.             then_compound.pretty_print;
  177.          end;
  178.       end;
  179.  
  180.    to_runnable(ct: TYPE): like Current is
  181.       local
  182.          e: like expression;
  183.          tc: like then_compound;
  184.          t: TYPE;
  185.       do
  186.          if current_type = Void then
  187.             current_type := ct;
  188.             e := expression.to_runnable(ct)
  189.             if e = Void then
  190.                error(expression.start_position,
  191.                      "Bad BOOLEAN expression.");
  192.             else
  193.                expression := e;
  194.                t := expression.result_type;
  195.                if not t.is_boolean then
  196.                   eh.append("Expression of if/elseif must be BOOLEAN. ");
  197.                   eh.add_type(expression.result_type,fz_is_not_boolean);
  198.                   eh.add_position(expression.start_position);
  199.                   eh.print_as_error;
  200.                end;
  201.             end;
  202.             if then_compound /= Void then
  203.                tc := then_compound.to_runnable(ct);
  204.                if tc /= Void then
  205.                   then_compound := tc;
  206.                end;
  207.             end;
  208.             if nb_errors = 0 then
  209.                Result := Current;
  210.             end;
  211.          else
  212.             !!Result.make(expression,then_compound);
  213.             Result := Result.to_runnable(ct);
  214.          end;
  215.       end;
  216.  
  217. feature {NONE}
  218.  
  219.    print_else(need_else: BOOLEAN) is
  220.       do
  221.          if need_else then
  222.             cpp.put_string(" else ");
  223.          end;
  224.       end;
  225.  
  226. invariant
  227.  
  228.    expression /= Void;
  229.  
  230. end -- IFTHEN
  231.  
  232.