Ejemplo n.º 1
0
 public virtual TypeswitchCase GetClosestMatch(TypeswitchCase/*!*/ nd1, TypeswitchCaseList/*!*/ list1, TypeswitchCaseList list2, int list1pos, ref int list2start,
   TrivialHashtable/*!*/ matchedNodes, out Differences closestDifferences, out int list2pos) {
   closestDifferences = null; list2pos = -1;
   if (list2 == null) return null;
   if (nd1 == null || list1 == null ||  matchedNodes == null || list1pos < 0 || list1pos >= list1.Count || list2start < 0 || list2start >= list2.Count) {
     Debug.Assert(false); return null;
   }
   TypeswitchCase closest = null;
   Differences winnerSoFar = null;
   for (int j = list2start, m = list2.Count; j < m; j++){
     TypeswitchCase nd2 = list2[j];
     if (list2start == j) list2start++;
     if (nd2 == null) continue;
     if (matchedNodes[nd2.UniqueKey] != null) continue;
     Differences diff = this.GetDifferences(nd1, nd2);
     if (diff == null){Debug.Assert(false); continue;}
     if (diff.Similarity <= 0.5){
       //Not a good enough match
       if (list2start == j+1) list2start--; //The next call to GetClosestMatch will start looking at list2start, so this node will be considered then
       continue; //ignore it for the rest of this call
     }
     if (winnerSoFar != null && winnerSoFar.Similarity >= diff.Similarity) continue;
     winnerSoFar = closestDifferences = diff;
     closest = nd2;
     list2pos = j;
     if (diff.NumberOfDifferences == 0) return closest; //Perfect match, no need to look for other matches
   }
   if (closest != null){
     //^ assert winnerSoFar != null;
     //closest is closer to nd1 than any other node in list2, but this is no good if some other node in list1 has a better claim on closest
     for (int i = list1pos+1, n = list1.Count; i < n; i++){
       TypeswitchCase nd1alt = list1[i];
       if (nd1alt == null) continue;
       if (matchedNodes[nd1alt.UniqueKey] != null) continue;
       Differences diff = this.GetDifferences(nd1alt, closest);
       if (diff == null){Debug.Assert(false); continue;}
       if (diff.Similarity <= winnerSoFar.Similarity) continue;
       //nd1alt has a better claim on closest. See if it wants closest.
       Differences diff2;
       int j, k = list2start;
       TypeswitchCase nd2alt = this.GetClosestMatch(nd1alt, list1, list2, i, ref k,  matchedNodes, out diff2, out j);
       if (nd2alt != closest){
         Debug.Assert(nd2alt != null && diff2 != null && diff2.Similarity >= diff.Similarity);
         continue; //nd1alt prefers nd2alt to closest, so closest is still available
       }
       //nd1alt wants closest, take it out of the running
       matchedNodes[closest.UniqueKey] = nd1alt;
       //Now that closest is out of the running, try again
       k = list2start;
       TypeswitchCase newClosest = this.GetClosestMatch(nd1, list1, list2, i, ref k, matchedNodes, out winnerSoFar, out list2pos);
       //put closest back in the running so that the next call to this routine will pick it up
       matchedNodes[closest.UniqueKey] = closest;
       closest = newClosest;
       break;
     }
   }
   closestDifferences = winnerSoFar;
   return closest;
 }
Ejemplo n.º 2
0
 public virtual TypeswitchCaseList VisitTypeswitchCaseList(TypeswitchCaseList typeswitchCases, TypeswitchCaseList changes, TypeswitchCaseList deletions, TypeswitchCaseList insertions){
   if (typeswitchCases == null) return changes;
   if (changes != null){
     if (deletions == null || insertions == null)
       Debug.Assert(false);
     else{
     }
   }else if (deletions != null)
     return null;
   return typeswitchCases;
 }
Ejemplo n.º 3
0
 public virtual TypeswitchCaseList VisitTypeswitchCaseList(TypeswitchCaseList typeswitchCases){
   if (typeswitchCases == null) return null;
   for (int i = 0, n = typeswitchCases.Count; i < n; i++)
     typeswitchCases[i] = this.VisitTypeswitchCase(typeswitchCases[i]);
   return typeswitchCases;
 } 
