コード例 #1
0
        public void FunctionPropertiesTest()
        {
            var availableProperties = new List <string> {
                "Function", "Operand", "Arguments"
            };

            using (var expression = NSExpression.FromFormat("2*2")) {
                Assert.AreEqual(NSExpressionType.Function, expression.ExpressionType);
                TestProperties(expression, availableProperties);
            }
        }
コード例 #2
0
        public void ConstantPropertiesTest()
        {
            var availableProperties = new List <string> {
                "ConstantValue"
            };

            using (var expression = NSExpression.FromFormat("2")) {
                Assert.AreEqual(NSExpressionType.ConstantValue, expression.ExpressionType);
                TestProperties(expression, availableProperties);
            }
        }
コード例 #3
0
 public void FromFormatWithNoArgsTest()
 {
     using (var expression = NSExpression.FromFormat("2*2"))
         using (var result = expression.EvaluateWith(null, null) as NSNumber)
             Assert.AreEqual(4.0, result.DoubleValue);
 }
コード例 #4
0
 public void FromFormatWithArgsTest()
 {
     using (var expression = NSExpression.FromFormat("%f*%f", new NSObject [] { new NSNumber(2.0), new NSNumber(2.0) }))
         using (var result = expression.EvaluateWith(null, null) as NSNumber)
             Assert.AreEqual(4.0, result.DoubleValue);
 }
コード例 #5
0
 public void FromFormatConstant()
 {
     using (var expression = NSExpression.FromFormat("2"))
         using (var result = expression.EvaluateWith(null, null) as NSNumber)
             Assert.AreEqual(2, result.DoubleValue);
 }
