Exemplo n.º 1
0
        private JSBinaryOperatorExpression MakeUnaryMutation(
            JSExpression expressionToMutate, JSBinaryOperator mutationOperator,
            TypeReference type
            )
        {
            var newValue = new JSBinaryOperatorExpression(
                mutationOperator, expressionToMutate, JSLiteral.New(1),
                type
                );
            var assignment = new JSBinaryOperatorExpression(
                JSOperator.Assignment,
                expressionToMutate, newValue, type
                );

            return(assignment);
        }
Exemplo n.º 2
0
        public static JSBinaryOperatorExpression MakeUnaryMutation(
            JSExpression expressionToMutate, JSBinaryOperator mutationOperator,
            TypeReference type, TypeSystem typeSystem
            )
        {
            var newValue = new JSBinaryOperatorExpression(
                mutationOperator, expressionToMutate, JSLiteral.New(1),
                type
                );
            var assignment = new JSBinaryOperatorExpression(
                JSOperator.Assignment,
                MakeLhsForAssignment(expressionToMutate), newValue, type
                );

            assignment = ConvertReadExpressionToWriteExpression(assignment, typeSystem);

            return(assignment);
        }
Exemplo n.º 3
0
        private string GetVerb(JSBinaryOperator op)
        {
            switch (op.Token)
            {
            case "+": return("op_Addition");

            case "-": return("op_Subtraction");

            case "/": return("op_Division");

            case "*": return("op_Multiplication");

            case "%": return("op_Modulus");

            case "|": return("op_BitwiseOr");

            case "^": return("op_ExclusiveOr");

            case "&": return("op_BitwiseAnd");

            case "<<": return("op_LeftShift");

            case ">>":
            case ">>>":
                return("op_RightShift");

            case "===": return("op_Equality");

            case "!==": return("op_Inequality");

            case "<": return("op_LessThan");

            case "<=": return("op_LessThanOrEqual");

            case ">": return("op_GreaterThan");

            case ">=": return("op_GreaterThanOrEqual");

            default:
                return(null);
            }
        }
Exemplo n.º 4
0
        public void VisitNode(JSWhileLoop whileLoop)
        {
            JSVariable       initVariable = null, lastVariable = null;
            JSBinaryOperator initOperator = null;
            JSExpression     initValue    = null;

            var prevEStmt = PreviousSibling as JSExpressionStatement;
            var prevVDS   = PreviousSibling as JSVariableDeclarationStatement;

            if (prevEStmt != null)
            {
                var boe = prevEStmt.Expression as JSBinaryOperatorExpression;
                if (
                    (boe != null) &&
                    (boe.Operator is JSAssignmentOperator) &&
                    (boe.Left is JSVariable)
                    )
                {
                    initVariable = (JSVariable)boe.Left;
                    initOperator = boe.Operator;
                    initValue    = boe.Right;
                }
            }
            else if (prevVDS != null)
            {
                var decl = prevVDS.Declarations.FirstOrDefault(
                    (d) => !d.IsNull
                    );
                if (decl != null)
                {
                    initVariable = (JSVariable)decl.Left;
                    initOperator = decl.Operator;
                    initValue    = decl.Right;
                }
            }

            var lastStatement = whileLoop.Statements.LastOrDefault();

            while ((lastStatement != null) && (lastStatement.GetType() == typeof(JSBlockStatement)))
            {
                lastStatement = ((JSBlockStatement)lastStatement).Statements.LastOrDefault();
            }

            var lastExpressionStatement = lastStatement as JSExpressionStatement;

            if (lastExpressionStatement != null)
            {
                var lastUoe = lastExpressionStatement.Expression as JSUnaryOperatorExpression;
                var lastBoe = lastExpressionStatement.Expression as JSBinaryOperatorExpression;

                if ((lastUoe != null) && (lastUoe.Operator is JSUnaryMutationOperator))
                {
                    lastVariable = lastUoe.Expression as JSVariable;
                }
                else if ((lastBoe != null) && (lastBoe.Operator is JSAssignmentOperator))
                {
                    lastVariable = lastBoe.Left as JSVariable;
                    if (
                        (lastVariable != null) &&
                        !lastBoe.Right.SelfAndChildrenRecursive.Any(
                            (n) => lastVariable.Equals(n)
                            )
                        )
                    {
                        lastVariable = null;
                    }
                }
            }

            var lastIfStatement = lastStatement as JSIfStatement;

            if (
                (lastIfStatement != null) &&
                whileLoop.Condition is JSBooleanLiteral &&
                ((JSBooleanLiteral)whileLoop.Condition).Value
                )
            {
                var innerStatement = lastIfStatement.TrueClause;
                while (innerStatement is JSBlockStatement)
                {
                    var bs = (JSBlockStatement)innerStatement;
                    if (bs.Statements.Count != 1)
                    {
                        innerStatement = null;
                        break;
                    }

                    innerStatement = bs.Statements[0];
                }

                var eStmt = innerStatement as JSExpressionStatement;

                if (eStmt != null)
                {
                    var breakExpr = eStmt.Expression as JSBreakExpression;
                    if ((breakExpr != null) && (breakExpr.TargetLoop == whileLoop.Index))
                    {
                        whileLoop.ReplaceChildRecursive(lastIfStatement, new JSNullStatement());

                        var doLoop = new JSDoLoop(
                            new JSUnaryOperatorExpression(JSOperator.LogicalNot, lastIfStatement.Condition, TypeSystem.Boolean),
                            whileLoop.Statements.ToArray()
                            );
                        doLoop.Index = whileLoop.Index;

                        ParentNode.ReplaceChild(whileLoop, doLoop);
                        VisitChildren(doLoop);
                        return;
                    }
                }
            }

            bool cantBeFor = false;

            if ((initVariable != null) && (lastVariable != null) &&
                !initVariable.Equals(lastVariable)
                )
            {
                cantBeFor = true;
            }
            else if ((initVariable ?? lastVariable) == null)
            {
                cantBeFor = true;
            }
            else if (!whileLoop.Condition.SelfAndChildrenRecursive.Any(
                         (n) => (initVariable ?? lastVariable).Equals(n)
                         ))
            {
                cantBeFor = true;
            }
            else if (
                !PostSwitchTransform && (
                    (lastStatement is JSSwitchStatement) ||
                    (lastStatement is JSLabelGroupStatement)
                    )
                )
            {
                cantBeFor = true;
            }

            if (!cantBeFor)
            {
                JSStatement initializer = null, increment = null;

                if (initVariable != null)
                {
                    initializer = PreviousSibling as JSStatement;

                    ParentNode.ReplaceChild(PreviousSibling, new JSNullStatement());
                }

                if (lastVariable != null)
                {
                    increment = lastExpressionStatement;

                    whileLoop.ReplaceChildRecursive(lastExpressionStatement, new JSNullStatement());
                }

                var forLoop = new JSForLoop(
                    initializer, whileLoop.Condition, increment,
                    whileLoop.Statements.ToArray()
                    );
                forLoop.Index = whileLoop.Index;

                ParentNode.ReplaceChild(whileLoop, forLoop);
                VisitChildren(forLoop);
            }
            else
            {
                VisitChildren(whileLoop);
            }
        }