package IR.Tree;
import java.util.Vector;
+import IR.TypeDescriptor;
+
public class ArrayInitializerNode extends ExpressionNode {
+ TypeDescriptor type;
Vector varInitList;
public ArrayInitializerNode(Vector vil) {
+ this.type = null;
varInitList=vil;
}
public ExpressionNode getVarInitializer(int i) {
return (ExpressionNode) varInitList.get(i);
}
+
+ public void setType(TypeDescriptor type) {
+ this.type = type;
+ }
+
+ public TypeDescriptor getType() {
+ return this.type;
+ }
public String printNode(int indent) {
String st="{";
}
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());
}
+ // descide the type of this variableInitializerNode
+ TypeDescriptor out_type = vec_type.elementAt(0);
+ for(int i = 1; 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) {
public class ArrayInitializerTest {
+
+ public String[] sa = {"hello ", "world ", "!"};
+
+ public ArrayInitializerTest(){}
+
static public void main( String[] args ) {
int[] a = { 1, 2, 3 };
System.out.println("ia[" + i + "] is null");
}
}
+
+ ArrayInitializerTest ait = new ArrayInitializerTest();
+ for(int i = 0; i < ait.sa.length; i++) {
+ System.out.println(ait.sa[i]);
+ }
}
}
\ No newline at end of file