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

📄 csharpast.cs

📁 C#编写的c#编译器
💻 CS
📖 第 1 页 / 共 5 页
字号:
		{	
			Dom.MethodDecl graph = new Dom.MethodDecl();
			return this.GetGraph(graph);
		}	
		public Dom.MethodDecl	
			GetGraph(Dom.MethodDecl graph)
		{		
			// Parameters, ReturnType, Statements, CustomAttributes
			//graph.Clear();
			this.OpenScope(graph);
			base.GetGraph(graph);

			AST tok = this.getFirstChild();
			while(tok != null)
			{
				if(tok is DeclArgs) 
					((DeclArgs)tok).GetGraph(graph.Parameters);
				else if(tok is TypeRef)
				{
					graph.ReturnType = (Dom.TypeRef)((TypeRef)tok).GetGraph();
				}
				else if(tok is QualIdent) 					
					graph.Name = QualIdent.GetFullName((QualIdent)tok);
				// in interfaces the name is an Ident
				else if(tok is Ident) 
					graph.Name = tok.getText();
				else if(tok is Statements)
					((Statements)tok).GetGraph(graph.Statements);

				tok = tok.getNextSibling();
			}
			this.AddDefinition(graph, graph.Name, graph.ReturnType);
			this.CloseScope();
			return graph;
		}
	}
    	#endregion 
		#region PropertyNode *
	public class						PropertyNode : TypeMemberNode
	{
		public override Dom.IGraph			GetGraph()				
		{	
			Dom.PropertyDecl graph = new Dom.PropertyDecl();
			return this.GetGraph(graph);
		}	
		public Dom.PropertyDecl	
			GetGraph(Dom.PropertyDecl graph)
		{		
			// GetAccessor, SetAccessor, Type
			//graph.Clear();
			base.GetGraph(graph);

			AST tok = this.getFirstChild();
			while(tok != null)
			{
				if(tok is TypeRef)
				{
					graph.Type = (Dom.TypeRef)((TypeRef)tok).GetGraph();
				}
				else if(tok is QualIdent) 
					graph.Name = QualIdent.GetFullName((QualIdent)tok);
				// in interfaces the name is an Ident
				else if(tok is Ident) 
					graph.Name = tok.getText();
				else if(tok is AccessorNode)
				{
					if(tok.getText() == "get")
					{
						//if(hasGet) // error;
						graph.HasGet = true;
						 ((AccessorNode)tok).GetGraph(graph.GetAccessor);
					}
					else if(tok.getText() == "set")
					{
						//if(hasSet) // error;
						graph.HasSet = true;
						((AccessorNode)tok).GetGraph(graph.SetAccessor);
					}
				}
				tok = tok.getNextSibling();
			}
			AddDefinition(graph, graph.Name, graph.Type);
			return graph;
		}

	}
		#endregion 
		#region EventNode *
	public class						EventNode : TypeMemberNode
	{
		public override Dom.IGraph			GetGraph()				
		{	
			Dom.EventDecl graph = new Dom.EventDecl();
			return this.GetGraph(graph);
		}	
		public Dom.EventDecl	GetGraph(Dom.EventDecl graph)
		{			
			base.GetGraph(graph);

			AST tok = this.getFirstChild();
			while(tok != null)
			{
				if(tok is TypeRef)
				{
					graph.Type = (Dom.TypeRef)((TypeRef)tok).GetGraph();
				}
				else if(tok is Declarator) 
				{
					((Declarator)tok).GetGraph(graph.Delcarators);
				}
				// note: name may also come from qualIdent in case of accessor
				// because the event name may be prepended with an interface name. 
				else if(tok is QualIdent)
				{
					graph.UsesAccessors = true;
					Dom.Declarator dd = new Dom.Declarator();
					dd.Name = QualIdent.GetFullName((QualIdent)tok); 
					graph.Delcarators.Add(dd);
				}
				else if(tok is AccessorNode)
				{
					if(tok.getText() == "add")
					{
						//if(hasAdd) // error;
						graph.UsesAccessors = true;
						graph.AddAccessor = (Dom.AccessorDecl)((AccessorNode)tok).GetGraph();
					}
					else if(tok.getText() == "remove")
					{
						//if(hasRemove) // error;
						graph.UsesAccessors = true;
						graph.RemoveAccessor = (Dom.AccessorDecl)((AccessorNode)tok).GetGraph();
					}
				}
				else if(tok is Expression)
				{
					// temp: taking out InitExpression, just in case it wasn't an error..
					throw new Exception("csharpAst event - cant have expr");
				}
				tok = tok.getNextSibling();
			}
			foreach(Dom.Declarator d in graph.Delcarators)
			{
				d.Definition.Type = graph.Type;
			}
			return graph;
		}	
	}
		#endregion 
		#region IndexerNode *
	public class						IndexerNode : TypeMemberNode
	{
		public override Dom.IGraph			GetGraph()				
		{	
			Dom.IndexerDecl graph = new Dom.IndexerDecl();
			return this.GetGraph(graph);
		}	
		public Dom.IndexerDecl	
			GetGraph(Dom.IndexerDecl graph)
		{		
			// GetAccessor, SetAccessor, Type, Name, Params
			//graph.Clear();
			this.OpenScope(graph);
			base.GetGraph(graph);

			AST tok = this.getFirstChild();
			while(tok != null)
			{
				if(tok is TypeRef)
				{
					graph.Type = (Dom.TypeRef)((TypeRef)tok).GetGraph();
				}
				if(tok is Ident)
				{
					// can't be array
					graph.InterfaceType = new Dom.TypeRef();
					graph.InterfaceType.TypeName = tok.getText();
				}
				else if(tok is DeclArgs) 
					 ((DeclArgs)tok).GetGraph(graph.Parameters);
				else if(tok is AccessorNode)
				{
					if(tok.getText() == "get")
					{
						//if(hasGet) // error;
						graph.HasGet = true;
						graph.GetAccessor = (Dom.AccessorDecl)((AccessorNode)tok).GetGraph();
					}
					else if(tok.getText() == "set")
					{
						//if(hasSet) // error;
						graph.HasSet = true;
						graph.SetAccessor = (Dom.AccessorDecl)((AccessorNode)tok).GetGraph();
					}
				}
				tok = tok.getNextSibling();
			}
			this.AddDefinition(graph, graph.Name, graph.Type);
			this.CloseScope();
			return graph;
		}

	}
		#endregion 
		#region OperatorNode *
	public class						OperatorNode : TypeMemberNode
	{
		public override Dom.IGraph			GetGraph()				
		{	
			Dom.OperatorDecl graph = 
				new Dom.OperatorDecl();
			return this.GetGraph(graph);
		}	
		public Dom.OperatorDecl	
			GetGraph(Dom.OperatorDecl graph)
		{		
			// FirstParameter, SecondParameter, Type, Statements
			//graph.Clear();
			this.OpenScope(graph);
			base.GetGraph(graph);

			AST tok = this.getFirstChild();
			while(tok != null)
			{
				if(tok is DeclArgs)
				{
					DeclArgs atok = ((DeclArgs)tok);
					// must have one arg
					DeclArg arg1 = (DeclArg)(atok.getFirstChild());
					// may have max of two - might be null? (todo: catch null exception)
					AST arg2 = arg1.getNextSibling();
					graph.FirstParameter = (Dom.ParamDecl)arg1.GetGraph();
					if(arg2 != null) graph.SecondParameter = 
										 (Dom.ParamDecl)((DeclArg)arg2).GetGraph();
				}
				else if(tok is TypeRef)
				{
					graph.Type = (Dom.TypeRef)((TypeRef)tok).GetGraph();
				}
				else if(tok is Op)
				{
					string opType = ((Op)tok).getText();
					string op = tok.getFirstChild().getText();
					// test for unary minus and plus
					if(opType == "unary" && (op == "-" || op == "+") )
					{
						op += "u";
					}
					graph.Operator = Dom.OperatorDecl.OperatorFromString(op);
				}
				else if(tok is Statements)
					((Statements)tok).GetGraph(graph.Statements);

				tok = tok.getNextSibling();
			}
			this.AddDefinition(graph, graph.Name, graph.Type);
			this.CloseScope();
			return graph;
		}
	}
		#endregion 
		#region ConstructorNode *
	public class						ConstructorNode : TypeMemberNode
	{
		public override Dom.IGraph			GetGraph()				
		{	
			Dom.ConstructorDecl graph = 
				new Dom.ConstructorDecl();
			return this.GetGraph(graph);
		}	
		public Dom.ConstructorDecl	
			GetGraph(Dom.ConstructorDecl graph)
		{		
			// Parameters, InitExpression, Statements
			//graph.Clear();
			this.OpenScope(graph);
			base.GetGraph(graph);

			AST tok = this.getFirstChild();
			while(tok != null)
			{
				if(tok is DeclArgs) 
					((DeclArgs)tok).GetGraph(graph.Parameters);
				else if(tok is Ident) 
					graph.Name = tok.getText();
				else if(tok is BaseRefExpr)
				{
					graph.InvokeBase = true;
					AST ctok = tok.getFirstChild();
					if(ctok != null)
						((Args)(ctok)).GetGraph(graph.BaseParameters);
				}
				else if(tok is ThisRefExpr)
				{
					graph.InvokeChain = true;
					AST ctok = tok.getFirstChild();
					if(ctok != null)
						((Args)(ctok)).GetGraph(graph.ChainParameters);
				}
				else if(tok is Statements)
					((Statements)tok).GetGraph(graph.Statements);

				tok = tok.getNextSibling();
			}
			this.AddDefinition(graph, graph.Name, this.curType); 
			this.CloseScope();
			return graph;
		}

	}
		#endregion 
		#region DestructorNode 
	public class						DestructorNode : TypeMemberNode
	{
		public override Dom.IGraph			GetGraph()				
		{	
			Dom.DestructorDecl graph = 
				new Dom.DestructorDecl();
			return this.GetGraph(graph);
		}	
		public Dom.DestructorDecl	
			GetGraph(Dom.DestructorDecl graph)
		{		
			// Statements
			//graph.Clear();
			this.OpenScope(graph);
			base.GetGraph(graph);

			AST tok = this.getFirstChild();
			while(tok != null)
			{
				if(tok is Statements)
					((Statements)tok).GetGraph(graph.Statements);
				else if(tok is Ident) 
					graph.Name = tok.getText();

				tok = tok.getNextSibling();
			}
			this.AddDefinition(graph, graph.Name, null);
			this.CloseScope();
			return graph;
		}
	}
		#endregion 
		#region AccessorNode *
	public class						AccessorNode : TypeMemberNode
	{
		public override Dom.IGraph			GetGraph()				
		{	
			Dom.AccessorDecl graph = new Dom.AccessorDecl();
			return this.GetGraph(graph);
		}	
		public Dom.AccessorDecl	GetGraph(Dom.AccessorDecl graph)
		{		
			// Attributes, CustomAttributes, Statements
			//graph.Clear();
			this.OpenScope(graph);
			base.GetGraph(graph);

			AST tok = this.getFirstChild();
			while(tok != null)
			{
				if(tok is Statements)
				{
					((Statements)tok).GetGraph(graph.Statements);
				}
				tok = tok.getNextSibling();
			}
			string tp = this.getText();
			if(tp != "")
			{
				if(tp == "get") 
					graph.AccessorModifier = Dom.AccessorModifiers.Get;
				else if(tp == "set") 
					graph.AccessorModifier = Dom.AccessorModifiers.Set;
				else if(tp == "add") 
					graph.AccessorModifier = Dom.AccessorModifiers.Add;
				else if(tp == "remove") 
					graph.AccessorModifier = Dom.AccessorModifiers.Remove;
				else return graph; // don't change if no match (shouldn't be...)
			}
			this.CloseScope();
			return graph;
		}
	}
	#endregion 
		#region EnumMemberNode 
	public class		EnumMemberNode : TypeMemberNode
	{	
		public override Dom.IGraph			GetGraph()				
		{	
			Dom.EnumMemberDecl graph = new Dom.EnumMemberDecl();
			return this.GetGraph(graph);
		}	
		public Dom.EnumMemberDecl	GetGraph(Dom.EnumMemberDecl graph)
		{		
			// Attributes, CustomAttributes
			//graph.Clear();
			base.GetGraph(graph);

			AST tok = this.getFirstChild();
			while(tok != null)
			{
				if(tok is Ident) 
					graph.Name = tok.getText();
				else if(tok is Expression)
				{
					graph.Value = Expression.Parse(((Expression)tok));
				}		

				tok = tok.getNextSibling();
			}
			AddDefinition(graph, graph.Name, this.curType); // no new scope so just add def
			return graph;
		}
	}
	#endregion 

	#region BaseTypes 
	public class				BaseTypes : CSharpAST
	{
		// holder class
		public override Dom.IGraph		GetGraph()				
		{	
			Dom.TypeRefCollection graph = 
				new Dom.TypeRefCollection();
			return this.GetGraph(graph);
		}	
		public Dom.TypeRefCollection	
			GetGraph(Dom.TypeRefCollection graph)
		{	
			base.GetGraph(graph);
			AST tok = this.getFirstChild();
			while(tok != null)
			{			
				if(tok is TypeRef) 
					graph.Add( (Dom.TypeRef)((UsingNode)tok).GetGraph() );
				else if(tok.getText() != "") 
				{
					// can't be array...
					Dom.TypeRef tr = new Dom.TypeRef();
					tr.TypeName = tok.getText();
					graph.Add(tr);
					this.AddSymbol(tr, tr.TypeName);
				}

				tok = tok.getNextSibling();
			}
			return graph;

		}

	}
	#endregion 

	#region	Statements *
	public class				Statements : CSharpAST
	{
		public override Dom.IGraph			GetGraph()				
		{	
			Dom.StatementCollection graph = 
				new Dom.StatementCollection();
			return this.GetGraph(graph);
		}	
		public Dom.StatementCollection	
			GetGraph(Dom.StatementCollection graph)
		{		
			base.GetGraph(graph);

			AST tok = this.getFirstChild();
			while(tok != null)
			{
				Dom.Statement stmt = Statement.ParseStatement( ((Statement)tok) );
				if(stmt != null)graph.Add(stmt);

				tok = tok.getNextSibling();
			}
			return graph;
		}
	}
	#endregion 
	#region	Statement 
	public class				Statement : CSharpAST
	{
		public override Dom.IGraph	GetGraph()
		{
			throw new MethodAccessException("Code should never call GetGraph() on Statement");
		}
		// TODO: this class will also be abstract.
		protected virtual Dom.IGraph	GetGraph(Dom.IGraph graph)
		{
			base.GetGraph(graph);
			return null;
		}

		public static Dom.Statement ParseStatement(Statement tok)
		{
			if(tok == null) return null;
			Dom.Statement stmt = null;

			if(tok is LabeledStmt)
				stmt = (Dom.LabeledStmt)((LabeledStmt)tok).GetGraph() ;
			else if(tok is ExprStmt)
				stmt = (Dom.ExprStmt)((ExprStmt)tok).GetGraph() ;
			else if(tok is VariableDeclStmt)
				stmt = (Dom.VariableDeclStmt)((VariableDeclStmt)tok).GetGraph() ;
			else if(tok is ConstantDeclStmt)
				stmt = (Dom.ConstantDeclStmt)((ConstantDeclStmt)tok).GetGraph() ;
			else if(tok is IfStmt)
				stmt = (Dom.IfStmt)((IfStmt)tok).GetGraph() ;
			else if(tok is SwitchStmt)
				stmt = (Dom.SwitchStmt)((SwitchStmt)tok).GetGraph() ;
			else if(tok is IterationStmt)
				stmt = (Dom.IterationStmt)((IterationStmt)tok).GetGraph() ;
			else if(tok is ForEachStmt)
				stmt = (Dom.ForEachStmt)((ForEachStmt)tok).GetGraph() ;
			else if(tok is GotoStmt)
				stmt = (Dom.GotoStmt)((GotoStmt)tok).GetGraph() ;
			else if(tok is ReturnStmt)
				stmt = (Dom.ReturnStmt)((ReturnStmt)tok).GetGraph() ;
			else if(tok is BreakStmt)
				stmt = (Dom.BreakStmt)((BreakStmt)tok).GetGraph() ;
			else if(tok is ContinueStmt)
				stmt = (Dom.ContinueStmt)((ContinueStmt)tok).GetGraph() ;
			else if(tok is CheckedStmt)

⌨️ 快捷键说明

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