}
public static boolean matchEvalAndReplace(Result<IValue> subject, Expression pat, List<Expression> conditions, Expression replacementExpr, IEvaluator<Result<IValue>> eval) {
Environment old = eval.getCurrentEnvt();
try {
IMatchingResult mp = pat.getMatcher(eval);
mp.initMatch(subject);
while (mp.hasNext()) {
if (eval.isInterrupted())
throw new InterruptException(eval.getStackTrace(), eval.getCurrentAST().getLocation());
if (mp.next()) {
int size = conditions.size();
if (size == 0) {
throw new Insert(replacementExpr.interpret(eval), mp, mp.getType(eval.getCurrentEnvt(), null));
}
IBooleanResult[] gens = new IBooleanResult[size];
Environment[] olds = new Environment[size];
Environment old2 = eval.getCurrentEnvt();
int i = 0;
try {
olds[0] = eval.getCurrentEnvt();
eval.pushEnv();
gens[0] = conditions.get(0).getBacktracker(eval);
gens[0].init();
while (i >= 0 && i < size) {
if (eval.isInterrupted()) {
throw new InterruptException(eval.getStackTrace(), eval.getCurrentAST().getLocation());
}
if (gens[i].hasNext() && gens[i].next()) {
if (i == size - 1) {
// in IfThen the body is executed, here we insert the expression
// NB: replaceMentExpr sees the latest bindings of the when clause
throw new Insert(replacementExpr.interpret(eval), mp, mp.getType(eval.getCurrentEnvt(), null));
}
i++;
gens[i] = conditions.get(i).getBacktracker(eval);
gens[i].init();