Exemple #1
0
        // Emits the codedom statement for an if conditional block.
        public static CodeStatement Emit(IfBlock ifBlock)
        {
            // Create the codedom if statement.
            var i = new CodeConditionStatement();

            // Emit the conditional statements for the if block.
            i.Condition = CodeDomEmitter.EmitCodeExpression(ifBlock.Conditional.ChildExpressions[0]);

            // Emit the lists of statements for the true and false bodies of the if block.

            // Comments need to be added in case the bodies are empty: these two properties
            // of the CodeConditionStatement can't be null.
            i.FalseStatements.Add(new CodeCommentStatement("If condition is false, execute these statements."));
            i.TrueStatements.Add(new CodeCommentStatement("If condition is true, execute these statements."));

            // Emit the statements for the true block
            foreach (var e in ifBlock.TrueBlock.ChildExpressions)
            {
                i.TrueStatements.Add(CodeDomEmitter.EmitCodeStatement(e));
            }

            // Emit the statements for the false block.
            foreach (var e in ifBlock.FalseBlock.ChildExpressions)
            {
                i.FalseStatements.Add(CodeDomEmitter.EmitCodeStatement(e));
            }

            return(i);
        }
Exemple #2
0
        // Emit a codedome expression representing a while loop.
        public static CodeStatement Emit(WhileLoop loop)
        {
            // A while loop is a for loop with no initializer or incrementor, only a condition.
            var i = new CodeIterationStatement(new CodeSnippetStatement(""),
                                               CodeDomEmitter.EmitCodeExpression(loop.Condition.ChildExpressions[0]),
                                               new CodeSnippetStatement(""));

            // Emit the statements found in the body of the while loop.
            foreach (var e in loop.ChildExpressions)
            {
                i.Statements.Add(CodeDomEmitter.EmitCodeStatement(e));
            }

            return(i);
        }
Exemple #3
0
        // Generate a series of codedom if statements representing a switch block.
        // (Codedom doesn't support switch)
        public static CodeStatement Emit(SwitchBlock switchBlock)
        {
            // Create the expression for whatever we're switching on.
            var codeVar = CodeDomEmitter.EmitCodeExpression(switchBlock.Variable.ChildExpressions[0]);

            // Store the first and previous if statements here, since it's needed on future loops.
            CodeConditionStatement codeIf = null;
            CodeConditionStatement lastIf = null;

            foreach (var e in switchBlock.ChildExpressions)
            {
                // Get the value being compared to: the right operand.
                var right = CodeDomEmitter.EmitCodeExpression((e as CaseBlock).Variable.ChildExpressions[0]);

                // Create the next if statement.
                var nestedIf = new CodeConditionStatement();

                // Each case block will compare codeVar (left) to the right operand.
                nestedIf.Condition = new CodeBinaryOperatorExpression(codeVar, CodeBinaryOperatorType.ValueEquality, right);
                nestedIf.TrueStatements.Add(new CodeCommentStatement("If condition is true, execute these statements."));
                foreach (var s in e.ChildExpressions)
                {
                    nestedIf.TrueStatements.Add(CodeDomEmitter.EmitCodeStatement(s));
                }
                nestedIf.FalseStatements.Add(new CodeCommentStatement("If condition is false, execute these statements."));
                // if codeIf is null, this is the first if statement.
                if (codeIf == null)
                {
                    codeIf = nestedIf;
                }
                else // It's not the first if statement, so attach it to the previous one.
                {
                    lastIf.FalseStatements.Add(nestedIf);
                }


                // Store the last if block for the next iteration.
                lastIf = nestedIf;
            }

            return(codeIf);
        }
        // Generate a codedom exception handler statement
        public static CodeStatement Emit(ExceptionHandler ex)
        {
            // Create the codedom exception handler statement
            var codeTry = new CodeTryCatchFinallyStatement();

            // Add the statements in the try block
            foreach (var e in ex.Try.ChildExpressions)
            {
                codeTry.TryStatements.Add(CodeDomEmitter.EmitCodeStatement(e));
            }

            // Add all the catch clauses.
            foreach (var c in ex.CatchClauses)
            {
                // Create the codedom catch statement.
                var catchClause = new CodeCatchClause();
                // To do: replace with non-test code
                catchClause.CatchExceptionType = new CodeTypeReference("System.Exception");
                catchClause.LocalName          = "ex";
                codeTry.CatchClauses.Add(catchClause);

                // Add the statemnts in the catch block
                foreach (var e in c.ChildExpressions)
                {
                    catchClause.Statements.Add(CodeDomEmitter.EmitCodeStatement(e));
                }
            }

            // Add the statements in the finally block.
            foreach (var e in ex.Finally.ChildExpressions)
            {
                codeTry.FinallyStatements.Add(CodeDomEmitter.EmitCodeStatement(e));
            }

            return(codeTry);
        }
