⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 codedomoutputvisitor.cs

📁 根据cs源码解析为codedom
💻 CS
📖 第 1 页 / 共 5 页
字号:
            doLoopStatement.EmbeddedStatement.AcceptVisitor(this, data);
            codeStack.Pop();

            if (forLoop.Statements.Count == 0)
            {
                forLoop.Statements.Add(new CodeSnippetStatement());
            }

            Breakable breakable = breakableStack.Pop();

            if (breakable.IsContinue)
            {
                forLoop.Statements.Add(new CodeSnippetStatement());
                forLoop.Statements.Add(new CodeLabeledStatement("continue" + breakable.Id, new CodeExpressionStatement(new CodeSnippetExpression())));
            }

            AddStmt(forLoop);

            if (breakable.IsBreak)
            {
                AddStmt(new CodeLabeledStatement("break" + breakable.Id, new CodeExpressionStatement(new CodeSnippetExpression())));
            }

            return forLoop;
        }

		public override object VisitForStatement(ForStatement forStatement, object data)
		{
			CodeIterationStatement forLoop = new CodeIterationStatement();
            breakableStack.Push(new Breakable());

            codeStack.Push(NullStmtCollection);

            if (forStatement.Initializers.Count > 0)
            {
                if (forStatement.Initializers.Count > 1)
                {
                    throw new NotSupportedException("CodeDom does not support Multiple For-Loop Initializer Statements");
                }

                foreach (object o in forStatement.Initializers)
                {
                    if (o is Expression)
                    {
                        forLoop.InitStatement = new CodeExpressionStatement((CodeExpression)((Expression)o).AcceptVisitor(this, data));
                    }
                    if (o is Statement)
                    {
                        forLoop.InitStatement = (CodeStatement)((Statement)o).AcceptVisitor(this, data);
                    }
                }
            }
            else
            {
                // RG: need to handle empty InitStatement
                forLoop.InitStatement = new CodeExpressionStatement(new CodeSnippetExpression());
            }
			
			if (forStatement.Condition == null) {
				forLoop.TestExpression = new CodePrimitiveExpression(true);
			} else {
				forLoop.TestExpression = (CodeExpression)forStatement.Condition.AcceptVisitor(this, data);
			}
			
			codeStack.Push(forLoop.Statements);
			forStatement.EmbeddedStatement.AcceptVisitor(this, data);
			codeStack.Pop();

            if (forStatement.Iterator.Count > 0)
            {
                if (forStatement.Initializers.Count > 1)
                {
                    throw new NotSupportedException("CodeDom does not support Multiple For-Loop Iterator Statements");
                }

                foreach (Statement stmt in forStatement.Iterator)
                {
                    forLoop.IncrementStatement = (CodeStatement)stmt.AcceptVisitor(this, data);
                }
            }
            else
            {
                // RG: need to handle empty IncrementStatement
                forLoop.IncrementStatement = new CodeExpressionStatement(new CodeSnippetExpression());
            }

            codeStack.Pop();

            Breakable breakable = breakableStack.Pop();

            if (breakable.IsContinue)
            {
                forLoop.Statements.Add(new CodeSnippetStatement());
                forLoop.Statements.Add(new CodeLabeledStatement("continue" + breakable.Id, new CodeExpressionStatement(new CodeSnippetExpression())));
            }

			AddStmt(forLoop);

            if (breakable.IsBreak)
            {
                AddStmt(new CodeLabeledStatement("break" + breakable.Id, new CodeExpressionStatement(new CodeSnippetExpression())));
            }
	
			return forLoop;
		}
		
		public override object VisitLabelStatement(LabelStatement labelStatement, object data)
		{
			System.CodeDom.CodeLabeledStatement labelStmt = new CodeLabeledStatement(labelStatement.Label,(CodeStatement)labelStatement.AcceptVisitor(this, data));
			
			// Add Statement to Current Statement Collection
			AddStmt(labelStmt);
			
			return labelStmt;
		}
		
		public override object VisitGotoStatement(GotoStatement gotoStatement, object data)
		{
			System.CodeDom.CodeGotoStatement gotoStmt = new CodeGotoStatement(gotoStatement.Label);
			
			// Add Statement to Current Statement Collection
			AddStmt(gotoStmt);
			
			return gotoStmt;
		}

        // RG
        int switchId = 0;

		public override object VisitSwitchStatement(SwitchStatement switchStatement, object data)
		{
            // switch(arg) { case label1: expr1; case label2: expr2; default: expr3; }
            //
            // Emulate With:
            // 
            //  object _switch1 = arg;
            //  if (arg.Equals(label1))
            //  {
            //      expr1;
            //  }
            //  else
            //  {
            //      if (arg.Equals(label2))
            //      {
            //          expr2;
            //      }
            //      else
            //      {
            //          expr3;
            //      }
            //  }
            //

            switchId++; // in case nested switch() statements
            string name = "_switch" + switchId.ToString();

            breakableStack.Push(new Breakable(false));

            bool isSwitchArg = false;

            CodeVariableReferenceExpression switchArg = null;
            SwitchSection defaultSection = null;

            // get default section
            foreach (SwitchSection section in switchStatement.SwitchSections)
            {
                foreach (CaseLabel label in section.SwitchLabels)
                {
                    if (label.IsDefault)
                    {
                        defaultSection = section;
                        break;
                    }
                }

                if (defaultSection != null)
                    break;
            }


            CodeConditionStatement _if = null;

            // get default section
            foreach (SwitchSection section in switchStatement.SwitchSections)
            {
                if (section != defaultSection)
                {
                    if (!isSwitchArg)
                    {
                        isSwitchArg = true;

                        codeStack.Push(NullStmtCollection);
                        CodeVariableDeclarationStatement switchStmt = new CodeVariableDeclarationStatement("System.Object", name, (CodeExpression)switchStatement.SwitchExpression.AcceptVisitor(this, data));
                        codeStack.Pop();

                        switchArg = new CodeVariableReferenceExpression(name);

                        AddStmt(switchStmt);
                        AddStmt(new CodeSnippetStatement());
                    }

                    codeStack.Push(NullStmtCollection);

                    CodeExpression condition = null;
                    foreach (CaseLabel label in section.SwitchLabels)
                    {
                        CodeMethodInvokeExpression cond = new CodeMethodInvokeExpression(switchArg, "Equals", (CodeExpression)label.Label.AcceptVisitor(this, data));
                        if (condition == null)
                        {
                            condition = cond;
                        }
                        else
                        {
                            condition = new CodeBinaryOperatorExpression(condition, CodeBinaryOperatorType.BooleanOr, cond);
                        }
                    }

                    codeStack.Pop();

                    if (_if == null)
                    {
                        _if = new CodeConditionStatement();
                        _if.Condition = condition;

                        AddStmt(_if);
                    }
                    else
                    {
                        CodeConditionStatement _if2 = new CodeConditionStatement();
                        _if2.Condition = condition;

                        _if.FalseStatements.Add(_if2);

                        _if = _if2;
                    }

                    codeStack.Push(_if.TrueStatements);

                    for (int i = 0; i < section.Children.Count; i++)
                    {
                        INode stmt = section.Children[i];

                        if (i == section.Children.Count - 1 && stmt is BreakStatement)
                            break;

                        stmt.AcceptVisitor(this, data);
                    }

                    codeStack.Pop();
                }
            }

            if (defaultSection != null)
            {
                if (_if != null)
                    codeStack.Push(_if.FalseStatements);

                for (int i = 0; i < defaultSection.Children.Count; i++)
                {
                    INode stmt = defaultSection.Children[i];

                    if (i == defaultSection.Children.Count - 1 && stmt is BreakStatement)
                        break;

                    stmt.AcceptVisitor(this, data);
                }

                if (_if != null)
                    codeStack.Pop();
            }

            Breakable breakable = breakableStack.Pop();

            if (breakable.IsContinue)
            {
                throw new Exception("Continue Inside Switch Not Supported");
            }

            if (breakable.IsBreak)
            {
                AddStmt(new CodeLabeledStatement("break" + breakable.Id, new CodeExpressionStatement(new CodeSnippetExpression())));
            }

            return null;
		}
		
		public override object VisitTryCatchStatement(TryCatchStatement tryCatchStatement, object data)
		{
			// add a try-catch-finally
			CodeTryCatchFinallyStatement tryStmt = new CodeTryCatchFinallyStatement();
			
			codeStack.Push(tryStmt.TryStatements);
			
			tryCatchStatement.StatementBlock.AcceptChildren(this, data);
			codeStack.Pop();
			
			if (!tryCatchStatement.FinallyBlock.IsNull) {
				codeStack.Push(tryStmt.FinallyStatements);
				
				tryCatchStatement.FinallyBlock.AcceptChildren(this,data);
				codeStack.Pop();
			}
			
			foreach (CatchClause clause in tryCatchStatement.CatchClauses)
			{
				CodeCatchClause catchClause = new CodeCatchClause(clause.VariableName);
				catchClause.CatchExceptionType = ConvType(clause.TypeReference);
				tryStmt.CatchClauses.Add(catchClause);
				
				codeStack.Push(catchClause.Statements);
				
				clause.StatementBlock.AcceptChildren(this, data);
				codeStack.Pop();
			}
			
			// Add Statement to Current Statement Collection
			AddStmt(tryStmt);
			
			return tryStmt;
		}
		
		public override object VisitThrowStatement(ThrowStatement throwStatement, object data)
		{
			CodeThrowExceptionStatement throwStmt = new CodeThrowExceptionStatement((CodeExpression)throwStatement.Expression.AcceptVisitor(this, data));
			
			// Add Statement to Current Statement Collection
			AddStmt(throwStmt);
			
			return throwStmt;
		}
		
		public override object VisitFixedStatement(FixedStatement fixedStatement, object data)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -