Example #1
0
        public void ReduceChildrenCascades()
        {
            ParameterExpression intVar = Expression.Variable(typeof(int));
            List <int>          list   = new List <int>();
            var foreachExp             = new ForEachExpression(
                intVar,
                Expression.Constant(Enumerable.Range(5, 4)),
                Expression.Call(
                    Expression.Constant(list),
                    typeof(List <int>).GetMethod(nameof(List <int> .Insert)),
                    Expression.Constant(0),
                    intVar
                    )
                );

            // Check that not only has the visitor reduced the foreach into a block
            // but the using within that block into a try…finally.
            Expression reduced    = new DefaultVisitor().Visit(foreachExp);
            var        block      = (BlockExpression)reduced;
            var        tryExp     = (TryExpression)block.Expressions[1];
            var        loop       = (LoopExpression)tryExp.Body;
            var        innerBlock = (BlockExpression)loop.Body;
            var        call       = (MethodCallExpression)innerBlock.Expressions.Last();
            var        instance   = (ConstantExpression)call.Object;

            Assert.Same(list, instance.Value);
        }
Example #2
0
        public void MutateRecursivelyThrowsNullReferenceException696()
        {
            IndexTrackingMutator   indexTrackingMutator;
            ForEachExpression      forEachExpression;
            PatternExpression      patternExpression;
            List <AphidExpression> list;
            List <AphidExpression> list1;

            indexTrackingMutator = new IndexTrackingMutator((string[])null);
            AphidExpressionContext s0 = new AphidExpressionContext();

            s0.Filename = (string)null;
            s0.Code     = (string)null;
            List <AphidExpression> list2 = new List <AphidExpression>();

            forEachExpression =
                new ForEachExpression(s0, (AphidExpression)null, list2, (AphidExpression)null);
            ((AphidExpression)forEachExpression).Index  = 0;
            ((AphidExpression)forEachExpression).Length = 0;
            AphidExpressionContext s1 = new AphidExpressionContext();

            s1.Filename       = (string)null;
            s1.Code           = (string)null;
            patternExpression = new PatternExpression
                                    (s1, (AphidExpression)forEachExpression, (List <AphidExpression>)null);
            ((AphidExpression)patternExpression).Index  = 0;
            ((AphidExpression)patternExpression).Length = 0;
            AphidExpression[] aphidExpressions = new AphidExpression[1];
            aphidExpressions[0] = (AphidExpression)patternExpression;
            list  = new List <AphidExpression>((IEnumerable <AphidExpression>)aphidExpressions);
            list1 = this.MutateRecursively((AphidMutator)indexTrackingMutator, list);
        }
Example #3
0
        ForEachExpression ILexicalScope <ForEachExpression, Action <MemberExpression> > .Build(Action <MemberExpression> scope)
        {
            var result = new ForEachExpression(collection, cancellationToken, configureAwait, ContinueLabel, BreakLabel);

            scope(result.Element);
            result.Body = Build();
            return(result);
        }
Example #4
0
        protected override Expression VisitForEachExpression(ForEachExpression expression)
        {
            this.Write("foreach (var ");
            this.Visit(expression.VariableExpression);
            this.Write(" in ");
            this.Visit(expression.Target);
            this.Write(")");
            this.Visit(expression.Body);

            return(expression);
        }
Example #5
0
 protected override void EmitForEachExpression(ForEachExpression expression, bool isStatement = false)
 {
     Append("for ");
     Emit(expression.Element);
     Append(" in ");
     Emit(expression.Collection);
     Append(":\r\n");
     Indent();
     Emit(expression.Body);
     Unindent();
 }
Example #6
0
 public object Visit(ForEachExpression node)
 {
     if (node.Variable != null)
     {
         Visit(node.Variable);
     }
     if (node.Body != null)
     {
         Visit(node.Body);
     }
     return(null);
 }
Example #7
0
 protected override void EmitForEachExpression(ForEachExpression expression, bool isStatement = false)
 {
     Append("foreach (");
     Emit(expression.Collection);
     Append(" as ");
     Emit(expression.Element);
     Append("){\r\n");
     Indent();
     Emit(expression.Body);
     Unindent();
     Append(GetTabs() + "}\r\n");
 }
        protected virtual Expression VisitForEachExpression(ForEachExpression expression)
        {
            var parameter = (ParameterExpression)this.Visit(expression.VariableExpression);
            var target    = this.Visit(expression.Target);
            var body      = this.Visit(expression.Body);

            if (parameter != expression.VariableExpression || target != expression.Target || body != expression.Body)
            {
                return(new ForEachExpression(parameter, target, body));
            }

            return(expression);
        }
