Example #1
0
        private static Func <object, object, object> CreateFunc(string program)
        {
            var assemblyName = new AssemblyName(Guid.NewGuid().ToString())
            {
                Version = new Version(1, 0)
            };
            AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName,
                                                                                            AssemblyBuilderAccess.Run);
            ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("Program");

            ParameterExpression valueParameter     = Expression.Parameter(typeof(object), "value");
            ParameterExpression parameterParameter = Expression.Parameter(typeof(object), "parameter");

            AstHelper astHelper = AstHelper.Default(moduleBuilder);

            astHelper.Variables.Add("value", valueParameter);
            astHelper.Variables.Add("parameter", parameterParameter);

            Expression rootExpression = ProgramExpression.Compile(astHelper, program);

            Expression <Func <object, object, object> > lambda =
                Expression.Lambda <Func <object, object, object> >(Expression.Convert(rootExpression, typeof(object)),
                                                                   valueParameter, parameterParameter);

            return(lambda.Compile());
        }
        public void Comparisons(string program, bool result)
        {
            Expression expression = ProgramExpression.Compile(new AstHelper(null), program);

            bool lamda = Expression.Lambda <Func <bool> >(expression).Compile()();

            Assert.AreEqual(result, lamda);
        }
        public void BinaryExpresionOperation(string program, ExpressionType operation)
        {
            TigerParser       parser            = Mother.CreateParser(program);
            ProgramExpression rootAstExpression = parser.parse();
            Expression        expression        = rootAstExpression.Compile();

            Assert.IsInstanceOf <BinaryExpression>(expression);
            var binary = expression as BinaryExpression;

            Assert.AreEqual(binary.NodeType, operation);
        }
Example #4
0
        public static Expression Compile(string program)
        {
            var assemblyName = new AssemblyName(Guid.NewGuid().ToString())
            {
                Version = new Version(1, 0)
            };
            AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName,
                                                                                            AssemblyBuilderAccess.Run);
            ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("Program");

            AstHelper runtime = CreateRuntime(moduleBuilder);

            return(ProgramExpression.Compile(runtime, program));
        }
        public void FunctionInvoked_ComplexFunctions(string program, int result)
        {
            var scope = new AstHelper(null);
            Expression <Func <int, int, int> > sum = (a, b) => a + b;

            scope.Functions.Add("sum", new FunctionReference(sum, typeof(int), typeof(int), typeof(int)));
            Expression <Func <int, int, int> > resta = (a, b) => a - b;

            scope.Functions.Add("resta", new FunctionReference(resta, typeof(int), typeof(int), typeof(int)));
            Expression <Func <int, int, int> > prod = (a, b) => a * b;

            scope.Functions.Add("prod", new FunctionReference(prod, typeof(int), typeof(int), typeof(int)));
            Expression <Func <int, int, int> > div = (a, b) => a / b;

            scope.Functions.Add("div", new FunctionReference(div, typeof(int), typeof(int), typeof(int)));

            Expression expression = ProgramExpression.Compile(scope, program);

            int lamda = Expression.Lambda <Func <int> >(expression).Compile()();

            Assert.AreEqual(lamda, result);
        }