Пример #1
0
        private void EmitUsing(AstNode expression, IEnumerable <AstNode> inner)
        {
            var visitor = new ReturnSearchVisitor();

            this.UsingStatement.EmbeddedStatement.AcceptVisitor(visitor);
            bool hasRet = visitor.Found;

            VariableInitializer variableInitializer = expression as VariableInitializer;

            if (variableInitializer != null)
            {
                if (hasRet)
                {
                    this.WriteReturn(true);
                }
                this.Write("System.using");
                this.WriteOpenParentheses();
                variableInitializer.Initializer.AcceptVisitor(this.Emitter);
                this.WriteComma();
                this.WriteFunction();
                this.WriteOpenParentheses();
                this.Write(variableInitializer.Name);
            }
            else
            {
                AssignmentExpression assignmentExpression = (AssignmentExpression)expression;
                assignmentExpression.AcceptVisitor(this.Emitter);
                this.WriteNewLine();
                if (hasRet)
                {
                    this.WriteReturn(true);
                }
                this.Write("System.using");
                this.WriteOpenParentheses();
                assignmentExpression.Left.AcceptVisitor(this.Emitter);
                this.WriteComma();
                this.WriteFunction();
                this.WriteOpenParentheses();
                assignmentExpression.Left.AcceptVisitor(this.Emitter);
            }

            this.WriteCloseParentheses();
            this.BeginFunctionBlock();
            this.UsingStatement.EmbeddedStatement.AcceptVisitor(this.Emitter);
            this.EndFunctionBlock();
            this.WriteCloseParentheses();
            this.WriteNewLine();
        }
Пример #2
0
        private void WriteMultiAssignmentExpression(AssignmentExpression assignmentExpression)
        {
            var right = assignmentExpression.Right as AssignmentExpression;

            if (right != null)
            {
                WriteMultiAssignmentExpression(right);
                this.WriteNewLine();

                var initCount        = this.Emitter.Writers.Count;
                var oldAssigment     = this.Emitter.IsAssignment;
                var oldAssigmentType = this.Emitter.AssignmentType;

                this.Emitter.IsAssignment   = true;
                this.Emitter.AssignmentType = AssignmentOperatorType.Assign;

                assignmentExpression.Left.AcceptVisitor(this.Emitter);

                this.Emitter.AssignmentType = oldAssigmentType;
                this.Emitter.IsAssignment   = oldAssigment;

                if (this.Emitter.Writers.Count == initCount)
                {
                    this.Write(" = ");
                }

                right.Left.AcceptVisitor(this.Emitter);
                if (this.Emitter.Writers.Count > initCount)
                {
                    this.PopWriter();
                }
            }
            else
            {
                assignmentExpression.AcceptVisitor(this.Emitter);
            }
        }
Пример #3
0
        public override object TrackedVisitInvocationExpression(InvocationExpression invocationExpression, object data)
        {
            Expression targetObject = invocationExpression.TargetObject;
            string     methodName   = null;

            if (targetObject is IdentifierExpression)
            {
                methodName = ((IdentifierExpression)targetObject).Identifier;
            }
            else if (targetObject is FieldReferenceExpression)
            {
                methodName = ((FieldReferenceExpression)targetObject).FieldName;
            }

            if (methodName.StartsWith("set") || methodName.StartsWith("get"))
            {
                if (targetObject is IdentifierExpression)
                {
                    TypeDeclaration typeDeclaration = (TypeDeclaration)AstUtil.GetParentOfType(invocationExpression, typeof(TypeDeclaration));
                    string          key;

                    if (ContainsReferences(typeDeclaration, methodName, out key))
                    {
                        IdentifierExpression identifierExpression = new IdentifierExpression((string)CodeBase.References[key]);
                        if (methodName.StartsWith("get"))
                        {
                            identifierExpression.Parent = invocationExpression.Parent;
                            ReplaceCurrentNode(identifierExpression);
                        }
                        else if (methodName.StartsWith("set"))
                        {
                            Expression           setValue   = (Expression)invocationExpression.Arguments[0];
                            AssignmentExpression assignment = new AssignmentExpression(identifierExpression, AssignmentOperatorType.Assign, setValue);
                            assignment.Parent = invocationExpression.Parent;
                            ReplaceCurrentNode(assignment);
                            assignment.AcceptVisitor(this, data);
                        }
                    }
                }
                else if (targetObject is FieldReferenceExpression)
                {
                    Expression    target      = ((FieldReferenceExpression)targetObject).TargetObject;
                    TypeReference invokerType = GetExpressionType(target);
                    if (invokerType != null)
                    {
                        string fullName = GetFullName(invokerType);

                        string key = fullName + "." + methodName;
                        if (CodeBase.References.Contains(key))
                        {
                            string property = (string)CodeBase.References[key];
                            FieldReferenceExpression replaced = new FieldReferenceExpression(target, property);
                            if (methodName.StartsWith("get"))
                            {
                                replaced.Parent = invocationExpression.Parent;
                                ReplaceCurrentNode(replaced);
                                replaced.AcceptVisitor(this, data);
                            }
                            else
                            {
                                Expression           setValue   = (Expression)invocationExpression.Arguments[0];
                                AssignmentExpression assignment = new AssignmentExpression(replaced, AssignmentOperatorType.Assign, setValue);
                                assignment.Parent = invocationExpression.Parent;
                                ReplaceCurrentNode(assignment);
                                assignment.AcceptVisitor(this, data);
                            }
                        }
                    }
                }
            }
            return(base.TrackedVisitInvocationExpression(invocationExpression, data));
        }
Пример #4
0
        private void WriteMultiAssignmentExpression(AssignmentExpression assignmentExpression) {
            var right = assignmentExpression.Right as AssignmentExpression;
            if(right != null) {
                WriteMultiAssignmentExpression(right);
                this.WriteNewLine();

                var initCount = this.Emitter.Writers.Count;
                var oldAssigment = this.Emitter.IsAssignment;
                var oldAssigmentType = this.Emitter.AssignmentType;

                this.Emitter.IsAssignment = true;
                this.Emitter.AssignmentType = AssignmentOperatorType.Assign;

                assignmentExpression.Left.AcceptVisitor(this.Emitter);

                this.Emitter.AssignmentType = oldAssigmentType;
                this.Emitter.IsAssignment = oldAssigment;

                if(this.Emitter.Writers.Count == initCount) {
                    this.Write(" = ");
                }

                right.Left.AcceptVisitor(this.Emitter);
                if(this.Emitter.Writers.Count > initCount) {
                    this.PopWriter();
                }
            }
            else {
                assignmentExpression.AcceptVisitor(this.Emitter);
            }
        }