Exemple #1
0
        static SimpleMaths()
        {
            var number = Terminals.Double("Number");

            var expression = Nonterminal.Create <double>("Expression");

            expression.SetProductions(
                number.AsIs(),
                expression.Extended().Append("+").Extend(expression).Finish((x1, x2) => x1 + x2),
                expression.Extended().Append("-").Extend(expression).Finish((x1, x2) => x1 - x2),
                expression.Extended().Append("*").Extend(expression).Finish((x1, x2) => x1 * x2),
                expression.Extended().Append("/").Extend(expression).Finish((x1, x2) => x1 / x2),
                "-".Appended().Extend(expression).WithPrecedence(out var NEG).Finish(x => - x),
                expression.Extended().Append("^").Extend(expression).Finish(Math.Pow),
                "(".Appended().Extend(expression).Append(")").AsIs());

            var opScope = new OperatorScope(
                new LeftAssociative("+", "-"),
                new LeftAssociative("*", "/"),
                new PrecedenceOnly(NEG),
                new RightAssociative("^"));

            Designtime = expression.WithOperatorScope(opScope);
            Runtime    = Designtime.Build();
        }