コード例 #1
0
ファイル: Engine.cs プロジェクト: hiromi-mi/kurogane
 /// <summary>
 /// 実際に実行する部分。
 /// </summary>
 protected virtual object ExecuteCore(TextReader stream, string filename)
 {
     Contract.Requires <ArgumentNullException>(stream != null);
     Contract.Requires <ArgumentNullException>(filename != null);
     Initialize();
     Func <Scope, object> program;
     {
         // Compile
         var sw    = Stopwatch.StartNew();
         var token = Tokenizer.Tokenize(stream, filename);
         var ast   = Parser.Parse(token, filename);
         var expr  = ExpressionGenerator.Generate(ast, this.Factory, filename);
         expr    = ExpressionOptimizer.Analyze(expr);
         program = CompilerHelpers.Compile(expr, true);
         //program = expr.Compile();
         sw.Stop();
         var ev = OnCompiled;
         if (ev != null)
         {
             ev(this, new EngineEventArgs(sw.ElapsedMilliseconds));
         }
     }
     {
         // Execute
         var sw     = Stopwatch.StartNew();
         var result = program(this.Global);
         sw.Stop();
         var ev = OnExecuted;
         if (ev != null)
         {
             ev(this, new EngineEventArgs(sw.ElapsedMilliseconds));
         }
         return(result);
     }
 }
コード例 #2
0
        public DynamicGenerator(int seed, ModuleDefinition mod, bool useMath = false)
        {
            rand = new Random(seed);

            Module = mod;

            injection = AssemblyDefinition.ReadAssembly(typeof(Iid).Assembly.Location);
            injection.MainModule.ReadSymbols();

            ExpGen  = new ExpressionGenerator(rand.Next(500000), Module);
            MathGen = new MathGenerator(rand.Next(500000), Module);

            emit = Instruction.Create(OpCodes.Call, Module.Import(typeof(System.Reflection.Emit.ILGenerator).GetMethod("Emit", new Type[] {
                typeof(System.Reflection.Emit.OpCode)
            })));

            int expNum = rand.Next(5, 50);

            for (int i = 0; i < expNum; i++)
            {
                ExpGen = new ExpressionGenerator(rand.Next(50000), Module);
                Expression ex = ExpGen.Generate(rand.Next(5, 14));

                int        evald = ExpressionEvaluator.Evaluate(ex, 5);
                Expression exR   = ExpressionInverser.InverseExpression(ex);

                CreateDynamic(ex, exR, 5, evald, useMath);
            }
        }
コード例 #3
0
        public void GeneratorDoesNotExposeStaticFunctionsWithUnsupportedTypeParameters()
        {
            var generator = new ExpressionGenerator();

            generator.AddFunctions(typeof(InvalidFunctions));
            Assert.Throws <UnknownFunctionException>(() => generator.Generate("RegexToString(\".*\")"));
        }
コード例 #4
0
        public void WhenTooFewParametersAreSupplied_ThrowsIncorrectParameterCountException()
        {
            var generator = new ExpressionGenerator();

            generator.AddFunctions(typeof(MockFormulae));
            Assert.Throws <IncorrectParameterCountException>(() => generator.Generate("Not()"));
        }
コード例 #5
0
        public void GeneratorDoesNotExposeStaticFunctionsWithUnsupportedReturnTypes()
        {
            var generator = new ExpressionGenerator();

            generator.AddFunctions(typeof(InvalidFunctions));
            Assert.Throws <UnknownFunctionException>(() => generator.Generate("ReturnRegex()"));
        }
コード例 #6
0
        private TwoCoordinateToValue2D CreatePerpendicularDotProduct2D()
        {
            var x0 = typeof(TValue).CreateParameterExpression();
            var y0 = typeof(TValue).CreateParameterExpression();
            var x1 = typeof(TValue).CreateParameterExpression();
            var y1 = typeof(TValue).CreateParameterExpression();

            var mula = ExpressionGenerator.Generate("Multiply", x0, y1);
            var mulb = ExpressionGenerator.Generate("Multiply", y0, x1);

            if (mula == null || mulb == null)
            {
                return(null);
            }

            var expression = ExpressionGenerator.Generate("Subtract", mula, mulb);

            if (expression == null)
            {
                return(null);
            }
            return(Expression.Lambda <TwoCoordinateToValue2D>(
                       expression, x0, y0, x1, y1
                       ).Compile());
        }