Exemple #5
0
        // Generate a codedom method declaration and attach it to the given codedom type.
        public static void Emit(CodeTypeDeclaration codeTypeDeclaration, MethodDeclaration methodDeclaration)
        {
            // Create the codedom member method and attach it to the codedom type.
            var codeMemberMethod = new CodeMemberMethod();

            codeTypeDeclaration.Members.Add(codeMemberMethod);

            // Assign the method name
            codeMemberMethod.Name = methodDeclaration.Name;

            // Assign the return type: make sure to check for null.
            if (methodDeclaration.ReturnTypeName == "void")
            {
                codeMemberMethod.ReturnType = null;
            }
            else
            {
                codeMemberMethod.ReturnType = new CodeTypeReference(methodDeclaration.ReturnTypeName);
            }

            // Translate the method parameters.
            foreach (Expression p in methodDeclaration.Parameters)
            {
                if (p is SimpleParameter) // For example "int i"
                {
                    codeMemberMethod.Parameters.Add(new CodeParameterDeclarationExpression((p as SimpleParameter).TypeName, (p as SimpleParameter).Name));
                }
                if (p is DirectionedParameter) // For example "ref int i"
                {
                    var codeParameter = new CodeParameterDeclarationExpression((p as DirectionedParameter).TypeName, (p as DirectionedParameter).Name);
                    switch ((p as DirectionedParameter).Direction)
                    {
                    case ParameterDirection.Out:
                        codeParameter.Direction = FieldDirection.Out;
                        break;

                    case ParameterDirection.Ref:
                        codeParameter.Direction = FieldDirection.Ref;
                        break;
                    }
                    codeMemberMethod.Parameters.Add(codeParameter);
                }
            }

            // Translate the method's accessibility
            MemberAttributes memberAttributes = MemberAttributes.Public;

            switch (methodDeclaration.Accessibility)
            {
            case Accessibility.Internal:
                memberAttributes = MemberAttributes.FamilyAndAssembly;
                break;

            case Accessibility.Private:
                memberAttributes = MemberAttributes.Private;
                break;

            case Accessibility.Protected:
                memberAttributes = MemberAttributes.Family;
                break;

            case Accessibility.Public:
                memberAttributes = MemberAttributes.Public;
                break;
            }

            // Shared = static
            if (methodDeclaration.IsShared)
            {
                memberAttributes |= MemberAttributes.Static;
            }
            if (methodDeclaration.IsAbstract)
            {
                memberAttributes |= MemberAttributes.Abstract;
            }
            if (!methodDeclaration.IsVirtual && !methodDeclaration.IsAbstract)
            {
                if (methodDeclaration.IsOverride)
                {
                    memberAttributes |= MemberAttributes.Override;
                }
                else
                {
                    memberAttributes |= MemberAttributes.Final;
                }
            }

            codeMemberMethod.Attributes = memberAttributes;

            // Add the statements found in the method body.
            foreach (var e in methodDeclaration.ChildExpressions)
            {
                codeMemberMethod.Statements.Add(CodeDomEmitter.EmitCodeStatement(e));
            }
        }
