/// <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); } }
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); } }
public void GeneratorDoesNotExposeStaticFunctionsWithUnsupportedTypeParameters() { var generator = new ExpressionGenerator(); generator.AddFunctions(typeof(InvalidFunctions)); Assert.Throws <UnknownFunctionException>(() => generator.Generate("RegexToString(\".*\")")); }
public void WhenTooFewParametersAreSupplied_ThrowsIncorrectParameterCountException() { var generator = new ExpressionGenerator(); generator.AddFunctions(typeof(MockFormulae)); Assert.Throws <IncorrectParameterCountException>(() => generator.Generate("Not()")); }
public void GeneratorDoesNotExposeStaticFunctionsWithUnsupportedReturnTypes() { var generator = new ExpressionGenerator(); generator.AddFunctions(typeof(InvalidFunctions)); Assert.Throws <UnknownFunctionException>(() => generator.Generate("ReturnRegex()")); }
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()); }
public void FunctionsNamesAreCaseInsensitive() { var generator = new ExpressionGenerator(); generator.AddFunctions(typeof(MockFormulae)); generator.Generate("ONE()"); }
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); }
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); }
public void CanAddTwoNumbersTogether() { var generator = new ExpressionGenerator(); var func = generator.Generate("5 + 4"); var result = func(null); Assert.Equal(9m, (decimal)result); }
public void TrueReturnsTrue() { var generator = new ExpressionGenerator(); var func = generator.Generate("true"); var result = func(null); Assert.True((bool)result); }
public void UnaryNegationOperatorReversesTheSignOfANumber() { var generator = new ExpressionGenerator(); var func = generator.Generate("-(1)"); var result = func(null); Assert.Equal(-1m, (decimal)result); }
public void AndExpressionPerformsLogicalAnd() { var generator = new ExpressionGenerator(); var func = generator.Generate("(1 = 0) & (1 = 1)"); var result = func(null); Assert.False((bool)result); }
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); }
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); }
public void NumberIsAValidExpression() { var generator = new ExpressionGenerator(); var func = generator.Generate("5"); var result = func(new Dictionary <string, string>()); Assert.Equal(5, (decimal)result); }
public void CanDoLessEqualOfTwoNumbers() { var generator = new ExpressionGenerator(); var func = generator.Generate("5.3 <= 8.4"); var result = func(null); Assert.True((bool)result); }
public void CanDoGreaterEqualOfTwoNumbers() { var generator = new ExpressionGenerator(); var func = generator.Generate("5.3 >= 8.4"); var result = func(null); Assert.False((bool)result); }
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); }
public void OrExpressionPerformsLogicalOr() { var generator = new ExpressionGenerator(); var func = generator.Generate("(1 = 0) | (1 = 1)"); var result = func(null); Assert.True((bool)result); }
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); }
public void BracketExpressionsEvaluateTheContentsOfTheBracket() { var generator = new ExpressionGenerator(); var func = generator.Generate("(5)"); var result = func(null); Assert.Equal(5m, (decimal)result); }
public void StringIsAValidExpression() { var generator = new ExpressionGenerator(); var func = generator.Generate("\"hello\""); var result = func(new Dictionary <string, string>()); Assert.Equal("hello", (string)result); }
public void FalseReturnsFalse() { var generator = new ExpressionGenerator(); var func = generator.Generate("false"); var result = func(null); Assert.False((bool)result); }
public void CanDoNotEqualOnTwoNumbers() { var generator = new ExpressionGenerator(); var func = generator.Generate("2 != 1"); var result = func(null); Assert.True((bool)result); }
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); }
public void CanDoNotEqualOnTwoStrings() { var generator = new ExpressionGenerator(); var func = generator.Generate("\"Hello\" != \"World\""); var result = func(null); Assert.True((bool)result); }
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); }
public void NotOperatorInvertsABooleanExpression() { var generator = new ExpressionGenerator(); var func = generator.Generate("!(1 = 0)"); var result = func(null); Assert.True((bool)result); }
public void CanSubtractTwoNumbers() { var generator = new ExpressionGenerator(); var func = generator.Generate("5 - 4"); var result = func(null); Assert.Equal(1m, (decimal)result); }
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; }