public void LocatingTheVariable()
 {
     var inspector = new ForEachInspector("my var thing in collection.thing foo");
     Assert.AreEqual("my var", inspector.VariableType);
     Assert.AreEqual("thing", inspector.VariableName);
     Assert.AreEqual("collection.thing foo", inspector.CollectionCode);
 }
Beispiel #2
0
        protected override void Visit(ForEachChunk chunk)
        {
            var forEach = new ForEachInspector(chunk.Code);

            if (!forEach.Recognized)
            {
                _source.Write("for ").Write(chunk.Code).WriteLine(":");
                _source.Indent++;
                _variables.PushScope();
                Accept(chunk.Body);
                _source.WriteLine("pass");
                _variables.PopScope();
                _source.Indent--;
                return;
            }

            _variables.PushScope();

            var detect = new DetectCodeExpressionVisitor(OuterPartial);
            var autoIndex = detect.Add(forEach.VariableName + "Index");
            var autoCount = detect.Add(forEach.VariableName + "Count");
            var autoIsFirst = detect.Add(forEach.VariableName + "IsFirst");
            var autoIsLast = detect.Add(forEach.VariableName + "IsLast");
            detect.Accept(chunk.Body);

            if (autoIsLast.Detected)
            {
                autoIndex.Detected = true;
                autoCount.Detected = true;
            }

            if (autoIndex.Detected)
            {
                _variables.Declare(forEach.VariableName + "Index");
                _source.Write(forEach.VariableName).WriteLine("Index=0");
            }
            if (autoIsFirst.Detected)
            {
                _variables.Declare(forEach.VariableName + "IsFirst");
                _source.Write(forEach.VariableName).WriteLine("IsFirst=True");
            }
            if (autoCount.Detected)
            {
                _variables.Declare(forEach.VariableName + "Count");
                _source
                    .Write(forEach.VariableName).Write("Count=len([0 for ")
                    .Write(chunk.Code).WriteLine("])");
            }

            _variables.Declare(forEach.VariableName);
            _source.Write("for ").Write(chunk.Code).WriteLine(":");
            _source.Indent++;
            if (autoIsLast.Detected)
            {
                _variables.Declare(forEach.VariableName + "IsLast");
                _source
                    .Write(forEach.VariableName).Write("IsLast=(")
                    .Write(forEach.VariableName).Write("Index==")
                    .Write(forEach.VariableName).WriteLine("Count - 1)");
            }
            Accept(chunk.Body);
            if (autoIndex.Detected)
            {
                _source
                    .Write(forEach.VariableName).Write("Index=")
                    .Write(forEach.VariableName).WriteLine("Index+1");
            }
            if (autoIsFirst.Detected)
            {
                _source.Write(forEach.VariableName).WriteLine("IsFirst=False");
            }
            _source.WriteLine("pass");
            _source.Indent--;

            _variables.PopScope();
        }