Ejemplo n.º 1
0
            static void DCGGoal(BaseTerm t, ref TermNode body, ref BaseTerm remainder, ref bool embedded)
            {
                BaseTerm temp;

                if (t.IsString || t is Cut)
                {
                    body.Append(t);
                }
                else if (t.HasFunctor(PrologParser.CURL))
                {
                    while (t.Arity == 2)
                    {
                        body.Append(t.Arg(0));
                        t        = t.Arg(1);
                        embedded = true;
                    }
                }
                else if (t.IsProperList)
                {
                    temp = new Variable();

                    t = (t.IsEmptyList) ? temp : ((ListTerm)t).Append(temp);

                    if (embedded)
                    {
                        body.Append(new CompoundTerm(PrologParser.EQ, remainder, t));
                        embedded = false;
                    }
                    else
                    {
                        ((Variable)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.IsNamedVar)
                {
                    IO.Error("Variable not allowed in DCG-clause: {0}", ((NamedVariable)t).Name);
                }
                else if (t.IsUnboundTerm)
                {
                    IO.Error("Unbound variable not allowed in DCG-clause");
                }
                else
                {
                    IO.Error("Illegal term in DCG-clause: {0}", t);
                }
            }
Ejemplo n.º 2
0
            public TermNode ToGoalList(int stackSize, int level) // called during execution (when there is a stack)
            {
                TermNode result = null;
                BaseTerm t0, t1;

                TermType  tt = this.TermType;
                AssocType at = this.AssocType;
                int       pr = this.Precedence;

                if (this is Cut)
                {
                    if (stackSize == 0)
                    {
                        return(new TermNode(this, null, level));
                    }
                    else
                    {
                        return(new TermNode(new Cut(stackSize), null, level));
                    }
                }

                switch (this.Functor as string)
                {
                case PrologParser.IMPLIES:
                    t0 = Arg(0);
                    if (!t0.IsCallable)
                    {
                        IO.Error("Illegal predicate head: {0}", t0);
                    }
                    t1     = Arg(1);
                    result = new TermNode(t0, t1.ToGoalList(stackSize, level));
                    break;

                case PrologParser.DCGIMPL:
                    t0 = Arg(0);
                    if (!t0.IsCallable)
                    {
                        IO.Error("Illegal DCG head: {0}", t0);
                    }
                    t1     = Arg(1);
                    result = new TermNode(t0, t1.ToGoalList(stackSize, level));
                    break;

                case PrologParser.COMMA:
                    t0     = Arg(0);
                    t1     = Arg(1);
                    result = t0.ToGoalList(stackSize, level);
                    result.Append(t1.ToGoalList(stackSize, level));
                    break;

                case PrologParser.DOT:
                    t0     = Arg(0);
                    t1     = Arg(1);
                    result = (new CompoundTerm("consult", new ListTerm(t0, t1))).ToGoalList(stackSize, level);
                    break;

                case PrologParser.CURL:
                    t0     = Arg(0);
                    result = t0.ToGoalList(stackSize, level);
                    break;

                default:
                    if (this.IsVar)
                    {
                        result = new TermNode(new CompoundTerm("meta$call", this), null, level);
                    }
                    else if (this.IsCallable)
                    {
                        result = new TermNode(this, null, level);
                    }
                    else
                    {
                        IO.Error("Illegal term {0} in goal list", this);
                    }
                    break;
                }

                return(result);
            }