Ejemplo n.º 1
0
            // DCG stuff

            public TermNode ToDCG(ref BaseTerm lhs) // called from parser
            {
                TermNode body   = new TermNode();
                BaseTerm result = null;

                BaseTerm inVar     = new Variable();
                BaseTerm inVarSave = inVar;
                BaseTerm outVar    = inVar;

                lhs = new DcgTerm(lhs, ref outVar); // outVar becomes new term
                BaseTerm remainder;

                List <BaseTerm> alternatives = AlternativesToArrayList();

                for (int i = 0; i < alternatives.Count; i++)
                {
                    BaseTerm        alt      = alternatives[i];
                    bool            embedded = (alternatives.Count > 1);
                    List <BaseTerm> terms    = alt.ToTermList();

                    body.Clear();
                    remainder = inVarSave;

                    for (int ii = 0; ii < terms.Count; ii++)
                    {
                        DCGGoal(terms[ii], ref body, ref remainder, ref embedded);
                    }

                    // create a term-tree from the array
                    if (i == 0)
                    {
                        result = body.TermSeq();
                    }
                    else
                    {
                        result = new OperatorTerm(SemiOpDescr, result, body.TermSeq());
                    }

                    ((Variable)remainder).Bind(outVar);
                }

                return((result == null) ? null : result.ToGoalList()); // empty body treated similar to null
            }