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

📄 vbnetvisitor.cs

📁 c#源代码
💻 CS
📖 第 1 页 / 共 4 页
字号:
			sourceText.Append("Try");AppendNewLine();
			++indentLevel;
			usingStatement.EmbeddedStatement.AcceptVisitor(this, data);
			--indentLevel;
			AppendIndentation();
			sourceText.Append("Finally");AppendNewLine();
			++indentLevel;
			LocalVariableDeclaration varDecl = (usingStatement.UsingStmnt as LocalVariableDeclaration);
			if (varDecl == null) {
				AppendIndentation();
				sourceText.Append("' could not find variable declaration");AppendNewLine();
				AppendIndentation();
				sourceText.Append("' TODO : Dispose object");AppendNewLine();
			} else {
				foreach(VariableDeclaration var in varDecl.Variables) {
					AppendIndentation();
					sourceText.AppendFormat("CType({0}, IDisposable).Dispose()", var.Name);
					AppendNewLine();
				}
			}
			--indentLevel;
			AppendIndentation();
			sourceText.Append("End Try");AppendNewLine();
			return null;
		}
		
		public override object Visit(TryCatchStatement tryCatchStatement, object data)
		{
			DebugOutput(tryCatchStatement);
			AppendIndentation();
			sourceText.Append("Try");
			AppendNewLine();
			
			++indentLevel;
			tryCatchStatement.StatementBlock.AcceptVisitor(this, data);
			--indentLevel;
			
			if (tryCatchStatement.CatchClauses != null) {
				int generated = 0;
				foreach (CatchClause catchClause in tryCatchStatement.CatchClauses) {
					AppendIndentation();
					sourceText.Append("Catch");
					if (catchClause.Type != null) {
						sourceText.Append(" ");
					
						if (catchClause.VariableName == null) {
							sourceText.Append("generatedExceptionVariable");
							sourceText.Append(generated.ToString());
							++generated;
						} else {
							sourceText.Append(catchClause.VariableName);
						}
						sourceText.Append(" As ");
						sourceText.Append(catchClause.Type);
					}
					
					AppendNewLine();
					++indentLevel;
					catchClause.StatementBlock.AcceptVisitor(this, data);
					--indentLevel;
				}
			}
			
			if (tryCatchStatement.FinallyBlock != null) {
				AppendIndentation();
				sourceText.Append("Finally");
				AppendNewLine();
				
				++indentLevel;
				tryCatchStatement.FinallyBlock.AcceptVisitor(this, data);
				--indentLevel;
			}
			AppendIndentation();
			sourceText.Append("End Try");
			AppendNewLine();
			return null;
		}
		
		public override 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());
			}
			AppendNewLine();
			return null;
		}
		
		public override object Visit(FixedStatement fixedStatement, object data)
		{
			DebugOutput(fixedStatement);
			errors.Error(-1, -1, String.Format("fixed statement not suported by VB.NET"));
			return null;
		}
		
		public override object Visit(CheckedStatement checkedStatement, object data)
		{
			DebugOutput(checkedStatement);
			errors.Error(-1, -1, String.Format("checked statement not suported by VB.NET"));
			return null;
		}
		
		public override object Visit(UncheckedStatement uncheckedStatement, object data)
		{
			DebugOutput(uncheckedStatement);
			errors.Error(-1, -1, String.Format("unchecked statement not suported by VB.NET"));
			return null;
		}
		
		string ConvertCharLiteral(char ch)
		{
			if (Char.IsControl(ch)) {
				return "Microsoft.VisualBasic.Chr(" + ((int)ch) + ")";
			} else {
				if (ch == '"') {
					return "\"\"\"\"C";
				}
				return String.Concat("\"", ch.ToString(), "\"C");
			}
		}
		
		string ConvertChar(char ch)
		{
			switch (ch) {
				case '\0':
				case '\a':
				case '\b':
				case '\f':
				case '\t':
				case '\v':
				case '\r':
				case '\n':
					return "\" & Microsoft.VisualBasic.Chr(" + ((int)ch) + ") & \"";
				case '"':
					return "\"\"";
			}
			return ch.ToString();
		}

		string ConvertString(string str)
		{
			StringBuilder sb = new StringBuilder();
			foreach (char ch in str) {
				sb.Append(ConvertChar(ch));
			}
			return sb.ToString();
		}

		public override object Visit(PrimitiveExpression primitiveExpression, object data)
		{
			DebugOutput(primitiveExpression);
			if (primitiveExpression.Value == null) {
				return "Nothing";
			}
			if (primitiveExpression.Value is bool) {
				if ((bool)primitiveExpression.Value) {
					return "True";
				}
				return "False";
			}
			
			if (primitiveExpression.Value is string) {
				return String.Concat('"',
				                     ConvertString(primitiveExpression.Value.ToString()),
				                     '"');
			}
			
			if (primitiveExpression.Value is char) {
				return ConvertCharLiteral((char)primitiveExpression.Value);
			}

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

			if (primitiveExpression.Value is float) {
				return String.Concat(primitiveExpression.Value.ToString(), "F");
			}
			
			return primitiveExpression.Value;
		}
		
		public override 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.Add:
					op = " + ";
					break;
				
				case BinaryOperatorType.Subtract:
					op = " - ";
					break;
				
				case BinaryOperatorType.Multiply:
					op = " * ";
					break;
				
				case BinaryOperatorType.Divide:
					op = " / ";
					break;
				
				case BinaryOperatorType.Modulus:
					op = " Mod ";
					break;
				
				case BinaryOperatorType.ShiftLeft:
					op = " << ";
					break;
				
				case BinaryOperatorType.ShiftRight:
					op = " >> ";
					break;
				
				case BinaryOperatorType.BitwiseAnd:
					op = " And ";
					break;
				case BinaryOperatorType.BitwiseOr:
					op = " Or ";
					break;
				case BinaryOperatorType.ExclusiveOr:
					op = " Xor ";
					break;
				
				case BinaryOperatorType.LogicalAnd:
					op = " AndAlso ";
					break;
				case BinaryOperatorType.LogicalOr:
					op = " OrElse ";
					break;
				
				case BinaryOperatorType.AS:
					return String.Concat("CType(ConversionHelpers.AsWorkaround(",
					                     left,
					                     ", GetType(",
					                     right,
					                     ")), ",
					                     right,
					                     ")");
				case BinaryOperatorType.IS:
					return String.Concat("TypeOf ",
					                     left,
					                     " Is ",
					                     right);
				
				case BinaryOperatorType.Equality:
					op = " = ";
					if (right == "Nothing") {
						op = " Is ";
					}
					break;
				case BinaryOperatorType.GreaterThan:
					op = " > ";
					break;
				case BinaryOperatorType.GreaterThanOrEqual:
					op = " >= ";
					break;
				case BinaryOperatorType.InEquality:
					if (right == "Nothing") {
						return String.Concat("Not (",
						                     left,
						                     " Is ",
						                     right,
						                     ")");
					} else {
						return String.Concat("Not (",
						                     left,
						                     " = ",
						                     right,
						                     ")");
					}
				case BinaryOperatorType.LessThan:
					op = " < ";
					break;
				case BinaryOperatorType.LessThanOrEqual:
					op = " <= ";
					break;
			}
			
			return String.Concat(left,
			                     op,
			                     right);
		}
		
		public override object Visit(ParenthesizedExpression parenthesizedExpression, object data)
		{
			DebugOutput(parenthesizedExpression);
			string innerExpr = parenthesizedExpression.Expression.AcceptVisitor(this, data).ToString();
			
			// parenthesized cast expressions evaluate to a single 'method call' and don't need
			// to be parenthesized anymore like in C#. Parenthesized cast expresions may lead to
			// a vb.net compiler error (method calls)
			if (parenthesizedExpression.Expression is CastExpression) {
				return innerExpr;
			}
			return String.Concat("(", innerExpr, ")");
		}
		
		public override object Visit(InvocationExpression invocationExpression, object data)
		{
			DebugOutput(invocationExpression);
			string backString;
			
			if (invocationExpression.TargetObject is ObjectCreateExpression) {
				backString = String.Concat("(",
				                     invocationExpression.TargetObject.AcceptVisitor(this, data),
				                     ")",
				                     GetParameters(invocationExpression.Parameters));
			} else {
				backString = String.Concat(invocationExpression.TargetObject.AcceptVisitor(this, data),
				                     GetParameters(invocationExpression.Parameters));
			}
			//todo: invocationExpression, indexer ... etc.
			Expression expr = invocationExpression.TargetObject;
			while (expr is FieldReferenceExpression) {
				expr = ((FieldReferenceExpression)expr).TargetObject;
			}
			
			if (data is StatementExpression && expr is ObjectCreateExpression) {
				return String.Concat("call ", backString);
			}
			return backString;
		}
		
		public override object Visit(IdentifierExpression identifierExpression, object data)
		{
			DebugOutput(identifierExpression);
			return identifierExpression.Identifier;
		}
		
		public override object Visit(TypeReferenceExpression typeReferenceExpression, object data)
		{
			DebugOutput(typeReferenceExpression);
			return GetTypeString(typeReferenceExpression.TypeReference);
		}
		
		public override object Visit(UnaryOperatorExpression unaryOperatorExpression, object data)
		{
			DebugOutput(unaryOperatorExpression);
			string expr = unaryOperatorExpression.Expression.AcceptVisitor(this, data).ToString();
			switch (unaryOperatorExpression.Op) {
				case UnaryOperatorType.BitNot:
					return String.Concat("Not ", expr);
				case UnaryOperatorType.Decrement:
					return String.Concat("System.Threading.Interlocked.Decrement(", expr, ")");
				case UnaryOperatorType.Increment:
					return String.Concat("System.Threading.Interlocked.Increment(", expr, ")");
				case UnaryOperatorType.Minus:
					return String.Concat("-", expr);
				case UnaryOperatorType.Not:
					return String.Concat("Not ", expr);
				case UnaryOperatorType.Plus:
					return expr;
				case UnaryOperatorType.PostDecrement:
					return String.Concat("System.Math.Max(System.Threading.Interlocked.Decrement(", expr, "),", expr, "+1)");
				case UnaryOperatorType.PostIncrement:
					return String.Concat("System.Math.Min(System.Threading.Interlocked.Increment(", expr, "),", expr, "-1)");
				case UnaryOperatorType.Star:
				case UnaryOperatorType.BitWiseAnd:
					break;
			}
			throw new System.NotSupportedException();
		}
		
		public override object Visit(AssignmentExpression assignmentExpression, object data)
		{
			DebugOutput(assignmentExpression);
			string op   = null;
			string left = assignmentExpression.Left.AcceptVisitor(this, data).ToString();
			string right = assignmentExpression.Right.AcceptVisitor(this, data).ToString();
			switch (assignmentExpression.Op) {
				case AssignmentOperatorType.Assign:
					op = " = ";
					break;
				case AssignmentOperatorType.Add:
					op = " += ";
					if (IsEventHandlerCreation(assignmentExpression.Right)) {
						return String.Format("AddHandler {0}, AddressOf {1}", 
						                     left, 
						                     ((Expression)((ObjectCreateExpression)assignmentExpression.Right).Parameters[0]).AcceptVisitor(this, data).ToString());
					}
					break;
				case AssignmentOperatorType.Subtract:
					op = " -= ";
					if (IsEventHandlerCreation(assignmentExpression.Right)) {
						return String.Format("RemoveHandler {0}, AddressOf {1}", 
						                     left, 
						                     ((Expression)((ObjectCreateExpression)assignmentExpression.Right).Parameters[0]).AcceptVisitor(this, data).ToString());
					}
					break;
				case AssignmentOperatorType.Multiply:
					op = " *= ";
					break;
				case AssignmentOperatorType.Divide:
					op = " /= ";
					break;
				case AssignmentOperatorType.ShiftLeft:
					op = " <<= ";
					break;
				case AssignmentOperatorType.ShiftRight:
					op = " >>= ";
					break;
				
				case AssignmentOperatorType.ExclusiveOr:
					return String.Format("{0} = {0} Xor ({1})", left, right);
				case AssignmentOperatorType.Modulus:
					return String.Format("{0} = {0} Mod ({1})", left, right);
				case AssignmentOperatorType.BitwiseAnd:
					return String.Format("{0} = {0} And ({1})", left, right);
				case AssignmentOperatorType.BitwiseOr:
					return String.Format("{0} = {0} Or ({1})", left, right);
			}
			return String.Concat(left,
			                     op,
			                     right);
		}
		
		public override object Visit(SizeOfExpression sizeOfExpression, object data)
		{
			DebugOutput(sizeOfExpression);
			errors.Error(-1, -1, String.Format("sizeof expression not suported by VB.NET"));
			return null;
		}
		
		public override object Visit(TypeOfExpression typeOfExpression, object data)
		{
			DebugOutput(typeOfExpression);
			return String.Concat("GetType(",
			                     GetTypeString(typeOfExpression.TypeReference),
			                     ")");
		}
		
		public override object Visit(CheckedExpression checkedExpression, object data)
		{
			return String.Concat("'Checked expression (can't convert):",

⌨️ 快捷键说明

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