Example #9
0
        public void ShouldIterateEnumerable()
        {
            ParameterExpression output = Expression.Parameter(typeof(List <string>), "output");
            ParameterExpression item   = Expression.Variable(typeof(string), "item");
            ConstantExpression  items  = Expression.Constant(Enumerable.Repeat("hello world", 10));

            List <string> addedItems = new List <string>();

            ForEachExpression loop = X.ForEach(item, items,
                                               Expression.Call(output, nameof(List <string> .Add), null, item)
                                               );

            Expression
            .Lambda <Action <List <string> > >(loop, output)
            .Compile()(addedItems);

            addedItems.Count.ShouldBe(10);
            addedItems[0].ShouldBe("hello world");
        }
Example #10
0
        public ForEachExpression Visit(ForEachExpression @foreach)
        {
            codeWriter.Write("foreach (", currentIndent);
            insideExpressionCount++;
            Visit(@foreach.Variable);
            codeWriter.Write(" in ");
            Visit(@foreach.Collection);
            insideExpressionCount--;
            codeWriter.Write(") {");
            codeWriter.NewLine();

            currentIndent++;
            Visit(@foreach.Body);
            currentIndent--;

            codeWriter.Write("}", currentIndent);
            codeWriter.NewLine();
            return(null);
        }
Example #11
0
        public string Visit(ForEachExpression @foreach)
        {
            var codeWriter = new XzaarCodeWriter();

            codeWriter.Write("for (", currentIndent);
            insideExpressionCount++;
            codeWriter.Write(Visit(@foreach.Variable));
            codeWriter.Write(" of ");
            codeWriter.Write(Visit(@foreach.Collection));
            insideExpressionCount--;
            codeWriter.Write(") {");
            codeWriter.NewLine();

            currentIndent++;
            codeWriter.Write(Visit(@foreach.Body));
            currentIndent--;

            codeWriter.Write("}", currentIndent);
            codeWriter.NewLine();
            return(codeWriter.ToString());
        }
        private AphidObject InterpretForEachExpression(ForEachExpression expression)
        {
            var collection = InterpretExpression(expression.Collection) as AphidObject;
            var elements   = collection.Value as List <AphidObject>;
            var elementId  = (expression.Element as IdentifierExpression).Identifier;

            foreach (var element in elements)
            {
                EnterChildScope();
                _currentScope.Variables.Add(elementId, element);
                Interpret(expression.Body, false);

                if (LeaveChildScope(true) || _isBreaking)
                {
                    _isBreaking = false;
                    break;
                }
            }

            return(null);
        }
Example #13
0
        public void ReduceChildrenCascades()
        {
            var intVar = Expression.Variable(typeof(int));
            List<int> list = new List<int>();
            var foreachExp = new ForEachExpression(
                intVar,
                Expression.Constant(Enumerable.Range(5, 4)),
                Expression.Call(
                    Expression.Constant(list),
                    typeof(List<int>).GetMethod(nameof(List<int>.Insert)),
                    Expression.Constant(0),
                    intVar
                    )
                );

            // Check that not only has the visitor reduced the foreach into a block
            // but the using within that block into a try…finally.
            var reduced = new DefaultVisitor().Visit(foreachExp);
            var block = (BlockExpression)reduced;
            var tryExp = (TryExpression)block.Expressions[1];
            var loop = (LoopExpression)tryExp.Body;
            var innerBlock = (BlockExpression)loop.Body;
            var call = (MethodCallExpression)innerBlock.Expressions.Last();
            var instance = (ConstantExpression)call.Object;
            Assert.Same(list, instance.Value);
        }
Example #14
0
 [DebuggerStepThrough] protected virtual void EmitForEachExpression(ForEachExpression expression, bool isStatement = false)
 {
     throw new NotImplementedException();
 }
 public object Visit(ForEachExpression node)
 {
     return(null);
 }
 public object Visit(ForEachExpression @foreach)
 {
     throw new System.NotImplementedException();
 }