///////////////////////////////////////////////////////////////////////////////
//                             Lisaac Compiler                               //
//                                                                           //
//                   LSIIT - ULP - CNRS - INRIA - FRANCE                     //
//                                                                           //
//   This program is free software: you can redistribute it and/or modify    //
//   it under the terms of the GNU General Public License as published by    //
//   the Free Software Foundation, either version 3 of the License, or       //
//   (at your option) any later version.                                     //
//                                                                           //
//   This program is distributed in the hope that it will be useful,         //
//   but WITHOUT ANY WARRANTY; without even the implied warranty of          //
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           //
//   GNU General Public License for more details.                            //
//                                                                           //
//   You should have received a copy of the GNU General Public License       //
//   along with this program.  If not, see <http://www.gnu.org/licenses/>.   //
//                                                                           //
//                     http://isaacproject.u-strasbg.fr/                     //
///////////////////////////////////////////////////////////////////////////////
Section Header
  
  + name    := LOOP;

  - copyright   := "2003-2007 Benoit Sonntag";

  
  - author  := "Sonntag Benoit (bsonntag@loria.fr)";
  - comment := "Loop (call tail recursive).";
  
Section Inherit
  
  + parent_instr:Expanded INSTR;
  
Section Public
  
  + link_count:INTEGER;
  
  + body:LIST;
  
  + name:STRING_CONSTANT;
  
  - set_link <-
  (
    link_count := link_count + 1;
  );
  
  - unset_link <-
  (
    link_count := link_count - 1;
    ? {link_count >= 0};
  );
  
  //
  // Creation.
  //
  
  - create p:POSITION name lab:STRING_CONSTANT body lst:LIST :SELF <-
  ( + result:SELF;
    result := clone;
    result.make p name lab body lst;
    result
  );
  
  - make p:POSITION name lab:STRING_CONSTANT body lst:LIST <-
  (     
    position := p;    
    name := lab;
    body := lst;
  );
  
  - my_copy:SELF <-
  ( + result:SELF;
    + new_body:LIST;
    + switch:SWITCH;
    + case:LIST;
    + loop_end:LOOP_END;
        
    new_body := body.my_copy;
    result := LOOP.create position name (ALIAS_STR.get_intern name) body new_body;
    //
    switch ?= new_body.item (new_body.upper - 1);
    (switch.list.lower).to (switch.list.upper) do { k:INTEGER;
      case := switch.list.item k.code;
      (case.count > 1).if {
	loop_end ?= case.item (case.upper - 1);
	(loop_end != NULL).if {	  
	  ? {loop_end.loop = Self};
	  (loop_end.loop != Self).if {
	    semantic_error (position,"LOOP.my_copy BUG!!!");
	  };
	  loop_end := LOOP_END.create (loop_end.position) loop result;
	  case.put loop_end to (case.upper - 1);
	};
      };
    };
    
    (result.link_count != link_count).if {      
      name.print;
      " Origin:".print;
      link_count.print;
      " copy:".print;
      result.link_count.print;
      '\n'.print;
      semantic_error (position,"LOOP: Bug in copy.");
    } else {
     // "c\n".print;
    };
    
    ? {result.link_count = link_count};
    result
  );

  //
  // Generation.
  //

  - remove <-
  ( 
    body.remove;    
    ? {link_count = 0};
  );
  
  - execute:INSTR <-
  ( + result:INSTR;     
    + cur_seq_call_local_and_loop:INTEGER;
    + cur_seq_call_and_loop:INTEGER;
    
    (link_count = 0).if {
      result := body.execute_unlink;
      new_execute_pass;
    } else {      
      cur_seq_call_local_and_loop := 
      seq_call_local_and_loop     := seq_call_local_and_loop + link_count;
      //
      cur_seq_call_and_loop :=
      seq_call_and_loop     := seq_call_and_loop + link_count;
      //
      seq_inline := seq_inline + 1;
      //
      result := Self;
      body.execute_case;      
      (loop_invariant = Self).if {
	loop_list := list_current;
	loop_seq_index := Old seq_index;
	//
	loop_seq_call_local_and_loop := cur_seq_call_local_and_loop;
	loop_seq_call_and_loop       := cur_seq_call_and_loop;
	//
	seq_call_local_and_loop := seq_call_local_and_loop + link_count;
	seq_call_and_loop       := seq_call_and_loop + link_count;
	body.execute_case;      
	loop_invariant := NULL;
      };
    };
    result
  );
  
  //
  // Display.
  //
  
  - display buffer:STRING <-
  (    
    buffer.append name;
    display_ref buffer;
    buffer.append ":\n";
    buffer.append indent;
    body.display buffer;
  );

  //
  // Generation.
  //
  
  - genere buffer:STRING <-
  (         
    (! genere_while buffer).if {
      buffer.append name;
      buffer.append ":\n";
      buffer.append indent;
      body.genere buffer;
    };
  );
  
  - genere_while buffer:STRING :BOOLEAN <-
  ( + switch:SWITCH;
    + lst_true,lst_false,lst:LIST;
    + inverse:BOOLEAN;
    + result:BOOLEAN;
    
    (body.upper - 1 < body.lower).if {        
      semantic_error (position,"LOOP BUG: Body loop empty !");
    };
    switch ?= body.item (body.upper-1);
    (
      (switch != NULL) && 
      {switch.list.count = 2} &&
      {switch.list.first.id = type_true } && 
      {switch.list.second.id = type_false}
    ).if {
      lst_true  := switch.list.first.code;
      lst_false := switch.list.second.code;
      ((lst_true.is_empty) || {lst_false.is_empty}).if {
	(! lst_false.is_empty).if {
	  inverse := TRUE;
	  lst := lst_true;
	  lst_true := lst_false;
	  lst_false := lst;
	};
	(body.count = 2).if {
	  //
	  // While (...) do {...}.
	  //
	  result := TRUE;
	  inverse.if {
	    buffer.append "while (!";	    
	  } else {
	    buffer.append "while (";	    
	  };
	  switch.expr.genere buffer;
	  //
	  buffer.append ") ";
	  lst_true.remove_index (lst_true.upper - 1);
	  lst_true.genere buffer;
	}.elseif {lst_true.count = 2} then {
	  //
	  // Do {...} while (...).
	  //
	  result := TRUE;
	  buffer.append "do ";
	  body.remove_index (body.upper - 1);
	  body.genere buffer;
	  inverse.if {
	    buffer.append " while (!";	    
	  } else {
	    buffer.append " while (";	    
	  };	  
	  //
	  switch.expr.genere buffer;
	  buffer.add_last ')';
	};	
      };
    };
    result
  );
  
