예제 #1
0
        public void VisitNode(JSDoLoop dl)
        {
            SynthesizeLoopLabel(dl);

            Formatter.ConditionalNewLine();
            Formatter.NewLine();
            Comment("do {{");
            EmitLabelledBlock(
                IndexToLabel(dl.Index),
                (__) =>
                __.WriteSExpr(
                    "loop",
                    (_) => {
                EmitBlockInterior(dl.Statements);

                _.ConditionalNewLine();
                _.NewLine();
                Comment("do {{ ... }} while ({0})", dl.Condition);
                var synthetic = new JSIfStatement(
                    dl.Condition,
                    new JSNullStatement(),
                    new JSExpressionStatement(new JSBreakExpression {
                    TargetLoop = dl.Index
                })
                    );
                Visit(synthetic);
            },
                    true, true
                    )
                );
        }
예제 #2
0
 public void VisitNode(JSIfStatement ifs)
 {
     Formatter.NewLine();
     Formatter.WriteSExpr(
         "if", (_) =>
         EmitArgumentList(_, new JSNode[] { ifs.Condition, ifs.TrueClause, ifs.FalseClause }, true),
         lineBreakInside: true,
         lineBreakAfter: true
         );
 }
예제 #3
0
        public void VisitNode(JSIfStatement ifs)
        {
            Output.WriteRaw("if");
            Output.Space();

            Output.LPar();
            Visit(ifs.Condition);
            Output.RPar();
            Output.Space();

            Output.OpenBrace();
            Visit(ifs.TrueClause);

            JSStatement falseClause = ifs.FalseClause;

            while (falseClause != null)
            {
                var nestedBlock = falseClause as JSBlockStatement;
                var nestedIf    = falseClause as JSIfStatement;
                if ((nestedBlock != null) && (nestedBlock.Statements.Count == 1))
                {
                    nestedIf = nestedBlock.Statements[0] as JSIfStatement;
                }

                if (nestedIf != null)
                {
                    Output.CloseAndReopenBrace((o) => {
                        if (o != this.Output)
                        {
                            throw new InvalidOperationException("Output mismatch");
                        }

                        o.WriteRaw("else if");
                        o.Space();
                        o.LPar();
                        Visit(nestedIf.Condition);
                        o.RPar();
                    });

                    Visit(nestedIf.TrueClause);

                    falseClause = nestedIf.FalseClause;
                }
                else
                {
                    Output.CloseAndReopenBrace("else");
                    Visit(falseClause);
                    falseClause = null;
                }
            }

            Output.CloseBrace();
        }
예제 #4
0
        public void VisitNode(JSForLoop fl)
        {
            SynthesizeLoopLabel(fl);

            if (fl.Initializer != null)
            {
                Formatter.ConditionalNewLine();
                Formatter.NewLine();
                Comment("for ({0}; ...)", fl.Initializer);
                Visit(fl.Initializer);
            }

            Formatter.ConditionalNewLine();
            Formatter.NewLine();
            EmitLabelledBlock(
                IndexToLabel(fl.Index),
                (__) =>
                __.WriteSExpr(
                    "loop",
                    (_) => {
                Comment("for (...; {0}; ...)", fl.Condition);
                var synthetic = new JSIfStatement(
                    fl.Condition,
                    new JSNullStatement(),
                    new JSExpressionStatement(new JSBreakExpression {
                    TargetLoop = fl.Index
                })
                    );
                Visit(synthetic);

                _.ConditionalNewLine();
                _.NewLine();
                Comment("for (...) {{ ");
                EmitBlockInterior(fl.Statements);

                if (fl.Increment != null)
                {
                    _.ConditionalNewLine();
                    _.NewLine();
                    Comment("for (...; ...; {0})", fl.Increment);
                    Visit(fl.Increment);
                }
            },
                    true, true
                    )
                );
        }
예제 #5
0
 public void VisitNode(JSIfStatement node)
 {
     VisitChildren(node);
 }
예제 #6
0
 public void VisitNode(JSIfStatement ifs)
 {
     VisitChildren(ifs);
 }
