Ejemplo n.º 1
0
        protected virtual Expression TranslateInvocationExpression(E.CallExpression callExpression)
        {
            var f = new E.FunctionExpression();

            var l = new S.VariableDeclaration();

            l.Type = S.VariableDeclarationType.Let;
            l.Bindings.Add(new S.VariableBinding()
            {
                Variable = new E.IdentifierExpression("h")
            });

            f.Statements.Add(l);

            var ifb = new S.IfStatement();

            ifb.Condition = callExpression.Target;

            var inner = new S.IfStatement();

            inner.Condition = callExpression.Target.IsArray();

            inner.TruePart.Add(
                callExpression.Target.Property("forEach").Call(
                    new E.ArrowFunctionExpression(
                        new D.Parameter("e"),
                        new E.IdentifierExpression("h").AssignedBy(new E.IdentifierExpression("e").Call(callExpression.Parameters))
                        )
                    )
                );

            inner.TruePart.Add(new E.IdentifierExpression("h").ToReturn());

            inner.FalsePart.Add(callExpression.ToReturn());

            ifb.TruePart.Add(inner);
            f.Statements.Add(ifb);

            return(f.Call());
        }
Ejemplo n.º 2
0
        protected virtual IEnumerable <Syntax> TranslateVariableDeclarationStatement(VariableDeclarationStatement variableDeclarationStatement, ILTranslationContext data)
        {
            var vd = new S.VariableDeclaration();

            vd.Type = (variableDeclarationStatement.Modifiers & Modifiers.Const) == Modifiers.Const ? S.VariableDeclarationType.Const : S.VariableDeclarationType.Let;

            foreach (var v in variableDeclarationStatement.Variables)
            {
                vd.Bindings.Add(new S.VariableBinding()
                {
                    Variable = new E.IdentifierExpression()
                    {
                        Name = v.Name
                    },
                    Type        = ResolveType(v, variableDeclarationStatement.Type),
                    Initializer = GetExpression(v.Initializer, data)
                });
            }
            if (vd.HasBinding)
            {
                yield return(vd);
            }
        }
Ejemplo n.º 3
0
        protected virtual IEnumerable <Syntax> TranslateUsingStatement(UsingStatement usingStatement, ILTranslationContext data)
        {
            var b = new S.BlockStatement();

            var rvd = usingStatement.ResourceAcquisition as VariableDeclarationStatement;
            var vds = new S.VariableDeclaration();

            vds.Type = S.VariableDeclarationType.Let;
            if (rvd != null)
            {
                foreach (var v in rvd.Variables)
                {
                    vds.Bindings.Add(new S.VariableBinding()
                    {
                        Variable = new E.IdentifierExpression()
                        {
                            Name = v.Name
                        },
                        Type        = ResolveType(v, rvd.Type),
                        Initializer = GetExpression(v.Initializer, data)
                    });
                }
            }
            else
            {
                var ex = (ICSharpCode.NRefactory.CSharp.Expression)usingStatement.ResourceAcquisition;
                vds.Bindings.Add(new S.VariableBinding()
                {
                    Variable = new E.IdentifierExpression()
                    {
                        Name = "__usingResource"
                    },
                    Initializer = GetExpression(ex, data)
                });
            }

            b.Statements.Add(vds);
            var tf = new S.TryStatement();

            tf.TryBlock = GetStatements(usingStatement.EmbeddedStatement, data);

            foreach (var bd in vds.Bindings)
            {
                var ib = new S.IfStatement();
                ib.Condition = bd.Variable;

                ib.TruePart.Add(new S.ExpressionStatement()
                {
                    Expression = new E.CallExpression()
                    {
                        Target = new E.PropertyExpression()
                        {
                            Object   = bd.Variable,
                            Property = "Dispose"
                        }
                    }
                });

                tf.FinallyBlock.Add(ib);
            }

            b.Statements.Add(tf);

            yield return(b);
        }