2005-08-04 Rafael Teixeira <rafaelteixeirabr@hotmail.com>
authorRafael Teixeira <monoman@gmail.com>
Thu, 4 Aug 2005 20:55:16 +0000 (20:55 -0000)
committerRafael Teixeira <monoman@gmail.com>
Thu, 4 Aug 2005 20:55:16 +0000 (20:55 -0000)
* statement.cs:  Refactored code/fields from Redim to RedimClause
so that it works for multiple arrays being redimensioned in the same statement
* mb-parser.jay: redim_clauses rule was losing additional items

2005-08-04  Aldo Monteiro <aldo@psl-pr.softwarelivre.org>,
Renato Suga <renato@psl-pr.softwarelivre.org>
* mb-parser.jay: treatment of opt_type_spec in redim_clause
* statement.cs: changed constructor in class RedimClause so that it gets
an Expression. Report.Error now emits the line number too for ReDim
errors. New attribute in RedimClause: Expression ExprAs

svn path=/trunk/mcs/; revision=48020

mcs/mbas/ChangeLog
mcs/mbas/Test/misc/RedimPreserve.vb
mcs/mbas/mb-parser.jay
mcs/mbas/statement.cs

index a29b2037eb8ade6c0cefc9db17cff91867f32421..636a65ff659687dba77be4d26ea86d47ddd4df51 100644 (file)
@@ -1,3 +1,15 @@
+2005-08-04  Rafael Teixeira <rafaelteixeirabr@hotmail.com>
+       * statement.cs:  Refactored code/fields from Redim to RedimClause 
+       so that it works for multiple arrays being redimensioned in the same statement 
+       * mb-parser.jay: redim_clauses rule was losing additional items
+
+2005-08-04  Aldo Monteiro <aldo@psl-pr.softwarelivre.org>, 
+       Renato Suga <renato@psl-pr.softwarelivre.org>
+       * mb-parser.jay: treatment of opt_type_spec in redim_clause
+       * statement.cs: changed constructor in class RedimClause so that it gets
+       an Expression. Report.Error now emits the line number too for ReDim
+       errors. New attribute in RedimClause: Expression ExprAs 
+
 2005-08-04 Maverson Eduardo Schulze Rosa <maverson@gmail.com>
        * mb-parser.jay: Fix local static variables initialization
        
index ca3e93b3b77c0e5c6af268ec39d5cf0b318b8e17..52769c5b5b69f1ea1233b2a43af10315e874321c 100644 (file)
@@ -5,39 +5,46 @@ Option Compare Text
 Imports System\r
 \r
 Module RedimPreserve\r
-\r
+
     Sub DoTest ()\r
-               Dim IntArray(10) As Integer\r
+               Dim A(10) As Integer\r
+               Dim B(10) As Integer\r
                Dim I As Integer\r
        \r
                Console.WriteLine("Array Size = 10 (Dim)")\r
        \r
                For I = 0 To 10\r
-                       IntArray(I) = 10 - I   \r
-                       Console.Write(I)\r
-                       Console.Write(" : ")\r
-                       Console.WriteLine(IntArray(I))\r
-               Next I\r
+                       A(I) = 10 - I 
+                       B(I) = I*3 \r
+               Next I
+
+               PrintArrays(A, B)\r
        \r
                Console.WriteLine("Array Size = 15 (ReDim Preserve)")\r
                \r
-               ReDim Preserve IntArray(15)\r
+               ReDim Preserve A(15), B(15)\r
+
+               PrintArrays(A, B)\r
                \r
-               For I = 0 To 15\r
-                       Console.Write(I)\r
-                       Console.Write(" : ")\r
-                       Console.WriteLine(IntArray(I))\r
-               Next I\r
                \r
-               Console.WriteLine("Array Size = 5 (ReDim Preserve")\r
+               Console.WriteLine("Array Size = 5 (ReDim Preserve)")\r
        \r
-               ReDim Preserve IntArray(5)\r
-               \r
-               For I = 0 To 5\r
-                       Console.Write(I)\r
-                       Console.Write(" : ")\r
-                       Console.WriteLine(IntArray(I))\r
-               Next I\r
+               ReDim Preserve A(5), B(5)               \r
+
+               PrintArrays(A, B)\r
+
+               Console.WriteLine("Array Size = 3 (ReDim)")\r
+       \r
+               ReDim A(3), B(3)                \r
+
+               PrintArrays(A, B)\r
     End Sub\r
 \r
+       Sub PrintArrays(ArrayA() as Integer, ArrayB() as Integer)
+               Dim I As Integer\r
+               For I = 0 To ArrayA.Length - 1\r
+                       Console.WriteLine("{0} : {1} {2}", I, ArrayA(I), ArrayB(I) )\r
+               Next I\r
+       End Sub
+\r
 End Module\r
