Example #1
0
 public void VisitNode(JSInitializerApplicationExpression iae)
 {
     Output.LPar();
     Visit(iae.Target);
     Output.RPar();
     Output.Dot();
     Output.Identifier("__Initialize__");
     Output.LPar();
     Visit(iae.Initializer);
     Output.RPar();
 }
Example #2
0
        public void VisitNode(JSBinaryOperatorExpression boe)
        {
            JSExpression left, right, nestedLeft;

            if (!JSReferenceExpression.TryDereference(JSIL, boe.Left, out left))
                left = boe.Left;
            if (!JSReferenceExpression.TryDereference(JSIL, boe.Right, out right))
                right = boe.Right;

            var nestedBoe = right as JSBinaryOperatorExpression;
            var isAssignment = (boe.Operator == JSOperator.Assignment);
            var leftNew = left as JSNewExpression;
            var rightNew = right as JSNewExpression;
            var leftVar = left as JSVariable;

            if (
                isAssignment && (nestedBoe != null) &&
                (left.IsConstant || (leftVar != null) || left is JSDotExpressionBase) &&
                !(ParentNode is JSVariableDeclarationStatement)
            ) {
                JSUnaryOperator prefixOperator;
                JSAssignmentOperator compoundOperator;

                if (!JSReferenceExpression.TryDereference(JSIL, nestedBoe.Left, out nestedLeft))
                    nestedLeft = nestedBoe.Left;

                var rightLiteral = nestedBoe.Right as JSIntegerLiteral;
                var areEqual = left.Equals(nestedLeft);

                if (
                    areEqual &&
                    PrefixOperators.TryGetValue(nestedBoe.Operator, out prefixOperator) &&
                    (rightLiteral != null) && (rightLiteral.Value == 1)
                ) {
                    var newUoe = new JSUnaryOperatorExpression(
                        prefixOperator, boe.Left,
                        boe.GetActualType(TypeSystem)
                    );

                    ParentNode.ReplaceChild(boe, newUoe);
                    VisitReplacement(newUoe);

                    return;
                } else if (
                    areEqual &&
                    CompoundAssignments.TryGetValue(nestedBoe.Operator, out compoundOperator)
                ) {
                    var newBoe = new JSBinaryOperatorExpression(
                        compoundOperator, boe.Left, nestedBoe.Right,
                        boe.GetActualType(TypeSystem)
                    );

                    ParentNode.ReplaceChild(boe, newBoe);
                    VisitReplacement(newBoe);

                    return;
                }
            } else if (
                isAssignment && (leftNew != null) &&
                (rightNew != null)
            ) {
                var rightType = rightNew.Type as JSDotExpressionBase;
                if (
                    (rightType != null) &&
                    (rightType.Member.Identifier == "CollectionInitializer")
                ) {
                    var newInitializer = new JSInitializerApplicationExpression(
                        boe.Left, new JSArrayExpression(
                            TypeSystem.Object,
                            rightNew.Arguments.ToArray()
                        )
                    );

                    ParentNode.ReplaceChild(boe, newInitializer);
                    VisitReplacement(newInitializer);

                    return;
                }
            } else if (
                isAssignment && (leftVar != null) &&
                leftVar.IsThis
            ) {
                var leftType = leftVar.GetActualType(TypeSystem);
                if (!TypeUtil.IsStruct(leftType)) {
                    ParentNode.ReplaceChild(boe, new JSUntranslatableExpression(boe));

                    return;
                } else {
                    var newInvocation = JSInvocationExpression.InvokeStatic(
                        JSIL.CopyMembers, new[] { boe.Right, leftVar }
                    );

                    ParentNode.ReplaceChild(boe, newInvocation);
                    VisitReplacement(newInvocation);

                    return;
                }
            }

            VisitChildren(boe);
        }
Example #3
0
        public void VisitNode(JSBinaryOperatorExpression boe)
        {
            JSExpression left, right, nestedLeft;

            if (!JSReferenceExpression.TryDereference(JSIL, boe.Left, out left))
            {
                left = boe.Left;
            }
            if (!JSReferenceExpression.TryDereference(JSIL, boe.Right, out right))
            {
                right = boe.Right;
            }

            var nestedBoe    = right as JSBinaryOperatorExpression;
            var isAssignment = (boe.Operator == JSOperator.Assignment);
            var leftNew      = left as JSNewExpression;
            var rightNew     = right as JSNewExpression;
            var leftVar      = left as JSVariable;

            if (
                isAssignment && (nestedBoe != null) &&
                (left.IsConstant || (leftVar != null) || left is JSDotExpressionBase) &&
                !(ParentNode is JSVariableDeclarationStatement)
                )
            {
                JSUnaryOperator      prefixOperator;
                JSAssignmentOperator compoundOperator;

                if (!JSReferenceExpression.TryDereference(JSIL, nestedBoe.Left, out nestedLeft))
                {
                    nestedLeft = nestedBoe.Left;
                }

                var rightLiteral = nestedBoe.Right as JSIntegerLiteral;
                var areEqual     = left.Equals(nestedLeft);

                if (
                    areEqual &&
                    PrefixOperators.TryGetValue(nestedBoe.Operator, out prefixOperator) &&
                    (rightLiteral != null) && (rightLiteral.Value == 1)
                    )
                {
                    var newUoe = new JSUnaryOperatorExpression(
                        prefixOperator, boe.Left,
                        boe.GetActualType(TypeSystem)
                        );

                    ParentNode.ReplaceChild(boe, newUoe);
                    VisitReplacement(newUoe);

                    return;
                }
                else if (
                    areEqual &&
                    CompoundAssignments.TryGetValue(nestedBoe.Operator, out compoundOperator)
                    )
                {
                    var newBoe = new JSBinaryOperatorExpression(
                        compoundOperator, boe.Left, nestedBoe.Right,
                        boe.GetActualType(TypeSystem)
                        );

                    ParentNode.ReplaceChild(boe, newBoe);
                    VisitReplacement(newBoe);

                    return;
                }
            }
            else if (
                isAssignment && (leftNew != null) &&
                (rightNew != null)
                )
            {
                var rightType = rightNew.Type as JSDotExpressionBase;
                if (
                    (rightType != null) &&
                    (rightType.Member.Identifier == "CollectionInitializer")
                    )
                {
                    var newInitializer = new JSInitializerApplicationExpression(
                        boe.Left, new JSArrayExpression(
                            TypeSystem.Object,
                            rightNew.Arguments.ToArray()
                            )
                        );

                    ParentNode.ReplaceChild(boe, newInitializer);
                    VisitReplacement(newInitializer);

                    return;
                }
            }
            else if (
                isAssignment && (leftVar != null) &&
                leftVar.IsThis
                )
            {
                var leftType = leftVar.GetActualType(TypeSystem);
                if (!TypeUtil.IsStruct(leftType))
                {
                    ParentNode.ReplaceChild(boe, new JSUntranslatableExpression(boe));

                    return;
                }
                else
                {
                    var newInvocation = JSInvocationExpression.InvokeStatic(
                        JSIL.CopyMembers, new[] { boe.Right, leftVar }
                        );

                    ParentNode.ReplaceChild(boe, newInvocation);
                    VisitReplacement(newInvocation);

                    return;
                }
            }

            VisitChildren(boe);
        }