public void TypeExpressions() { var expected = LinqExpression.Block( typeof(object), LinqExpression.Default(typeof(string))); var actual = @" @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . @prefix : <http://example.com/> . :s :blockType [ :typeName ""System.Object"" ; ] ; :blockExpressions ( [ :defaultType [ :typeName ""System.String"" ; ] ; ] ) ; . "; ShouldBe(actual, expected); }
public override MSAst TransformCore(ScriptGenerator generator) { var expression = Expression; if (expression == null) return MSAst.Default(typeof(object)); var locals = _locals.ToArray(); var scope = generator.PushNewScope(); try { var localInitializers = locals.Select(o => o.Initializer.Transform(generator)).ToArray(); //var existingParamCount = generator.Scope.Parent.Locals.Count(); foreach (var local in locals) scope.CreateParameter(local.VariableName); var lambdaBody = expression.Transform(generator); var innerLambda = scope.FinishScope(lambdaBody); return MSAst.Invoke( innerLambda, localInitializers); } finally { //generator.PopScope(); generator.PopScope(); } }
public override MSAst TransformCore(ScriptGenerator generator) { if (Kind == LiteralKind.Null) { return(MSAst.Default(typeof(object))); } return(MSAst.Constant( _value, Type)); }
public override bool CanConvertFrom(Type fromType, Type toType, bool toNotNullable, NarrowingLevel level) { var conversionResultKind = level.ToConversionResultKind(); if (!conversionResultKind.HasValue) { return(toType.IsAssignableFrom(fromType)); } var conversionResult = ConvertExpression( MSAst.Default(fromType), toType, conversionResultKind.Value, _context.OverloadResolver); return(conversionResult != null); }
public void ExpressionsVariables() { var expected = LinqExpression.Block( new[] { LinqExpression.Parameter( typeof(string)), }, new[] { LinqExpression.Default( typeof(string)), }); var actual = @" @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . @prefix : <http://example.com/> . :s :blockVariables ( [ :parameterType [ :typeName ""System.String"" ; ] ; ] ) ; :blockExpressions ( [ :defaultType [ :typeName ""System.String"" ; ] ; ] ) ; . "; ShouldBe(actual, expected); }
public static Ex NoResult(Type t) => Ex.Default(typeof(ConversionResult <>).MakeGenericType(t));
public void All() { var expected = LinqExpression.Switch( typeof(SampleClass), LinqExpression.Constant(0L), LinqExpression.Default(typeof(SampleDerivedClass)), typeof(SampleClass).GetMethod("Equal"), LinqExpression.SwitchCase( LinqExpression.Default(typeof(SampleDerivedClass)), LinqExpression.Constant(0L))); using var g = new GraphEngine.Graph(); g.LoadFromString(@" @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . @prefix : <http://example.com/> . :s :switchType [ :typeName ""GraphEngine.Tests.SampleClass, GraphEngine.Tests"" ; ] ; :switchSwitchValue _:zero ; :switchDefaultBody [ :defaultType [ :typeName ""GraphEngine.Tests.SampleDerivedClass, GraphEngine.Tests"" ; ] ] ; :switchCases ( [ :caseTestValues ( _:zero ) ; :caseBody [ :defaultType [ :typeName ""GraphEngine.Tests.SampleDerivedClass, GraphEngine.Tests"" ; ] ] ; ] ) ; :switchComparison [ :memberType [ :typeName ""GraphEngine.Tests.SampleClass, GraphEngine.Tests"" ; ] ; :memberName ""Equal"" ; ] ; . _:zero :constantValue 0 ; . "); var s = g.GetUriNode(":s"); var actual = Expression.Parse(s).LinqExpression; actual.Should().Be(expected); Console.WriteLine(actual.GetDebugView()); // Make sure custom type is used Assert.AreEqual(typeof(SampleClass), actual.Type); // Make sure custom comparison is used Assert.ThrowsException <TargetInvocationException>(() => LinqExpression.Lambda(actual).Compile().DynamicInvoke()); }
/// <summary> /// If the switcher is 1, return the result, otherwise the default value. /// </summary> public static TEx <T> If1 <T>(tfloat switcher, TEx <T> result) => Ex.Condition(Ex.Equal(switcher, E1), result, Ex.Default(typeof(T)));
public override MSAst TransformCore(ScriptGenerator generator) { return(MSAst.Default(_resolvedType ?? TypeManager.CoreTypes.Object)); }