Array field initialization was accidentally broken. Fix it and add ArrayInitializerTe...
authorjzhou <jzhou>
Tue, 29 Nov 2011 20:47:41 +0000 (20:47 +0000)
committerjzhou <jzhou>
Tue, 29 Nov 2011 20:47:41 +0000 (20:47 +0000)
Robust/src/IR/Tree/SemanticCheck.java
Robust/src/Tests/DoTests
Robust/src/Tests/output/ArrayInitializerTest.output.goal [new file with mode: 0644]

index 1fa4a7c9203c4c3cde685175856a6ae0c26b9aef..4ada35098785878217baf0ab3f4214c9b446c490 100644 (file)
@@ -1072,13 +1072,47 @@ public class SemanticCheck {
   void checkArrayInitializerNode(Descriptor md, SymbolTable nametable, ArrayInitializerNode ain, TypeDescriptor td) {
     Vector<TypeDescriptor> vec_type = new Vector<TypeDescriptor>();
     for( int i = 0; i < ain.numVarInitializers(); ++i ) {
-      checkExpressionNode(md, nametable, ain.getVarInitializer(i), td.dereference());
+      checkExpressionNode(md, nametable, ain.getVarInitializer(i), td==null?td:td.dereference());
       vec_type.add(ain.getVarInitializer(i).getType());
     }
-    if (td==null)
-      throw new Error();
-
-    ain.setType(td);
+    // descide the type of this variableInitializerNode
+    TypeDescriptor out_type = null;
+    for(int i = 0; i < vec_type.size(); i++) {
+      TypeDescriptor tmp_type = vec_type.elementAt(i);
+      if(out_type == null) {
+        if(tmp_type != null) {
+          out_type = tmp_type;
+        }
+      } else if(out_type.isNull()) {
+        if(!tmp_type.isNull() ) {
+          if(!tmp_type.isArray()) {
+            throw new Error("Error: mixed type in var initializer list");
+          } else {
+            out_type = tmp_type;
+          }
+        }
+      } else if(out_type.isArray()) {
+        if(tmp_type.isArray()) {
+          if(tmp_type.getArrayCount() > out_type.getArrayCount()) {
+            out_type = tmp_type;
+          }
+        } else if((tmp_type != null) && (!tmp_type.isNull())) {
+          throw new Error("Error: mixed type in var initializer list");
+        }
+      } else if(out_type.isInt()) {
+        if(!tmp_type.isInt()) {
+          throw new Error("Error: mixed type in var initializer list");
+        }
+      } else if(out_type.isString()) {
+        if(!tmp_type.isString()) {
+          throw new Error("Error: mixed type in var initializer list");
+        }
+      }
+    }
+    if(out_type != null) {
+      out_type = out_type.makeArray(state);
+    }
+    ain.setType(out_type);
   }
 
   void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
index 687668f14fe1925ecf9f72abeebbc4223793cdf0..1f27017129c9b3f02d930f646a84ead01aa437ec 100755 (executable)
@@ -19,6 +19,7 @@ dotest IntegerTest IntegerTest.java
 dotest AbstractTest AbstractTest.java
 dotest EnumTest EnumTest.java
 dotest InitializerTest InitializerTest.java
+dotest ArrayInitializerTest ArrayInitializerTest.java
 dotest InterfaceTest InterfaceTest.java
 dotest StaticInnerClassTest StaticInnerClassTest.java
 dotest StaticTest StaticTest.java
diff --git a/Robust/src/Tests/output/ArrayInitializerTest.output.goal b/Robust/src/Tests/output/ArrayInitializerTest.output.goal
new file mode 100644 (file)
index 0000000..2f37304
--- /dev/null
@@ -0,0 +1,10 @@
+a[2] should be 3: 3
+1
+2
+ia[1] is null
+hello 
+world 
+!
+ja[0] is null
+3
+4