Exemple #6
0
        // Emit a codedome expression representing a for loop.
        public static CodeStatement Emit(ForLoop loop)
        {
            // The expression describing how the for loop starts.
            CodeExpression startEx = null;
            // The expression describing how the for loop ends.
            CodeExpression endEx = null;

            // The for loop has both a start and end defined. For example: for int i in 0 to 10
            if (loop.Condition.ChildExpressions.Count > 1)
            {
                startEx = CodeDomEmitter.EmitCodeExpression(loop.Condition.ChildExpressions[0]);
                endEx   = CodeDomEmitter.EmitCodeExpression(loop.Condition.ChildExpressions[1]);
            }
            else // The for loop has only the end defined. For examle: for int i in 10
            {
                startEx = new CodePrimitiveExpression(0);
                endEx   = CodeDomEmitter.EmitCodeExpression(loop.Condition.ChildExpressions[0]);
            }

            // The statement that initializes the for loop (int i, i, etc).
            CodeStatement varInit = null;
            // The variable reference from the initialization: we need this when the initialization is "int i".
            // The variable reference is "i".
            CodeExpression varRef = null;

            // An intializer that is an explicit variable declaration must be handled differently.
            if (loop.Initialization.ChildExpressions[0] is ExplicitVariableDeclaration)
            {
                // Get the explicit variable declaration
                var explicitVariable = loop.Initialization.ChildExpressions[0] as ExplicitVariableDeclaration;
                varInit = new CodeVariableDeclarationStatement(explicitVariable.TypeName, explicitVariable.Name, // The declaration
                                                               startEx);                                         // The assignment
                varRef = new CodeVariableReferenceExpression((loop.Initialization.ChildExpressions[0] as ExplicitVariableDeclaration).Name);
            }
            else // It's a variable reference
            {
                // Get the variable reference.
                var variable = loop.Initialization.ChildExpressions[0] as VariableReference;
                varRef  = new CodeVariableReferenceExpression((loop.Initialization.ChildExpressions[0] as VariableReference).Name);
                varInit = new CodeAssignStatement(varRef, // Left operand
                                                  startEx);
            }

            // Pie's for loop is limited to less than or equal conditional.
            var op = new CodeBinaryOperatorExpression(varRef,
                                                      CodeBinaryOperatorType.LessThanOrEqual,
                                                      endEx);

            // The step: the amount incremented or decremented.
            CodeStatement stepSt = null;

            if (loop.Step.ChildExpressions[0] is Literal)
            {
                var literal = loop.Step.ChildExpressions[0] as Literal;
                stepSt = new CodeAssignStatement(varRef, new CodeBinaryOperatorExpression(
                                                     varRef, CodeBinaryOperatorType.Add, new CodePrimitiveExpression(literal.Value)));
            }
            var i = new CodeIterationStatement(varInit,
                                               op,
                                               stepSt);

            // Emit the statements found in the body of the while loop.
            foreach (var e in loop.ChildExpressions)
            {
                i.Statements.Add(CodeDomEmitter.EmitCodeStatement(e));
            }

            return(i);
        }
