public void AssertFirst(string predicateName, int arity, ArrayList code)
{
AMInstructionSet iset = new AMInstructionSet();
if (!_labels.ContainsKey(predicateName + "/" + arity))
{
AddLabel(predicateName + "/" + arity, new ProgramClause(predicateName, arity));
_labelOccurrence[predicateName + "/" + arity] = 1;
// add instructions
return;
}
string pLabel = predicateName + "/" + arity;
int occurrence = (int)_labelOccurrence[predicateName + "/" + arity];
if (occurrence == 1)
{
ProgramClause oldPredicate = (ProgramClause)_labels[pLabel];
oldPredicate.Instruction = iset.CreateInstruction("trust_me");
string nextLabel = predicateName + "%" + occurrence + "/" + arity;
oldPredicate.Name = nextLabel;
_labels[nextLabel] = oldPredicate;
ProgramClause newFirst = new ProgramClause(predicateName, arity, iset.CreateInstruction("try_me_else", nextLabel));
newFirst.NextPredicate = oldPredicate;
_labels[pLabel] = newFirst;
occurrence++;
_labelOccurrence[pLabel] = occurrence;
AddProgramNode(newFirst);
foreach (AbstractInstruction inst in code)
{
AddInstruction(inst);
}
}
else
{
ProgramClause oldFirst = (ProgramClause)_labels[pLabel];
ProgramClause newFirst = new ProgramClause(predicateName, arity, iset.CreateInstruction("try_me_else", predicateName + "%1/" + arity));
newFirst.NextPredicate = oldFirst;
oldFirst.Name = predicateName + "%1/" + arity;
oldFirst.Instruction = iset.CreateInstruction("retry_me_else", predicateName + "%2/" + arity);
_labels[pLabel] = newFirst;
AddProgramNode(newFirst);
foreach (AbstractInstruction inst in code)
{
AddInstruction(inst);
}
occurrence++;
_labelOccurrence[pLabel] = occurrence;
PatchPredicates(predicateName, arity, oldFirst);
}
}