Nice/src/bossa/syntax tools.nice,1.105,1.106 typecheck.nice,1.128,1.129

Artem Gr Kozarezov <[email protected]>
Newsgroups gmane.comp.lang.nice.cvs
Message-ID <[email protected]>
Update of /cvsroot/nice/Nice/src/bossa/syntax
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1907/src/bossa/syntax

Modified Files:
	tools.nice typecheck.nice 
Log Message:
Type merging works (for IfExp).

Index: tools.nice
===================================================================
RCS file: /cvsroot/nice/Nice/src/bossa/syntax/tools.nice,v
retrieving revision 1.105
retrieving revision 1.106
diff -C2 -d -r1.105 -r1.106
*** tools.nice	6 Mar 2005 12:55:57 -0000	1.105
--- tools.nice	8 Mar 2005 19:33:19 -0000	1.106
***************
*** 196,199 ****
--- 196,200 ----
  ?mlsub.typing.TypeConstructor TypeConstructor_fromTypeSymbol(mlsub.typing.TypeSymbol) = native mlsub.typing.TypeConstructor mlsub.typing.TypeConstructor.fromTypeSymbol(mlsub.typing.TypeSymbol);
  ?gnu.bytecode.Type TypeImport_lookup(String, ?Location) = native gnu.bytecode.Type nice.tools.code.TypeImport.lookup(String, Location);
+ ?mlsub.typing.Monotype Types_merge(mlsub.typing.Monotype, mlsub.typing.Monotype) = native mlsub.typing.Monotype nice.tools.typing.Types.merge(mlsub.typing.Monotype, mlsub.typing.Monotype);
  
  // Retypings needed since java types are not strict.

Index: typecheck.nice
===================================================================
RCS file: /cvsroot/nice/Nice/src/bossa/syntax/typecheck.nice,v
retrieving revision 1.128
retrieving revision 1.129
diff -C2 -d -r1.128 -r1.129
*** typecheck.nice	7 Mar 2005 20:52:53 -0000	1.128
--- typecheck.nice	8 Mar 2005 19:33:19 -0000	1.129
***************
*** 533,555 ****
  }
  
  /**
   * Collect inference information from branches,
   * so that it can be propagated outwards.
   */
- class DeepMemoryRec {
-   MonoSymbol variable;
-   mlsub.typing.Monotype type;
-   toString() = "(" variable "," type ")";
- }
- var Stack<Stack<DeepMemoryRec>> deepMemory = new Stack();
- equals( DeepMemoryRec a, DeepMemoryRec b ) = a.variable == b.variable;
- 
  void populateDeepMemory(MonoSymbol variable, mlsub.typing.Monotype type)
  {
    if(deepMemory.size() == 0) return;
    let branch = deepMemory.peek();
!   let rec = new DeepMemoryRec(variable: variable, type: type);
!   while( branch.removeElement( rec ) ){}
!   branch.push(rec);
  }
  
--- 533,547 ----
  }
  
+ var Stack<Map<MonoSymbol,mlsub.typing.Monotype>> deepMemory = new Stack();
+ 
  /**
   * Collect inference information from branches,
   * so that it can be propagated outwards.
   */
  void populateDeepMemory(MonoSymbol variable, mlsub.typing.Monotype type)
  {
    if(deepMemory.size() == 0) return;
    let branch = deepMemory.peek();
!   branch.put( variable, type );
  }
  
***************
*** 566,571 ****
    enterBlock();
  
!   let Stack<DeepMemoryRec> ifBranchInference = new Stack();
!   let Stack<DeepMemoryRec> elseBranchInference = new Stack();
    deepMemory.push( elseBranchInference );
  
--- 558,563 ----
    enterBlock();
  
!   let Map<MonoSymbol,mlsub.typing.Monotype> thenBranchInference = new HashMap();
!   let Map<MonoSymbol,mlsub.typing.Monotype> elseBranchInference = new HashMap();
    deepMemory.push( elseBranchInference );
  
***************
*** 575,579 ****
      ?List<(MonoSymbol, mlsub.typing.Monotype)> instanceofInElse;
  
!     deepMemory.push( ifBranchInference );
  
      try {
--- 567,571 ----
      ?List<(MonoSymbol, mlsub.typing.Monotype)> instanceofInElse;
  
!     deepMemory.push( thenBranchInference );
  
      try {
***************
*** 602,606 ****
        // which is good when calling the compiler repeatedly from the same JVM.
        enterElse();
!       let got = deepMemory.pop(); assert got == ifBranchInference;
      }
  
--- 594,598 ----
        // which is good when calling the compiler repeatedly from the same JVM.
        enterElse();
!       let got = deepMemory.pop(); assert got == thenBranchInference;
      }
  
***************
*** 635,655 ****
  
    // Check if we can propagate some of the type inference information outwards.
!   if(ifBranchInference.size() != 0 || elseBranchInference.size() != 0)
!     ifPropagation(e, ifBranchInference, elseBranchInference);
  }
  
! void ifPropagation(IfExp e, Stack<DeepMemoryRec> ifBranchInference, Stack<DeepMemoryRec> elseBranchInference){
!   let condition = e.condition;
!   if(! (condition instanceof bossa.syntax.CallExp) || ! condition.isCallTo("==")) return;
!   if(condition.arguments.size() != 2) return;
!   let argle = condition.arguments.get(0); let leNull = argle.value.isNull();
!   let argri = condition.arguments.get(1); let riNull = argri.value.isNull();
!   if((leNull || riNull) && leNull != riNull){
!     if(e.elseExp instanceof bossa.syntax.VoidConstantExp && ifBranchInference.size() != 0){
!       let arg = leNull ? argri.value : argle.value;
!       let variable = localVariable( arg ); if( null == variable ) return;
!       for(rec : ifBranchInference) if(variable == rec.variable)
!         variable.setVarType( rec.type, otherBranch: variable.type, out: variable.type );
!     }
    }
  }
--- 627,645 ----
  
    // Check if we can propagate some of the type inference information outwards.
!   if(thenBranchInference.size() != 0 || elseBranchInference.size() != 0)
!     ifMerging(e, thenBranchInference, elseBranchInference);
  }
  
! void ifMerging(IfExp e,
!                Map<MonoSymbol,mlsub.typing.Monotype> thenBranchInference,
!                Map<MonoSymbol,mlsub.typing.Monotype> elseBranchInference){
!   let entryIt = thenBranchInference.entrySet().iterator();
!   while( entryIt.hasNext() ){
!     Map$Entry<MonoSymbol,mlsub.typing.Monotype> leEntry = entryIt.next();
!     MonoSymbol variable = leEntry.getKey();
!     ?mlsub.typing.Monotype riType = elseBranchInference.get( variable ); if(riType == null) continue;
!     mlsub.typing.Monotype leType = leEntry.getValue();
!     ?mlsub.typing.Monotype merged = Types_merge( leType, riType ); if(merged == null) continue;
!     variable.setVarType( merged, otherBranch: variable.type, out: variable.type );
    }
  }



-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.