コード例 #7
0
        public void FunctionsNamesAreCaseInsensitive()
        {
            var generator = new ExpressionGenerator();

            generator.AddFunctions(typeof(MockFormulae));
            generator.Generate("ONE()");
        }
コード例 #8
0
        public void IfReturnsSecondResultWhenConditionIsFalse()
        {
            var generator = new ExpressionGenerator();
            var func      = generator.Generate("If(2 = 1, 8, 9)");
            var result    = func(null);

            Assert.Equal(9m, (decimal)result);
        }
コード例 #9
0
        public void DateIsAValidExpression()
        {
            var generator = new ExpressionGenerator();
            var func      = generator.Generate("'2010-12-31'");
            var result    = func(new Dictionary <string, string>());

            Assert.Equal(new DateTime(2010, 12, 31), (DateTime)result);
        }
コード例 #10
0
        public void CanAddTwoNumbersTogether()
        {
            var generator = new ExpressionGenerator();
            var func      = generator.Generate("5 + 4");
            var result    = func(null);

            Assert.Equal(9m, (decimal)result);
        }
コード例 #11
0
        public void TrueReturnsTrue()
        {
            var generator = new ExpressionGenerator();
            var func      = generator.Generate("true");
            var result    = func(null);

            Assert.True((bool)result);
        }
コード例 #12
0
        public void UnaryNegationOperatorReversesTheSignOfANumber()
        {
            var generator = new ExpressionGenerator();
            var func      = generator.Generate("-(1)");
            var result    = func(null);

            Assert.Equal(-1m, (decimal)result);
        }
コード例 #13
0
        public void AndExpressionPerformsLogicalAnd()
        {
            var generator = new ExpressionGenerator();
            var func      = generator.Generate("(1 = 0) & (1 = 1)");
            var result    = func(null);

            Assert.False((bool)result);
        }
コード例 #14
0
        public void OrExpressionThrowsExceptionIfRhsIsNotBoolean()
        {
            var generator = new ExpressionGenerator();
            var ex        = Assert.Throws <TypeMismatchException>(() => generator.Generate("(1 = 0) | 1)"));

            Assert.Equal(typeof(bool), ex.Expected);
            Assert.Equal(typeof(decimal), ex.Actual);
        }
コード例 #15
0
        public void IfReturnsFirstResultWhenConditionIsTrue()
        {
            var generator = new ExpressionGenerator();
            var func      = generator.Generate("If(1 = 1, 8, 9)");
            var result    = func(null);

            Assert.Equal(8m, (decimal)result);
        }
コード例 #16
0
        public void NumberIsAValidExpression()
        {
            var generator = new ExpressionGenerator();
            var func      = generator.Generate("5");
            var result    = func(new Dictionary <string, string>());

            Assert.Equal(5, (decimal)result);
        }
コード例 #17
0
        public void CanDoLessEqualOfTwoNumbers()
        {
            var generator = new ExpressionGenerator();
            var func      = generator.Generate("5.3 <= 8.4");
            var result    = func(null);

            Assert.True((bool)result);
        }
コード例 #18
0
        public void CanDoGreaterEqualOfTwoNumbers()
        {
            var generator = new ExpressionGenerator();
            var func      = generator.Generate("5.3 >= 8.4");
            var result    = func(null);

            Assert.False((bool)result);
        }
コード例 #19
0
        public void CanDoLessEqualOfTwoDates()
        {
            var generator = new ExpressionGenerator();
            var func      = generator.Generate("'2010-12-30' <= '2010-12-31'");
            var result    = func(null);

            Assert.True((bool)result);
        }