Ejemplo n.º 4
0
 public virtual Differences VisitTypeswitchCaseList(TypeswitchCaseList list1, TypeswitchCaseList list2,
   out TypeswitchCaseList changes, out TypeswitchCaseList deletions, out TypeswitchCaseList insertions){
   changes = list1 == null ? null : list1.Clone();
   deletions = list1 == null ? null : list1.Clone();
   insertions = list1 == null ? new TypeswitchCaseList() : list1.Clone();
   //^ assert insertions != null;
   Differences differences = new Differences();
   for (int j = 0, n = list2 == null ? 0 : list2.Count; j < n; j++){
     //^ assert list2 != null;
     TypeswitchCase nd2 = list2[j];
     if (nd2 == null) continue;
     insertions.Add(null);
   }
   TrivialHashtable savedDifferencesMapFor = this.differencesMapFor;
   this.differencesMapFor = null;
   TrivialHashtable matchedNodes = new TrivialHashtable();
   for (int i = 0, k = 0, n = list1 == null ? 0 : list1.Count; i < n; i++){
     //^ assert list1 != null && changes != null && deletions != null;
     TypeswitchCase nd1 = list1[i]; 
     if (nd1 == null) continue;
     Differences diff;
     int j;
     TypeswitchCase nd2 = this.GetClosestMatch(nd1, list1, list2, i, ref k, matchedNodes, out diff, out j);
     if (nd2 == null || diff == null){Debug.Assert(nd2 == null && diff == null); continue;}
     matchedNodes[nd1.UniqueKey] = nd1;
     matchedNodes[nd2.UniqueKey] = nd2;
     changes[i] = diff.Changes as TypeswitchCase;
     deletions[i] = diff.Deletions as TypeswitchCase;
     insertions[i] = diff.Insertions as TypeswitchCase;
     insertions[n+j] = nd1; //Records the position of nd2 in list2 in case the change involved a permutation
     Debug.Assert(diff.Changes == changes[i] && diff.Deletions == deletions[i] && diff.Insertions == insertions[i]);
     differences.NumberOfDifferences += diff.NumberOfDifferences;
     differences.NumberOfSimilarities += diff.NumberOfSimilarities;
   }
   //Find deletions
   for (int i = 0, n = list1 == null ? 0 : list1.Count; i < n; i++){
     //^ assert list1 != null && changes != null && deletions != null;
     TypeswitchCase nd1 = list1[i]; 
     if (nd1 == null) continue;
     if (matchedNodes[nd1.UniqueKey] != null) continue;
     changes[i] = null;
     deletions[i] = nd1;
     insertions[i] = null;
     differences.NumberOfDifferences += 1;
   }
   //Find insertions
   for (int j = 0, n = list1 == null ? 0 : list1.Count, m = list2 == null ? 0 : list2.Count; j < m; j++){
     //^ assert list2 != null;
     TypeswitchCase nd2 = list2[j]; 
     if (nd2 == null) continue;
     if (matchedNodes[nd2.UniqueKey] != null) continue;
     insertions[n+j] = nd2;  //Records nd2 as an insertion into list1, along with its position in list2
     differences.NumberOfDifferences += 1; //REVIEW: put the size of the tree here?
   }
   if (differences.NumberOfDifferences == 0){
     changes = null;
     deletions = null;
     insertions = null;
   }
   this.differencesMapFor = savedDifferencesMapFor;
   return differences;
 }
Ejemplo n.º 5
0
 public virtual void VisitTypeswitchCaseList(TypeswitchCaseList typeswitchCases)
 {
   if (typeswitchCases == null) return;
   for (int i = 0, n = typeswitchCases.Count; i < n; i++)
     this.VisitTypeswitchCase(typeswitchCases[i]);
 }
