home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 3 / AACD03.BIN / AACD / Programming / sofa / archive / SmallEiffel.lha / SmallEiffel / lib_se / e_inspect.e < prev    next >
Text File  |  1999-06-05  |  8KB  |  300 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_INSPECT
  17.    --
  18.    -- The Eiffel inspect instruction.
  19.    --
  20.  
  21. inherit INSTRUCTION;
  22.  
  23. creation make
  24.  
  25. feature
  26.  
  27.    start_position: POSITION;
  28.          -- Of keyword `inspect'.
  29.  
  30.    expression: EXPRESSION;
  31.          -- Heading expression after keyword `inspect'.
  32.  
  33.    when_list: WHEN_LIST;
  34.          -- List of when clauses.
  35.  
  36.    else_position: POSITION;
  37.          -- Of the keyword `else' if any.
  38.  
  39.    else_compound: COMPOUND;
  40.          -- Else compound if any.
  41.  
  42. feature {NONE}
  43.  
  44.    current_type: TYPE;
  45.  
  46. feature {NONE}
  47.  
  48.    make(sp: like start_position; exp: like expression) is
  49.       require
  50.          sp /= Void;
  51.          exp /= Void;
  52.       do
  53.          start_position := sp;
  54.          expression := exp;
  55.       ensure
  56.          start_position = sp;
  57.          expression = exp;
  58.       end;
  59.  
  60. feature
  61.  
  62.    is_pre_computable: BOOLEAN is false;
  63.  
  64.    end_mark_comment: BOOLEAN is true;
  65.  
  66. feature
  67.  
  68.    afd_check is
  69.       do
  70.          expression.afd_check;
  71.          if when_list /= Void then
  72.             when_list.afd_check;
  73.          end;
  74.          if else_compound /= Void then
  75.             else_compound.afd_check;
  76.          end;
  77.       end;
  78.  
  79.    includes(v: INTEGER): BOOLEAN is
  80.       -- True if a when clause includes `v'.
  81.       do
  82.          Result := when_list.includes_integer(v);
  83.       end;
  84.  
  85.    collect_c_tmp is
  86.       do
  87.       end;
  88.  
  89.    compile_to_c is
  90.       local
  91.          no_check: BOOLEAN;
  92.          debug_check: BOOLEAN;
  93.       do
  94.          no_check := run_control.no_check;
  95.          debug_check := run_control.debug_check;
  96.          cpp.inspect_incr;
  97.          cpp.put_string("{int ");
  98.          cpp.put_inspect;
  99.          cpp.put_character('=');
  100.          if debug_check then
  101.             cpp.put_character('(');
  102.             cpp.se_trace_exp(expression.start_position);
  103.             cpp.put_character(',');
  104.          end;
  105.          expression.compile_to_c;
  106.          if debug_check then
  107.             cpp.put_character(')');
  108.          end;
  109.          cpp.put_string(fz_00);
  110.          if when_list = Void then
  111.             if else_position = Void then
  112.                if no_check then
  113.                   exceptions_handler.incorrect_inspect_value(start_position);
  114.                end;
  115.             elseif else_compound /= Void then
  116.                else_compound.compile_to_c;
  117.             end;
  118.          else
  119.             when_list.compile_to_c(else_position);
  120.             if else_position = Void then
  121.                if no_check then
  122.                   cpp.put_character(' ');
  123.                   cpp.put_string(fz_else);
  124.                   cpp.put_character('{');
  125.                   exceptions_handler.incorrect_inspect_value(start_position);
  126.                   cpp.put_character('}');
  127.                end;
  128.             elseif else_compound /= Void then
  129.                cpp.put_character(' ');
  130.                cpp.put_string(fz_else);
  131.                cpp.put_character('{');
  132.                else_compound.compile_to_c;
  133.                cpp.put_character('}');
  134.             end;
  135.          end;
  136.          cpp.put_string(fz_12);
  137.          cpp.inspect_decr;
  138.       end;
  139.  
  140.    compile_to_jvm is
  141.       do
  142.          expression.compile_to_jvm;
  143.          if when_list /= Void then
  144.             when_list.compile_to_jvm(else_position);
  145.          end;
  146.          if else_compound /= Void then
  147.             else_compound.compile_to_jvm;
  148.          elseif else_position = Void then
  149.             if run_control.no_check then
  150.                code_attribute.runtime_error_inspect(expression);
  151.             end;
  152.          end;
  153.          if when_list /= Void then
  154.             when_list.compile_to_jvm_resolve_branch;
  155.          end;
  156.          code_attribute.opcode_pop;
  157.       end;
  158.  
  159.    use_current: BOOLEAN is
  160.       do
  161.          Result := Result or else expression.use_current;
  162.          if when_list /= Void then
  163.             Result := Result or else when_list.use_current;
  164.          end;
  165.          if else_compound /= Void then
  166.             Result := Result or else else_compound.use_current;
  167.          end;
  168.       end;
  169.  
  170.    add_when(e_when: E_WHEN) is
  171.       require
  172.          e_when /= Void
  173.       do
  174.          if when_list = Void then
  175.             !!when_list.make(<<e_when>>);
  176.          else
  177.             when_list.add_last(e_when);
  178.          end;
  179.       end;
  180.  
  181.    set_else_compound(sp: like else_position; ec: like else_compound) is
  182.       do
  183.          else_position := sp;
  184.          else_compound := ec;
  185.       end;
  186.  
  187.    to_runnable(ct: TYPE): like Current is
  188.       local
  189.          e: like expression;
  190.          te: TYPE;
  191.          wl: WHEN_LIST;
  192.       do
  193.          if current_type = Void then
  194.             current_type := ct;
  195.             e := expression.to_runnable(ct);
  196.             if nb_errors = 0 then
  197.                expression := e;
  198.                te := e.result_type.run_type;
  199.                --                  ********
  200.                --                  VIRABLE
  201.             end;
  202.             if nb_errors = 0 then
  203.                if te.is_character then
  204.                   if when_list /= Void then
  205.                      when_list := when_list.to_runnable_character(Current);
  206.                      if when_list = Void then
  207.                         error(start_position,em1);
  208.                      end;
  209.                   end;
  210.                elseif te.is_integer then
  211.                   if when_list /= Void then
  212.                      when_list := when_list.to_runnable_integer(Current);
  213.                      if when_list = Void then
  214.                         error(start_position,em1);
  215.                      end;
  216.                   end;
  217.                else
  218.                   eh.append("Expression must be INTEGER or CHARACTER.");
  219.                   eh.add_type(te," is not allowed.");
  220.                   eh.add_position(start_position);
  221.                   eh.print_as_error;
  222.                end;
  223.             end;
  224.             if else_compound /= Void then
  225.                else_compound := else_compound.to_runnable(ct);
  226.             end;
  227.             Result := Current
  228.          else
  229.             Result := twin;
  230.             !!wl.from_when_list(when_list);
  231.             Result.set_when_list(wl);
  232.             Result.clear_current_type;
  233.             Result := Result.to_runnable(ct);
  234.          end;
  235.       end;
  236.  
  237. feature
  238.  
  239.    pretty_print is
  240.       do
  241.          fmt.keyword(fz_inspect);
  242.          fmt.level_incr;
  243.          if not fmt.zen_mode then
  244.             fmt.indent;
  245.          end;
  246.          fmt.set_semi_colon_flag(false);
  247.          expression.pretty_print;
  248.          fmt.level_decr;
  249.          fmt.indent;
  250.          if when_list /= Void then
  251.             when_list.pretty_print;
  252.          end;
  253.          if else_compound = Void then
  254.             if else_position /= Void then
  255.                fmt.indent;
  256.                fmt.keyword(fz_else);
  257.             end;
  258.          else
  259.             fmt.indent;
  260.             fmt.keyword(fz_else);
  261.             fmt.level_incr;
  262.             else_compound.pretty_print;
  263.             fmt.level_decr;
  264.          end;
  265.          fmt.indent;
  266.          fmt.keyword("end;");
  267.          if fmt.print_end_inspect then
  268.             fmt.put_end(fz_inspect);
  269.          end;
  270.       end;
  271.  
  272. feature {E_INSPECT}
  273.  
  274.    set_when_list(wl: like when_list) is
  275.       do
  276.          when_list := wl;
  277.       ensure
  278.          when_list = wl;
  279.       end;
  280.  
  281. feature {NONE}
  282.  
  283.    em1: STRING is "Bad inspect.";
  284.  
  285. feature {E_INSPECT}
  286.  
  287.    clear_current_type is
  288.       do
  289.          current_type := Void;
  290.       ensure
  291.          current_type = Void
  292.       end;
  293.  
  294. invariant
  295.  
  296.    expression /= Void;
  297.  
  298. end -- E_INSPECT
  299.  
  300.