示例#1
0
 protected override void Visit(ConditionalChunk chunk)
 {
     switch (chunk.Type)
     {
         case ConditionalType.If:
             _source.Write("if ").Write(chunk.Condition).WriteLine(":");
             break;
         case ConditionalType.ElseIf:
             _source.Write("elif ").Write(chunk.Condition).WriteLine(":");
             break;
         case ConditionalType.Else:
             _source.Write("else ").Write(chunk.Condition).WriteLine(":");
             break;
         case ConditionalType.Once:
             _source.Write("if Once(").Write(chunk.Condition).WriteLine("):");
             break;
         case ConditionalType.Unless:
             _source.Write("if not ").Write(chunk.Condition).WriteLine(":");
             break;
         default:
             throw new CompilerException(string.Format("Unknown ConditionalChunk type {0}", chunk.Type));
     }
     _source.Indent++;
     _variables.PushScope();
     Accept(chunk.Body);
     _variables.PopScope();
     _source.WriteLine("pass");
     _source.Indent--;
 }
示例#2
0
 public void ConditionalChunkUnlessNegates()
 {
     var condition1 = new ConditionalChunk { Condition = "x != 12", Type = ConditionalType.Unless };
     condition1.Body.Add(new SendLiteralChunk { Text = "ok1" });
     var condition2 = new ConditionalChunk { Condition = "x == 12", Type = ConditionalType.Unless };
     condition2.Body.Add(new SendLiteralChunk { Text = "fail" });
     var chunks = Chunks(
         new LocalVariableChunk { Name = "x", Value = "12" },
         new SendLiteralChunk { Text = "a" },
         condition1,
         condition2,
         new SendLiteralChunk { Text = "b" });
     _compiler.CompileView(chunks, chunks);
     var contents = ExecuteView();
     Assert.AreEqual("aok1b", contents);
 }
示例#3
0
        public void ChainingElseIfNestsProperly()
        {
            var condition1 = new ConditionalChunk { Type = ConditionalType.If, Condition = "x == 1" };
            condition1.Body.Add(new SendLiteralChunk { Text = "a" });
            var condition2 = new ConditionalChunk { Type = ConditionalType.ElseIf, Condition = "x == 2" };
            condition2.Body.Add(new SendLiteralChunk { Text = "b" });
            var condition3 = new ConditionalChunk { Type = ConditionalType.ElseIf, Condition = "x == 3" };
            condition3.Body.Add(new SendLiteralChunk { Text = "c" });
            var condition4 = new ConditionalChunk { Type = ConditionalType.Else };
            condition4.Body.Add(new SendLiteralChunk { Text = "d" });

            var loop = new ForEachChunk { Code = "x in numbers" };
            loop.Body.Add(condition1);
            loop.Body.Add(condition2);
            loop.Body.Add(condition3);
            loop.Body.Add(condition4);

            var chunks = Chunks(
                loop);
            _compiler.CompileView(chunks, chunks);
            var contents = ExecuteView(new StubViewData { { "numbers", new[] { 0, 1, 2, 3, 4, 7, 2, -4 } } });
            Assert.AreEqual("dabcddbd", contents);
        }
示例#4
0
        public void ElseBlockFollowsIf()
        {
            var condition1 = new ConditionalChunk { Condition = "x != 12" };
            condition1.Body.Add(new SendLiteralChunk { Text = "fail" });
            var else1 = new ConditionalChunk { Type = ConditionalType.Else };
            else1.Body.Add(new SendLiteralChunk { Text = "ok1" });

            var condition2 = new ConditionalChunk { Condition = "x == 12" };
            condition2.Body.Add(new SendLiteralChunk { Text = "ok2" });
            var else2 = new ConditionalChunk { Type = ConditionalType.Else };
            else2.Body.Add(new SendLiteralChunk { Text = "fail" });

            var chunks = Chunks(
                new LocalVariableChunk { Name = "x", Value = "12" },
                new SendLiteralChunk { Text = "a" },
                condition1,
                else1,
                condition2,
                else2,
                new SendLiteralChunk { Text = "b" });
            _compiler.CompileView(chunks, chunks);
            var contents = ExecuteView();
            Assert.AreEqual("aok1ok2b", contents);
        }