Ejemplo n.º 6
0
 public virtual void VisitTypeswitchCaseList(TypeswitchCaseList oldCases, TypeswitchCaseList newCases, TypeUnion tUnion) {
   if (oldCases == null || newCases == null || tUnion == null) { Debug.Assert(false); return; }
   TypeNodeList types = tUnion.Types;
   if (types == null) return;
   bool complainedAboutDefault = false;
   for (int i = 0, n = oldCases.Count; i < n; i++) {
     TypeswitchCase tcase = this.VisitTypeswitchCase(oldCases[i]);
     if (tcase == null) continue;
     TypeNode t = tcase.LabelType;
     if (t == null) {
       if (tcase.LabelTypeExpression == null && !complainedAboutDefault) {
         complainedAboutDefault = true;
         this.HandleError(tcase, Error.DefaultNotAllowedInTypeswitch);
       }
       continue;
     }
     int j = types.SearchFor(t);
     if (j < 0) {
       //TODO: look for a single field tuple with the same type as t and arrange for coercion
       this.HandleError(tcase.LabelTypeExpression, Error.TypeCaseNotFound, this.GetTypeName(t), this.GetTypeName(tUnion));
       continue;
     }
     if (newCases[j] != null) continue; //TODO: give an error
     newCases[j] = tcase;
   }
 }
Ejemplo n.º 7
0
 public override TypeswitchCaseList VisitTypeswitchCaseList(TypeswitchCaseList typeswitchCases)
 {
     if (typeswitchCases == null) return null;
     return base.VisitTypeswitchCaseList(typeswitchCases.Clone());
 }
 public override TypeswitchCaseList VisitTypeswitchCaseList(TypeswitchCaseList typeswitchCases)
 {
   throw new ApplicationException("unimplemented");
 }
Ejemplo n.º 9
0
 public virtual TypeswitchCaseList VisitTypeswitchCaseList(TypeswitchCaseList typeswitchCases1, TypeswitchCaseList typeswitchCases2)
 {
     if (typeswitchCases1 == null) return null;
     for (int i = 0, n = typeswitchCases1.Count, m = typeswitchCases2 == null ? 0 : typeswitchCases2.Count; i < n; i++)
     {
         //^ assert typeswitchCases2 != null;
         if (i >= m)
             typeswitchCases1[i] = this.VisitTypeswitchCase(typeswitchCases1[i], null);
         else
             typeswitchCases1[i] = this.VisitTypeswitchCase(typeswitchCases1[i], typeswitchCases2[i]);
     }
     return typeswitchCases1;
 }
Ejemplo n.º 10
0
 public virtual void VisitTypeswitchCaseList(TypeswitchCaseList oldCases, BlockList targets, StatementList statements, Block nextStatement, Local temp){
   for (int i = 0, n = oldCases.Count; i < n; i++){
     TypeswitchCase tcase = oldCases[i];
     StatementList stats = new StatementList(3);
     Block b = new Block(stats);
     if (tcase != null){
       Expression expr = null;
       if (tcase.LabelType.IsValueType)
         expr = new AddressDereference(new BinaryExpression(temp, new Literal(tcase.LabelType, SystemTypes.Type), NodeType.Unbox), tcase.LabelType);
       else
         expr = new BinaryExpression(temp, new Literal(tcase.LabelType, SystemTypes.Type), NodeType.Castclass);
       stats.Add(new AssignmentStatement(this.VisitTargetExpression(tcase.LabelVariable), expr));
       stats.Add(this.VisitBlock(tcase.Body));
     }
     stats.Add(new Branch(null, nextStatement));
     statements.Add(b);
     targets.Add(b);
   }
 }
Ejemplo n.º 11
0
 public Typeswitch(Expression expression, TypeswitchCaseList cases)
     : base(NodeType.Typeswitch)
 {
     this.Cases = cases;
     this.Expression = expression;
 }
Ejemplo n.º 12
0
 public override TypeswitchCaseList VisitTypeswitchCaseList(TypeswitchCaseList typeswitchCases)
 {
     throw new NotImplementedException("Node type not yet supported");
 }
Ejemplo n.º 13
0
 public EventingVisitor(Action<TypeswitchCaseList> visitTypeswitchCaseList) { VisitedTypeswitchCaseList += visitTypeswitchCaseList; } public event Action<TypeswitchCaseList> VisitedTypeswitchCaseList; public override TypeswitchCaseList VisitTypeswitchCaseList(TypeswitchCaseList typeswitchCases) { if (VisitedTypeswitchCaseList != null) VisitedTypeswitchCaseList(typeswitchCases); return base.VisitTypeswitchCaseList(typeswitchCases); }