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

📄 csharpvisitor.cs

📁 c#源代码
💻 CS
📖 第 1 页 / 共 4 页
字号:
					exitType = typeof(SelectStatement);
					break;
				case ExitType.Try:
					exitType = typeof(TryCatchStatement);
					break;
			}
			if (exitType != null) {
				AppendIndentation();
				sourceText.Append("goto ");
				sourceText.Append(AddExitOnConstructStack(exitType));
				sourceText.Append(";");
				AppendNewLine();
			} else {
				AppendIndentation();
				sourceText.Append("ERROR IN GENERATION: EXIT TO ");
				sourceText.Append(exitStatement.ExitType);
				sourceText.Append(" FAILED!!!");
				AppendNewLine();
			}
			return null;
		}
		
		public object Visit(ForeachStatement foreachStatement, object data)
		{
			DebugOutput(foreachStatement);
			exitConstructStack.Push(new DictionaryEntry(typeof(ForStatement), null));
			
			AppendIndentation();
			sourceText.Append("foreach (");
			if (foreachStatement.LoopControlVariable.Type != null) {
				sourceText.Append(this.GetTypeString(foreachStatement.LoopControlVariable.Type));
				sourceText.Append(" ");
			}
			sourceText.Append(foreachStatement.LoopControlVariable.Name);
			sourceText.Append(" in ");
			sourceText.Append(foreachStatement.Expression.AcceptVisitor(this, data));
			sourceText.Append(") {");
			AppendNewLine();
			
			++indentLevel;
			foreachStatement.EmbeddedStatement.AcceptVisitor(this, data);
			--indentLevel;
			
			AppendIndentation();
			sourceText.Append("}");
			AppendNewLine();
			GenerateExitConstructLabel();
			return null;
		}
		
		public object Visit(ForStatement forStatement, object data)
		{
			DebugOutput(forStatement);
			exitConstructStack.Push(new DictionaryEntry(typeof(ForStatement), null));
			bool   stepIsNegative = false;
			string step           = null;
			if (forStatement.Step != null) {
				step = forStatement.Step.AcceptVisitor(this, data).ToString();
				stepIsNegative = step.StartsWith("-");
			}
			
			AppendIndentation();
			sourceText.Append("for (");
			
			
			if (forStatement.LoopControlVariable.Type != null) {
				sourceText.Append(this.GetTypeString(forStatement.LoopControlVariable.Type));
				sourceText.Append(" ");
			}
			sourceText.Append(forStatement.LoopControlVariable.Name);
			sourceText.Append(" = ");
			
			sourceText.Append(forStatement.Start.AcceptVisitor(this, data));
			sourceText.Append("; ");
			sourceText.Append(forStatement.LoopControlVariable.Name);
			sourceText.Append(stepIsNegative ? " >= " : " <= ");
			sourceText.Append(forStatement.End.AcceptVisitor(this, data));
			sourceText.Append("; ");
			if (forStatement.Step == null) {
				sourceText.Append(forStatement.LoopControlVariable.Name);
				sourceText.Append("++");
			} else {
				sourceText.Append(forStatement.LoopControlVariable.Name);
				if (stepIsNegative) {
					if (step == "-1") {
						sourceText.Append("--");
					} else {
						sourceText.Append(" -= ");
						sourceText.Append(step.Substring(1));
					}
				} else {
					sourceText.Append(" += ");
					sourceText.Append(step);
				}
			}
			sourceText.Append(") {");
			AppendNewLine();
			
			++indentLevel;
			forStatement.EmbeddedStatement.AcceptVisitor(this, data);
			--indentLevel;
			AppendIndentation();
			sourceText.Append("}");
			AppendNewLine();
			GenerateExitConstructLabel();
			
			return null;
		}
		
		public object Visit(LockStatement lockStatement, object data)
		{
			DebugOutput(lockStatement);
			AppendIndentation();
			sourceText.Append("lock (");
			sourceText.Append(lockStatement.LockExpression.AcceptVisitor(this, data));
			sourceText.Append(") {");
			AppendNewLine();
			
			++indentLevel;
			lockStatement.EmbeddedStatement.AcceptVisitor(this, data);
			--indentLevel;
			
			AppendIndentation();
			sourceText.Append("}");
			AppendNewLine();
			return null;
		}
		
		public object Visit(RaiseEventStatement raiseEventStatement, object data)
		{
			DebugOutput(raiseEventStatement);
			AppendIndentation();
			sourceText.Append("if (");
			sourceText.Append(raiseEventStatement.EventName);
			sourceText.Append(" != null) {");
			AppendNewLine();
			
			++indentLevel;
			AppendIndentation();
			sourceText.Append(raiseEventStatement.EventName);
			sourceText.Append(GetParameters(raiseEventStatement.Parameters));
			sourceText.Append(";");
			AppendNewLine();
			--indentLevel;
			
			AppendIndentation();
			sourceText.Append("}");
			AppendNewLine();
			return null;
		}
		
		
		public object Visit(ReturnStatement returnStatement, object data)
		{
			DebugOutput(returnStatement);
			AppendIndentation();
			sourceText.Append("return");
			if (returnStatement.ReturnExpression != null) {
				sourceText.Append(" ");
				sourceText.Append(returnStatement.ReturnExpression.AcceptVisitor(this,data));
			}
			sourceText.Append(";");
			AppendNewLine();
			return null;
		}
		
		public object Visit(ThrowStatement throwStatement, object data)
		{
			DebugOutput(throwStatement);
			AppendIndentation();
			sourceText.Append("throw");
			if (throwStatement.ThrowExpression != null) {
				sourceText.Append(" ");
				sourceText.Append(throwStatement.ThrowExpression.AcceptVisitor(this, data).ToString());
			}
			sourceText.Append(";");
			AppendNewLine();
			return null;
		}
		
		public object Visit(TryCatchStatement tryCatchStatement, object data)
		{
			DebugOutput(tryCatchStatement);
			exitConstructStack.Push(new DictionaryEntry(typeof(TryCatchStatement), null));
			AppendIndentation();
			sourceText.Append("try {");
			AppendNewLine();
			
			++indentLevel;
			tryCatchStatement.StatementBlock.AcceptVisitor(this, data);
			--indentLevel;
			AppendIndentation();
			sourceText.Append("}");
			if (tryCatchStatement.CatchClauses != null) {
				foreach (CatchClause catchClause in tryCatchStatement.CatchClauses) {
					sourceText.Append(" catch ");
					if (catchClause.Type != null) {
						sourceText.Append("(");
						sourceText.Append(GetTypeString(catchClause.Type));
						if (catchClause.VariableName != null) {
							sourceText.Append(" ");
							sourceText.Append(catchClause.VariableName);
						}
						sourceText.Append(") ");
					}
					sourceText.Append("{");
					AppendNewLine();
					++indentLevel;
					if (catchClause.Condition != null) {
						AppendIndentation();
						sourceText.Append("//TODO: review the original conditional catch clause");
						AppendNewLine();
						AppendIndentation();
						sourceText.Append("if (");
						sourceText.Append(catchClause.Condition.AcceptVisitor(this, data));
						sourceText.Append(") {");
						AppendNewLine();
						++indentLevel;
						catchClause.StatementBlock.AcceptVisitor(this, data);
						--indentLevel;
						AppendIndentation();
						sourceText.Append("}");
						AppendNewLine();
					} else {
						catchClause.StatementBlock.AcceptVisitor(this, data);
					}
					--indentLevel;
					AppendIndentation();
					sourceText.Append("}");
				}
			}
			
			if (tryCatchStatement.FinallyBlock != null) {
				sourceText.Append(" finally {");
				AppendNewLine();
				
				++indentLevel;
				tryCatchStatement.FinallyBlock.AcceptVisitor(this, data);
				--indentLevel;
				AppendIndentation();
				sourceText.Append("}");
			}
			AppendNewLine();
			GenerateExitConstructLabel();
			return null;
		}
		
		public object Visit(WhileStatement whileStatement, object data)
		{
			DebugOutput(whileStatement);
			exitConstructStack.Push(new DictionaryEntry(typeof(WhileStatement), null));
			AppendIndentation();
			sourceText.Append("while (");
			sourceText.Append(whileStatement.Condition.AcceptVisitor(this, data).ToString());
			sourceText.Append(") {");
			AppendNewLine();
			
			++indentLevel;
			whileStatement.EmbeddedStatement.AcceptVisitor(this, data);
			--indentLevel;
			
			AppendIndentation();
			sourceText.Append("}");
			AppendNewLine();
			GenerateExitConstructLabel();
			return null;
		}
		
		public object Visit(WithStatement withStatement, object data)
		{
			DebugOutput(withStatement);
			withExpressionStack.Push(withStatement.WithExpression);
			withStatement.Body.AcceptVisitor(this, data);
			withExpressionStack.Pop();
			return null;
		}
		
		public object Visit(ICSharpCode.SharpRefactory.Parser.AST.VB.Attribute attribute, object data)
		{
			DebugOutput(attribute);
			AppendIndentation();
			sourceText.Append("// Should never happen (this is handled in AttributeSection) attribute was:");
			sourceText.Append(attribute);
			AppendNewLine();
			return null;
		}
		
		public object Visit(AttributeSection attributeSection, object data)
		{
			DebugOutput(attributeSection);
			AppendIndentation();
			sourceText.Append("[");
			if (attributeSection.AttributeTarget != null && attributeSection.AttributeTarget.Length > 0) {
				sourceText.Append(attributeSection.AttributeTarget);
				sourceText.Append(": ");
			}
			for (int j = 0; j < attributeSection.Attributes.Count; ++j) {
				ICSharpCode.SharpRefactory.Parser.AST.VB.Attribute attr = (ICSharpCode.SharpRefactory.Parser.AST.VB.Attribute)attributeSection.Attributes[j];
				
				sourceText.Append(attr.Name);
				sourceText.Append("(");
				for (int i = 0; i < attr.PositionalArguments.Count; ++i) {
					Expression expr = (Expression)attr.PositionalArguments[i];
					sourceText.Append(expr.AcceptVisitor(this, data).ToString());
					if (i + 1 < attr.PositionalArguments.Count | attr.NamedArguments.Count > 0) { 
						sourceText.Append(", ");
					}
				}

				for (int i = 0; i < attr.NamedArguments.Count; ++i) {
					NamedArgumentExpression named = (NamedArgumentExpression)attr.NamedArguments[i];
					sourceText.Append(named.AcceptVisitor(this, data).ToString());
					if (i + 1 < attr.NamedArguments.Count) { 
						sourceText.Append(", ");
					}
				}
				sourceText.Append(")");
				if (j + 1 < attributeSection.Attributes.Count) {
					sourceText.Append(", ");
				}
			}
			sourceText.Append("]");
			AppendNewLine();
			return null;
		}
		
		public object Visit(OptionCompareDeclaration optionCompareDeclaration, object data)
		{
			DebugOutput(optionCompareDeclaration);
			AppendIndentation();
			sourceText.Append("// TODO: NotImplemented statement: ");
			sourceText.Append(optionCompareDeclaration);
			AppendNewLine();
			return null;
		}
		
		public object Visit(OptionExplicitDeclaration optionExplicitDeclaration, object data)
		{
			DebugOutput(optionExplicitDeclaration);
			AppendIndentation();
			sourceText.Append("// TODO: NotImplemented statement: ");
			sourceText.Append(optionExplicitDeclaration);
			AppendNewLine();
			return null;
		}
		
		public object Visit(OptionStrictDeclaration optionStrictDeclaration, object data)
		{
			DebugOutput(optionStrictDeclaration);
			AppendIndentation();
			sourceText.Append("// TODO: NotImplemented statement: ");
			sourceText.Append(optionStrictDeclaration);
			AppendNewLine();
			return null;
		}
