}
else {
// Var #i is not a set variable.
if(availableSetElements.getType().isSet()){
// Var #i should match single element in elements
ISet set = (ISet) availableSetElements;
assert set.size() == 1;
varVal[0] = elem = set.iterator().next();
} else {
varVal[0] = elem = availableSetElements;
//varVal[i] = ctx.getValueFactory().set(elem.getType()).insert(elem); // TODO: Should this be a set?
}
}
varPat[0].initMatch(ResultFactory.makeResult(elem.getType(), elem, ctx));
hasNext = varPat[0].hasNext();
}
}
currentVar = 0;
if(!makeGen(currentVar, availableSetElements)){
return false;
}
} else {
currentVar = nVar - 1;
}
hasNext = true;
if(debug)System.err.println("\nStart assigning Vars for " + this + ":= " + subject);
if (patternSize == 1 && (isSetVar(0) || availableSetElements.size() == 1)) {
if (varPat[0].hasNext() && varPat[0].next()) {
return true;
}
return false;
}
main:
do {
if (ctx.isInterrupted()) {
throw new InterruptException(ctx.getStackTrace(), ctx.getCurrentAST().getLocation());
}
if(debug)System.err.println("\n=== MAIN: Pattern = " + this + ":= " + subject + "\ncurrentVar[" + currentVar + "]=" + varName[currentVar]);
if(debug)printVars();
IValue v = null;
boolean b = false;
if(isNested[currentVar]){
if(varPat[currentVar].hasNext()){
b = varPat[currentVar].next();
if(b)
v = varVal[currentVar];
} else if(varGen[currentVar].hasNext()){
v = (IValue) varGen[currentVar].next();
Result<IValue> r = ResultFactory.makeResult(v.getType(), v, ctx);
varPat[currentVar].initMatch(r);
b = varPat[currentVar].next();
if(b)
varVal[currentVar] = v;
}
} else if(isSetVar[currentVar]){
if(varGen[currentVar].hasNext()){
v = (IValue) varGen[currentVar].next();
Result<IValue> r = ResultFactory.makeResult(v.getType().lub(setSubjectType), v, ctx);
varPat[currentVar].initMatch(r);
b = varPat[currentVar].next();
if(b){
varVal[currentVar] = v;
ctx.getCurrentEnvt().storeVariable(varName[currentVar], r);
if(debug)System.err.println("Store in " + varName[currentVar] + ": " + r + " / " + v + " / " + v.getType() + " / " +
ctx.getCurrentEnvt().getVariable(varName[currentVar]).getType());
}
}
} else if(isBinding[currentVar]){
if(varGen[currentVar].hasNext()){
v = (IValue) varGen[currentVar].next();
Result<IValue> r = ResultFactory.makeResult(v.getType(), v, ctx);
varPat[currentVar].initMatch(r);
b = varPat[currentVar].next();
if(b){
varVal[currentVar] = v;
ctx.getCurrentEnvt().storeVariable(varName[currentVar], r);
if(debug)System.err.println("Store in " + varName[currentVar] + ": " + r + " / " + v + " / " + v.getType() + " / " +
ctx.getCurrentEnvt().getVariable(varName[currentVar]).getType());
}
}
} else if(varPat[currentVar] instanceof QualifiedNamePattern && ((QualifiedNamePattern)varPat[currentVar] ).isAnonymous()){
if(varGen[currentVar].hasNext()){
v = (IValue) varGen[currentVar].next();
Result<IValue> r = ResultFactory.makeResult(v.getType(), v, ctx);
varPat[currentVar].initMatch(r);
b = varPat[currentVar].next();
if(b){
varVal[currentVar] = v;
}
}
} else if(!isBinding[currentVar] && (varPat[currentVar] instanceof QualifiedNamePattern || varPat[currentVar] instanceof TypedVariablePattern) && varGen[currentVar].hasNext()){
v = (IValue) varGen[currentVar].next();
Result<IValue> r = ResultFactory.makeResult(v.getType(), v, ctx);
varPat[currentVar].initMatch(r);
b = varPat[currentVar].next();
System.err.println("Try match " + varName[currentVar] + ": " + r + " / " + v + " / " + v.getType());
if(b){
varVal[currentVar] = v;
System.err.println("Matches " + varName[currentVar] + ": " + r + " / " + v + " / " + v.getType());
}
} else {
System.err.println("CANNOT HANDLE THIS 2");
}
//if(debug) System.err.println("currentVar[" + currentVar + "] = " + varName[currentVar] + "; v = " + v + "; type: " + v.getType() + "; matchPatternElement = " + b);
if(b){
currentVar++;
ISet avail = available();
if(currentVar <= nVar - 1){
if(!makeGen(currentVar, avail)){
varGen[currentVar] = null;
currentVar--;
}
continue main;
} else {
if(!avail.isEmpty()){
if(debug)System.err.println("nomatch: currentVar = " + currentVar + " avail = " + available());
currentVar--;
continue main;
}
hasNext = unexploredAlternatives();