public Predicate exec(Prolog engine) {
engine.setB0();
Term a1;
a1 = arg1;
if (!a1.unify(new JavaObjectTerm(this), engine.trail)) {
return engine.fail();
}
Predicate code = cont;
int B = engine.stack.top();
outOfScope = false;
outOfLoop = false;
engine.trail.add(new OutOfLoop(this));
try {
main_loop: while (true) {
while (engine.exceptionRaised == 0) {
// TODO check if this is correctly removed?
// if (engine.control.thread == null)
// break main_loop;
if (outOfLoop) {
break main_loop;
}
code = code.exec(engine);
}
switch (engine.exceptionRaised) {
case 1: // halt/0
break main_loop;
case 2: // freeze/2
throw new SystemException("freeze/2 is not supported yet");
// Do something here
// engine.exceptionRaised = 0 ;
// break
default:
break main_loop;
}
}
} catch (PrologException e) {
if (outOfScope) {
throw e;
}
engine.setException(engine.copy(e.getMessageTerm()));
engine.cut(B);
return engine.fail();
} catch (Exception e) {
if (outOfScope) {
throw new JavaException(e);
}
engine.setException(new JavaObjectTerm(e));
engine.cut(B);
return engine.fail();
}
return code;
}