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

📄 symboltables.cs

📁 C#编写的c#编译器
💻 CS
📖 第 1 页 / 共 2 页
字号:
		}	
		#endregion	
		#region SourceGraph  
		private IDeclaration p_graph;
		/// <summary>
		/// Link to the defining element.
		/// </summary>
		public IDeclaration				SourceGraph					
		{
			get
			{
				return p_graph;
			}
			set
			{
				p_graph = value;
			}
		}	
		#endregion	
		#region  GetHashCode 
		/// <summary>
		/// Gets the hash of this defintion. The hash is made from the full path.
		/// </summary>
		public override int	GetHashCode()					
		{
			return this.Name.GetHashCode();
		}	
		#endregion	

		#region  ToString 
		public override string	ToString()					
		{
			if(Scope == null) return "";
			string s = "";
			// Some defs may not have been attrib'ed at this point 
			// (eg. we don't dig into mscorlib etc), so 'try'
			try
			{
			if(Scope.EnclosingScope is INamedScope)
				s += ((INamedScope)Scope.EnclosingScope).Definition.FullName + ".";
			}
			catch(NullReferenceException){}
			try
			{
				if(!(SourceGraph is IScope) && Scope is INamedScope)
					s += ((INamedScope)Scope).Definition.Name + ".";
			}
			catch(NullReferenceException){}
			s += Name + " #" + Id;
			return s;
		}	
		#endregion	

	}
	#endregion	

	#region  ISymbol
	public interface ISymbol : IDefinition
	{
		IDefinition DeclaredType{get;set;}	
		IDefinition ActualType{get;set;}	
	}
	#endregion	
	#region  Symbol
	public class Symbol : Definition, ISymbol
	{
		#region  DeclaredType 
		private IDefinition p_declType; 
		/// <summary>
		/// Gets or sets the declared type of the symbol.
		/// </summary>
		public IDefinition				DeclaredType					
		{
			get
			{
				return p_declType;
			}
			set
			{
				p_declType = value;
			}
		}	
		#endregion	
		#region  ActualType 
		private IDefinition p_actualType; 
		/// <summary>
		/// Gets or sets the declared type of the symbol.
		/// </summary>
		public IDefinition				ActualType					
		{
			get
			{
				return p_actualType;
			}
			set
			{
				p_actualType = value;
			}
		}	
		#endregion		
	}
	#endregion	

	#region  OverloadableDefinition 
	/// <summary>
	/// Base class for Definitions. Definitions may be variables, or scopes that have names.
	/// </summary>
	public class OverloadableDefinition : Definition
	{
		#region  Definitions  
		private DefinitionCollection p_defs = new DefinitionCollection();
		/// <summary>
		/// Gets the collection of Definitions that share the same name but use different arguments in the same scope (eg that are overloaded).
		/// </summary>
		public DefinitionCollection			Definitions					
		{
			get
			{
				return p_defs;
			}
		}	
		#endregion	
	}
	#endregion	
	#region  BuiltInDefinition 
	/// <summary>
	/// Base class for Definitions. Definitions may be variables, or scopes that have names.
	/// </summary>
	public class BuiltInDefinition : Definition
	{
		#region  Name  
		/// <summary>
		/// Gets or sets the name of the built in type.
		/// </summary>
		public override string				Name					
		{
			get
			{
				return p_literalType.ToString().ToLower();
			}
			set
			{
				p_literalType = (LiteralType)Enum.Parse(LiteralType.GetType(), value, true);
			}
		}	
		#endregion
		#region  LiteralType  
		private LiteralType p_literalType;
		/// <summary>
		/// Gets or sets the literal type of this built in type.
		/// </summary>
		public LiteralType				LiteralType					
		{
			get
			{
				return p_literalType;
			}
			set
			{
				p_literalType = value;
			}
		}	
		#endregion		
		#region Built in IDeclarations
		public static StructDecl SbyteDecl = new StructDecl();
		#endregion
		#region widening auto conversion arrays
		private LiteralType[] widenSbyte = new LiteralType[]
			{
				LiteralType.Sbyte,
				LiteralType.Short,
				LiteralType.Int, 
				LiteralType.Long,
				LiteralType.Float,
				LiteralType.Double,
				LiteralType.Decimal
			};
		private LiteralType[] widenByte = new LiteralType[]
			{
				LiteralType.Byte,
				LiteralType.Short,
				LiteralType.Ushort,
				LiteralType.Int, 
				LiteralType.Uint, 
				LiteralType.Long,
				LiteralType.Ulong,
				LiteralType.Float,
				LiteralType.Double,
				LiteralType.Decimal
			};
		private LiteralType[] widenShort = new LiteralType[]
			{
				LiteralType.Short,
				LiteralType.Int, 
				LiteralType.Long,
				LiteralType.Float,
				LiteralType.Double,
				LiteralType.Decimal
			};
		private LiteralType[] widenUshort = new LiteralType[]
			{
				LiteralType.Ushort,
				LiteralType.Int, 
				LiteralType.Uint, 
				LiteralType.Long,
				LiteralType.Ulong,
				LiteralType.Float,
				LiteralType.Double,
				LiteralType.Decimal
			};
		private LiteralType[] widenInt = new LiteralType[]
			{
				LiteralType.Int, 
				LiteralType.Long,
				LiteralType.Float,
				LiteralType.Double,
				LiteralType.Decimal
			};
		private LiteralType[] widenUint = new LiteralType[]
			{
				LiteralType.Uint, 
				LiteralType.Long,
				LiteralType.Ulong,
				LiteralType.Float,
				LiteralType.Double,
				LiteralType.Decimal
			};
		private LiteralType[] widenLong = new LiteralType[]
			{
				LiteralType.Long,
				LiteralType.Float,
				LiteralType.Double,
				LiteralType.Decimal
			};
		private LiteralType[] widenUlong = new LiteralType[]
			{
				LiteralType.Ulong,
				LiteralType.Float,
				LiteralType.Double,
				LiteralType.Decimal
			};
		private LiteralType[] widenFloat = new LiteralType[]
			{
				LiteralType.Float,
				LiteralType.Double
			};
		private LiteralType[] widenChar = new LiteralType[]
			{
				LiteralType.Char,
				LiteralType.Ushort,
				LiteralType.Int, 
				LiteralType.Uint, 
				LiteralType.Long,
				LiteralType.Ulong,
				LiteralType.Float,
				LiteralType.Double,
				LiteralType.Decimal
			};
		#endregion
		#region ConvertDistance
		public int ConvertDistance(LiteralType destinationType)
		{
			LiteralType[] convArray;
			switch(LiteralType)
			{
				case LiteralType.Sbyte :
				{
					convArray = widenSbyte;
					break;
				}
				case LiteralType.Byte :
				{
					convArray = widenByte;
					break;
				}
				case LiteralType.Short :
				{
					convArray = widenShort;
					break;
				}	
				case LiteralType.Ushort :
				{
					convArray = widenUshort;
					break;
				}	
				case LiteralType.Int :
				{
					convArray = widenInt;
					break;
				}	
				case LiteralType.Uint :
				{
					convArray = widenUint;
					break;
				}	
				case LiteralType.Long :
				{
					convArray = widenLong;
					break;
				}	
				case LiteralType.Ulong :
				{
					convArray = widenUlong;
					break;
				}	
				case LiteralType.Float :
				{
					convArray = widenFloat;
					break;
				}	
				case LiteralType.Char :
				{
					convArray = widenChar;
					break;
				}	
				default :
				{
					convArray = new LiteralType[0];
					break;
				}
			}
			int dist = Int32.MaxValue;
			for(int i = 0; i < convArray.Length; i++)
			{
				if(convArray[i] == destinationType)
				{
					dist = i;
					break;
				}
			}
			return dist;
		}
		#endregion
	}
	#endregion	

	#region ExpressionRoot
	public class ExpressionRoot
	{
		public Expression Expression;
		public Type Instance;
		public string PropertyName;

		public void ReplaceExpression(Expression ex)
		{
			if(Instance == null) return;       
			Type instType = Instance.GetType();
			PropertyInfo pi = instType.GetProperty(PropertyName);

			pi.SetValue(Instance, ex, null);
		}

	}
	#endregion
	
	#region LiteralType
	/// <summary>
	/// An enumeration of the built in types, from 'lowest' to 'highest'.
	/// </summary>
	public enum LiteralType
	{
		Void,
		Null,
		Bool,
		Sbyte,
		Byte,
		Short,
		Ushort,
		Char,
		Int,
		Uint,
		Long,
		Ulong,
		Float,
		Double,
		Decimal,
		String,	
		Object,
	}
	#endregion
}

⌨️ 快捷键说明

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