[Mono-list] XSP

Daniel Stodden stodden@in.tum.de
03 Jun 2002 19:11:52 +0200


--=-S2ya/QdOpJLAqZH9Wrt8
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

On Mon, 2002-06-03 at 13:21, Miguel de Icaza wrote:
> 
> Thanks for the good news.  I did not realize that we were so close to
> have CodeDOM complete, I should add a news item to the web page.

ok, demo. nonsensical example attached.

build this on windows against the ms runtime with mono's
CSharpCodegenerator pulled in statically, i.e as follows:

csc testcodedom4.cs `cygpath -w
<mono_root>/mcs/class/System/Microsoft.CSharp/CSharpCodeGenerator.cs`

on windows, it allows for switching between 
mono::csharpcodegenerator and
the original microsoft.csharp.* codegenerator implementation:
$ testcodedom4.cs [ms | mono]

i'm currently using this kind of stuff to spot differences.

works on linux, but of course won't make a difference there.

indentifying some of the bugs is left as an exercise :), but you get the
idea. so much to do, so little time...

regards,
dns

-- 
___________________________________________________________________________
 mailto:stodden@in.tum.de

--=-S2ya/QdOpJLAqZH9Wrt8
Content-Disposition: attachment; filename=testcodedom4.cs
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; name=testcodedom4.cs; charset=ISO-8859-1

namespace Test {
	using System;
	using System.CodeDom;
	using System.CodeDom.Compiler;
	using System.Reflection;
	using Microsoft.CSharp;
	using Mono.CSharp;

	public class TestCodeDom3
	{
		static void PopulateImports( object sender, EventArgs args )
		{
			CodeNamespace ns =3D (CodeNamespace)sender;
		=09
			CodeNamespaceImportCollection imports =3D ns.Imports;
		=09
			imports.Add( new CodeNamespaceImport( "System.Collections" ) );
			imports.Add( new CodeNamespaceImport( "System.Collections.Specialized" )=
 );
		}