index c22bfea64c8d3c4c7dae06fc668b58885fef7ae3..a97a2ae03c8f9ad29f752e9e087d843b55fd5590 100644 (file)
@@ -2824,12 +2824,11 @@ array_handling_statement
        ;
        
 redim_statement
-       : REDIM opt_preserve redim_clauses
+       : REDIM _mark_ opt_preserve redim_clauses
          {
-               ArrayList list = (ArrayList) $3;
-               ReDim r = new ReDim (list, (bool) $2, lexer.Location);
+               ArrayList list = (ArrayList) $4;
+               ReDim r = new ReDim (list, (bool) $3, (Location)$2);
                $$ = r;
-
          }
        ;
        
@@ -2849,7 +2848,7 @@ redim_clauses
        | redim_clauses COMMA redim_clause
          {
                ArrayList clauses = (ArrayList) ($1);
-               clauses.Add ($2);
+               clauses.Add ($3);
 
                $$ = clauses;
          }
@@ -2859,7 +2858,7 @@ redim_clause
        : invocation_expression opt_type_spec
          {
                Invocation i = (Invocation) $1;
-               RedimClause rc = new RedimClause (i.expr, i.Arguments);
+               RedimClause rc = new RedimClause (i.expr, i.Arguments, (Expression) $2);
                $$ = rc;
          }
        ;
