[Nice-commit] Nice/src/bossa/link Alternative.java,1.37,1.38 Dispatch.java,1.49,1.50
Brought to you by:
bonniot
|
From: <ar...@us...> - 2003-02-25 12:31:03
|
Update of /cvsroot/nice/Nice/src/bossa/link
In directory sc8-pr-cvs1:/tmp/cvs-serv9231/F:/nice/src/bossa/link
Modified Files:
Alternative.java Dispatch.java
Log Message:
Added dispatch on integer and character literals.
Index: Alternative.java
===================================================================
RCS file: /cvsroot/nice/Nice/src/bossa/link/Alternative.java,v
retrieving revision 1.37
retrieving revision 1.38
diff -C2 -d -r1.37 -r1.38
*** Alternative.java 10 Dec 2002 18:46:33 -0000 1.37
--- Alternative.java 25 Feb 2003 12:30:57 -0000 1.38
***************
*** 105,108 ****
--- 105,127 ----
}
+ boolean matchesTypePart(TypeConstructor[] tags, boolean[] isValue)
+ {
+ for(int i = 0, n = 0; i < patterns.length; i++)
+ if (!isValue[i] && !patterns[i].matches(tags[n++]))
+ return false;
+
+ return true;
+ }
+
+ boolean matchesValuePart(long[] values, boolean[] isValue)
+ {
+ for(int i = 0, n = 0; i < patterns.length; i++)
+ if (isValue[i] && !patterns[i].matchesValue(values[n++]))
+ return false;
+
+ return true;
+ }
+
+
/****************************************************************
* Code generation
Index: Dispatch.java
===================================================================
RCS file: /cvsroot/nice/Nice/src/bossa/link/Dispatch.java,v
retrieving revision 1.49
retrieving revision 1.50
diff -C2 -d -r1.49 -r1.50
*** Dispatch.java 18 Feb 2003 14:21:20 -0000 1.49
--- Dispatch.java 25 Feb 2003 12:30:58 -0000 1.50
***************
*** 127,130 ****
--- 127,134 ----
List multitags = enumerate(type, used);
+ boolean[] isValue = new boolean[method.getArity()];
+ List values = generateValues(sortedAlternatives, isValue);
+ boolean hasValues = values.size() > 0;
+
int nb_errors = 0;
for(Iterator i = multitags.iterator(); i.hasNext();)
***************
*** 133,138 ****
--- 137,149 ----
if (test(method, tags, sortedAlternatives))
+ {
if (++nb_errors > 9)
break;
+ }
+ else if (hasValues &&
+ testValues(method, tags, values, isValue, sortedAlternatives) )
+ if (++nb_errors > 9)
+ break;
+
}
if (nb_errors > 0)
***************
*** 197,200 ****
--- 208,263 ----
}
+ /**
+ Special version of above that tests tags with all combinations of integer values.
+ */
+ private static boolean testValues(NiceMethod method,
+ TypeConstructor[] tags,
+ List valueCombis,
+ boolean[] isValue,
+ final Stack sortedAlternatives)
+ {
+ boolean failed = false;
+ List sortedTypeMatches = new ArrayList();
+ for (Iterator i = sortedAlternatives.iterator(); i.hasNext(); )
+ {
+ Alternative a = (Alternative) i.next();
+ if (a.matchesTypePart(tags, isValue))
+ sortedTypeMatches.add(a);
+ }
+
+ for (Iterator valit = valueCombis.iterator(); valit.hasNext(); )
+ {
+ Alternative first = null;
+ long[] values = (long[]) valit.next();
+ for (Iterator i = sortedTypeMatches.iterator(); i.hasNext();)
+ {
+ Alternative a = (Alternative) i.next();
+ if (a.matchesValuePart(values, isValue))
+ if (first == null)
+ first = a;
+ else if (!Alternative.less(first, a))
+ {
+ failed = true;
+ User.warning
+ (method,
+ "Ambiguity for method "+method+
+ "\nFor parameters of type/value " + toString(tags, values, isValue)+
+ "\nboth\n" + first.printLocated() +
+ "\nand\n" + a.printLocated() + "\nmatch.");
+ }
+ }
+ if(first==null)
+ {
+ failed = true;
+ User.warning(method,
+ "Method " + method + " is not completely covered:\n" +
+ "no alternative matches " +
+ toString(tags, values, isValue));
+ }
+ }
+ return failed;
+ }
+
+
private static String toString(TypeConstructor[] tags)
{
***************
*** 210,213 ****
--- 273,293 ----
}
+ private static String toString(TypeConstructor[] tags, long[] values, boolean[] isValue)
+ {
+ StringBuffer res = new StringBuffer();
+ res.append('(');
+ for (int i = 0, n = 0; i < tags.length; i++)
+ {
+ if(isValue[n])
+ res.append(values[n++]);
+ else
+ res.append(tags[n++]);
+ if (i + 1 < tags.length)
+ res.append(", ");
+ }
+ return res.append(')').toString();
+ }
+
+
/**
Enumerate all the tuples of tags in the domain of a polytype.
***************
*** 337,339 ****
--- 417,475 ----
return tags;
}
+
+ /** Generate all combinations of integer values from the alternatives
+ */
+ private static List generateValues(List alternatives, boolean[] isValue)
+ {
+ List values = new ArrayList();
+ if (alternatives.size() < 1) return values;
+ int len = isValue.length;
+ for (int pos = 0; pos < len; pos++)
+ {
+ long[] valuesAtPos = new long[alternatives.size()];
+ int valueCount = 0;
+ for (Iterator i = alternatives.iterator(); i.hasNext(); )
+ {
+ Pattern pat = ((Alternative)i.next()).getPatterns()[pos];
+ if (pat.atIntValue) {
+ isValue[pos] = true;
+ valuesAtPos[valueCount++] = pat.value;
+ }
+ }
+ if (valueCount > 0)
+ {
+ List res = new ArrayList();
+ //remove duplicates
+ for (int i = 0; i < valueCount; i++)
+ for (int j = i+1; j < valueCount; j++)
+ if (valuesAtPos[i] == valuesAtPos[j])
+ valuesAtPos[i] = valuesAtPos[--valueCount];
+
+ if (values.size() == 0)
+ for (int i = 0; i < valueCount; i++)
+ {
+ long[] arr2 = new long[len];
+ arr2[pos] = valuesAtPos[i];
+ res.add(arr2);
+ }
+
+ else
+ for (Iterator it = values.iterator(); it.hasNext(); )
+ {
+ long[] arr = (long[])it.next();
+ for (int i = 0; i < valueCount; i++)
+ {
+ long[] arr2 = new long[len];
+ System.arraycopy(arr,0,arr2,0,len);
+ arr2[pos] = valuesAtPos[i];
+ res.add(arr2);
+ }
+ }
+
+ values = res;
+ }
+ }
+ return values;
+ }
+
}
|