コード例 #20
0
        public void OrExpressionPerformsLogicalOr()
        {
            var generator = new ExpressionGenerator();
            var func      = generator.Generate("(1 = 0) | (1 = 1)");
            var result    = func(null);

            Assert.True((bool)result);
        }
コード例 #21
0
        public void IfThrowsExceptionIfConditionisNotABooleanExpression()
        {
            var generator = new ExpressionGenerator();
            var ex        = Assert.Throws <TypeMismatchException>(() => generator.Generate("If(1, 8, 9)"));

            Assert.Equal(typeof(bool), ex.Expected);
            Assert.Equal(typeof(decimal), ex.Actual);
        }
コード例 #22
0
        public void BracketExpressionsEvaluateTheContentsOfTheBracket()
        {
            var generator = new ExpressionGenerator();
            var func      = generator.Generate("(5)");
            var result    = func(null);

            Assert.Equal(5m, (decimal)result);
        }
コード例 #23
0
        public void StringIsAValidExpression()
        {
            var generator = new ExpressionGenerator();
            var func      = generator.Generate("\"hello\"");
            var result    = func(new Dictionary <string, string>());

            Assert.Equal("hello", (string)result);
        }
コード例 #24
0
        public void FalseReturnsFalse()
        {
            var generator = new ExpressionGenerator();
            var func      = generator.Generate("false");
            var result    = func(null);

            Assert.False((bool)result);
        }
コード例 #25
0
        public void CanDoNotEqualOnTwoNumbers()
        {
            var generator = new ExpressionGenerator();
            var func      = generator.Generate("2 != 1");
            var result    = func(null);

            Assert.True((bool)result);
        }
コード例 #26
0
        public void UnaryNegationExpressionThrowsExceptionWhenExpressionIsNotANumber()
        {
            var generator = new ExpressionGenerator();
            var ex        = Assert.Throws <TypeMismatchException>(() => generator.Generate("-\"String\""));

            Assert.Equal(typeof(decimal), ex.Expected);
            Assert.Equal(typeof(string), ex.Actual);
        }
コード例 #27
0
        public void CanDoNotEqualOnTwoStrings()
        {
            var generator = new ExpressionGenerator();
            var func      = generator.Generate("\"Hello\" != \"World\"");
            var result    = func(null);

            Assert.True((bool)result);
        }
コード例 #28
0
        public void CanDoNotEqualOnTwoDates()
        {
            var generator = new ExpressionGenerator();
            var func      = generator.Generate("'2012-12-31' != '2010-06-05'");
            var result    = func(null);

            Assert.True((bool)result);
        }
コード例 #29
0
        public void NotOperatorInvertsABooleanExpression()
        {
            var generator = new ExpressionGenerator();
            var func      = generator.Generate("!(1 = 0)");
            var result    = func(null);

            Assert.True((bool)result);
        }
コード例 #30
0
        public void CanSubtractTwoNumbers()
        {
            var generator = new ExpressionGenerator();
            var func      = generator.Generate("5 - 4");
            var result    = func(null);

            Assert.Equal(1m, (decimal)result);
        }
コード例 #31
0
ファイル: CompilerNET.cs プロジェクト: hultqvist/lax
        public Func<double> Compile(Block block)
        {
            ParameterExpression result = Expression.Parameter(typeof(int), "result");

            var compiler = new ExpressionGenerator();
            compiler.SetReturnType<double>();

            foreach (var varDef in block.Variables.Values)
            {
                //var v = Expression.Variable(ExpressionCompiler.GetCsType(varDef.ValueType), varDef.Name.Name);
                //expressions.Add(v);
                compiler.AddVariable(varDef);
            }

            foreach (var statement in block.Statements)
            {
                compiler.AddExpression(statement);
            }

            BlockExpression blockExpression = compiler.Generate();
            var lambda = Expression.Lambda<Func<double>>(blockExpression);
            var compiled = lambda.Compile();
            return compiled;
        }