public static Condition ( |
||
test | An |
|
ifTrue | An |
|
ifFalse | An |
|
리턴 |
public LambdaExpression CreateLambda(Type from, Type to) { var input = Ex.Parameter(from, "input"); var fromInfo = infos[from]; var toInfo = infos[to]; if (fromInfo <= toInfo) // Can make use of an implicit conversion { var block = Result(to, Ex.Convert(input, to)); var lambda = Ex.Lambda(block, input); return(lambda); } else // Cannot make use of an implicit conversion, bounds must be checked. Precision might be lost. { var block = Ex.Condition( Ex.MakeBinary(Et.AndAlso, Ex.MakeBinary(Et.GreaterThanOrEqual, input, Ex.Convert(Ex.Constant(toInfo.MinValue), from)), Ex.MakeBinary(Et.LessThanOrEqual, input, Ex.Convert(Ex.Constant(toInfo.MaxValue), from))), Result(to, Ex.Convert(input, to)), NoResult(to)); var lambda = Ex.Lambda(block, input); return(lambda); } }
private LambdaExpression fromEnumerableLambda(Type from) { var input = Ex.Parameter(from, "input"); var eType = from.GetTypeInfo().ImplementedInterfaces .Where(i => i.GenericTypeArguments.Length == 1 && i.GetGenericTypeDefinition() == typeof(IEnumerable <>)) .Select(i => i.GenericTypeArguments[0]).SingleOrDefault() ?? from.GetTypeInfo().GenericTypeArguments[0]; var res = Ex.Parameter(typeof(string), "res"); var result = Ex.Block(new[] { res }, Ex.Assign(res, Ex.Call((from mi in typeof(string).GetTypeInfo().GetDeclaredMethods(nameof(string.Join)) where mi.GetGenericArguments().Length == 1 let par = mi.GetParameters() where par.Length == 2 && par[0].ParameterType == typeof(string) && par[1].ParameterType == typeof(IEnumerable <>).MakeGenericType(mi.GetGenericArguments()[0]) select mi).Single().MakeGenericMethod(eType), Ex.Constant(Separators[0].ToString()), input)), Ex.Condition(Ex.MakeBinary(Et.Equal, Ex.Property(res, nameof(string.Length)), Ex.Constant(0)), NoResult(typeof(string)), Result(typeof(string), res))); var block = Ex.Condition(Ex.MakeBinary(Et.Equal, input, Ex.Default(from)), NoResult(typeof(string)), result); var lambda = Ex.Lambda(block, input); return(lambda); }
public void Condition() { var param = LinqExpression.Parameter(typeof(bool)); var expected = LinqExpression.Condition(param, param, param); using var g = new GraphEngine.Graph(); g.LoadFromString(@" @prefix : <http://example.com/> . :s :conditionTest _:param ; :conditionIfTrue _:param ; :conditionIfFalse _:param ; . _:param :parameterType [ :typeName ""System.Boolean"" ; ] ; . "); var s = g.GetUriNode(":s"); var actual = Expression.Parse(s).LinqExpression; Console.WriteLine(actual.GetDebugView()); actual.Should().Be(expected); }
public LambdaExpression CreateLambda(Type from, Type to) { var toParameters = to.GetTypeInfo().GenericTypeArguments; var tupa = toParameters.Length; var input = Ex.Parameter(from, "input"); var converters = toParameters.Select(p => Ref.GetLambda(typeof(string), p)).ToArray(); var res = toParameters.Select(p => Ex.Parameter(typeof(ConversionResult <>).MakeGenericType(p))).ToArray(); var end = Ex.Label(typeof(ConversionResult <>).MakeGenericType(to), "end"); var indexer = typeof(string[]).GetTypeInfo().GetDeclaredProperty("Item"); var split = Ex.Parameter(typeof(string[]), "split"); var conversion = Ex.Block(converters.Select((c, i) => Ex.Block( Ex.Assign(res[i], c.ApplyTo(Ex.MakeIndex(split, indexer, new[] { Ex.MakeBinary(Et.Add, Ex.Constant(i), Ex.MakeBinary(Et.Subtract, Ex.Property(split, nameof(Array.Length)), Ex.Constant(tupa))) }))), Ex.IfThen(Ex.Not(Ex.Property(res[i], nameof(IConversionResult.IsSuccessful))), Ex.Goto(end, NoResult(to)))))); var block = Ex.Block(new[] { split }, Ex.Assign(split, Ex.Call(input, nameof(string.Split), Type.EmptyTypes, _separator)), Ex.Condition(Ex.MakeBinary(Et.LessThan, Ex.Property(split, nameof(Array.Length)), Ex.Constant(tupa)), NoResult(to), Ex.Block(res, Ex.IfThen(Ex.MakeBinary(Et.GreaterThan, Ex.Property(split, nameof(Array.Length)), Ex.Constant(tupa)), Ex.Assign(Ex.ArrayAccess(split, Ex.MakeBinary(Et.Subtract, Ex.Property(split, nameof(Array.Length)), Ex.Constant(tupa))), Ex.Call(typeof(string), nameof(string.Join), Type.EmptyTypes, _separatorString, Ex.Call(typeof(Enumerable), nameof(Enumerable.Take), new[] { typeof(string) }, split, Ex.MakeBinary(Et.Add, Ex.Constant(1), Ex.MakeBinary(Et.Subtract, Ex.Property(split, nameof(Array.Length)), Ex.Constant(tupa))))))), conversion, Ex.Label(end, Result(to, Ex.Call(Creator(to), res.Select(r => Ex.Property(r, nameof(IConversionResult.Result))))))))); var lambda = Ex.Lambda(block, input); return(lambda); }
public LambdaExpression CreateLambda(Type from, Type to) { var fromParameters = from.GetTypeInfo().GenericTypeArguments; var toParameters = to.GetTypeInfo().GenericTypeArguments; var converters = fromParameters .Zip(toParameters, (f, t) => Ref.GetLambda(f, t)) .ToArray(); var input = Ex.Parameter(from, "input"); var res = toParameters.Select(t => Ex.Parameter(typeof(ConversionResult <>).MakeGenericType(t))).ToArray(); var conversion = res.Select((r, i) => Ex.Assign(res[i], converters[i].ApplyTo(Ex.PropertyOrField(input, $"Item{i + 1}")))).ToArray(); var conversionSuccesful = Enumerable.Aggregate(res, (Ex)Ex.Constant(true), (c, p) => Ex.MakeBinary(Et.AndAlso, c, Ex.Property(p, nameof(IConversionResult.IsSuccessful)))); var block = Ex.Block(res, Ex.Block(conversion), Ex.Condition(conversionSuccesful, Result(to, Ex.Call(Creator(to), Enumerable.Select(res, p => Ex.Property(p, nameof(IConversionResult.Result))))), NoResult(to))); var lambda = Ex.Lambda(block, input); return(lambda); }
public void Factorial() { var value = LinqExpression.Parameter(typeof(int), "value"); var result = LinqExpression.Parameter(typeof(int), "result"); var label = LinqExpression.Label(typeof(int), "label"); var one = LinqExpression.Constant(1); var expression = LinqExpression.Block( new[] { result }, LinqExpression.Assign( result, one), LinqExpression.Loop( LinqExpression.Condition( LinqExpression.GreaterThan( value, one), LinqExpression.MultiplyAssign( result, LinqExpression.PostDecrementAssign( value)), LinqExpression.Break( label, result), typeof(void)), label)); ShouldRoundrip(expression); }
public void Condition() { var param = LinqExpression.Parameter(typeof(bool)); var expression = LinqExpression.Condition(param, param, param); ShouldRoundrip(expression); }
public static BlockExpression RotateLerp(Ex target, Ex source, TExArgCtx bpi, bool isRate, bool isTrue, Ex rate) { if (isRate) { rate = rate.Mul(M.degRad); } if (isTrue) { rate = rate.Mul(ETime.FRAME_TIME); } TExV2 v = TExV2.Variable(); TEx <float> ang = ExUtils.VFloat(); Expression[] exprs = new Expression[3]; exprs[1] = ang.Is(RadDiff(target, v)); if (isTrue) { var key = bpi.Ctx.NameWithSuffix("_RotateLerpKey"); exprs[0] = v.Is( Ex.Condition(FiringCtx.Contains <Vector2>(bpi, key), FiringCtx.GetValue <Vector2>(bpi, key), FiringCtx.SetValue <Vector2>(bpi, key, source) )); exprs[2] = FiringCtx.SetValue <Vector2>(bpi, key, RotateRad(isRate ? (Ex)Limit(rate, ang) : ang.Mul(rate), v)); } else { exprs[0] = v.Is(source); exprs[2] = RotateRad(isRate ? (Ex)Limit(bpi.t.Mul(rate), ang) : ang.Mul(Min(bpi.t.Mul(rate), E1)), v); } return(Ex.Block(new ParameterExpression[] { v, ang }, exprs)); }
public void ConditionType() { var expected = LinqExpression.Condition( LinqExpression.Parameter(typeof(bool)), LinqExpression.Parameter(typeof(SampleClass)), LinqExpression.Parameter(typeof(SampleDerivedClass)), typeof(SampleClass)); using var g = new GraphEngine.Graph(); g.LoadFromString(@" @prefix : <http://example.com/> . :s :conditionTest [:parameterType [:typeName ""System.Boolean"" ;]] ; :conditionIfTrue [:parameterType _:C1] ; :conditionIfFalse [:parameterType [:typeName ""GraphEngine.Tests.SampleDerivedClass, GraphEngine.Tests"" ;]] ; :conditionType _:C1 ; . _:C1 :typeName ""GraphEngine.Tests.SampleClass, GraphEngine.Tests"" . "); var s = g.GetUriNode(":s"); var actual = Expression.Parse(s).LinqExpression; Console.WriteLine(actual.GetDebugView()); actual.Should().Be(expected); }
public static BlockExpression LaserRotateLerp(Ex target, Ex source, TExArgCtx bpi, Ex rate) { var r1 = rate.Mul(ExC(ETime.FRAME_TIME)); TExV2 v = TExV2.Variable(); TEx <float> ang = ExUtils.VFloat(); var dirKey = bpi.Ctx.NameWithSuffix("_LaserRotateLerpDirKey"); var sideKey = bpi.Ctx.NameWithSuffix("_LaserRotateLerpSideKey"); var inter_ang = HighPass(ExC(0.01f), RadDiff(target, v)); return(Ex.Block(new ParameterExpression[] { v, ang }, Ex.Condition( bpi.FCtxHas <Vector2>(dirKey).And(bpi.t.GT0()), Ex.Block( v.Is(bpi.FCtxGet <Vector2>(dirKey)), ang.Is(Ex.Condition(bpi.FCtxGet <float>(sideKey).LT0(), RadToNeg(inter_ang), RadToPos(inter_ang) )), bpi.FCtxSet <Vector2>(dirKey, RotateRad(Limit(r1, ang), v)) ), Ex.Block( v.Is(source), ang.Is(RadDiff(target, v)), bpi.FCtxSet <float>(sideKey, Sign(ang)), bpi.FCtxSet <Vector2>(dirKey, RotateRad(Limit(r1, ang), v)) ) ) )); }
internal MetaObject /*!*/ CreateMetaObject(MetaObjectBinder /*!*/ action, MetaObject /*!*/[] /*!*/ siteArgs) { var expr = _error ? Ast.Throw(_result) : _result; Restrictions restrictions; if (_condition != null) { var deferral = action.Defer(siteArgs); expr = Ast.Condition(_condition, AstUtils.Convert(expr, typeof(object)), deferral.Expression); restrictions = deferral.Restrictions; } else { restrictions = Restrictions.Empty; } if (_temps != null) { expr = Ast.Block(_temps, expr); } if (_restriction != null) { restrictions = restrictions.Merge(Restrictions.GetExpressionRestriction(_restriction)); } return(new MetaObject(expr, restrictions)); }
//See Design/Engine Math Tips for details on these two functions. They are not raw easing. public static Func <T, TEx <R> > Ease <T, R>(Func <tfloat, tfloat> easer, float maxTime, Func <T, TEx <R> > f, Func <T, Ex> t, Func <T, Ex, T> withT) // x = f(g(t)), where g(t) = T e(t/T) => bpi => Ex.Condition(Ex.GreaterThan(t(bpi), ExC(maxTime)), f(bpi), f(withT(bpi, ExC(maxTime).Mul( easer(t(bpi).Mul(1f / maxTime)) )) ));
private static Ex conditional(Type from, Ex input, Ex result) { return(Ex.Condition( Ex.MakeBinary(Et.OrElse, Ex.MakeBinary(Et.Equal, input, Ex.Default(from)), Ex.MakeBinary(Et.Equal, Ex.Property(input, nameof(Array.Length)), Ex.Constant(0))), NoResult(typeof(string)), Result(typeof(string), result))); }
public void ConditionType() { var expression = LinqExpression.Condition( LinqExpression.Parameter(typeof(bool)), LinqExpression.Parameter(typeof(SampleDerivedClass)), LinqExpression.Parameter(typeof(SampleClass)), typeof(SampleClass)); ShouldRoundrip(expression); }
/// <summary> /// Lerp from the target parametric to zero. /// </summary> /// <param name="from_time">Time to start lerping</param> /// <param name="end_time">Time to end lerping</param> /// <param name="p">Target parametric</param> /// <returns></returns> public static ExTP LerpOut(float from_time, float end_time, ExTP p) { Ex etr = ExC(1f / (end_time - from_time)); Ex ex_end = ExC(end_time); return(bpi => Ex.Condition(Ex.GreaterThan(bpi.t, ex_end), v20, Ex.Multiply(p(bpi), Ex.Condition(Ex.LessThan(bpi.t, ExC(from_time)), E1, Ex.Multiply(etr, Ex.Subtract(ex_end, bpi.t)) )) )); }
public LambdaExpression CreateLambda(Type from, Type to) { var input = Ex.Parameter(from, "input"); var result = Ex.Parameter(to, "result"); var m = GetMethod(to); var block = Ex.Block(new[] { result }, Ex.Condition(Ex.Call(m, m.GetParameters().Select(pi => getParameter(pi, input, result))), Result(to, result), NoResult(to))); return(Ex.Lambda(block, input)); }
private Exp ToFloat(Exp expression) { if (expression.Type == typeof(float)) { return(expression); } if (expression.Type == typeof(bool)) { expression = Exp.Condition(expression, Exp.Constant(1.0f), Exp.Constant(0.0f)); } return(Exp.Convert(expression, typeof(float))); }
private Exp ToDouble(Exp expression) { if (expression.Type == typeof(double)) { return(expression); } if (expression.Type == typeof(bool)) { expression = Exp.Condition(expression, Exp.Constant(1.0), Exp.Constant(0.0)); } return(Exp.Convert(expression, typeof(double))); }
private Exp ToInteger(Exp expression) { if (expression.Type == typeof(int)) { return(expression); } if (expression.Type == typeof(bool)) { return(Exp.Condition(expression, Exp.Constant(1), Exp.Constant(0))); } return(Exp.Convert(expression, typeof(int))); }
private Exp MakeFunction(CompilerState state, Node node) { if (state == null) { throw new Exception(); } if (node == null) { throw new Exception(); } var data = node.Token.Data as Tokenizing.CustomFunctionData; if (data == null) { throw new Exception(); } if (data.Type == typeof(Triggers.Const) || data.Type == typeof(Triggers.GetHitVar)) { return(MakeSpecialFunction(state, data.Type, (string)node.Arguments[0])); } var functionargs = MakeDescendants(state, node); var method = FindCorrectMethod(data.Type, functionargs); var parameters = method.GetParameters(); functionargs.Insert(0, state.FunctionState); functionargs.Insert(1, state.ErrorVariable); var methodParamTypes = method.GetParameters().Select(o => o.ParameterType).ToList(); for (int i = 2; i < methodParamTypes.Count; i++) { var paramType = methodParamTypes[i]; var argType = functionargs[i].Type; if (paramType == typeof(int) && argType == typeof(bool)) { functionargs[i] = Exp.Condition(functionargs[i], Exp.Constant(1), Exp.Constant(0)); } else if (paramType != argType) { functionargs[i] = Exp.Convert(functionargs[i], paramType); } } var result = Exp.Call(null, method, functionargs); return(result); }
public LambdaExpression Creator(Type t) { var valueType = GetValueType(t); var inner = Ex.Parameter(typeof(IDictionary <,>).MakeGenericType(typeof(string), valueType), "inner"); return(Ex.Lambda( Ex.Condition( Ex.Property(Ex.Convert(inner, typeof(ICollection <>).MakeGenericType(typeof(KeyValuePair <,>).MakeGenericType(typeof(string), valueType))), nameof(ICollection <object> .IsReadOnly)), Ex.Default(typeof(DictionaryRecord <>).MakeGenericType(valueType)), Ex.New( typeof(DictionaryRecord <>).MakeGenericType(valueType).GetTypeInfo().DeclaredConstructors.First(), inner)), inner)); }
public LambdaExpression CreateLambda(Type from, Type to) { var input = Ex.Parameter(from, "input"); var ut = Enum.GetUnderlyingType(to); if (from == ut) { if (to.GetTypeInfo().GetCustomAttributes <FlagsAttribute>().Any()) { var vals = Enum.GetValues(to).OfType <object>().Select(x => Convert.ToInt64(x)); var mask = vals.Aggregate(0L, (x, y) => x | y); var block = Ex.Condition( Ex.MakeBinary(Et.Equal, Ex.MakeBinary(Et.And, input, Ex.Constant(helper.DoGeneralConversion(mask, ut).Result, ut)), input), Result(to, Ex.Convert(input, to)), NoResult(to)); var lambda = Ex.Lambda(block, input); return(lambda); } else { var vals = Enum.GetValues(to).OfType <object>().Select(x => Convert.ToInt64(x)); var min = vals.Min(); var max = vals.Max(); var block = Ex.Condition( Ex.MakeBinary(Et.AndAlso, Ex.MakeBinary(Et.GreaterThanOrEqual, input, Ex.Constant(helper.DoGeneralConversion(min, ut).Result, ut)), Ex.MakeBinary(Et.LessThanOrEqual, input, Ex.Constant(helper.DoGeneralConversion(max, ut).Result, ut))), Result(to, Ex.Convert(input, to)), NoResult(to)); var lambda = Ex.Lambda(block, input); return(lambda); } } else { var res = Ex.Parameter(typeof(ConversionResult <>).MakeGenericType(ut), "res"); var c1 = Ref.GetLambda(from, ut); var c2 = Ref.GetLambda(ut, to); var block = Ex.Block(new[] { res }, Ex.Assign(res, c1.ApplyTo(input)), Ex.Condition(Ex.Property(res, nameof(IConversionResult.IsSuccessful)), c2.ApplyTo(Ex.Property(res, nameof(IConversionResult.Result))), NoResult(to))); var lambda = Ex.Lambda(block, input); return(lambda); } }
private ConditionalExpression ConditionalExpression(ExpressionType nodeType, System.Type type, JObject obj) { var test = this.Prop(obj, "test", this.Expression); var ifTrue = this.Prop(obj, "ifTrue", this.Expression); var ifFalse = this.Prop(obj, "ifFalse", this.Expression); switch (nodeType) { case ExpressionType.Conditional: return(Expr.Condition(test, ifTrue, ifFalse, type)); default: throw new NotSupportedException(); } }
public void TestIfThen() { var y = VF("y"); var ex = Ex.Block(new[] { y }, Ex.Assign(y, ExC(4f)), Ex.Condition(Ex.GreaterThan(y, ExC(3f)), Ex.Assign(y, ExC(3f)), Ex.Add(y, ExC(1f)) ), Ex.Add(y, ExC(2f))); AreEqual("((y=4);\nif(y>3){(y=3)}else{(y+1)};\n(y+2);)", ex.Debug()); ex = Ex.Block(new[] { y }, Ex.Assign(y, ExC(4f)), Ex.Condition(Ex.GreaterThan(y, ExC(3f)), Ex.Add(y, ExC(1f)), Ex.Assign(y, ExC(3f)) ), Ex.Add(y, ExC(2f))); AreEqual("((y=4);\n5;\n6;)", ex.FlatDebug()); }
protected override Expression VisitMethodCall(MethodCallExpression node) { var args = node.Arguments; if (mathTypes.Contains(node.Method.DeclaringType)) { var a1 = Visit(args[0]); if (node.Method.Name == "Sin") { return(a1.Mul(Cos(args[0]))); } if (node.Method.Name == "Cos") { return(a1.Neg().Mul(Sin(args[0]))); } if (node.Method.Name == "SinDeg") { return(a1.Mul(RadDeg(CosDeg(args[0])))); } if (node.Method.Name == "CosDeg") { return(a1.Neg().Mul(RadDeg(SinDeg(args[0])))); } if (node.Method.Name == "Pow" && args[1] is UnaryExpression exp) { //TODO a safer architecture for handling double casting if (!(Visit(exp.Operand).TryAsConst(out float f) && f == 0)) { throw new DerivativeException("Power call has a non-constant exponent"); } else { return(exp.Operand.Mul(Pow(args[0], exp.Operand.Sub(E1)))); } } if (node.Method.Name == "Floor" || node.Method.Name == "Ceiling") { return(E0); } if (node.Method.Name == "Min") { return(Ex.Condition(args[0].LT(args[1]), Visit(args[0]), Visit(args[1]))); } if (node.Method.Name == "Max") { return(Ex.Condition(args[0].GT(args[1]), Visit(args[0]), Visit(args[1]))); } }
public static Func <TExArgCtx, TEx <T> > Pivot <S, T>(Func <TExArgCtx, TEx <float> > pivot, Func <TExArgCtx, TEx <T> > f1, Func <TExArgCtx, TEx <T> > f2, Func <TExArgCtx, TEx> pivotVar) where S : TEx, new() => t => { var pv = VFloat(); var pivotT = t.MakeCopyForType <S>(out var currEx, out var pivotEx); return(Ex.Block(new[] { pv }, pv.Is(pivot(t)), Ex.Condition(pv.LT(pivotVar(t)), Ex.Block(new ParameterExpression[] { pivotEx }, Ex.Assign(pivotEx, currEx), Ex.Assign(pivotVar(pivotT), pv), Ex.Add(f1(pivotT), Ex.Subtract(f2(t), f2(pivotT))) ), f1(t) ) )); };
private LambdaExpression fromStringLambda(Type to) { var input = Ex.Parameter(typeof(string), "input"); var split = Ex.Parameter(typeof(string[]), "split"); var converter = Ref.GetLambda(typeof(string[]), to); var block = Ex.Condition(Ex.MakeBinary(Et.Equal, input, Ex.Default(typeof(string))), NoResult(to), Ex.Block(new[] { split }, Ex.Assign(split, Ex.Call(input, nameof(string.Split), Type.EmptyTypes, Ex.Constant(Separators))), Ex.Condition(Ex.MakeBinary(Et.Equal, Ex.Property(split, nameof(Array.Length)), Ex.Constant(1)), NoResult(to), converter.ApplyTo(split)))); var lambda = Ex.Lambda(block, input); return(lambda); }
public static ExTP LSaveNearestEnemy() => b => { var key = b.Ctx.NameWithSuffix("_LSaveNearestEnemyKey"); var eid_in = ExUtils.V <int?>(); var eid = ExUtils.V <int>(); var loc = new TExV2(); return(Ex.Block(new[] { eid_in, eid, loc }, eid_in.Is(Ex.Condition(FiringCtx.Contains <int>(b, key), FiringCtx.GetValue <int>(b, key).As <int?>(), Ex.Constant(null).As <int?>()) ), Ex.IfThenElse(Enemy.findNearestSave.Of(b.loc, eid_in, eid, loc), FiringCtx.SetValue <int>(b, key, eid), loc.Is(Ex.Constant(new Vector2(0f, 50f))) ), loc )); };
public LambdaExpression CreateLambda(Type from, Type to) { if (from == typeof(bool)) { var input = Ex.Parameter(typeof(bool), "input"); if (to == typeof(string)) { return(Ex.Lambda(Result(to, Ex.Call(input, nameof(bool.ToString), Type.EmptyTypes)), input)); } else { return(Ex.Lambda(Result(to, Ex.Condition(input, Ex.Constant(Convert.ChangeType(1, to), to), Ex.Constant(Convert.ChangeType(0, to), to))), input)); } } else // to == typeof(bool) { var input = Ex.Parameter(from, "input"); if (from == typeof(string)) { var i = Ex.Parameter(typeof(int), "i"); var body = Ex.Block(new[] { i }, Ex.Condition( Ex.Call(typeof(string), nameof(string.Equals), Type.EmptyTypes, input, Ex.Constant("true"), Ex.Constant(StringComparison.OrdinalIgnoreCase)), Result(to, Ex.Constant(true)), Ex.Condition( Ex.Call(typeof(string), nameof(string.Equals), Type.EmptyTypes, input, Ex.Constant("false"), Ex.Constant(StringComparison.OrdinalIgnoreCase)), Result(to, Ex.Constant(false)), Ex.Condition(Ex.Call(typeof(int), nameof(int.TryParse), Type.EmptyTypes, input, i), Result(to, Ex.MakeBinary(ExpressionType.NotEqual, i, Ex.Constant(0))), NoResult(to))))); return(Ex.Lambda(body, input)); } else { return(Ex.Lambda(Result(to, Ex.MakeBinary(ExpressionType.NotEqual, input, Ex.Constant(Convert.ChangeType(0, from), from))), input)); } } }
public void TestCond() { var x = VF("x"); var yi = VF("y"); var zi = VF("z"); var ex = Ex.Condition(Ex.Block(zi.Is(ExC(5f)), ExC(true)), Ex.Block(new[] { yi }, yi.Is(ExC(5f)), yi.Add(x)), ExC(2f) ); AreEqual(ex.Linearize().ToCSharpString(), @" float linz_0; z = (5f); if (true) { float y; y = (5f); linz_0 = ((y) + (x)); } else { linz_0 = (2f); } linz_0;;"); //ternary ok var ex2 = Ex.Condition(Ex.Block(zi.Is(ExC(5f)), ExC(true)), yi.Add(x), ExC(2f) ); AreEqual(ex2.Linearize().ToCSharpString(), @" z = (5f); (true ? (y) + (x) : 2f);;"); //cond can be simplified var ex3 = Ex.Condition(Ex.GreaterThan(zi, ExC(5f)), yi.Add(x), ExC(2f) ); AreEqual(ex3.Linearize().ToCSharpString(), @"((z) > (5f) ? (y) + (x) : 2f);"); }