Пример #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();
        }
 /// <summary>
 /// Converts the <see cref="sourceValue" /> parameter to the <see cref="destinationType" /> parameter using <see cref="formatProvider"
 /// /> and <see cref="ignoreCase" />
 /// </summary>
 /// <param name="sourceValue">the <see cref="System.Object"/> to convert from</param>
 /// <param name="destinationType">the <see cref="System.Type" /> to convert to</param>
 /// <param name="formatProvider">not used by this TypeConverter.</param>
 /// <param name="ignoreCase">when set to <c>true</c>, will ignore the case when converting.</param>
 /// <returns>
 /// an instance of <see cref="OperatorScope" />, or <c>null</c> if there is no suitable conversion.
 /// </returns>
 public override object ConvertFrom(object sourceValue, global::System.Type destinationType, global::System.IFormatProvider formatProvider, bool ignoreCase) => OperatorScope.CreateFrom(sourceValue);