///////////////////////////////////////////////////////////////////////////////
//                             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        := CONTEXT;

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

  
  - author      := "Sonntag Benoit (bsonntag@loria.fr)";
  - comment     := "Create a new context for analyser";
  
  // BSBS: A revoir, car globalement, maintenant on compile
  // que un slot à la fois, donc, tu n'as plus besoin de ca...
  
Section Inherit
  
  - parent_any:ANY := ANY;
  
Section Private
  
  - stack:FAST_ARRAY[CONTEXT] := FAST_ARRAY[CONTEXT].create_with_capacity 16;
  
  - top:INTEGER := -1;
  
Section Public

  //
  // Extern
  //  
  
  - push_extern pos:POSITION profil prof:PROFIL_SLOT :LOCAL <-
  ( 
    top := top + 1;
    (top > stack.upper).if {
      stack.add_last clone;
    };
    stack.item top.elt_push_extern pos profil prof
  );
  
  - pop_extern <-
  (
    stack.item top.elt_pop_extern;
    top := top - 1;
  );
  
  //
  // Block.
  //
  
  - push_block p:POSITION profil prof:PROFIL_BLOCK :LOCAL <-
  (
    top := top + 1;
    (top > stack.upper).if {
      stack.add_last clone;
    };
    stack.item top.elt_push_block p profil prof
  );
   
  - pop_block <-
  (
    stack.item top.elt_pop_block;
    top := top - 1;
  );

  //
  // Intern
  //
  
  - push_intern p:POSITION <-
  (
    top := top + 1;
    (top > stack.upper).if {
      stack.add_last clone;
    };
    stack.item top.elt_push_intern p;
  );
      
  - limit_context:INTEGER <- stack.item top.local;
  
  - pop_intern <-
  (
    stack.item top.elt_pop_intern;
    top := top - 1;
  );
  
Section Private  
  
  + local:INTEGER;
  
  + result:INTEGER;
  
  + list:LIST;
  
  + profil:PROFIL;
  
  + old_profil_first:PROFIL_SLOT; // BSBS: NE DOIT PAS ETRE UTILE !
  
  + context_extern:LOCAL;
  
  //
  // Extern
  //  
  
  - elt_push_extern pos:POSITION profil prof:PROFIL_SLOT :LOCAL <-
  ( + res:LOCAL;
    
    local  := stack_local_lower;
    result := stack_result_lower;    
    list   := list_current;
    old_profil_first := profil_first;
    profil := profil_second;
    //    
    stack_local_lower  := stack_local.upper + 1;
    stack_result_lower := stack_result.upper + 1;    
    list_current       := LIST.create pos;
    profil_second := profil_first := prof;    
    //
    (debug_level_option != 0).if {
      // Debug mode : Add context local.
      res := TYPE_CONTEXT.default.new_local pos name (ALIAS_STR.variable_context) style '+';
      res.set_ensure_count 1;      
      list_current.add_last (PUSH.create pos context res first TRUE);
    };
    res
  );
  
  - elt_pop_extern <-
  (        
    ITM_OBJECT.pop_stack_until stack_local_lower;
    stack_local_lower  := local;
    stack_result.remove_since stack_result_lower;
    stack_result_lower := result;    
    list_current       := list;
    profil_second := profil;
    profil_first := old_profil_first;
  );

  //
  // Block
  //
  
  - elt_push_block p:POSITION profil prof:PROFIL_BLOCK :LOCAL <-
  ( + res:LOCAL;
    local  := stack_local.upper + 1;
    result := stack_result_lower;
    list   := list_current;
    profil := profil_second;
    profil_second := prof;
    //
    stack_result_lower := stack_result.upper + 1;
    list_current       := LIST.create p;    
    context_extern := ITM_OBJECT.context_extern;
    ITM_OBJECT.set_context_extern NULL;
    //
    (debug_level_option != 0).if {
      // Debug mode : Add context local.
      res := TYPE_CONTEXT.default.new_local p name (ALIAS_STR.variable_context) style '+';
      res.set_ensure_count 1;      
      list_current.add_last (PUSH.create p context res first TRUE);
    };
    res
  );
  
  - elt_pop_block <-
  (
    list_current := list;
    ITM_OBJECT.pop_stack_until local;
    stack_result.remove_since stack_result_lower;
    stack_result_lower := result;
    (ITM_OBJECT.context_extern = NULL).if {
      ITM_OBJECT.set_context_extern context_extern;
    };
    profil_second := profil;
  );
  
  //
  // Intern
  //
  
  - elt_push_intern p:POSITION <-
  (     
    local  := stack_local.upper + 1;
    result := stack_result_lower;
    list   := list_current;
    //profil := NULL;
    //
    stack_result_lower := stack_result.upper + 1;
    list_current       := LIST.create p;    
    context_extern := ITM_OBJECT.context_extern;
    ITM_OBJECT.set_context_extern NULL;
  );
  
  - elt_pop_intern <-
  (
    list_current := list;
    ITM_OBJECT.pop_stack_until local;
    stack_result.remove_since stack_result_lower;
    stack_result_lower := result;
    (ITM_OBJECT.context_extern = NULL).if {
      ITM_OBJECT.set_context_extern context_extern;
    };
  );
  
