Esempio n. 1
0
        public static ExpressionEvaluator <bool> CreateBool(string expression)
        {
            ExpressionEvaluatorBuilder <bool> builder = new ExpressionEvaluatorBuilder <bool>();

            builder.SetExpression(expression);

            builder.AddSkip('\n');
            builder.AddSkip('\r');
            builder.AddSkip('\t');
            builder.AddSkip(' ');

            builder.AddLexem(@"\(", n => new Bracket(n, true, ")"));
            builder.AddLexem(@"\)", n => new Bracket(n, false, "("));
            builder.AddLexem(@"\[", n => new Bracket(n, true, "]"));
            builder.AddLexem(@"\]", n => new Bracket(n, false, "["));
            builder.AddLexem(@"\{", n => new Bracket(n, true, "}"));
            builder.AddLexem(@"\}", n => new Bracket(n, false, "{"));

            builder.AddLexem(@"true", n => new Constant <bool>(n, bool.Parse(n)));
            builder.AddLexem(@"false", n => new Constant <bool>(n, bool.Parse(n)));
            builder.AddLexem(@"[A-Za-zА-Яа-я]+[A-Za-zА-Яа-я\d]*", n => new Variable <bool>(n));

            builder.AddLexem(@"\&", n => new OperationAnd(n));
            builder.AddLexem(@"\|", n => new OperationOr(n));
            builder.AddLexem(@"\!", n => new OperationNot(n));

            return(builder.Build());
        }
Esempio n. 2
0
        public static ExpressionEvaluator <bool> CreateLogical(string expression)
        {
            ExpressionEvaluatorBuilder <bool> builder = new ExpressionEvaluatorBuilder <bool>();

            builder.SetExpression(expression);

            builder.AddSkip('\n');
            builder.AddSkip('\r');
            builder.AddSkip('\t');
            builder.AddSkip(' ');

            builder.AddLexem(@"\(", n => new Bracket(n, true, ")"));
            builder.AddLexem(@"\)", n => new Bracket(n, false, "("));
            builder.AddLexem(@"\[", n => new Bracket(n, true, "]"));
            builder.AddLexem(@"\]", n => new Bracket(n, false, "["));
            builder.AddLexem(@"\{", n => new Bracket(n, true, "}"));
            builder.AddLexem(@"\}", n => new Bracket(n, false, "{"));

            builder.AddLexem(@"true", n => new Constant <bool>(n, bool.Parse(n)));
            builder.AddLexem(@"false", n => new Constant <bool>(n, bool.Parse(n)));
            builder.AddLexem(@"\d+\.*\d*", n => new Constant <double>(n, double.Parse(n, CultureInfo.InvariantCulture)));

            builder.AddLexem(@"\+", n => new OperationPlus(n));
            builder.AddLexem(@"\-", n => new OperationMinus(n));
            builder.AddLexem(@"\*", n => new OperationMultiply(n));
            builder.AddLexem(@"\/", n => new OperationDivide(n));
            builder.AddLexem(@"\^", n => new OperationPow(n));
            builder.AddLexem(@"\&", n => new OperationAnd(n));
            builder.AddLexem(@"\|", n => new OperationOr(n));
            builder.AddLexem(@"\!", n => new OperationNot(n));
            builder.AddLexem(@">", n => new OperationGreater(n));

            return(builder.Build());
        }
Esempio n. 3
0
        public static ExpressionEvaluator <double> CreateNumeric(string expression)
        {
            ExpressionEvaluatorBuilder <double> builder = new ExpressionEvaluatorBuilder <double>();

            builder.SetExpression(expression);

            builder.AddSkip('\n');
            builder.AddSkip('\r');
            builder.AddSkip('\t');
            builder.AddSkip(' ');

            builder.AddLexem(@"\(", n => new Bracket(n, true, ")"));
            builder.AddLexem(@"\)", n => new Bracket(n, false, "("));
            builder.AddLexem(@"\[", n => new Bracket(n, true, "]"));
            builder.AddLexem(@"\]", n => new Bracket(n, false, "["));
            builder.AddLexem(@"\{", n => new Bracket(n, true, "}"));
            builder.AddLexem(@"\}", n => new Bracket(n, false, "{"));

            builder.AddLexem(@"[A-Za-zА-Яа-я]+[A-Za-zА-Яа-я\d]*", n => new Variable <double>(n));
            builder.AddLexem(@"\d+\.*\d*", n => new Constant <double>(n, double.Parse(n, CultureInfo.InvariantCulture)));

            builder.AddLexem(@"\+", n => new OperationPlus(n));
            builder.AddLexem(@"\-", n => new OperationMinus(n));
            builder.AddLexem(@"\*", n => new OperationMultiply(n));
            builder.AddLexem(@"\/", n => new OperationDivide(n));
            builder.AddLexem(@"\^", n => new OperationPow(n));

            return(builder.Build());
        }
Esempio n. 4
0
        public static ExpressionEvaluator CreateDefault(string expression)
        {
            ExpressionEvaluatorBuilder builder = new ExpressionEvaluatorBuilder();

            builder.SetExpression(expression);

            builder.AddSkip('\n');
            builder.AddSkip('\r');
            builder.AddSkip('\t');
            builder.AddSkip(' ');

            builder.AddLexem(@"\(", n => new Bracket(n, true, ")"));
            builder.AddLexem(@"\)", n => new Bracket(n, false, "("));
            builder.AddLexem(@"\[", n => new Bracket(n, true, "]"));
            builder.AddLexem(@"\]", n => new Bracket(n, false, "["));
            builder.AddLexem(@"\{", n => new Bracket(n, true, "}"));
            builder.AddLexem(@"\}", n => new Bracket(n, false, "{"));

            builder.AddLexem(@"true", n => new Constant(n, bool.Parse(n)));
            builder.AddLexem(@"false", n => new Constant(n, bool.Parse(n)));
            builder.AddLexem(@"\d+\.*\d*", n => new Constant(n, double.Parse(n, CultureInfo.InvariantCulture)));
            builder.AddLexem(@"[A-Za-zА-Яа-я]+[A-Za-zА-Яа-я\d]*", n => new Variable(n));
            builder.AddLexem("\"[^\"]*\"", n => new Constant(n, n.Substring(1, n.Length - 2)));

            builder.AddOperation(@"\^", (args) => Math.Pow(args[0], args[1]), 30, 2);
            builder.AddOperation(@"\*", (args) => args[0] * args[1], 20, 2);
            builder.AddOperation(@"\/", (args) => args[0] / args[1], 20, 2);
            builder.AddOperation(@"\+", (args) => args[0] + args[1], 10, 2);
            builder.AddOperation(@"\-", (args) => args[0] - args[1], 10, 2);
            builder.AddOperation(@">", (args) => args[0] > args[1], 9, 2);
            builder.AddOperation(@">=", (args) => args[0] >= args[1], 9, 2);
            builder.AddOperation(@"<", (args) => args[0] < args[1], 9, 2);
            builder.AddOperation(@"<=", (args) => args[0] <= args[1], 9, 2);
            builder.AddOperation(@"=", (args) => args[0] == args[1], 8, 2);
            builder.AddOperation(@"\!", (args) => !args[0], 7, 1);
            builder.AddOperation(@"\&", (args) => args[0] && args[1], 6, 2);
            builder.AddOperation(@"\|", (args) => args[0] || args[1], 5, 2);

            return(builder.Build());
        }