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

📄 convertvisitorexpressions.cs

📁 SharpDevelop2.0.0 c#开发免费工具
💻 CS
📖 第 1 页 / 共 2 页
字号:
			}
		}
		
		
		B.UnaryOperatorType ConvertOperator(UnaryOperatorType op)
		{
			switch (op) {
				case UnaryOperatorType.BitNot:
					return B.UnaryOperatorType.OnesComplement;
				case UnaryOperatorType.Not:
					return B.UnaryOperatorType.LogicalNot;
				case UnaryOperatorType.Decrement:
					return B.UnaryOperatorType.Decrement;
				case UnaryOperatorType.Increment:
					return B.UnaryOperatorType.Increment;
				case UnaryOperatorType.Minus:
					return B.UnaryOperatorType.UnaryNegation;
				case UnaryOperatorType.PostDecrement:
					return B.UnaryOperatorType.PostDecrement;
				case UnaryOperatorType.PostIncrement:
					return B.UnaryOperatorType.PostIncrement;
				default:
					return B.UnaryOperatorType.None;
			}
		}
		
		public object Visit(BinaryOperatorExpression binaryOperatorExpression, object data)
		{
			B.Expression left = ConvertExpression(binaryOperatorExpression.Left);
			B.Expression right = ConvertExpression(binaryOperatorExpression.Right);
			B.BinaryOperatorType op = ConvertOperator(binaryOperatorExpression.Op);
			if (op == B.BinaryOperatorType.None) {
				AddError(binaryOperatorExpression, "Unknown operator.");
				return null;
			}
//			if (binaryOperatorExpression.Op == BinaryOperatorType.DivideInteger) {
//				AddWarning(binaryOperatorExpression, "Integer division converted to normal division.");
//			}
			return new B.BinaryExpression(GetLexicalInfo(binaryOperatorExpression), op, left, right);
		}
		
		public object Visit(UnaryOperatorExpression unaryOperatorExpression, object data)
		{
			B.Expression expr = ConvertExpression(unaryOperatorExpression.Expression);
			if (unaryOperatorExpression.Op == UnaryOperatorType.Plus)
				return expr;
			B.UnaryOperatorType op = ConvertOperator(unaryOperatorExpression.Op);
			if (op == B.UnaryOperatorType.None) {
				AddError(unaryOperatorExpression, "Unknown operator.");
				return null;
			}
			return new B.UnaryExpression(GetLexicalInfo(unaryOperatorExpression), op, expr);
		}
		
		public object Visit(ParenthesizedExpression parenthesizedExpression, object data)
		{
			return ConvertExpression(parenthesizedExpression.Expression);
		}
		
		public object Visit(InvocationExpression ie, object data)
		{
			if (ie.TypeArguments != null && ie.TypeArguments.Count > 0) {
				AddError(ie, "Generic method calls are not supported.");
			}
			B.Expression e = ConvertExpression(ie.TargetObject);
			if (e == null)
				return null;
			if (settings.IsVisualBasic && ie.TargetObject is IdentifierExpression && currentStatement != null) {
				VariableResolver resolver = new VariableResolver(nameComparer);
				TypeReference typeRef = resolver.FindType((ie.TargetObject as IdentifierExpression).Identifier, currentStatement);
				if (typeRef != null && typeRef.IsArrayType) {
					// Visual Basic: indexer expression
					B.SlicingExpression s = new B.SlicingExpression(GetLexicalInfo(ie));
					s.Target = e;
					foreach (Expression expr in ie.Arguments) {
						s.Indices.Add(new B.Slice(ConvertExpression(expr)));
					}
					return s;
				}
			}
			B.MethodInvocationExpression r = new B.MethodInvocationExpression(GetLexicalInfo(ie), e);
			foreach (Expression expr in ie.Arguments) {
				e = ConvertExpression(expr);
				if (e != null) {
					r.Arguments.Add(e);
				}
			}
			return r;
		}
		
		public object Visit(ObjectCreateExpression objectCreateExpression, object data)
		{
			TypeReference t = objectCreateExpression.CreateType;
			if (t.IsArrayType) {
				throw new ApplicationException("ObjectCreateExpression cannot be called with an ArrayType");
			}
			// HACK: Tricking out event handlers
			if (t.SystemType.EndsWith("EventHandler") && objectCreateExpression.Parameters.Count == 1)
				return ConvertExpression((Expression)objectCreateExpression.Parameters[0]);
			
			B.MethodInvocationExpression mie = new B.MethodInvocationExpression(GetLexicalInfo(objectCreateExpression), MakeReferenceExpression(t));
			ConvertExpressions(objectCreateExpression.Parameters, mie.Arguments);
			return mie;
		}
		
		public object Visit(TypeReferenceExpression typeReferenceExpression, object data)
		{
			return MakeReferenceExpression(typeReferenceExpression.TypeReference);
		}
		
		public object Visit(SizeOfExpression sizeOfExpression, object data)
		{
			AddError(sizeOfExpression, "sizeof is not supported.");
			return null;
		}
		
		public object Visit(DefaultValueExpression defaultValueExpression, object data)
		{
			AddError(defaultValueExpression, "default() is not supported.");
			return null;
		}
		
		public object Visit(TypeOfExpression typeOfExpression, object data)
		{
			return new B.TypeofExpression(GetLexicalInfo(typeOfExpression), ConvertTypeReference(typeOfExpression.TypeReference));
		}
		
		public object Visit(TypeOfIsExpression typeOfIsExpression, object data)
		{
			return new B.BinaryExpression(GetLexicalInfo(typeOfIsExpression), B.BinaryOperatorType.TypeTest,
			                              ConvertExpression(typeOfIsExpression.Expression),
			                              MakeReferenceExpression(typeOfIsExpression.TypeReference));
		}
		
		public object Visit(AddressOfExpression addressOfExpression, object data)
		{
			// Boo can reference methods directly
			return ConvertExpression(addressOfExpression.Expression);
		}
		
		public object Visit(PointerReferenceExpression pointerReferenceExpression, object data)
		{
			AddError(pointerReferenceExpression, "Pointers are not supported.");
			return null;
		}
		
		public object Visit(CastExpression castExpression, object data)
		{
			switch (castExpression.CastType) {
				case CastType.Cast:
				case CastType.Conversion:
				case CastType.PrimitiveConversion:
					return new B.CastExpression(GetLexicalInfo(castExpression),
					                            ConvertExpression(castExpression.Expression),
					                            ConvertTypeReference(castExpression.CastTo));
				case CastType.TryCast:
					return new B.TryCastExpression(GetLexicalInfo(castExpression),
					                               ConvertExpression(castExpression.Expression),
					                               ConvertTypeReference(castExpression.CastTo));
				default:
					AddError(castExpression, "Unknown cast: " + castExpression);
					return null;
			}
		}
		
		public object Visit(StackAllocExpression stackAllocExpression, object data)
		{
			AddError(stackAllocExpression, "StackAlloc is not supported.");
			return null;
		}
		
		public object Visit(ThisReferenceExpression thisReferenceExpression, object data)
		{
			return new B.SelfLiteralExpression(GetLexicalInfo(thisReferenceExpression));
		}
		
		public object Visit(BaseReferenceExpression baseReferenceExpression, object data)
		{
			return new B.SuperLiteralExpression(GetLexicalInfo(baseReferenceExpression));
		}
		
		public object Visit(DirectionExpression directionExpression, object data)
		{
			// boo does not need to specify the direction when calling out/ref methods
			return ConvertExpression(directionExpression.Expression);
		}
		
		public object Visit(ArrayCreateExpression arrayCreateExpression, object data)
		{
			if (!arrayCreateExpression.ArrayInitializer.IsNull) {
				B.ArrayLiteralExpression ale = ConvertArrayLiteralExpression(arrayCreateExpression.ArrayInitializer);
				ale.Type = (B.ArrayTypeReference)ConvertTypeReference(arrayCreateExpression.CreateType);
				return ale;
			}
			string builtInName = (arrayCreateExpression.Arguments.Count > 1) ? "matrix" : "array";
			B.MethodInvocationExpression mie = new B.MethodInvocationExpression(GetLexicalInfo(arrayCreateExpression),
			                                                                    MakeReferenceExpression(builtInName));
			TypeReference elementType = arrayCreateExpression.CreateType.Clone();
			int[] newRank = new int[elementType.RankSpecifier.Length - 1];
			for (int i = 0; i < newRank.Length; i++)
				newRank[i] = elementType.RankSpecifier[i + 1];
			elementType.RankSpecifier = newRank;
			mie.Arguments.Add(MakeReferenceExpression(elementType));
			if (arrayCreateExpression.Arguments.Count == 1) {
				mie.Arguments.Add(ConvertExpression(arrayCreateExpression.Arguments[0]));
			} else {
				B.ArrayLiteralExpression dims = new B.ArrayLiteralExpression(GetLexicalInfo(arrayCreateExpression));
				ConvertExpressions(arrayCreateExpression.Arguments, dims.Items);
				mie.Arguments.Add(dims);
			}
			return mie;
		}
		
		public object Visit(ArrayInitializerExpression aie, object data)
		{
			return ConvertArrayLiteralExpression(aie);
		}
		
		B.ArrayLiteralExpression ConvertArrayLiteralExpression(ArrayInitializerExpression aie)
		{
			B.ArrayLiteralExpression dims = new B.ArrayLiteralExpression(GetLexicalInfo(aie));
			ConvertExpressions(aie.CreateExpressions, dims.Items);
			return dims;
		}
		
		public object Visit(IndexerExpression indexerExpression, object data)
		{
			B.SlicingExpression s = new B.SlicingExpression(GetLexicalInfo(indexerExpression));
			s.Target = ConvertExpression(indexerExpression.TargetObject);
			foreach (Expression expr in indexerExpression.Indices) {
				s.Indices.Add(new B.Slice(ConvertExpression(expr)));
			}
			return s;
		}
		
		public object Visit(AnonymousMethodExpression anonymousMethodExpression, object data)
		{
			B.CallableBlockExpression cbe = new B.CallableBlockExpression(GetLexicalInfo(anonymousMethodExpression));
			cbe.EndSourceLocation = GetLocation(anonymousMethodExpression.EndLocation);
			cbe.Body = ConvertBlock(anonymousMethodExpression.Body);
			ConvertParameters(anonymousMethodExpression.Parameters, cbe.Parameters);
			return cbe;
		}
		
		public object Visit(ConditionalExpression conditionalExpression, object data)
		{
			B.ConditionalExpression te = new B.ConditionalExpression(GetLexicalInfo(conditionalExpression));
			te.Condition = ConvertExpression(conditionalExpression.Condition);
			te.TrueValue = ConvertExpression(conditionalExpression.TrueExpression);
			te.FalseValue = ConvertExpression(conditionalExpression.FalseExpression);
			return te;
		}
		
		public object Visit(CheckedExpression checkedExpression, object data)
		{
			AddError(checkedExpression, "Using 'checked' inside an expression is not supported by boo, " +
			         "use the checked {} block instead.");
			return MakeMethodCall("checked", ConvertExpression(checkedExpression.Expression));
		}
		
		public object Visit(UncheckedExpression uncheckedExpression, object data)
		{
			AddError(uncheckedExpression, "Using 'unchecked' inside an expression is not supported by boo, " +
			         "use the unchecked {} block instead.");
			return MakeMethodCall("unchecked", ConvertExpression(uncheckedExpression.Expression));
		}
	}
}

⌨️ 快捷键说明

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