예제 #7
0
        public void VisitNode(JSIfStatement ifs)
        {
            var  boe                  = ifs.Condition as JSBinaryOperatorExpression;
            var  uoe                  = ifs.Condition as JSUnaryOperatorExpression;
            var  invocation           = ifs.Condition as JSInvocationExpression;
            bool invocationIsInverted = false;

            if ((boe != null) && (boe.Operator == JSOperator.Equal))
            {
                var leftVar     = boe.Left as JSVariable;
                var leftIgnored = boe.Left as JSIgnoredMemberReference;
                var rightNull   = (JSLiteral)(boe.Right as JSDefaultValueLiteral) ?? (JSLiteral)(boe.Right as JSNullLiteral);

                if (rightNull != null)
                {
                    if (leftVar != null)
                    {
                        NullChecks[leftVar] = new NullCheck {
                            Statement      = ifs,
                            SwitchVariable = leftVar,
                            Goto           = ifs.AllChildrenRecursive.OfType <JSGotoExpression>().FirstOrDefault()
                        };
                    }
                    else if (leftIgnored != null)
                    {
                        var leftField = leftIgnored.Member as FieldInfo;

                        if (leftField != null)
                        {
                            var initializer = Initializers[leftField] = new Initializer {
                                Field     = leftField,
                                Statement = ifs,
                            };

                            foreach (var _invocation in ifs.TrueClause.AllChildrenRecursive.OfType <JSInvocationExpression>())
                            {
                                if (_invocation.JSMethod == null)
                                {
                                    continue;
                                }
                                if (_invocation.JSMethod.Identifier != "Add")
                                {
                                    continue;
                                }
                                if (_invocation.Arguments.Count != 2)
                                {
                                    continue;
                                }

                                var value = _invocation.Arguments[0];
                                var index = _invocation.Arguments[1] as JSIntegerLiteral;
                                if (index == null)
                                {
                                    continue;
                                }

                                initializer.Values[(int)index.Value] = value;
                            }

                            foreach (var iae in ifs.TrueClause.AllChildrenRecursive.OfType <JSInitializerApplicationExpression>())
                            {
                                var targetNew = iae.Target as JSNewExpression;
                                if (targetNew == null)
                                {
                                    continue;
                                }

                                var targetNewJSType = targetNew.Type as JSType;
                                if (targetNewJSType == null)
                                {
                                    continue;
                                }

                                var targetNewType = targetNewJSType.Type as GenericInstanceType;
                                if (targetNewType == null)
                                {
                                    continue;
                                }
                                if (!targetNewType.Name.Contains("Dictionary"))
                                {
                                    continue;
                                }
                                if (targetNewType.GenericArguments.Count != 2)
                                {
                                    continue;
                                }
                                if (targetNewType.GenericArguments[1].MetadataType != MetadataType.Int32)
                                {
                                    continue;
                                }

                                var initArray = iae.Initializer as JSArrayExpression;
                                if (initArray == null)
                                {
                                    continue;
                                }

                                foreach (var item in initArray.Values)
                                {
                                    var itemArray = item as JSArrayExpression;
                                    if (itemArray == null)
                                    {
                                        continue;
                                    }

                                    var value = itemArray.Values.First();
                                    var index = itemArray.Values.Skip(1).First() as JSIntegerLiteral;
                                    if (index == null)
                                    {
                                        continue;
                                    }

                                    initializer.Values[(int)index.Value] = value;
                                }
                            }
                        }
                    }
                }
            }
            else if ((uoe != null) && (uoe.Operator == JSOperator.LogicalNot))
            {
                invocation           = uoe.Expression as JSInvocationExpression;
                invocationIsInverted = true;
            }

            if (
                (invocation != null) &&
                (invocation.Arguments.Count == 2) &&
                (invocation.JSMethod != null) &&
                (invocation.JSMethod.Identifier == "TryGetValue")
                )
            {
                var thisIgnored = invocation.ThisReference as JSIgnoredMemberReference;
                var switchVar   = invocation.Arguments[0] as JSVariable;
                var outRef      = invocation.Arguments[1] as JSPassByReferenceExpression;

                if ((thisIgnored != null) && (switchVar != null) && (outRef != null))
                {
                    var thisField   = thisIgnored.Member as FieldInfo;
                    var outReferent = outRef.Referent as JSReferenceExpression;

                    if ((thisField != null) && (outReferent != null))
                    {
                        var outVar = outReferent.Referent as JSVariable;

                        if (outVar != null)
                        {
                            IndexLookups[outVar] = new IndexLookup {
                                OutputVariable = outVar,
                                SwitchVariable = switchVar,
                                Field          = thisField,
                                Statement      = ifs,
                                Goto           = ifs.TrueClause.AllChildrenRecursive.OfType <JSGotoExpression>().FirstOrDefault(),
                                IsInverted     = invocationIsInverted
                            };

                            if (!invocationIsInverted)
                            {
                                var replacement = ifs.TrueClause;
                                ParentNode.ReplaceChild(ifs, replacement);
                                VisitReplacement(replacement);
                                return;
                            }
                        }
                    }
                }
            }

            VisitChildren(ifs);
        }