home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Programming / sofa / archive / SmallEiffel.lha / SmallEiffel / lib_se / e_loop.e < prev    next >
Text File  |  1999-06-05  |  10KB  |  314 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 E_LOOP
  17.    --
  18.    -- The Eiffel instruction : "from ... until ... loop ... end".
  19.    --
  20.  
  21. inherit INSTRUCTION;
  22.  
  23. creation make
  24.  
  25. feature
  26.  
  27.    start_position: POSITION;
  28.          -- Of letter 'f' of "from".
  29.  
  30.    initialize: COMPOUND;
  31.  
  32.    invariant_clause: LOOP_INVARIANT;
  33.  
  34.    variant_clause: LOOP_VARIANT;
  35.  
  36.    until_expression: EXPRESSION;
  37.  
  38.    loop_body: COMPOUND;
  39.  
  40. feature {NONE}
  41.  
  42.    current_type: TYPE;
  43.  
  44. feature {NONE}
  45.  
  46.    make(sp: like start_position;
  47.         i: like initialize;
  48.         ic : like invariant_clause;
  49.         vc: like variant_clause;
  50.         ue : like until_expression;
  51.         lb : like loop_body) is
  52.       require
  53.          sp /= Void;
  54.          ue /= Void;
  55.       do
  56.          start_position := sp;
  57.          initialize := i;
  58.          invariant_clause := ic;
  59.          variant_clause := vc;
  60.          until_expression := ue;
  61.          loop_body := lb;
  62.       ensure
  63.          start_position = sp;
  64.          initialize = i;
  65.          invariant_clause = ic;
  66.          variant_clause = vc;
  67.          until_expression = ue;
  68.          loop_body = lb;
  69.       end;
  70.  
  71. feature
  72.  
  73.    is_pre_computable: BOOLEAN is false;
  74.  
  75.    end_mark_comment: BOOLEAN is true;
  76.  
  77. feature
  78.  
  79.    afd_check is
  80.       do
  81.          if run_control.loop_check then
  82.             if variant_clause /= Void then
  83.                variant_clause.afd_check;
  84.             end;
  85.             if invariant_clause /= Void then
  86.                invariant_clause.afd_check;
  87.             end;
  88.          end;
  89.          if initialize /= Void then
  90.             initialize.afd_check;
  91.          end;
  92.          until_expression.afd_check;
  93.          if loop_body /= Void then
  94.             loop_body.afd_check;
  95.          end;
  96.       end;
  97.  
  98.    collect_c_tmp is
  99.       do
  100.       end;
  101.  
  102.    compile_to_c is
  103.       local
  104.          loop_check, variant_flag, invariant_flag: BOOLEAN;
  105.       do
  106.          loop_check := run_control.loop_check;
  107.          if loop_check and then variant_clause /= Void then
  108.             cpp.put_string("{int c=0;int v=0;%N");
  109.             variant_flag := true;
  110.          end;
  111.          if initialize /= Void then
  112.             initialize.compile_to_c;
  113.          end;
  114.          if loop_check and then invariant_clause /= Void then
  115.             invariant_clause.compile_to_c;
  116.             invariant_flag := true;
  117.          end;
  118.          cpp.put_string("while (!(");
  119.          if run_control.no_check then
  120.             cpp.trace_boolean_expression(until_expression);
  121.          else
  122.             until_expression.compile_to_c;
  123.          end;
  124.          cpp.put_string(")) {%N");
  125.          if variant_flag then
  126.             cpp.variant_check(variant_clause.expression);
  127.          end;
  128.          if loop_body /= Void then
  129.             loop_body.compile_to_c;
  130.          end;
  131.          if invariant_flag then
  132.             invariant_clause.compile_to_c;
  133.          end;
  134.          cpp.put_string(fz_12);
  135.          if variant_flag then
  136.             cpp.put_string(fz_12);
  137.          end;
  138.       end;
  139.  
  140.    compile_to_jvm is
  141.       local
  142.          until_point, exit_point: INTEGER;
  143.          ca: like code_attribute;
  144.          loop_check: BOOLEAN;
  145.          counter, previous: INTEGER;
  146.       do
  147.          loop_check := run_control.loop_check;
  148.          ca := code_attribute;
  149.          if loop_check then
  150.             counter := ca.extra_local_size1;
  151.             ca.opcode_iconst_0;
  152.             ca.opcode_istore(counter);
  153.             previous := ca.extra_local_size1;
  154.             ca.opcode_iconst_0;
  155.             ca.opcode_istore(previous);
  156.          end;
  157.          if initialize /= Void then
  158.             initialize.compile_to_jvm;
  159.          end;
  160.          if loop_check then
  161.             if invariant_clause /= Void then
  162.                invariant_clause.compile_to_jvm(true);
  163.                ca.opcode_pop;
  164.             end;
  165.          end;
  166.          until_point := ca.program_counter;
  167.          exit_point := until_expression.jvm_branch_if_true;
  168.          if loop_check then
  169.             if variant_clause /= Void then
  170.                ca.opcode_iload(counter);
  171.                ca.opcode_iload(previous);
  172.                ca.runtime_check_loop_variant(variant_clause.expression);
  173.                ca.opcode_istore(previous);
  174.             end;
  175.          end;
  176.          if loop_body /= Void then
  177.             loop_body.compile_to_jvm;
  178.          end;
  179.          if loop_check then
  180.             if invariant_clause /= Void then
  181.                invariant_clause.compile_to_jvm(true);
  182.                ca.opcode_pop;
  183.             end;
  184.          end;
  185.          if loop_check then
  186.             ca.opcode_iinc(counter,1);
  187.          end;
  188.          ca.opcode_goto_backward(until_point);
  189.          ca.resolve_u2_branch(exit_point);
  190.       end;
  191.  
  192.    use_current: BOOLEAN is
  193.       local
  194.          loop_check: BOOLEAN;
  195.       do
  196.          loop_check := run_control.loop_check;
  197.          if loop_check and then variant_clause /= Void then
  198.             Result := Result or else variant_clause.use_current;
  199.          end;
  200.          if initialize /= Void then
  201.             Result := Result or else initialize.use_current;
  202.          end;
  203.          Result := Result or else until_expression.use_current;
  204.          if loop_check and then invariant_clause /= Void then
  205.             Result := Result or else invariant_clause.use_current;
  206.          end;
  207.          if loop_body /= Void then
  208.             Result := Result or else loop_body.use_current;
  209.          end;
  210.       end;
  211.  
  212.    to_runnable(ct: TYPE): like Current is
  213.       local
  214.          ue : like until_expression;
  215.          loop_check: BOOLEAN;
  216.       do
  217.          loop_check := run_control.loop_check;
  218.          if current_type = Void then
  219.             current_type := ct;
  220.             if initialize /= Void then
  221.                initialize := initialize.to_runnable(ct);
  222.                if initialize = Void then
  223.                   error(start_position,"Bad initialisation part.");
  224.                end;
  225.             end;
  226.             if loop_check and then invariant_clause /= Void then
  227.                invariant_clause := invariant_clause.to_runnable(ct);
  228.                if invariant_clause = Void then
  229.                   error(start_position,"Bad invariant.");
  230.                end;
  231.             end;
  232.             if loop_check and then variant_clause /= Void then
  233.                variant_clause := variant_clause.to_runnable(ct);
  234.                if variant_clause = Void then
  235.                   error(start_position,"Bad variant for this loop.");
  236.                end;
  237.             end;
  238.             ue := until_expression.to_runnable(ct);
  239.             if ue /= Void then
  240.                if not ue.result_type.is_boolean then
  241.                   error(ue.start_position,
  242.                         "Expression of until must be BOOLEAN.");
  243.                   eh.add_type(ue.result_type,fz_is_not_boolean);
  244.                   eh.print_as_error;
  245.                end;
  246.                until_expression := ue;
  247.             else
  248.                error(start_position, "This loop has an invalid expression.");
  249.             end;
  250.             if loop_body /= Void then
  251.                loop_body := loop_body.to_runnable(ct);
  252.                if loop_body = Void then
  253.                   error(start_position,"Invalid loop body.");
  254.                end;
  255.             end;
  256.             if nb_errors = 0 then
  257.                Result := Current;
  258.             end;
  259.          else
  260.             !!Result.make(start_position,initialize,invariant_clause,
  261.                           variant_clause,until_expression,loop_body);
  262.             Result := Result.to_runnable(ct);
  263.          end;
  264.       end;
  265.  
  266.    pretty_print is
  267.       local
  268.          semi_colon_flag: BOOLEAN;
  269.       do
  270.          fmt.indent;
  271.          fmt.keyword(fz_from);
  272.             if initialize /= Void then
  273.                initialize.pretty_print;
  274.             end;
  275.          if invariant_clause /= Void then
  276.             invariant_clause.pretty_print;
  277.          end;
  278.          if variant_clause /= Void then
  279.             fmt.indent;
  280.             fmt.keyword(fz_variant);
  281.             semi_colon_flag := fmt.semi_colon_flag;
  282.             fmt.set_semi_colon_flag(false);
  283.             variant_clause.pretty_print;
  284.             fmt.set_semi_colon_flag(semi_colon_flag);
  285.          end;
  286.          fmt.indent;
  287.          fmt.keyword(fz_until);
  288.          fmt.level_incr;
  289.          fmt.indent;
  290.          fmt.set_semi_colon_flag(false);
  291.          until_expression.pretty_print;
  292.          fmt.level_decr;
  293.          fmt.indent;
  294.          fmt.keyword(fz_loop);
  295.          fmt.indent;
  296.          if loop_body /= Void then
  297.             loop_body.pretty_print;
  298.          end;
  299.          fmt.indent;
  300.          fmt.keyword("end;");
  301.          if fmt.print_end_loop then
  302.             fmt.put_end(fz_loop);
  303.          end;
  304.       end;
  305.  
  306. invariant
  307.  
  308.    start_position /= Void;
  309.  
  310.    until_expression /= Void;
  311.  
  312. end -- E_LOOP
  313.  
  314.