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

📄 csharpgen.cs

📁 C#编写的c#编译器
💻 CS
📖 第 1 页 / 共 3 页
字号:
			}
			sb.Write("]");

			Parse(gr.RankSpecifiers);
			Parse(gr.Initializer);
		}
		#endregion
		#region ObjectCreateExpr
		public override void ParseElement(ObjectCreateExpr gr)
		{
			sb.Write("new ");
			sb.Write(gr.CreateType.TypeName);
			sb.Write("(");
			if(gr.Parameters != null)
			{
				string comma = "";
				foreach(Param o in gr.Parameters)
				{
					sb.Write(comma);
					Parse(o);
					comma = ", ";
				}
			}
			sb.Write(")");
		}
		#endregion
		#region CreateDelegateExpr
		public override void ParseElement(CreateDelegateExpr gr)
		{
			sb.Write("new ");
			if(gr.Target != null)
			{
				Parse(gr.Target);
				sb.Write(".");
			}
			sb.Write(gr.Type.TypeName);
			sb.Write("(");
			sb.Write(gr.MethodName);
			sb.Write(")");
		}
		#endregion

		#region BooleanLiteral
		public override void ParseElement(BooleanLiteral gr)
		{
			if(gr.Value == true)
				sb.Write("true");
			else
				sb.Write("false");
		}
		#endregion
		#region CharLiteral
		public override void ParseElement(CharLiteral gr)
		{
			sb.Write("'" + gr.Value + "'");
		}
		#endregion
		#region IntegerLiteral
		public override void ParseElement(IntegerLiteral gr)
		{
			sb.Write(gr.Value);
		}
		#endregion
		#region NullLiteral
		public override void ParseElement(NullLiteral gr)
		{
			sb.Write("null");
		}
		#endregion
		#region RealLiteral
		public override void ParseElement(RealLiteral gr)
		{
			sb.Write(gr.Value + "f");
		}
		#endregion
		#region StringLiteral
		public override void ParseElement(StringLiteral gr)
		{
			sb.Write("\"" + gr.Value + "\"");
		}
		#endregion

		#region ArrayInitializer
		public override void ParseElement(ArrayInitializer gr)
		{
			if(gr == null || gr.InitialValues.Count == 0) return;
			sb.Write("{");
			string comma = "";
			for(int i = 0; i < gr.InitialValues.Count; i++)
			{
				sb.Write(comma);
				Parse(gr.InitialValues[i]);
				comma = ", ";
			} 
			sb.Write("}");
		}
		#endregion
		#region CustomAttribute
		public override void ParseElement(CustomAttribute gr)
		{
			sb.Write("[");
			if(gr.AttributeTarget != AttributeTarget.Empty)
			{
				sb.Write(CustomAttribute.StringFromAttributeTarget(gr.AttributeTarget));
				sb.Write(": ");
			}
			sb.Write(gr.Name);
			if(gr.Parameters.Count != 0)
			{
				sb.Write("(");
				string comma = "";
				foreach(Expression o in gr.Parameters)
				{
					sb.Write(comma);
					Parse(o);
					comma = ", ";
				}
				sb.Write(")");
			}			
			sb.WriteLine("]");
		}
		#endregion
		#region TypeRef
		public override void ParseElement(TypeRef gr)
		{
			sb.Write(gr.TypeName);
			Parse(gr.ArrayRanks);
			if(addSpace)
				sb.Write(" ");
		}
		#endregion
		#region BuiltInType
		public override void ParseElement(BuiltInType gr)
		{
			sb.Write(gr.TypeName);
			if(addSpace)
				sb.Write(" ");
		}
		#endregion
		#region ParamDecl
		public override void ParseElement(ParamDecl gr)
		{
			if(gr.Direction == ParamDirection.Out) sb.Write("out ");
			else if(gr.Direction == ParamDirection.Ref) sb.Write("ref ");
			else if(gr.IsParams) sb.Write("params ");
			Parse(gr.Type);
			sb.Write(gr.Name);
		}
		#endregion
		#region Param
		public override void ParseElement(Param gr)
		{
			if(gr.Direction == ParamDirection.Out) sb.Write("out ");
			else if(gr.Direction == ParamDirection.Ref) sb.Write("ref ");
			Parse(gr.Value);
		}
		#endregion
		#region VariableDecl : TODO
		public override void ParseElement(VariableDecl gr)
		{
		}
		#endregion
		#region LinePragma
		public override void ParseElement(LinePragma gr)
		{
		}
		#endregion
		#region Comment
		public override void ParseElement(Comment gr)
		{
		}
		#endregion
		#region AssemblyReference
		public override void ParseElement(AssemblyReference gr)
		{
		}
		#endregion
		#region RankSpecifier
		public override void ParseElement(RankSpecifier gr)
		{
			sb.Write("[");
			for(int i = 1; i < gr.Dimensions; i++)
			{
				sb.Write(",");
			}
			sb.Write("]");
		}
		#endregion
		#region Declarator
		public override void ParseElement(Declarator gr)
		{
			sb.Write(gr.Name);
			if(gr.InitExpression != null)
			{
				sb.Write(" = ");
				Parse(gr.InitExpression);
			}
		}
		#endregion

		#region CompileUnitCollection
		public override void ParseElement(CompileUnitCollection gr)
		{
			if(gr == null) return;
		}
		#endregion
		#region ImportCollection
		public override void ParseElement(ImportCollection gr)
		{
			if(gr == null) return;
			foreach(Import o in gr)
			{
				ParseElement(o);
			}
		}
		#endregion
		#region NamespaceDeclCollection
		public override void ParseElement(NamespaceDeclCollection gr)
		{
			if(gr == null) return;
			foreach(NamespaceDecl o in gr)
			{
				ParseElement(o);
			}
		}
		#endregion
		#region TypeDeclCollection
		public override void ParseElement(TypeDeclCollection gr)
		{
			if(gr == null) return;
			foreach(TypeDecl o in gr)
			{
				Parse(o);
			}
		}
		#endregion
		#region MemberDeclCollection
		public override void ParseElement(MemberDeclCollection gr)
		{
			if(gr == null) return;
			foreach(MemberDecl o in gr)
			{
				Parse(o);
			}
		}
		#endregion
		#region TypeRefCollection
		public override void ParseElement(TypeRefCollection gr)
		{
			if(gr == null) return;
			string comma = ": ";
			foreach(TypeRef o in gr)
			{
				sb.Write(comma + o.TypeName);
				comma = ", ";
			}
		}
		#endregion
		#region ParamCollection
		public override void ParseElement(ParamCollection gr)
		{
			if(gr == null) return;
			string comma = "";
			sb.Write("(");
			foreach(Param o in gr)
			{
				sb.Write(comma);
				ParseElement(o);
				comma = ", ";
			}
			sb.Write(")");
		}
		#endregion
		#region ParamDeclCollection
		public override void ParseElement(ParamDeclCollection gr)
		{
			if(gr == null) return;
			string comma = "";
			sb.Write("(");
			foreach(ParamDecl o in gr)
			{
				sb.Write(comma);
				ParseElement(o);
				comma = ", ";
			}
			sb.Write(")");
		}
		#endregion
		#region StatementCollection
		public override void ParseElement(StatementCollection gr)
		{
			if(gr == null) return;
			foreach(Statement o in gr)
			{
				Parse(o);
			}
		}
		#endregion
		#region CommentStmtCollection
		public override void ParseElement(CommentStmtCollection gr)
		{
			if(gr == null) return;
			foreach(CommentStmt o in gr)
			{
				ParseElement(o);
			}
		}
		#endregion
		#region CatchCollection
		public override void ParseElement(CatchCollection gr)
		{
			if(gr == null) return;
			foreach(Catch o in gr)
			{
				ParseElement(o);
			}
		}
		#endregion
		#region ExpressionCollection
		public override void ParseElement(ExpressionCollection gr)
		{
			if(gr == null) return;
			// todo: this can be comma separated, as in ConstructorDecl
			foreach(Expression o in gr)
			{
				Parse(o);
			}
		}
		#endregion
		#region CustomAttributeCollection
		public override void ParseElement(CustomAttributeCollection gr)
		{
			if(gr.Count == 0) return;
			foreach(CustomAttribute o in gr)
			{
				ParseElement(o);
			}
		}
		#endregion
		#region CaseCollection
		public override void ParseElement(CaseCollection gr)
		{
			if(gr == null) return;
			foreach(Case o in gr)
			{
				ParseElement(o);
			}
		}
		#endregion
		#region AssemblyReferenceCollection
		public override void ParseElement(AssemblyReferenceCollection gr)
		{
			if(gr == null) return;
			foreach(AssemblyReference o in gr)
			{
				ParseElement(o);
			}
		}
		#endregion
		#region RankSpecifierCollection
		public override void ParseElement(RankSpecifierCollection gr)
		{
			if(gr == null) return;
			for(int i = 0; i < gr.Count; i++)
			{
				ParseElement(gr[i]);
			}
		}
		#endregion
		#region DeclaratorCollection
		public override void ParseElement(DeclaratorCollection gr)
		{
		}
		#endregion

		#region Modifiers Enum
		public override void ParseElement(Modifiers gr)
		{
			if(gr == Modifiers.Empty) return;
			if((gr & Modifiers.Public) != 0)		sb.Write("public ");
			if((gr & Modifiers.Protected) != 0)		sb.Write("protected ");
			if((gr & Modifiers.Private) != 0)		sb.Write("private ");
			if((gr & Modifiers.Static) != 0)		sb.Write("static ");
			if((gr & Modifiers.New) != 0)			sb.Write("new ");
			if((gr & Modifiers.Override) != 0)		sb.Write("override ");
			if((gr & Modifiers.Abstract) != 0)		sb.Write("abstract ");
			if((gr & Modifiers.Virtual) != 0)		sb.Write("virtual ");
			if((gr & Modifiers.Sealed) != 0)		sb.Write("sealed ");
			if((gr & Modifiers.Internal) != 0)		sb.Write("internal ");
			if((gr & Modifiers.Readonly) != 0)		sb.Write("readonly ");
			if((gr & Modifiers.Volatile) != 0)		sb.Write("volatile ");
			if((gr & Modifiers.Extern) != 0)		sb.Write("extern ");
		}
		#endregion
		#region TypeModifiers Enum
		public override void ParseElement(TypeModifiers gr)
		{
		}
		#endregion
		#region AccessorModifiers Enum
		public override void ParseElement(AccessorModifiers gr)
		{
		}
		#endregion
		#region AttributeTarget Enum
		public override void ParseElement(AttributeTarget gr)
		{
		}
		#endregion
		#region OverloadableOperator Enum
		public override void ParseElement(OverloadableOperator gr)
		{
			sb.Write(OperatorDecl.StringFromOperator(gr) + " ");
		}
		#endregion
		#region BinaryOperator Enum
		public override void ParseElement(BinaryOperator gr)
		{
			sb.Write(BinaryExpr.StringFromOperator(gr) + " ");
		}
		#endregion
		#region UnaryOperator Enum
		public override void ParseElement(UnaryOperator gr)
		{
			sb.Write(UnaryExpr.StringFromOperator(gr) + " ");
		}
		#endregion
		#region PostfixOperator Enum
		public override void ParseElement(PostfixOperator gr)
		{
		}
		#endregion
		#region AssignOperator Enum
		public override void ParseElement(AssignOperator gr)
		{
		}
		#endregion
		#region IterationType Enum
		public override void ParseElement(IterationType gr)
		{
		}
		#endregion
		#region ParamDirection Enum
		public override void ParseElement(ParamDirection gr)
		{
		}
		#endregion


		private void OpenBlock()
		{
			sb.WriteLine("");
			sb.WriteLine("{");
			sb.Indent++;
		}
		private void CloseBlock()
		{
			sb.Indent--;
			sb.WriteLine("}");
		}

		protected override void BeginParse(IGraph gr)
		{
			gr.LinePragma.Column = sb.Column;
			gr.LinePragma.Line = sb.Line;
		}
		protected override void EndParse(IGraph gr)
		{
			gr.LinePragma.EndColumn = sb.Column;
			gr.LinePragma.EndLine = sb.Line;
		}
		public override string ToString()
		{
			return sb.ToString();
		}
		public void Close()
		{
			sb.Close();
		}

	}
}

⌨️ 快捷键说明

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