#endregion

#region Expressions
		public object Visit(PrimitiveExpression primitiveExpression, object data)
		{
			DebugOutput(primitiveExpression);
			if (primitiveExpression.Value == null) {
				return "null";
			}
			if (primitiveExpression.Value is bool) {
				if ((bool)primitiveExpression.Value) {
					return "true";
				}
				return "false";
			}
			
			if (primitiveExpression.Value is string) {
				string s = primitiveExpression.Value.ToString();
				s = s.Replace("\\","\\\\");
				s = s.Replace("\"","\\\"");
				return String.Concat('"', s, '"');
			}
			
			if (primitiveExpression.Value is char) {
				string s = primitiveExpression.Value.ToString();
				s = s.Replace("\\","\\\\");
				s = s.Replace("\'","\\\'");
				return String.Concat("'", s, "'");
			}
			
			if (primitiveExpression.Value is System.DateTime) {
				string s = primitiveExpression.StringValue;
				s = s.Replace("\\","\\\\");
				s = s.Replace("\"","\\\"");
				return String.Concat("System.DateTime.Parse(\"", s, "\")");
			}

			if (primitiveExpression.Value is decimal) {
				return String.Concat(primitiveExpression.Value.ToString(), "M");
			}

			if (primitiveExpression.Value is float) {
				return String.Concat(primitiveExpression.Value.ToString(), "F");
			}

			return primitiveExpression.Value;
		}
		
		public object Visit(BinaryOperatorExpression binaryOperatorExpression, object data)
		{
			DebugOutput(binaryOperatorExpression);
			string op   = null;
			string left = binaryOperatorExpression.Left.AcceptVisitor(this, data).ToString();
			string right = binaryOperatorExpression.Right.AcceptVisitor(this, data).ToString();
			
			switch (binaryOperatorExpression.Op) {
				case BinaryOperatorType.Concat:
					op = " + ";
					break;
				
				case BinaryOperatorType.Add:
					op = " + ";
					break;
				
				case BinaryOperatorType.Subtract:
					op = " - ";
					break;
				
				case BinaryOperatorType.Multiply:
					op = " * ";
					break;
				
				case BinaryOperatorType.DivideInteger:
				case BinaryOperatorType.Divide:
					op = " / ";
					break;
				
				case BinaryOperatorType.Modulus:
					op = " % ";
					break;
				
				case BinaryOperatorType.ShiftLeft:
					op = " << ";
					break;
				
				case BinaryOperatorType.ShiftRight:
					op = " >> ";
					break;
				
				case BinaryOperatorType.BitwiseAnd:
					op = " & ";
					break;
				case BinaryOperatorType.BitwiseOr:
					op = " | ";
					break;
				case BinaryOperatorType.ExclusiveOr:
					op = " ^ ";
					break;
				
				case BinaryOperatorType.BooleanAnd:
					op = " && ";
					break;
				case BinaryOperatorType.BooleanOr:
					op = " || ";
					break;
				
				case BinaryOperatorType.Equality:
					op = " == ";
					break;
				case BinaryOperatorType.GreaterThan:
					op = " > ";
					break;
				case BinaryOperatorType.GreaterThanOrEqual:
					op = " >= ";
					break;
				case BinaryOperatorType.InEquality:
					op = " != ";
					break;
				case BinaryOperatorType.LessThan:
					op = " < ";
					break;

⌨️ 快捷键说明

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