コード例 #6
0
        static NSExpression ToExpression(NSArray array)
        {
            var objects   = SubArray(array, 0);
            var @operator = objects[0].ToString();
            var arguments = SubArray(objects, 1);

            if (OperatorMap.TryGetValue(@operator, out var functionName))
            {
                var subexpressions = ToSubexpressions(arguments);
                switch (functionName)
                {
                case "+":
                    if (subexpressions.Count > 2)
                    {
                        var sumArguments = NSExpression.FromAggregate(subexpressions.ToArray());

                        return(NSExpression.FromFunction("sum:", new[] { sumArguments }));
                    }

                    break;

                case "^":
                    if (arguments[0].ToString() == "e")
                    {
                        functionName   = @"exp:";
                        subexpressions = SubArray(subexpressions, 1);
                    }

                    break;

                default:
                    break;
                }

                return(NSExpression.FromFunction(functionName, subexpressions.ToArray()));
            }
            else
            {
                switch (@operator)
                {
                case "collator":
                    return(NSExpression.FromFunction("MGL_FUNCTION", ToSubexpressions(objects).ToArray()));

                case "literal":
                    if (arguments[0] is NSArray literalArray)
                    {
                        return(NSExpression.FromConstant(literalArray));
                        // WARNING This may be wrong
                        //var expressions = ToSubexpressions(SubArray(literalArray, 0));
                        //return NSExpression.FromAggregate(expressions.ToArray());
                    }

                    return(ToExpression(arguments[0]));

                case "to-boolean": {
                    var operand = ToExpression(arguments[0]);
                    return(NSExpression.FromFunction(operand, "boolValue", new NSExpression[0]));
                }

                case "to-number":
                case "number": {
                    var operand = ToExpression(arguments[0]);
                    if (arguments.Count == 1)
                    {
                        if (arguments[0] is NSNumber number)
                        {
                            return(NSExpression.FromFormat("%@", new NSObject[] { arguments[0] }));
                        }
                        return(NSExpression.FromFormat("CAST(%@, 'NSNumber')", new NSObject[] { operand }));
                    }
                    else
                    {
                        var innerArguments = SubArray(arguments, 1);
                        return(NSExpression.FromFunction(operand, "mgl_numberWithFallbackValues",
                                                         ToSubexpressions(innerArguments).ToArray()));
                    }
                }

                case "to-string":
                case "string": {
                    var operand = ToExpression(arguments[0]);
                    return(NSExpression.FromFormat("CAST(%@, 'NSString')", new NSObject[] { operand }));
                }

                case "to-color":
                case "color": {
                    var operand = ToExpression(arguments[0]);

                    if (arguments.Count == 1)
                    {
#if __IOS__
                        return(NSExpression.FromFormat("CAST(%@, 'UIColor')", new NSObject[] { operand }));
#else
                        return(NSExpression.FromFormat("CAST(%@, 'NSColor')", new NSObject[] { operand }));
#endif
                    }

                    return(NSExpression.FromFunction("MGL_FUNCTION", ToSubexpressions(objects).ToArray()));
                }

                case "to-rgba": {
                    var operand = ToExpression(arguments[0]);
                    return(NSExpression.FromFormat("CAST(noindex(%@), 'NSArray')", new NSObject[] { operand }));
                }

                case "get": {
                    if (arguments.Count == 2)
                    {
                        var operand = ToExpression(arguments[arguments.Count - 1]);

                        if (arguments[0] is NSString value)
                        {
                            return(NSExpression.FromFormat("%@.%K", new NSObject[] { operand, value }));
                        }

                        var keyExpression = ToExpression(arguments[0]);
                        return(NSExpression.FromFormat("%@.%@", new NSObject[] { operand, keyExpression }));
                    }

                    return(NSExpression.FromKeyPath(arguments[0].ToString()));
                }

                case "length": {
                    var subexpressions = ToSubexpressions(arguments);
                    var function       = @"count:";

                    if (subexpressions[0].ExpressionType == NSExpressionType.ConstantValue &&
                        subexpressions[0].ConstantValue is NSString value)
                    {
                        function = "length:";
                    }

                    return(NSExpression.FromFunction(function, new NSExpression[] { subexpressions[0] }));
                }
                    ;

                case "rgb": {
                    var subexpressions = ToSubexpressions(arguments);

                    return(UIColorAdditions.FromRgb(subexpressions.ToArray()));
                }

                case "rgba": {
                    var subexpressions = ToSubexpressions(arguments);

                    return(UIColorAdditions.FromRgba(subexpressions.ToArray()));
                }

                case "min":
                case "max": {
                    var subexpressions = ToSubexpressions(arguments);
                    var subexpression  = NSExpression.FromAggregate(subexpressions.ToArray());
                    return(NSExpression.FromFunction(@operator, new[] { subexpression }));
                }

                case "e": {
                    return(NSExpression.FromConstant(NSNumber.FromDouble(Math.E)));
                }

                case "pi": {
                    return(NSExpression.FromConstant(NSNumber.FromDouble(Math.PI)));
                }

                case "concat": {
                    var subexpressions = ToSubexpressions(arguments);
                    var subexpression  = NSExpression.FromAggregate(subexpressions.ToArray());
                    return(NSExpression.FromFunction("mgl_join:", new[] { subexpression }));
                }

                case "at": {
                    var subexpressions = ToSubexpressions(arguments);

                    return(NSExpression.FromFunction("objectFrom:withIndex:",
                                                     new[] { subexpressions[1], subexpressions[0] }));
                }

                case "has": {
                    var subexpressions = ToSubexpressions(arguments);
                    var operand        = subexpressions.Count > 1
                                ? subexpressions[1]
                                : NSExpression.ExpressionForEvaluatedObject;
                    var key = subexpressions[0];
                    return(NSExpression.FromFunction("mgl_does:have:", new[] { operand, key }));
                }

                case "interpolate": {
                    var firstObject = arguments[0];
                    if (firstObject is NSArray == false)
                    {
                        return(null);
                    }

                    var interpolationOptions = SubArray((NSArray)firstObject);

                    var      curveType           = interpolationOptions[0].ToString();
                    var      curveTypeExpression = ToExpression(interpolationOptions[0]);
                    NSObject curveParameters;
                    switch (curveType)
                    {
                    case "linear": {
                        var temp = new NSMutableArray(2);
                        temp.Add((NSString)"literal");
                        temp.Add(NSNumber.FromDouble(1.0));
                        curveParameters = temp.Copy();
                        temp.Dispose();
                        break;
                    }

                    case "exponential":
                        curveParameters = interpolationOptions[1];
                        break;

                    case "cubic-bezier": {
                        var temp = new NSMutableArray(5);
                        temp.Add((NSString)"literal");
                        temp.Add(interpolationOptions[1]);
                        temp.Add(interpolationOptions[2]);
                        temp.Add(interpolationOptions[3]);
                        temp.Add(interpolationOptions[4]);
                        curveParameters = temp.Copy();
                        temp.Dispose();
                        break;
                    }

                    default:
                        curveParameters = NSNull.Null;
                        break;
                    }

                    var curveParameterExpression = ToExpression(curveParameters);
                    arguments = SubArray(arguments, 1);

                    var inputExpression = ToExpression(arguments[0]);
                    var stopExpressions = SubArray(arguments, 1);
                    var stops           = new NSMutableDictionary();
                    for (int i = 0; i < stopExpressions.Count; i++)
                    {
                        var keyExpression   = stopExpressions[i++];
                        var valueExpression = ToExpression(stopExpressions[i]);

                        stops[keyExpression] = valueExpression;
                    }

                    var stopExpression = NSExpression.FromConstant(stops);

                    return(NSExpression.FromFunction("mgl_interpolate:withCurveType:parameters:stops:", new[]
                        {
                            inputExpression,
                            curveTypeExpression,
                            curveParameterExpression,
                            stopExpression
                        }));
                }

                case "step": {
                    var          inputExpression  = ToExpression(arguments[0]);
                    var          stopExpressions  = SubArray(arguments, 1);
                    NSExpression miniumExpression = null;

                    if (stopExpressions.Count % 2 == 1)
                    {
                        miniumExpression = ToExpression(stopExpressions[0]);
                        stopExpressions  = SubArray(stopExpressions, 1);
                    }

                    var stops = new NSMutableDictionary();
                    for (int i = 0; i < stopExpressions.Count; i++)
                    {
                        var keyExpression   = stopExpressions[i++];
                        var valueExpression = ToExpression(stopExpressions[i]);

                        if (miniumExpression == null)
                        {
                            miniumExpression = valueExpression;
                            continue;
                        }

                        stops[keyExpression] = valueExpression;
                    }

                    if (miniumExpression == null)
                    {
                        return(null);
                    }

                    var stopExpression = NSExpression.FromConstant(stops);

                    return(NSExpression.FromFunction("mgl_step:from:stops:", new[]
                        {
                            inputExpression,
                            miniumExpression,
                            stopExpression
                        }));
                }

                case "zoom": {
                    return(NSExpression_MGLAdditions.ZoomLevelVariableExpression(null));
                }

                case "heatmap-density": {
                    return(NSExpression_MGLAdditions.HeatmapDensityVariableExpression(null));
                }

                case "line-progress": {
                    return(NSExpression_MGLAdditions.LineProgressVariableExpression(null));
                }

                case "geometry-type": {
                    return(NSExpression_MGLAdditions.GeometryTypeVariableExpression(null));
                }

                case "id": {
                    return(NSExpression_MGLAdditions.FeatureIdentifierVariableExpression(null));
                }

                case "properties": {
                    return(NSExpression_MGLAdditions.FeatureAttributesVariableExpression(null));
                }

                case "var": {
                    return(NSExpression.FromVariable(arguments[0].ToString()));
                }

                case "case": {
                    var args = new NSMutableArray();

                    for (int i = 0; i < arguments.Count; i++)
                    {
                        if (i % 2 == 0 & i != arguments.Count - 1)
                        {
                            var predicate = ToPredicate(arguments[i]);
                            var arg       = NSExpression.FromConstant(predicate);
                            args.Add(arg);
                        }
                        else
                        {
                            args.Add(ToExpression(arguments[i]));
                        }
                    }

                    if (args.Count == 3)
                    {
                        return(NSExpression.FromConditional(
                                   (NSPredicate)ObjCRuntime.Runtime.GetNSObject <NSExpression>(args.ValueAt(0))
                                   .ConstantValue,
                                   ObjCRuntime.Runtime.GetNSObject <NSExpression>(args.ValueAt(1)),
                                   ObjCRuntime.Runtime.GetNSObject <NSExpression>(args.ValueAt(2))));
                    }

                    return(NSExpression.FromFunction("MGL_IF",
                                                     NSArray.FromArray <NSExpression>((NSArray)args.Copy())));
                }

                case "match": {
                    var optionsArray = new NSMutableArray();

                    for (int i = 0; i < arguments.Count; i++)
                    {
                        NSExpression option = ToExpression(arguments[i]);

                        if (i > 0 && i < arguments.Count - 1 && !(i % 2 == 0) && arguments[i] is NSArray sub)
                        {
                            option = NSExpression.FromAggregate(ToSubexpressions(SubArray(sub)).ToArray());
                        }

                        optionsArray.Add(option);
                    }

                    return(NSExpression.FromFunction("MGL_MATCH",
                                                     NSArray.FromArray <NSExpression>((NSArray)optionsArray.Copy())));
                }

                case "format": {
                    var attributedExpressions = new List <NSExpression>();

                    for (int i = 0; i < arguments.Count; i += 2)
                    {
                        var expression = ToExpression(arguments[i]);
                        var attrs      = new NSMutableDictionary();

                        if (i + 1 < arguments.Count)
                        {
                            attrs = NSMutableDictionary.FromDictionary(arguments[i + 1] as NSDictionary);
                        }

                        foreach (var key in attrs.Keys)
                        {
                            attrs[key] = ToExpression(attrs[key]);
                        }

                        var attributedExpression = new MGLAttributedExpression(expression, attrs);

                        attributedExpressions.Add(NSExpression.FromConstant(attributedExpression));
                    }

                    return(NSExpression.FromFunction("mgl_attributed:", attributedExpressions.ToArray()));
                }

                case "coalesce": {
                    var subexpressions = ToSubexpressions(arguments);
                    return(NSExpression.FromFormat("mgl_coalesce(%@)", subexpressions.ToArray()));
                }

                default: {
                    var subexpressions = ToSubexpressions(objects);
                    return(NSExpression.FromFunction(@"MGL_FUNCTION", subexpressions.ToArray()));
                }
                }
            }
        }