[svn] r7612: nemerle/trunk/ncc/codedom/NemerleCodeGenerator.n

VladD2 svnadmin at nemerle.org
Mon Apr 23 11:28:28 CEST 2007


Log:
Fix array generation in NemerleCodeGenerator

Author: VladD2
Date: Mon Apr 23 11:28:27 2007
New Revision: 7612

Modified:
   nemerle/trunk/ncc/codedom/NemerleCodeGenerator.n

Modified: nemerle/trunk/ncc/codedom/NemerleCodeGenerator.n
==============================================================================
--- nemerle/trunk/ncc/codedom/NemerleCodeGenerator.n	(original)
+++ nemerle/trunk/ncc/codedom/NemerleCodeGenerator.n	Mon Apr 23 11:28:27 2007
@@ -97,7 +97,7 @@
       set { 
         when (Options == null) 
           options = CodeGeneratorOptions();
-        output = IndentedTextWriter(value,Options.IndentString);
+        output = IndentedTextWriter(value, Options.IndentString);
       }
     }
 
@@ -123,7 +123,7 @@
     {
       def output = Output;
 
-      output.Write( "array " );
+      output.Write("array");
 
       def initializers = expression.Initializers;
       mutable createType = expression.CreateType;
@@ -131,27 +131,32 @@
       if (initializers.Count > 0) {
         output.WriteLine ("[");
         ++Indent;
-        OutputExpressionList ( initializers, true );
+        OutputExpressionList (initializers,  true);
         --Indent;
         output.Write ("]");
       }
       else {
         mutable arrayType = createType.ArrayElementType;
-        while ( arrayType != null ) {
+
+        while (arrayType != null) {
           createType = arrayType;
           arrayType = arrayType.ArrayElementType;
         }
 
-        output.Write( '(' );
+        output.Write('(');
 
         def size = expression.SizeExpression;
-        if ( size != null )
-          GenerateExpression( size );
+        if (size != null)
+          GenerateExpression(size);
         else
-          output.Write( expression.Size );
+          output.Write(expression.Size);
 
-        output.Write( ')' );
+        output.Write(')');
       }
+  
+      when (createType.BaseType == "System.Object" || createType.BaseType == "object")
+        output.Write(" :> array[object]");
+
       output.WriteLine ();
     }
     
@@ -193,24 +198,24 @@
     protected override GenerateCastExpression (expression : CodeCastExpression) : void
     {
       def output = Output;
-      output.Write( "(" );
-      GenerateExpression( expression.Expression );
-      output.Write( " :> " );
-      OutputType( expression.TargetType );
-      output.Write( ")" );
+      output.Write("(");
+      GenerateExpression(expression.Expression);
+      output.Write(" :> ");
+      OutputType(expression.TargetType);
+      output.Write(")");
     }
 
     protected override GenerateCompileUnitStart (_compileUnit : CodeCompileUnit) : void
     {
-      GenerateComment( CodeComment( "------------------------------------------------------------------------------" ) );
-      GenerateComment( CodeComment( " <autogenerated>" ) );
-      GenerateComment( CodeComment( "     This code was generated by a tool." ) );
-      GenerateComment( CodeComment( "     Runtime Version: " +  System.Environment.Version.ToString () ) );
-      GenerateComment( CodeComment( "" ) );
-      GenerateComment( CodeComment( "     Changes to this file may cause incorrect behavior and will be lost if " ) );
-      GenerateComment( CodeComment( "     the code is regenerated." ) );
-      GenerateComment( CodeComment( " </autogenerated>" ) );
-      GenerateComment( CodeComment( "------------------------------------------------------------------------------" ) );
+      GenerateComment(CodeComment("------------------------------------------------------------------------------"));
+      GenerateComment(CodeComment(" <autogenerated>"));
+      GenerateComment(CodeComment("     This code was generated by a tool."));
+      GenerateComment(CodeComment("     Runtime Version: " +  System.Environment.Version.ToString ()));
+      GenerateComment(CodeComment(""));
+      GenerateComment(CodeComment("     Changes to this file may cause incorrect behavior and will be lost if "));
+      GenerateComment(CodeComment("     the code is regenerated."));
+      GenerateComment(CodeComment(" </autogenerated>"));
+      GenerateComment(CodeComment("------------------------------------------------------------------------------"));
 
       when (Options.BracingStyle == "Indent")
         Output.WriteLine("#pragma indent");
@@ -226,15 +231,15 @@
       // one generate code and use delegate object, instead
       // our cool functional values
       OutputType(expression.DelegateType);
-      output.Write( '(' );
+      output.Write('(');
                         
       def targetObject = expression.TargetObject;
-      when ( targetObject != null ) {
-        GenerateExpression( targetObject );
-        Output.Write( '.' );
+      when (targetObject != null) {
+        GenerateExpression(targetObject);
+        Output.Write('.');
       }
-      output.Write( GetSafeName (expression.MethodName) );
-      output.Write( ')' );
+      output.Write(GetSafeName (expression.MethodName));
+      output.Write(')');
     }
 
     protected override GenerateDirectives (directives : CodeDirectiveCollection) : void
@@ -294,9 +299,9 @@
       def output = Output;
 
       GenerateExpression (expression.TargetObject);
-      output.Write ( '[' );
-      OutputExpressionList( expression.Indices );
-      output.Write( ']' );
+      output.Write ('[');
+      OutputExpressionList(expression.Indices);
+      output.Write(']');
     }
     
     protected override GenerateArrayIndexerExpression (expression : CodeArrayIndexerExpression) : void
@@ -371,7 +376,7 @@
 
     protected override GeneratePropertySetValueReferenceExpression (_ : CodePropertySetValueReferenceExpression) : void
     {
-      Output.Write ( "value" );
+      Output.Write ("value");
     }
 
     protected override GenerateThisReferenceExpression (_ : CodeThisReferenceExpression) : void
@@ -392,14 +397,14 @@
     {
       def output = Output;
 
-      output.Write( "for (" );
+      output.Write("for (");
       dont_write_semicolon = true;
-      GenerateStatement( statement.InitStatement );
-      output.Write( "; " );
-      GenerateExpression( statement.TestExpression );
-      output.Write( "; " );
-      GenerateStatement( statement.IncrementStatement );
-      output.Write( ") " );
+      GenerateStatement(statement.InitStatement);
+      output.Write("; ");
+      GenerateExpression(statement.TestExpression);
+      output.Write("; ");
+      GenerateStatement(statement.IncrementStatement);
+      output.Write(") ");
       dont_write_semicolon = false;
 
       GenerateBlock(statement.Statements);
@@ -432,7 +437,7 @@
       def lines = comment.Text.Split ('\n');
       mutable first = true;
       foreach (line : string in lines) {
-        if ( comment.DocComment )
+        if (comment.DocComment)
           output.Write ("///");
         else
           output.Write ("//");
@@ -463,15 +468,15 @@
       def falses = statement.FalseStatements;
 
       if (falses.Count > 0)
-        output.Write( "if (" );
+        output.Write("if (");
       else
-        output.Write( "when (" );
+        output.Write("when (");
 
       GenerateExpression (statement.Condition);
 
-      output.Write( ") " );
+      output.Write(") ");
 
-      GenerateBlock ( statement.TrueStatements, false );
+      GenerateBlock (statement.TrueStatements,  false);
 
       if (falses.Count > 0) {
         if (this.Options.ElseOnClosing)
@@ -479,7 +484,7 @@
         else
           output.WriteLine ();
         output.Write ("else ");
-        GenerateBlock ( falses );
+        GenerateBlock (falses);
       }
       else
         output.WriteLine(); 
@@ -494,7 +499,7 @@
       GenerateBlock (statement.TryStatements);
 
       when (statement.CatchClauses.Count > 0) {
-        output.WriteLine ( " catch " );
+        output.WriteLine (" catch ");
         def bs = BracingStyle;
         OutputBlockStart (bs);
         ++Indent;
@@ -507,16 +512,16 @@
           --Indent;
         }
         --Indent;
-        OutputBlockEnd (bs,false);
+        OutputBlockEnd (bs, false);
       }
 
       def finallies = statement.FinallyStatements;
-      if ( finallies.Count > 0 ) {
-        if ( options.ElseOnClosing )
-          output.Write( ' ' );
+      if (finallies.Count > 0) {
+        if (options.ElseOnClosing)
+          output.Write(' ');
         else
           output.WriteLine();
-        output.Write( "finally " );
+        output.Write("finally ");
         GenerateBlock (finallies);
       }
       else
@@ -590,7 +595,7 @@
       def initExpression = statement.InitExpression;
       if (initExpression != null) {
         output.Write(" = ");
-        GenerateExpression( initExpression );
+        GenerateExpression(initExpression);
         match (initExpression) {
           | x is CodePrimitiveExpression =>
             when (x.Value == null)
@@ -612,7 +617,7 @@
           ()
       }
       unless (dont_write_semicolon)
-        output.WriteLine( ';' );
+        output.WriteLine(';');
     }
 
     protected override GenerateLinePragmaStart (linePragma : CodeLinePragma) : void
@@ -664,9 +669,9 @@
       }
 
       def initExpression = field.InitExpression;
-      when ( initExpression != null ) {
-        output.Write ( " = " );
-        GenerateExpression ( initExpression );
+      when (initExpression != null) {
+        output.Write (" = ");
+        GenerateExpression (initExpression);
       }
 
       if (!IsCurrentEnum)
@@ -796,9 +801,9 @@
       output.Write (" : ");
       OutputType (method.ReturnType);
 
-      if ( (attributes %& MemberAttributes.ScopeMask) == MemberAttributes.Abstract 
-           || declaration.IsInterface )
-        output.WriteLine ( ';' );
+      if ((attributes %& MemberAttributes.ScopeMask) == MemberAttributes.Abstract 
+           || declaration.IsInterface)
+        output.WriteLine (';');
       else {
         when (privateType != null)
         {
@@ -817,11 +822,11 @@
       def output = Output;
 
       when (property.CustomAttributes.Count > 0)
-        OutputAttributeDeclarations( property.CustomAttributes );
+        OutputAttributeDeclarations(property.CustomAttributes);
 
       def attributes = property.Attributes;
-      OutputMemberAccessModifier( attributes );
-      OutputMemberScopeModifier( attributes );
+      OutputMemberAccessModifier(attributes);
+      OutputMemberScopeModifier(attributes);
 
 
       if (property.Name == "Item")
@@ -837,7 +842,7 @@
       }
       else
       {
-        OutputTypeNamePair( property.Type, GetSafeName (property.Name));
+        OutputTypeNamePair(property.Type,  GetSafeName (property.Name));
       }
 
       def bs = BracingStyle;
@@ -939,32 +944,32 @@
       def output = Output;
 
       when (declaration.CustomAttributes.Count > 0)
-        OutputAttributeDeclarations ( declaration.CustomAttributes );
+        OutputAttributeDeclarations (declaration.CustomAttributes);
 
       OutputTypeAttributes (declaration);
 
       output.Write (GetSafeName (declaration.Name));
 
-      output.Write( ' ' );
+      output.Write(' ');
       
       def enumerator = declaration.BaseTypes.GetEnumerator();
-      when ( enumerator.MoveNext() ) {
+      when (enumerator.MoveNext()) {
         mutable ty = (enumerator.Current :> CodeTypeReference);
       
-        output.Write( ": " );
-        OutputType( ty );
+        output.Write(": ");
+        OutputType(ty);
         
-        while ( enumerator.MoveNext() ) {
+        while (enumerator.MoveNext()) {
           ty = enumerator.Current :> CodeTypeReference;
         
-          output.Write( ", " );
-          OutputType( ty );
+          output.Write(",  ");
+          OutputType(ty);
         }
 
-        output.Write( ' ' );
+        output.Write(' ');
       }
       if (declaration is CodeTypeDelegate)
-        output.Write ( "(" );
+        output.Write ("(");
       else
         OutputBlockStart (BracingStyle);
       ++Indent;
@@ -1001,9 +1006,9 @@
       def output = Output;
       
       def name = ns.Name;
-      when ( name != null && name != "" ) {
-        output.Write( "namespace " );
-        output.Write( GetSafeName (name) );
+      when (name != null && name != "") {
+        output.Write("namespace ");
+        output.Write(GetSafeName (name));
         OutputBlockStart (BracingStyle);
         ++Indent;
       }
@@ -1012,7 +1017,7 @@
     protected override GenerateNamespaceEnd (ns : CodeNamespace) : void
     {
       def name = ns.Name;
-      when ( name != null && name != "" ) {
+      when (name != null && name != "") {
         --Indent;
         OutputBlockEnd (BracingStyle);
       }
@@ -1022,14 +1027,14 @@
     {
       def output = Output;
 
-      output.Write( "using " );
-      output.Write( GetSafeName (import.Namespace) );
-      output.WriteLine( ';' );
+      output.Write("using ");
+      output.Write(GetSafeName (import.Namespace));
+      output.WriteLine(';');
     }
 
     protected override GenerateAttributeDeclarationsStart (attributes : CodeAttributeDeclarationCollection) : void
     {
-      Output.Write( '[' );
+      Output.Write('[');
 
       match (CurrentMember) {
         | met is CodeMemberMethod when
@@ -1048,7 +1053,7 @@
 
     protected override GenerateAttributeDeclarationsEnd (_ : CodeAttributeDeclarationCollection) : void
     {
-      Output.WriteLine( ']' );
+      Output.WriteLine(']');
     }
 
     protected override OutputType (ty : CodeTypeReference) : void
@@ -1237,7 +1242,7 @@
         | v is decimal =>
           GenerateDecimalValue(v);
         | v is bool =>
-          Output.Write( if(v) "true" else "false" ); 
+          Output.Write(if(v) "true" else "false"); 
         | v => 
           throw ArgumentException($"InvalidPrimitiveType: $(v.GetType().ToString())");
       }
@@ -1311,7 +1316,7 @@
         if (first)
           first = false;
         else if (newlineBetweenItems)
-          ContinueOnNewLine(",");
+          ContinueOnNewLine(", ");
         else
           output.Write(", ");
         (this:ICodeGenerator).GenerateCodeFromExpression(e, (output :> IndentedTextWriter).InnerWriter, options);
@@ -1359,13 +1364,11 @@
     {
       def arrayType = ty.ArrayElementType;
       def output =
-        if ( arrayType != null ) {
-          "array [" +
-          (if (ty.ArrayRank > 1) { ty.ArrayRank.ToString () + ", " } else "") +
-          GetTypeOutput (arrayType) + "]";
-        }
-        else
-          match (ty.BaseType.ToLower(System.Globalization.CultureInfo.InvariantCulture))
+        if (arrayType != null)
+          "array["
+          + (if (ty.ArrayRank > 1) { ty.ArrayRank.ToString () + ",  " } else "")
+          + GetTypeOutput (arrayType) + "]"
+        else match (ty.BaseType.ToLower(System.Globalization.CultureInfo.InvariantCulture))
           {
             | "system.decimal" => "decimal";
             | "system.double" => "double";
@@ -1383,7 +1386,7 @@
             | "system.string" => "string";
             | "system.object" => "object";
             | "system.void" => "void";
-            | _ => GetSafeTypeName (ty.BaseType);
+          | _ => GetSafeTypeName(ty.BaseType);
           };
 
       output.Replace ("+", ".");
@@ -1417,32 +1420,37 @@
 
     static GetSafeTypeName (id : string) : string
     {
-      when (keywordsTable == null) {
+      when (keywordsTable == null)
         FillKeywordTable ();
-      }
-      if (keywordsTable.Contains (id))
-        "@" + id;
-      else
-        id;
+
+      if (keywordsTable.Contains (id)) "@" + id else id;
     }
 
     static FillKeywordTable () : void
     {
       keywordsTable = Hashtable ();
-      foreach (keyword : string in keywords) keywordsTable.Add (keyword,keyword);
+      foreach (keyword : string in keywords) keywordsTable.Add (keyword, keyword);
       typesTable = Hashtable ();
-      foreach (ty : string in types) typesTable.Add (ty,ty);
+      foreach (ty : string in types) typesTable.Add (ty, ty);
     }
 
     static mutable typesTable : Hashtable;
     static types : array [ string] = array [
-      "object","bool","byte","float","uint","char","ulong","ushort",
-      "decimal","int","sbyte","short","double","long","string","void"
+      "object", "bool", "byte", "float", "uint", "char", "ulong", "ushort", 
+      "decimal", "int", "sbyte", "short", "double", "long", "string", "void"
     ];
 
     static mutable keywordsTable : Hashtable;
-    static keywords : array [string] = array [
-      "abstract", "and", "array", "as", "base", "catch", "class", "def", "delegate", "do", "else", "enum", "event", "extends", "extern", "false", "finally", "for", "foreach", "fun", "if", "implements", "in", "interface", "internal", "lock", "macro", "match", "module", "mutable", "namespace", "new", "null", "out", "override", "params", "private", "protected", "public", "ref", "sealed", "static", "struct", "syntax", "this", "throw", "true", "try", "type", "typeof", "unless", "using", "variant", "virtual", "void", "when", "where", "while", "assert", "abort", "ignore"
+    static keywords : array[string] = array[
+      "abstract",  "and",  "array",  "as",  "base",  "catch",  "class",  "def",
+      "delegate",  "do",  "else",  "enum",  "event",  "extends",  "extern",
+      "false",  "finally",  "for",  "foreach",  "fun",  "if",  "implements",
+      "in",  "interface",  "internal",  "lock",  "macro",  "match",  "module",
+      "mutable",  "namespace",  "new",  "null",  "out",  "override",  "params",
+      "private",  "protected",  "public",  "ref",  "sealed",  "static",  "struct",
+      "syntax",  "this",  "throw",  "true",  "try",  "type",  "typeof",  "unless",
+      "using",  "variant",  "virtual",  "void",  "when",  "where",  "while",
+      "assert",  "abort",  "ignore"
     ];
   }
 }



More information about the svn mailing list