public static CodeCompileUnit TestElseIfCodeCompileUnit()
        {
            CodeCompileUnit     compileUnit      = new CodeCompileUnit();
            CodeNamespace       codeNamespace    = new CodeNamespace("Test.Namespace");
            CodeTypeDeclaration classDeclaration = new CodeTypeDeclaration("TestClass")
            {
                IsClass = true
            };

            classDeclaration.TypeAttributes =
                (classDeclaration.TypeAttributes & ~TypeAttributes.VisibilityMask) | TypeAttributes.Public;

            var method = new CodeMemberMethod()
            {
                Name       = "Method",
                Attributes = MemberAttributes.Public | MemberAttributes.Final
            };

            method.Statements.Add(
                ElseIf.New(
                    new Tuple <CodeExpression, IEnumerable <CodeStatement> >(
                        Primitives.Bool(true), new CodeStatement[] { new CodeMethodReturnStatement() })));
            method.Statements.Add(
                ElseIf.New(
                    new CodeStatement[] { new CodeMethodReturnStatement() },
                    new Tuple <CodeExpression, IEnumerable <CodeStatement> >(
                        Primitives.Int(1), new CodeStatement[] { new CodeMethodReturnStatement() }),
                    new Tuple <CodeExpression, IEnumerable <CodeStatement> >(
                        Primitives.Int(2), new CodeStatement[] { new CodeMethodReturnStatement() }),
                    new Tuple <CodeExpression, IEnumerable <CodeStatement> >(
                        Primitives.Int(3), new CodeStatement[] { new CodeMethodReturnStatement() })));

            compileUnit.Namespaces.Add(codeNamespace);
            codeNamespace.Types.Add(classDeclaration);
            classDeclaration.Members.Add(method);
            return(compileUnit);
        }
예제 #2
0
        public static CodeCompileUnit TestExpressionsAndStatementsCompileUnit(bool generateIncrement = true)
        {
            CodeCompileUnit     compileUnit      = new CodeCompileUnit();
            CodeNamespace       codeNamespace    = new CodeNamespace("Test.Namespace");
            CodeTypeDeclaration classDeclaration = new CodeTypeDeclaration("TestClass")
            {
                IsClass = true
            };

            classDeclaration.TypeAttributes =
                (classDeclaration.TypeAttributes & ~TypeAttributes.VisibilityMask) | TypeAttributes.Public;

            var method = new CodeMemberMethod()
            {
                Name       = "Method",
                Attributes = MemberAttributes.Public | MemberAttributes.Final
            };

            method.Statements.Add(new CodeConditionalOperatorExpression(
                                      new CodeBinaryOperatorExpression(new CodeVariableReferenceExpression("i"), CodeBinaryOperatorType.GreaterThan, Primitives.Int(0)),
                                      new CodeVariableReferenceExpression("i"),
                                      Primitives.Null));
            method.Statements.Add(new CodePostTestIterationStatement(
                                      new CodeUnaryOperatorExpression(Primitives.Bool(true), CodeUnaryOperatorType.BooleanNot),
                                      new CodeOperationAssignmentStatement(
                                          new CodeVariableReferenceExpression("a"),
                                          CodeBinaryOperatorTypeMore.LeftBitShift,
                                          Primitives.Int(2))));
            method.Statements.Add(new CodeAssignStatement(
                                      new CodeVariableReferenceExpression("lambda"),
                                      new CodeLambdaDeclarationExpression(
                                          new CodeLambdaParameterDeclarationExpression[]
            {
                new CodeLambdaParameterDeclarationExpression(Types.Int, "i"),
                new CodeLambdaParameterDeclarationExpression(Types.Int, "j")
            },
                                          new CodeExpressionStatement(new CodeBinaryOperatorExpressionMore(
                                                                          new CodeVariableReferenceExpression("b"),
                                                                          CodeBinaryOperatorTypeMore.NullCoalescing,
                                                                          Primitives.Int(0))),
                                          new CodeExpressionStatement(new CodeObjectCreateExpressionExt(
                                                                          new CodeTypeReference("A"),
                                                                          new CodeExpression[0],
                                                                          new CodePropertyInitializerExpression("Prop", Primitives.Int(0)),
                                                                          new CodePropertyInitializerExpression("Prop2", Primitives.Int(2)))))));
            var iteration = new CodeIterationStatement(null, Primitives.Bool(false), null);

            if (generateIncrement)
            {
                iteration.Statements.Add(new CodeExpressionStatement(new CodeUnaryOperatorExpression(
                                                                         new CodeVariableReferenceExpression("i"),
                                                                         CodeUnaryOperatorType.PostIncrement)));
            }
            else
            {
                iteration.Statements.Add(new CodeOperationAssignmentStatement(
                                             new CodeVariableReferenceExpression("i"), CodeBinaryOperatorTypeMore.Add, Primitives.Int(1)));
            }

            iteration.Statements.Add(new CodeBreakStatement());
            method.Statements.Add(iteration);
            method.Statements.Add(new CodeUsingStatement("stream", new CodeObjectCreateExpression("Stream"),
                                                         new CodeVariableDeclarationStatement("", "matrix",
                                                                                              new CodeMultidimensionalArrayCreateExpression(
                                                                                                  Types.Int,
                                                                                                  2,
                                                                                                  new CodeArrayInitializerExpression(
                                                                                                      new CodeArrayInitializerExpression(Primitives.Int(1), Primitives.Int(1)),
                                                                                                      new CodeArrayInitializerExpression(Primitives.Int(1), Primitives.Int(1)))))));
            method.Statements.Add(new CodeForEachStatement(
                                      Types.Int,
                                      "i",
                                      new CodeVariableReferenceExpression("collection"),
                                      new CodeExpressionStatement(new CodeMultidimensionalArrayCreateExpression(
                                                                      Types.Int,
                                                                      new CodeExpression[] { Primitives.Int(1), Primitives.Int(1) },
                                                                      new CodeArrayInitializerExpression(new CodeArrayInitializerExpression(Primitives.Int(1))))),
                                      new CodeExpressionStatement(new CodeMultidimensionalArrayCreateExpression(
                                                                      Types.Int,
                                                                      Primitives.Int(1), Primitives.Int(1)))));

            compileUnit.Namespaces.Add(codeNamespace);
            codeNamespace.Types.Add(classDeclaration);
            classDeclaration.Members.Add(method);

            return(compileUnit);
        }