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

📄 csharpvisitor.cs

📁 c#源代码
💻 CS
📖 第 1 页 / 共 4 页
字号:
				propertyGetRegion.Block.AcceptVisitor(this, data);
				--indentLevel;
			} 
			AppendIndentation();
			sourceText.Append("}");
			AppendNewLine();
			GenerateExitConstructLabel();
			return null;
		}
		
		public object Visit(PropertySetRegion propertySetRegion, object data)
		{
			DebugOutput(propertySetRegion);
			exitConstructStack.Push(new DictionaryEntry(typeof(PropertyDeclaration), null));
			AppendAttributes(propertySetRegion.Attributes);
			AppendIndentation();
			sourceText.Append("set {");
			AppendNewLine();
			if (propertySetRegion.Block != null) {
				++indentLevel;
				propertySetRegion.Block.AcceptVisitor(this, data);
				--indentLevel;
			}
			AppendIndentation();
			sourceText.Append("}");
			AppendNewLine();
			GenerateExitConstructLabel();
			return null;
		}
		
		public object Visit(TypeReference typeReference, object data)
		{
			return ConvertTypeString(typeReference.Type);
		}
#endregion

#region Statements
		public object Visit(Statement statement, object data)
		{
			AppendIndentation();
			sourceText.Append("// warning visited unknown statment :");
			sourceText.Append(statement);
			AppendNewLine();
			return String.Empty;
		}
		
		public object Visit(BlockStatement blockStatement, object data)
		{
			DebugOutput(blockStatement);
			blockStatement.AcceptChildren(this, data);
			return null;
		}
		
		public object Visit(StatementExpression statementExpression, object data)
		{
			DebugOutput(statementExpression);
			AppendIndentation();
			if (statementExpression.Expression == null) {
				sourceText.Append("// warning got empty statement expression :");
				sourceText.Append(statementExpression);
			} else {
				sourceText.Append(statementExpression.Expression.AcceptVisitor(this, data).ToString());
				sourceText.Append(";");
			}
			AppendNewLine();
			return null;
		}
		
		public object Visit(LocalVariableDeclaration localVariableDeclaration, object data)
		{
			DebugOutput(localVariableDeclaration);
			for (int i = 0; i < localVariableDeclaration.Variables.Count; ++i) {
				VariableDeclaration localVar = (VariableDeclaration)localVariableDeclaration.Variables[i];
				AppendIndentation();
				sourceText.Append(GetModifier(localVariableDeclaration.Modifier));
				ArrayCreateExpression ace = localVar.Initializer as ArrayCreateExpression;
				if (ace != null && (ace.ArrayInitializer == null || ace.ArrayInitializer.CreateExpressions == null)) {
					sourceText.Append(ConvertTypeString(ace.CreateType.Type));
					sourceText.Append(" ");
					sourceText.Append(localVar.Name);
					sourceText.Append("[");
					sourceText.Append(GetExpressionList(ace.Parameters));
					sourceText.Append("]");
					
				} else {
					if (localVar.Type == null) {
						bool foundType = false;
						for (int j = i + 1; j < localVariableDeclaration.Variables.Count; ++j) {
							VariableDeclaration nextLocalVar = (VariableDeclaration)localVariableDeclaration.Variables[j];
							if (nextLocalVar.Type != null) {
								sourceText.Append(GetTypeString(nextLocalVar.Type));
								foundType = true;
								break;
							}
						}
						if (!foundType) {
							sourceText.Append("object");
						}
					} else {
						sourceText.Append(GetTypeString(localVar.Type));
					}
					sourceText.Append(" ");
					sourceText.Append(localVar.Name);
					if (localVar.Initializer != null) {
						sourceText.Append(" = ");
						sourceText.Append(localVar.Initializer.AcceptVisitor(this, data).ToString());
					} else {
						if (localVar.Type != null && localVar.Type.Dimension != null) {
							sourceText.Append(" = new ");
							sourceText.Append(ConvertTypeString(localVar.Type.Type));
							sourceText.Append("[");
							sourceText.Append(GetExpressionList(localVar.Type.Dimension));
							sourceText.Append("]");
						}
					}
				}
				sourceText.Append(";");
				AppendNewLine();
			}
			return null;
		}
		
		public object Visit(SimpleIfStatement ifStatement, object data)
		{
			AppendIndentation();
			sourceText.Append("if (");
			sourceText.Append(ifStatement.Condition.AcceptVisitor(this, data).ToString());
			sourceText.Append(") {");
			AppendNewLine();
			++indentLevel;
			foreach(Statement statement in ifStatement.Statements) {
				statement.AcceptVisitor(this, data);
			}
			--indentLevel;
			AppendIndentation();
			sourceText.Append("}");
			
			if(ifStatement.ElseStatements != null && ifStatement.ElseStatements.Count > 0) {
				sourceText.Append(" else {");
				AppendNewLine();
				++indentLevel;
				foreach(Statement statement in ifStatement.ElseStatements) {
					statement.AcceptVisitor(this, data);
				}
				--indentLevel;
				AppendIndentation();
				sourceText.Append("}");
			}
			
			AppendNewLine();
			return null;
		}
		
		public object Visit(IfStatement ifStatement, object data)
		{
			DebugOutput(ifStatement);
			AppendIndentation();
			sourceText.Append("if (");
			sourceText.Append(ifStatement.Condition.AcceptVisitor(this, data).ToString());
			sourceText.Append(") {");
			AppendNewLine();
			++indentLevel;
			ifStatement.EmbeddedStatement.AcceptVisitor(this, data);
			--indentLevel;
			
			AppendIndentation();
			sourceText.Append("}");
			
			if (ifStatement.ElseIfStatements != null) {
				foreach (ElseIfSection elseIfSection in ifStatement.ElseIfStatements) {
					sourceText.Append(" else if (");
					sourceText.Append(elseIfSection.Condition.AcceptVisitor(this, data).ToString());
					sourceText.Append(") {");
					AppendNewLine();
					++indentLevel;
					elseIfSection.EmbeddedStatement.AcceptVisitor(this, data);
					--indentLevel;
					AppendIndentation();
					sourceText.Append("}");
				}
			}
			
			if (ifStatement.EmbeddedElseStatement != null) {
				sourceText.Append(" else {");
				AppendNewLine();
				++indentLevel;
				ifStatement.EmbeddedElseStatement.AcceptVisitor(this, data);
				--indentLevel;
				AppendIndentation();
				sourceText.Append("}");
			}
			
			AppendNewLine();
			return null;
		}
		
		public object Visit(LabelStatement labelStatement, object data)
		{
			DebugOutput(labelStatement);
			AppendIndentation();
			sourceText.Append(labelStatement.Label);
			sourceText.Append(":");
			AppendNewLine();
			if (labelStatement.EmbeddedStatement != null) {
				labelStatement.EmbeddedStatement.AcceptVisitor(this, data);
			}
			return null;
		}
		
		public object Visit(GoToStatement goToStatement, object data)
		{
			DebugOutput(goToStatement);
			AppendIndentation();
			sourceText.Append("goto");
			sourceText.Append(goToStatement.LabelName);
			sourceText.Append(";");
			AppendNewLine();
			return null;
		}
		
		public object Visit(SelectStatement selectStatement, object data)
		{
			DebugOutput(selectStatement);
			exitConstructStack.Push(new DictionaryEntry(typeof(SelectStatement), null));
			string selectExpression = selectStatement.SelectExpression.AcceptVisitor(this, data).ToString();
			AppendIndentation();
			for (int j = 0; j < selectStatement.SelectSections.Count; ++j) {
				SelectSection selectSection = (SelectSection)selectStatement.SelectSections[j];
				if (selectSection.CaseClauses.Count == 1 && ((CaseClause)selectSection.CaseClauses[0]).IsDefaultCase) {
					sourceText.Append("{");
				} else {
					sourceText.Append("if (");
					for (int i = 0; i < selectSection.CaseClauses.Count; ++i) {
						CaseClause caseClause = (CaseClause)selectSection.CaseClauses[i];
						if (caseClause.BoundaryExpression != null) {
							sourceText.Append(caseClause.ComparisonExpression.AcceptVisitor(this, data));
							sourceText.Append(" <= ");
							sourceText.Append(selectExpression);
							sourceText.Append(" && ");
							sourceText.Append(selectExpression);
							sourceText.Append(" <= ");
							sourceText.Append(caseClause.BoundaryExpression.AcceptVisitor(this, data));
						} else {
							if (caseClause.ComparisonExpression != null) {
								sourceText.Append(selectExpression);
								sourceText.Append(" == ");
								sourceText.Append(caseClause.ComparisonExpression.AcceptVisitor(this, data));
							} else {
								// dummy default should never evaluate (only for default case)
								sourceText.Append(" true ");
							}
						}
						if (i + 1 < selectSection.CaseClauses.Count) {
							sourceText.Append(" || ");
						}
					}
					sourceText.Append(") {");
				}
				AppendNewLine();
				++indentLevel;
				selectSection.EmbeddedStatement.AcceptChildren(this, data);
				--indentLevel;
				AppendIndentation();
				sourceText.Append("}");
				if (j + 1 < selectStatement.SelectSections.Count) {
					sourceText.Append(" else ");
				} 
			}
			AppendNewLine();
			GenerateExitConstructLabel();
			return null;
		}
		
		public object Visit(StopStatement stopStatement, object data)
		{
			DebugOutput(stopStatement);
			AppendIndentation();
			sourceText.Append("Debugger.Break();");
			AppendNewLine();
			return null;
		}
		
		public object Visit(ResumeStatement resumeStatement, object data)
		{
			DebugOutput(resumeStatement);
			AppendIndentation();
			sourceText.Append("// TODO: NotImplemented statement: ");
			sourceText.Append(resumeStatement);
			AppendNewLine();
			return null;
		}
		
		public object Visit(EraseStatement eraseStatement, object data)
		{
			DebugOutput(eraseStatement);
			AppendIndentation();
			sourceText.Append("// TODO: NotImplemented statement: ");
			sourceText.Append(eraseStatement);
			AppendNewLine();
			return null;
		}
		
		public object Visit(ErrorStatement errorStatement, object data)
		{
			DebugOutput(errorStatement);
			AppendIndentation();
			sourceText.Append("// TODO: NotImplemented statement: ");
			sourceText.Append(errorStatement);
			AppendNewLine();
			return null;
		}
		
		public object Visit(OnErrorStatement onErrorStatement, object data)
		{
			DebugOutput(onErrorStatement);
			AppendIndentation();
			sourceText.Append("// TODO: NotImplemented statement: ");
			sourceText.Append(onErrorStatement);
			AppendNewLine();
			return null;
		}
		
		public object Visit(ReDimStatement reDimStatement, object data)
		{
			DebugOutput(reDimStatement);
			AppendIndentation();
			sourceText.Append("// TODO: NotImplemented statement: ");
			sourceText.Append(reDimStatement);
			AppendNewLine();
			return null;
		}
		
		public object Visit(AddHandlerStatement addHandlerStatement, object data)
		{
			DebugOutput(addHandlerStatement);
			AppendIndentation();
			sourceText.Append(addHandlerStatement.EventExpression.AcceptVisitor(this, data));
			sourceText.Append(" += ");
			sourceText.Append(addHandlerStatement.HandlerExpression.AcceptVisitor(this, data));
			sourceText.Append(";");
			AppendNewLine();
			return null;
		}
		
		public object Visit(RemoveHandlerStatement removeHandlerStatement, object data)
		{
			DebugOutput(removeHandlerStatement);
			AppendIndentation();
			sourceText.Append(removeHandlerStatement.EventExpression.AcceptVisitor(this, data));
			sourceText.Append(" -= ");
			sourceText.Append(removeHandlerStatement.HandlerExpression.AcceptVisitor(this, data));
			sourceText.Append(";");
			AppendNewLine();
			return null;
		}
		
		public object Visit(DoLoopStatement doLoopStatement, object data)
		{
			DebugOutput(doLoopStatement);
			exitConstructStack.Push(new DictionaryEntry(typeof(DoLoopStatement), null));
			if (doLoopStatement.ConditionPosition == ConditionPosition.Start) {
				AppendIndentation();
				sourceText.Append("while (");
				if (doLoopStatement.ConditionType == ConditionType.Until) {
					sourceText.Append("!(");
				}
				sourceText.Append(doLoopStatement.Condition.AcceptVisitor(this, data).ToString());
				if (doLoopStatement.ConditionType == ConditionType.Until) {
					sourceText.Append(")");
				}
				sourceText.Append(") {");
				
				AppendNewLine();
				
				++indentLevel;
				doLoopStatement.EmbeddedStatement.AcceptVisitor(this, data);
				--indentLevel;
				
				AppendIndentation();
				sourceText.Append("}");
				AppendNewLine();
			} else {
				AppendIndentation();
				sourceText.Append("do {");
				AppendNewLine();
				
				++indentLevel;
				doLoopStatement.EmbeddedStatement.AcceptVisitor(this, data);
				--indentLevel;
				
				AppendIndentation();
				sourceText.Append("} while (");
				if (doLoopStatement.Condition == null) {
					sourceText.Append("true");
				} else {
					if (doLoopStatement.ConditionType == ConditionType.Until) {
						sourceText.Append("!(");
					}
					sourceText.Append(doLoopStatement.Condition.AcceptVisitor(this, data).ToString());
					if (doLoopStatement.ConditionType == ConditionType.Until) {
						sourceText.Append(")");
					}
				}
				sourceText.Append(");");
				AppendNewLine();
			}
			GenerateExitConstructLabel();
			return null;
		}
		
		public object Visit(EndStatement endStatement, object data)
		{
			DebugOutput(endStatement);
			AppendIndentation();
			sourceText.Append("System.Environment.Exit(0);");
			AppendNewLine();
			return null;
		}
		
		Stack exitConstructStack = new Stack();
		int   exitLabelCount     = 0;
		public string AddExitOnConstructStack(Type exitType)
		{
			string labelName = String.Concat("exit" + exitType.Name, exitLabelCount++);
			if (exitConstructStack.Count > 0) {
				object[] exitArray = exitConstructStack.ToArray();
				for (int i = exitArray.Length - 1; i >= 0; --i) {
					if ((Type)((DictionaryEntry)exitArray[i]).Key == exitType) {
						exitArray[i] = new DictionaryEntry(((DictionaryEntry)exitArray[i]).Key, labelName);
					}
				}
				Array.Reverse(exitArray);
				exitConstructStack = new Stack(exitArray);
			}
			return String.Concat(labelName);
		}
		
		public void GenerateExitConstructLabel()
		{
			if (exitConstructStack.Count > 0) {
				DictionaryEntry entry = (DictionaryEntry)exitConstructStack.Pop();
				if (entry.Value != null) {
					AppendIndentation();
					sourceText.Append(entry.Value.ToString());
					sourceText.Append(": ;");
					AppendNewLine();
				}
			}
		}
		
		public object Visit(ExitStatement exitStatement, object data)
		{
			DebugOutput(exitStatement);
			Type   exitType  = null;
			switch (exitStatement.ExitType) {
				case ExitType.Sub:
					sourceText.Append("return;");
					AppendNewLine();
					return null;
				case ExitType.Function:
					sourceText.Append("return null;");
					AppendNewLine();
					return null;
				case ExitType.Property:
					exitType = typeof(PropertyDeclaration);
					break;
				case ExitType.Do:
					exitType = typeof(DoLoopStatement);
					break;
				case ExitType.For:
					exitType = typeof(ForStatement);
					break;
				case ExitType.While:
					exitType = typeof(WhileStatement);
					break;
				case ExitType.Select:

⌨️ 快捷键说明

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