Ejemplo n.º 1
0
        public static Either <Error, Tuple <string, ExpressionBuilder> > Expr(string str)
        {
            Either <Error, Tuple <string, ExpressionBuilder> > res1 = Expr1(str);

            if (res1.IsLeft)
            {
                return(res1.Left);
            }

            var t1 = res1.Right;

            str = t1.Item1;
            ExpressionBuilder builder = t1.Item2;
            bool errorOrEmpty         = false;

            while (!errorOrEmpty)
            {
                str = Parser.Space(str);
                if (!str.IsEmpty())
                {
                    Either <Error, Tuple <string, ExpressionBuilder.Op> > res2 = BinaryOp(str);
                    if (!res2.IsLeft)
                    {
                        Tuple <string, ExpressionBuilder.Op> t2 = res2.Right;
                        str = t2.Item1;
                        ExpressionBuilder.Op op = t2.Item2;

                        str = Parser.Space(str);

                        Either <Error, Tuple <string, ExpressionBuilder> > res3 = Expr1(str);
                        if (res3.IsLeft)
                        {
                            return(res3.Left);
                        }
                        Tuple <string, ExpressionBuilder> t3 = res3.Right;

                        str = t3.Item1;
                        ExpressionBuilder e2 = t3.Item2;

                        builder = new ExpressionBuilder.Binary(op, builder, e2);
                    }
                    else
                    {
                        errorOrEmpty = true;
                    }
                }
                else
                {
                    errorOrEmpty = true;
                }
            }

            return(new Tuple <string, ExpressionBuilder>(str, builder));
        }
Ejemplo n.º 2
0
        public static Either <Error, Tuple <string, ExpressionBuilder> > Expr1(string s)
        {
            Either <Error, Tuple <string, ExpressionBuilder> > res1 = Expr2(s);

            if (res1.IsLeft)
            {
                return(res1.Left);
            }
            Tuple <string, ExpressionBuilder> t1 = res1.Right;

            s = t1.Item1;
            ExpressionBuilder e = t1.Item2;
            bool errorOrEmpty   = false;

            while (!errorOrEmpty)
            {
                s = Parser.Space(s);
                if (s.Length != 0)
                {
                    Either <Error, Tuple <string, ExpressionBuilder.Op> > res2 = BinaryOp1(s);
                    if (res2.IsRight)
                    {
                        Tuple <string, ExpressionBuilder.Op> t2 = res2.Right;
                        s = t2.Item1;
                        ExpressionBuilder.Op op = t2.Item2;

                        s = Parser.Space(s);

                        Either <Error, Tuple <string, ExpressionBuilder> > res3 = Expr2(s);
                        if (res3.IsLeft)
                        {
                            return(res3.Left);
                        }
                        Tuple <string, ExpressionBuilder> t3 = res3.Right;

                        s = t3.Item1;
                        ExpressionBuilder e2 = t3.Item2;

                        e = new ExpressionBuilder.Binary(op, e, e2);
                    }
                    else
                    {
                        errorOrEmpty = true;
                    }
                }
                else
                {
                    errorOrEmpty = true;
                }
            }

            return(new Tuple <string, ExpressionBuilder>(s, e));
        }