Exemple #7
0
        // Generate a codedom property expression and attach it to the codedom type.
        public static void Emit(CodeTypeDeclaration codeTypeDeclaration, Property property)
        {
            // Create the codedom property and attach it to the codedom type.
            var codeProperty = new CodeMemberProperty();

            codeTypeDeclaration.Members.Add(codeProperty);

            // Assign the name.
            codeProperty.Name = property.Name;

            // Assign the return type, making sure to check for null.
            if (property.TypeName == "void")
            {
                codeProperty.Type = null;
            }
            else
            {
                codeProperty.Type = new CodeTypeReference(property.TypeName);
            }

            // Translate the accessibility.
            MemberAttributes memberAttributes = MemberAttributes.Public;

            switch (property.Accessibility)
            {
            case Accessibility.Internal:
                memberAttributes = MemberAttributes.FamilyAndAssembly;
                break;

            case Accessibility.Private:
                memberAttributes = MemberAttributes.Private;
                break;

            case Accessibility.Protected:
                memberAttributes = MemberAttributes.Family;
                break;

            case Accessibility.Public:
                memberAttributes = MemberAttributes.Public;
                break;
            }

            // Shared = static
            if (property.IsShared)
            {
                memberAttributes |= MemberAttributes.Static;
            }
            if (property.IsAbstract)
            {
                memberAttributes |= MemberAttributes.Abstract;
            }
            if (property.IsOverride)
            {
                memberAttributes |= MemberAttributes.Override;
            }

            codeProperty.Attributes = memberAttributes;

            // Add the statements for the get block.
            if (property.GetBlock.ChildExpressions.Count > 0)
            {
                foreach (var e in property.GetBlock.ChildExpressions)
                {
                    codeProperty.GetStatements.Add(CodeDomEmitter.EmitCodeStatement(e));
                }
            }
            else
            {
                codeProperty.GetStatements.Add(new CodeCommentStatement("Placeholder statement"));
            }

            // Add the statements for the set block.
            if (property.SetBlock.ChildExpressions.Count > 0)
            {
                foreach (var e in property.SetBlock.ChildExpressions)
                {
                    codeProperty.SetStatements.Add(CodeDomEmitter.EmitCodeStatement(e));
                }
            }
            else
            {
                codeProperty.SetStatements.Add(new CodeCommentStatement("Placeholder statement"));
            }
        }
        // Generates a codedom constructor expression and attaches it to the given type.
        public static void Emit(CodeTypeDeclaration codeTypeDeclaration, Constructor ctor)
        {
            // Create the codedom constructor
            var codeCtor = new CodeConstructor();

            codeTypeDeclaration.Members.Add(codeCtor);

            // Translate accessibility of the constructor
            MemberAttributes memberAttributes = MemberAttributes.Public;

            switch (ctor.Accessibility)
            {
            case Accessibility.Internal:
                memberAttributes |= MemberAttributes.FamilyAndAssembly;
                break;

            case Accessibility.Private:
                memberAttributes |= MemberAttributes.Private;
                break;

            case Accessibility.Protected:
                memberAttributes |= MemberAttributes.Family;
                break;

            case Accessibility.Public:
                memberAttributes |= MemberAttributes.Public;
                break;
            }
            codeCtor.Attributes = memberAttributes;

            // Translate the parameters of the constructor
            foreach (Expression p in ctor.Parameters)
            {
                if (p is SimpleParameter) // ex "int i"
                {
                    codeCtor.Parameters.Add(new CodeParameterDeclarationExpression((p as SimpleParameter).TypeName, (p as SimpleParameter).Name));
                }
                if (p is DirectionedParameter) // ex "ref int i"
                {
                    var codeParameter = new CodeParameterDeclarationExpression((p as DirectionedParameter).TypeName, (p as DirectionedParameter).Name);
                    switch ((p as DirectionedParameter).Direction)
                    {
                    case ParameterDirection.Out:
                        codeParameter.Direction = FieldDirection.Out;
                        break;

                    case ParameterDirection.Ref:
                        codeParameter.Direction = FieldDirection.Ref;
                        break;
                    }
                    codeCtor.Parameters.Add(codeParameter);
                }
            }

            // Add call to a constructor of the base class or another in the same class.
            foreach (var a in ctor.SubParameters.ChildExpressions)
            {
                if (ctor.Sub)
                {
                    codeCtor.ChainedConstructorArgs.Add(CodeDomEmitter.EmitCodeExpression(a));
                }
                else
                {
                    codeCtor.BaseConstructorArgs.Add(CodeDomEmitter.EmitCodeExpression(a));
                }
            }

            // Add all the statements in the body of the constructor
            foreach (var e in ctor.ChildExpressions)
            {
                codeCtor.Statements.Add(CodeDomEmitter.EmitCodeStatement(e));
            }
        }