public TermNode ToGoalList(int stackTop, int level) // called during execution (when there is a stack) { TermNode result = null; Term t0, t1; FType ft = this.FType; OType ot = this.OType; int pr = this.Precedence; if (this is Cut) { if (stackTop == 0) return new TermNode(this, level); else return new TermNode(new Cut(stackTop), level); } switch (this.Functor) { case Parser.IMPLIES: t0 = Arg(0); if (!t0.IsGoal) PrologIO.Error("Illegal predicate head: {0}", t0); t1 = Arg(1); result = new TermNode(t0, t1.ToGoalList(stackTop, level)); break; case Parser.DCGIMPL: t0 = Arg(0); if (!t0.IsGoal) PrologIO.Error("Illegal DCG head: {0}", t0); t1 = Arg(1); result = new TermNode(t0, t1.ToGoalList(stackTop, level)); break; case Parser.COMMA: t0 = Arg(0); t1 = Arg(1); result = t0.ToGoalList(stackTop, level); result.Append(t1.ToGoalList(stackTop, level)); break; case Parser.DOT: t0 = Arg(0); t1 = Arg(1); if (readingDCGClause) result = (new ListTerm(t0, t1)).ToGoalList(stackTop, level); else result = (new Term("consult", new ListTerm(t0, t1), OType.fy, 0)).ToGoalList(stackTop, level); break; case Parser.CURL: if (readingDCGClause) { t0 = Arg(0); result = t0.ToGoalList(stackTop, level); } else PrologIO.Error("Curly brackets only allowed in a DCG-clause: {0}", this); break; default: if (this.IsVar) result = new TermNode(new Term("'$metacall'", this), level); else if (this.IsGoal) result = new TermNode(this, level); else PrologIO.Error("Illegal term {0} in goal list", this); break; } return result; }
private static void DCGGoal(Term t, ref TermNode body, ref Term remainder, ref bool embedded) { Term temp; if (t.IsString || t is Cut) body.Append(t); else if (t.Functor == Parser.CURL) { while (t.Arity == 2) { body.Append(t.Arg(0)); t = t.Arg(1); embedded = true; } } else if (t.IsList) { temp = new Term(); if (t == NULLLIST) t = temp; else t.SetRightmostArg(temp); if (embedded) { body.Append(new Term(Parser.EQ, remainder, t, FType.comp, OType.xfx, 700)); embedded = false; } else remainder.Bind(t); // in this case, nothing is appended to body, which may be left empty (e.g. t-->[x]) remainder = temp; } else if (t.IsAtom || t.IsCompound) { t = new DCGTerm(t, ref remainder); body.Append(t); } else if (t.IsVar) PrologIO.Error("Variable not allowed in DCG-clause: {0}", t.VarName()); else PrologIO.Error("Illegal term in DCG-clause: {0}", t); }