Пример #1
0
        public Expression CreateChoice(Expression exp1, Expression exp2)
        {
            if (exp1 == Expression.NotAllowed)
            {
                return(exp2);
            }
            if (exp2 == Expression.NotAllowed)
            {
                return(exp1);
            }

            if (exp1 == Expression.Empty && exp2.IsNullable)
            {
                return(exp2);
            }
            else
            if (exp2 == Expression.Empty && exp1.IsNullable)
            {
                return(exp1);
            }

            if (exp2 is ChoiceExp)
            {
                // operatos are left-associative.
                ChoiceExp right = (ChoiceExp)exp2;
                return(CreateChoice(CreateChoice(exp1, right.exp1), right.exp2));
            }

            // see if exp1 has already contained exp2
            Expression e = exp1;

            while (true)
            {
                if (e == exp2)
                {
                    // exp1 has already contained exp2.
                    return(exp1);
                }
                if (!(e is ChoiceExp))
                {
                    break;
                }
                ChoiceExp left = (ChoiceExp)e;
                if (left.exp2 == exp2)
                {
                    // exp1 has already contained exp2.
                    return(exp1);
                }
                e = left.exp1;
            }

            return(Unify(new ChoiceExp(exp1, exp2)));
        }
Пример #2
0
 public void OnChoice(ChoiceExp exp)
 {
     if (exp.exp1 == Expression.Empty)
     {
         OptimizedChoice(exp.exp2);
         return;
     }
     if (exp.exp2 == Expression.Empty)
     {
         OptimizedChoice(exp.exp1);
         return;
     }
     OnBinExp(exp, "|");
 }