Beispiel #1
0
        protected override Symbol ReduceImplementation(ReductionContext context, Identifier identifier)
        {
            Symbol
                operandA = context.Reduce(identifier.Tail[0]),
                operandB = context.Reduce(identifier.Tail[1]);

            return(new Literal <bool>(operandA.Equals(operandB)));
        }
        protected override Symbol ReduceImplementation(ReductionContext context, Identifier identifier)
        {
            Symbol reducedFirstSymbol = context.Reduce(identifier.Tail[0]);
            bool   boolean            = context.BooleanConverter.Convert(reducedFirstSymbol);

            return(boolean
                ? reducedFirstSymbol
                : identifier.Tail[1]);
        }
Beispiel #3
0
        protected override Symbol ReduceImplementation(ReductionContext context, Identifier identifier)
        {
            Symbol result = null;

            foreach (Symbol symbol in identifier.Tail)
            {
                result = context.Reduce(symbol);
            }

            return(result);
        }
Beispiel #4
0
        public Symbol Reduce(ReductionContext context, Symbol s)
        {
            Symbol current = s;

            for (var i = 0; i < _reducers.Length; i++)
            {
                Symbol newSymbol = context.Reduce(current, _reducers[i]);

                if (newSymbol.Equals(current))
                {
                    continue;
                }

                i       = -1;
                current = newSymbol;
            }

            return(current);
        }
        protected override Symbol ReduceImplementation(ReductionContext context, Identifier identifier)
        {
            decimal GetOperand(int index) => context.NumbersConverter.Convert(context.Reduce(identifier.Tail[index]));

            Lazy <decimal>
            operandA     = new Lazy <decimal>(() => GetOperand(0)),
                operandB = new Lazy <decimal>(() => GetOperand(1));

            decimal?value = identifier.Name switch
            {
                nameof(Add) => (decimal?)operandA.Value + operandB.Value,
                nameof(Sub) => operandA.Value - operandB.Value,
                nameof(Mul) => operandA.Value * operandB.Value,
                nameof(Div) => operandA.Value / operandB.Value,

                _ => null
            };

            return(value is null
                ? null
                : new Literal <decimal>(value.Value));
        }
    }
Beispiel #6
0
        public static void Main(string[] args)
        {
            var ctx = new ReductionContext(
                new BuiltInsReducer()
                );

            Identifier n = I("n"), Fact = I("Fact");

            Symbol res = ctx.Reduce(
                Let(Fact)[
                    Func(n)[
                        If[n][
                            Mul[n][
                                Fact[
                                    ReduceFirst[Sub[n][1]]
                                ]
                            ]
                        ][1]
                    ]
                ][10]
                );

            Console.WriteLine(res);
        }