		static void AddArrayTypes( CodeMemberMethod method )
		{
			CodeTypeReference type;
			type =3D new CodeTypeReference( new CodeTypeReference( "System.Int32", 3=
 ), 1 );
			CodeArrayCreateExpression create =3D new CodeArrayCreateExpression( type=
 );
			create.Size =3D 42;
			create.Initializers.Add( new CodePrimitiveExpression( 1 ) );
			method.Statements.Add( new CodeVariableDeclarationStatement( type, "arra=
y1", create ) );

			type =3D new CodeTypeReference( "System.Int32", 1 );
			create =3D new CodeArrayCreateExpression( type );
			method.Statements.Add( new CodeVariableDeclarationStatement( type, "arra=
y2", create ) );
		=09
			CodeTypeReference arrayType;
			arrayType =3D new CodeTypeReference( "System.Int32", 2 );
			type =3D new CodeTypeReference( arrayType, 1 );
			create =3D new CodeArrayCreateExpression( type,=20
								new CodeExpression[] {=20
									new CodeArrayCreateExpression( arrayType,
												       new CodeExpression[] {=20
													       new CodePrimitiveExpression( 1 )
												       } ),
									new CodeArrayCreateExpression( arrayType,
												       new CodeExpression[] {=20
													       new CodePrimitiveExpression( 2 )
												       } ),
									new CodeArrayCreateExpression( arrayType,
												       new CodeExpression[] {=20
													       new CodePrimitiveExpression( 3 )
												       } )=20
								} );
			create.Size =3D 3;
			method.Statements.Add( new CodeVariableDeclarationStatement( type, "arra=
y3", create ) );

			arrayType =3D new CodeTypeReference( "System.Int32", 2 );
			type =3D new CodeTypeReference( arrayType, 1 );
			create =3D new CodeArrayCreateExpression( type, 2 );
			method.Statements.Add( new CodeVariableDeclarationStatement( type, "arra=
y4", create ) );
		}

		static void AddTryCatch( CodeMemberMethod method )
		{
			method.Statements.Add( new CodeTryCatchFinallyStatement( new CodeStateme=
nt[] {
					new CodeAssignStatement( new CodeFieldReferenceExpression( new CodeThi=
sReferenceExpression(), "member" ),
						new CodeBinaryOperatorExpression( new CodeCastExpression( "System.Int=
64", new CodeVariableReferenceExpression( "param" ) ),
										   CodeBinaryOperatorType.Add,
										   new CodePrimitiveExpression( 1 ) ) )
				},
				new CodeCatchClause[] {
					new CodeCatchClause( "e",
						new CodeTypeReference( "Exception" ),
							     new CodeExpressionStatement(=20
									new CodeMethodInvokeExpression(=20
											new CodeTypeReferenceExpression(new CodeTypeReference(typeof(Sys=
tem.Console))),
											"WriteLine",=20
											new CodePrimitiveExpression("an \"error\" occurred.") ) ) )
				},
				new CodeStatement[] {
					new CodeCommentStatement( "give up" )=20
				} ) );
		}

		static void PopulateStatements( object sender, EventArgs args )
		{
			CodeMemberMethod method =3D (CodeMemberMethod)sender;

			AddArrayTypes( method );
			AddTryCatch( method );
		=09
			method.Statements.Add( new CodeExpressionStatement( new CodeArrayIndexer=
Expression( new CodeBaseReferenceExpression(),
													    new CodeExpression[] {
														    new CodePrimitiveExpression( 10 ),
														    new CodeFieldReferenceExpression( new CodeThisReferenceEx=
pression(), "arrayindexer" )
													    } ) ) );

			method.Statements.Add( new CodeExpressionStatement( new CodeIndexerExpre=
ssion( new CodeBaseReferenceExpression(),
												       new CodeExpression[] {
													       new CodePrimitiveExpression( 10 ),
													       new CodeFieldReferenceExpression( new CodeThisReference=
Expression(), "indexer" )
												       } ) ) );
			method.Statements.Add( new CodeExpressionStatement( new CodeSnippetExpre=
ssion( "this" ) ) );
			method.Statements.Add( new CodeExpressionStatement( new CodeSnippetExpre=
ssion( "\"this\"" ) ) );
		}

		static void PopulateTypes( object sender, EventArgs args )
		{
			CodeNamespace ns =3D (CodeNamespace)sender;
		=09
			CodeTypeDeclaration test =3D new CodeTypeDeclaration( "Test" );
#if false
			test.TypeAttributes =3D=20
				TypeAttributes.Public |=20
				TypeAttributes.Sealed |
				TypeAttributes.Abstract;
#endif
			test.IsClass =3D true;

			test.BaseTypes.Add( new CodeTypeReference( typeof(System.Array) ) );
			test.BaseTypes.Add( new CodeTypeReference( typeof(System.Collections.ICo=
llection) ) );
			test.BaseTypes.Add( new CodeTypeReference( "System.Collections.IEnumerab=
le" ) );

			CodeMemberMethod method =3D new CodeMemberMethod();

			method.Name =3D "GetEnumerator";
			method.Parameters.Add( new CodeParameterDeclarationExpression( new CodeT=
ypeReference( typeof(System.Collections.ICollection) ), "collection" ) );
			method.Parameters.Add( new CodeParameterDeclarationExpression( "System.I=
nt32", "value" ) );
			CodeArrayCreateExpression array =3D new CodeArrayCreateExpression( new C=
odeTypeReference( "System.Int32", 2 ) );
#if false
			array.Initializers.Add( new CodePrimitiveExpression( 1 ) );
			array.Initializers.Add( new CodePrimitiveExpression( 2 ) );
			array.Initializers.Add( new CodePrimitiveExpression( 3 ) );
#endif
			method.Statements.Add( array );
			method.Statements.Add( new CodeConditionStatement( new CodePrimitiveExpr=
ession( true ),
									   new CodeStatement[] { new CodeAssignStatement( new CodeVariable=
ReferenceExpression( "a" ), new CodePrimitiveExpression( 10 ) ) },
									   new CodeStatement[] { new CodeAssignStatement( new CodeVariable=
ReferenceExpression( "b" ), new CodePrimitiveExpression( "hello" ) ) } ) );
			method.PrivateImplementationType =3D new CodeTypeReference( "IEnumerable=
" );

			method.Attributes =3D MemberAttributes.Final | MemberAttributes.Static |=
 MemberAttributes.Public;

			method.CustomAttributes.Add( new CodeAttributeDeclaration( "Flags" ) );
			method.CustomAttributes.Add( new CodeAttributeDeclaration( "Serializable=
" ) );
			method.CustomAttributes.Add( new CodeAttributeDeclaration( "SomeOtherAtt=
ribute",=20
										   new CodeAttributeArgument( "name",=20
													      new CodeSnippetExpression( "value" ) ) ) );
			test.Members.Add( method );

			method =3D new CodeMemberMethod();
			method.PopulateStatements +=3D new EventHandler( PopulateStatements );
			method.Name =3D "MoveNext";
			method.Attributes =3D MemberAttributes.Family;
			method.CustomAttributes.Add( new CodeAttributeDeclaration( "System.Compo=
nentModel.ReadOnly", new CodeAttributeArgument( new CodeSnippetExpression( =
"true" ) ) ) );
			test.Members.Add( method );

			ns.Types.Add( test );
		}
	=09
		static ICodeGenerator GetCodeGenerator( string argv0 )
		{
			CSharpCodeProvider provider;
			ICodeGenerator codegen =3D null;
			switch ( argv0 ) {
			case "ms":
				provider =3D new Microsoft.CSharp.CSharpCodeProvider();
				codegen =3D provider.CreateGenerator();
				break;
			case "mono":
				codegen =3D new Mono.CSharp.CSharpCodeGenerator();
				break;
			default:
				Console.WriteLine( "either 'ms' or 'mono'" );
				break;
			}
		=09
			return codegen;
		}

		static void GenCode( CodeCompileUnit unit, string argv0 )
		{
			ICodeGenerator codegen =3D GetCodeGenerator( argv0 );
			CodeGeneratorOptions options =3D new CodeGeneratorOptions();
			options.BlankLinesBetweenMembers =3D true;
			options.ElseOnClosing =3D true;
			options.BracingStyle =3D "C";
			codegen.GenerateCodeFromCompileUnit( unit, Console.Out, options );
		}

		static void Main( string[] argv )
		{
			CodeCompileUnit unit =3D new CodeCompileUnit();

			unit.AssemblyCustomAttributes.Add( new CodeAttributeDeclaration( "Assemb=
lyAttribute",=20
											 new CodeAttributeArgument[] {
												 new CodeAttributeArgument( "name", new CodePrimitiveExpression=
( "value" ) )
											 } ) );
			unit.ReferencedAssemblies.Add( "XXXXXXXXXXXXXXXXXXXXXXXXX" );

			CodeNamespaceCollection namespaces =3D unit.Namespaces;

			CodeNamespace ns;
			ns =3D new CodeNamespace( "System" );
			namespaces.Add( ns );
			ns.PopulateImports +=3D new EventHandler( PopulateImports );

			ns =3D new CodeNamespace( "CodeDom.Test" );
			namespaces.Add( ns );
			ns.Comments.Add( new CodeCommentStatement( "This program is complete rub=
bish and only used for Testing System.CodeDom " +
								   "and System.CodeDom.Compiler implementatations.", true ) );
			ns.PopulateImports +=3D new EventHandler( PopulateImports );
			ns.PopulateTypes +=3D new EventHandler( PopulateTypes );
		=09
			GenCode( unit, argv[0] );
		}
	}
}

--=-S2ya/QdOpJLAqZH9Wrt8--