Пример #1
0
        protected virtual Expression TranslateAssignmentExpression(E.AssignmentExpression expression)
        {
            if (expression.CompoundOperator == E.BinaryOperator.Add)
            {
                var array = expression.Target.LogicalOrWith(expression.Target.AssignedBy(new E.ArrayExpression()));

                return(new E.ConditionalExpression()
                {
                    Condition = expression.Value,
                    TruePart = expression.Target.AssignedBy(array.Property("concat").Call(expression.Target)),
                    FalsePart = ExpressionBuilder.Undefined()
                });
            }
            else
            {
                var f   = new E.FunctionExpression();
                var ifb = new S.IfStatement();
                ifb.Condition = expression.Target.LogicalAndWith(expression.Value);

                var inner = new S.IfStatement();
                inner.Condition = expression.Value.IsArray();

                inner.TruePart.Add(
                    expression.Target.AssignedBy(expression.Target.Property("filter").Call(
                                                     new E.ArrowFunctionExpression(
                                                         new D.Parameter("e"),
                                                         expression.Value.Property("indexOf").Call(new E.IdentifierExpression("e")).IsLessThan(new E.NumberExpression(0)))
                                                     )
                                                 )
                    );

                inner.FalsePart.Add(
                    expression.Target.Property("filter").Call(
                        new E.ArrowFunctionExpression(
                            new D.Parameter("e"),
                            new E.IdentifierExpression("e").IsStrictNotEqualTo(expression.Value)
                            )
                        )
                    );

                ifb.TruePart.Add(inner);

                f.Statements.Add(ifb);

                return(f.Call());
            }
        }
Пример #2
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());
        }