index 9b669826f1d5798af3fd63d66d0496257516332b..3377925d19318f3c7bdc28fceb02f30cfb48878d 100644 (file)
@@ -5459,29 +5459,95 @@ namespace Mono.MonoBASIC {
        }
 
        public class RedimClause {
-               public Expression Expr;
-               public ArrayList NewIndexes;
+               private Expression RedimTarget;
+               private ArrayList NewIndexes;
+               private Expression AsType;
                
-               public RedimClause (Expression e, ArrayList args)
+               private LocalTemporary localTmp = null;
+               private Expression origRedimTarget = null;
+               private StatementExpression ReDimExpr;
+
+               public RedimClause (Expression e, ArrayList args, Expression e_as)
                {
                        if (e is SimpleName)
                                ((SimpleName) e).IsInvocation = false;
                        if (e is MemberAccess)
                                ((MemberAccess) e).IsInvocation = false;
                
-                       Expr = e;
+                       RedimTarget = e;
+                       NewIndexes = args;
+                       AsType = e_as;
+               }
+
+               public void Resolve (EmitContext ec, bool Preserve, Location loc)
+               {
+                       RedimTarget = RedimTarget.Resolve (ec);
+
+                       if (AsType != null) {
+                               Report.Error (30811, "'ReDim' statements can no longer be used to declare array variables");
+                               return;
+                       }
+                       
+                       if (!RedimTarget.Type.IsArray) {
+                               Report.Error (49, "'ReDim' statement requires an array");
+                               return;
+                       }
+
+                       ArrayList args = new ArrayList();
+                       foreach (Argument a in NewIndexes) {
+                               if (a.Resolve(ec, loc))
+                                       args.Add (a.Expr);
+                       }
+
+                       for (int x = 0; x < args.Count; x++) {
+                               args[x] = new Binary (Binary.Operator.Addition,
+                                                       (Expression) args[x], new IntLiteral (1), Location.Null);       
+                       }
+
                        NewIndexes = args;
+                       if (RedimTarget.Type.GetArrayRank() != NewIndexes.Count) {
+                               Report.Error (30415, "'ReDim' cannot change the number of dimensions of an array.");
+                               return;
+                       }
+                       
+                       Type BaseType = RedimTarget.Type.GetElementType();
+                       Expression BaseTypeExpr = MonoBASIC.Parser.DecomposeQI(BaseType.FullName.ToString(), Location.Null);
+                       ArrayCreation acExpr = new ArrayCreation (BaseTypeExpr, NewIndexes, "", null, Location.Null);   
+                       if (Preserve)
+                       {
+                               ExpressionStatement PreserveExpr = null;
+                               if (RedimTarget is PropertyGroupExpr) {
+                                       localTmp = new LocalTemporary (ec, RedimTarget.Type);
+                                       PropertyGroupExpr pe = RedimTarget as PropertyGroupExpr;
+                                       origRedimTarget = new PropertyGroupExpr (pe.Properties, pe.Arguments, pe.InstanceExpression, loc);
+                                       if ((origRedimTarget = origRedimTarget.Resolve (ec)) == null)  {
+                                               Report.Error (-1, "'ReDim' vs PropertyGroup");
+                                               return;
+                                       }
+                                       PreserveExpr = (ExpressionStatement) new Preserve(localTmp, acExpr, loc);
+                               } else
+                                       PreserveExpr = (ExpressionStatement) new Preserve(RedimTarget, acExpr, loc);
+                               ReDimExpr = (StatementExpression) new StatementExpression ((ExpressionStatement) new Assign (RedimTarget, PreserveExpr, loc), loc);
+                       }
+                       else
+                               ReDimExpr = (StatementExpression) new StatementExpression ((ExpressionStatement) new Assign (RedimTarget, acExpr, loc), loc);
+                       ReDimExpr.Resolve(ec);                  
                }
+
+               public void DoEmit (EmitContext ec)
+               {
+                       if (localTmp != null) {
+                               origRedimTarget.Emit (ec);
+                               localTmp.Store (ec);
+                       }
+                       ReDimExpr.Emit(ec);
+               }               
+
        }
 
        public class ReDim : Statement {
                ArrayList RedimTargets;
-               Type BaseType;
                bool Preserve;
-               LocalTemporary localTmp = null;
-               Expression origRedimTarget = null;
-
-               private StatementExpression ReDimExpr;
 
                public ReDim (ArrayList targets, bool opt_preserve, Location l)
                {
@@ -5492,64 +5558,15 @@ namespace Mono.MonoBASIC {
 
                public override bool Resolve (EmitContext ec)
                {
-                       Expression RedimTarget;
-                       ArrayList NewIndexes;
-
-                       foreach (RedimClause rc in RedimTargets) {
-                               RedimTarget = rc.Expr;
-                               NewIndexes = rc.NewIndexes;
-                               RedimTarget = RedimTarget.Resolve (ec);
-       
-                               if (!RedimTarget.Type.IsArray)
-                                       Report.Error (49, "'ReDim' statement requires an array");
-
-                               ArrayList args = new ArrayList();
-                               foreach (Argument a in NewIndexes) {
-                                       if (a.Resolve(ec, loc))
-                                               args.Add (a.Expr);
-                               }
-
-                               for (int x = 0; x < args.Count; x++) {
-                                       args[x] = new Binary (Binary.Operator.Addition,
-                                                               (Expression) args[x], new IntLiteral (1), Location.Null);       
-                               }
-
-                               NewIndexes = args;
-                               if (RedimTarget.Type.GetArrayRank() != args.Count)
-                                       Report.Error (30415, "'ReDim' cannot change the number of dimensions of an array.");
-
-                               BaseType = RedimTarget.Type.GetElementType();
-                               Expression BaseTypeExpr = MonoBASIC.Parser.DecomposeQI(BaseType.FullName.ToString(), Location.Null);
-                               ArrayCreation acExpr = new ArrayCreation (BaseTypeExpr, NewIndexes, "", null, Location.Null);   
-                               // TODO: we are in a foreach we probably can't reuse ReDimExpr, must turn it into an array(list)
-                               if (Preserve)
-                               {
-                                       ExpressionStatement PreserveExpr = null;
-                                       if (RedimTarget is PropertyGroupExpr) {
-                                               localTmp = new LocalTemporary (ec, RedimTarget.Type);
-                                               PropertyGroupExpr pe = RedimTarget as PropertyGroupExpr;
-                                               origRedimTarget = new PropertyGroupExpr (pe.Properties, pe.Arguments, pe.InstanceExpression, loc);
-                                               if ((origRedimTarget = origRedimTarget.Resolve (ec)) == null) 
-                                                       return false;
-                                               PreserveExpr = (ExpressionStatement) new Preserve(localTmp, acExpr, loc);
-                                       } else
-                                               PreserveExpr = (ExpressionStatement) new Preserve(RedimTarget, acExpr, loc);
-                                       ReDimExpr = (StatementExpression) new StatementExpression ((ExpressionStatement) new Assign (RedimTarget, PreserveExpr, loc), loc);
-                               }
-                               else
-                                       ReDimExpr = (StatementExpression) new StatementExpression ((ExpressionStatement) new Assign (RedimTarget, acExpr, loc), loc);
-                               ReDimExpr.Resolve(ec);
-                       }
+                       foreach (RedimClause rc in RedimTargets)
+                               rc.Resolve(ec, Preserve, loc);
                        return true;
                }
                                
                protected override bool DoEmit (EmitContext ec)
                {
-                       if (localTmp != null) {
-                               origRedimTarget.Emit (ec);
-                               localTmp.Store (ec);
-                       }
-                       ReDimExpr.Emit(ec);
+                       foreach (RedimClause rc in RedimTargets)
+                               rc.DoEmit(ec);
                        return false;
                }