case Kind.InstanceOfNode:
return flattenInstanceOfNode((InstanceOfNode)en,out_temp);
+
+ case Kind.ArrayInitializerNode:
+ return flattenArrayInitializerNode((ArrayInitializerNode)en,out_temp);
}
throw new Error();
}
return new NodePair(cond.getBegin(),fion);
}
+ private NodePair flattenArrayInitializerNode(ArrayInitializerNode ain, TempDescriptor out_temp) {
+ /*
+ TempDescriptor expr_temp=TempDescriptor.tempFactory("arry_init",ain.getType());
+
+ // create a new array of size equal to the array initializer
+ //FlatNode first=null;
+ //FlatNode last=null;
+ TempDescriptor[] temps=new TempDescriptor[ain.numVarInitializers()];
+
+ for (int i=0; i<con.numArgs(); i++) {
+ ExpressionNode en=con.getArg(i);
+ TempDescriptor tmp=TempDescriptor.tempFactory("arg",en.getType());
+ temps[i]=tmp;
+ NodePair np=flattenExpressionNode(en, tmp);
+ if (first==null)
+ first=np.getBegin();
+ else
+ last.addNext(np.getBegin());
+ last=np.getEnd();
+
+ TempDescriptor tmp2=(i==0) ?
+ out_temp :
+ TempDescriptor.tempFactory("arg",en.getType());
+ }
+ FlatNew fn=new FlatNew(td, out_temp, temps[0], con.isGlobal(), con.getDisjointId());
+ last.addNext(fn);
+
+
+ // assign each element of the new array to the flattened expression
+
+
+ FlatOpNode fonAssignArray=new FlatOpNode(out_temp, newarry_temp, null, new Operation(Operation.ASSIGN));
+ */
+ //return new NodePair( , fonAssignArray );
+ ain.printNode(0);
+ System.out.println( "Array initializers not implemented yet." );
+ System.exit( -1 );
+ return null;
+ }
+
private NodePair flattenTertiaryNode(TertiaryNode tn, TempDescriptor out_temp) {
TempDescriptor cond_temp=TempDescriptor.tempFactory("tert_cond",new TypeDescriptor(TypeDescriptor.BOOLEAN));
TempDescriptor true_temp=TempDescriptor.tempFactory("tert_true",tn.getTrueExpr().getType());
--- /dev/null
+package IR.Tree;
+import java.util.Vector;
+import IR.TypeDescriptor;
+import IR.MethodDescriptor;
+
+public class ArrayInitializerNode extends ExpressionNode {
+ TypeDescriptor td;
+ Vector varInitList;
+
+ public ArrayInitializerNode(TypeDescriptor type, Vector vil) {
+ td=type;
+ varInitList=vil;
+ }
+
+ public TypeDescriptor getType() {
+ return td;
+ }
+
+ public int numVarInitializers() {
+ return varInitList.size();
+ }
+
+ public ExpressionNode getVarInitializer(int i) {
+ return (ExpressionNode) varInitList.get(i);
+ }
+
+ public String printNode(int indent) {
+ String st="{";
+ for(int i=0; i<varInitList.size(); i++) {
+ ExpressionNode en=(ExpressionNode)varInitList.get(i);
+ st+=en.printNode(indent);
+ if ((i+1)!=varInitList.size()) {
+ st+=", ";
+ }
+ }
+ return st+"}";
+ }
+
+ public int kind() {
+ return Kind.ArrayInitializerNode;
+ }
+}
ExpressionNode exp=parseExpression(pn.getChild("exp").getFirstChild());
TypeDescriptor t=parseTypeDescriptor(pn);
return new InstanceOfNode(exp,t);
+ } else if (isNode(pn, "array_initializer")) {
+ System.out.println( "Array initializers not implemented yet." );
+ System.exit( -1 );
+ TypeDescriptor td=parseTypeDescriptor(pn);
+ Vector initializers=parseVariableInitializerList(pn);
+ return new ArrayInitializerNode(td, initializers);
} else {
System.out.println("---------------------");
System.out.println(pn.PPrint(3,true));
return new Vector[] {varlist, arglist};
}
+ private Vector parseVariableInitializerList(ParseNode pn) {
+ Vector varInitList=new Vector();
+ ParseNode vin=pn.getChild("variable_init_list");
+ if (vin==null) /* No argument list */
+ return varInitList;
+ ParseNodeVector vinv=vin.getChildren();
+ for(int i=0; i<vinv.size(); i++) {
+ varInitList.add(parseExpression(vinv.elementAt(i)));
+ }
+ return varInitList;
+ }
+
private ExpressionNode parseAssignmentExpression(ParseNode pn) {
AssignOperation ao=new AssignOperation(pn.getChild("op").getTerminal());
ParseNodeVector pnv=pn.getChild("args").getChildren();
public final static int ContinueBreakNode=24;
public final static int TertiaryNode=25;
public final static int InstanceOfNode=26;
+ public final static int ArrayInitializerNode=27;
}
case Kind.InstanceOfNode:
checkInstanceOfNode(md, nametable, (InstanceOfNode) en, td);
return;
+
+ case Kind.ArrayInitializerNode:
+ checkArrayInitializerNode(md, nametable, (ArrayInitializerNode) en, td);
+ return;
}
throw new Error();
}
checkExpressionNode(md, nametable, tn.getExpr(), null);
}
+ void checkArrayInitializerNode(Descriptor md, SymbolTable nametable, ArrayInitializerNode ain, TypeDescriptor td) {
+ for( int i = 0; i < ain.numVarInitializers(); ++i ) {
+ checkExpressionNode(md, nametable, ain.getVarInitializer(i), td);
+ }
+ }
void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
boolean postinc=true;
//non terminal ParseNode interface_member_declaration, constant_declaration;
//non terminal ParseNode abstract_method_declaration;
// 19.10) Arrays
-//non terminal ParseNode array_initializer;
-//non terminal ParseNode variable_initializers;
+non terminal ParseNode array_initializer;
+non terminal ParseNode variable_initializers;
// 19.11) Blocks and Statements
non terminal ParseNode block;
non terminal ParseNode block_statements_opt, block_statements, block_statement;
;
variable_initializer ::=
expression:exp {: RESULT=exp; :}
-// | array_initializer
+ | array_initializer {: RESULT=new ParseNode("array_initializer"); :}
;
// 19.8.3) Method Declarations
// 19.10) Arrays
-//array_initializer ::=
-// LBRACE variable_initializers COMMA RBRACE
-// | LBRACE variable_initializers RBRACE
-// | LBRACE COMMA RBRACE
-// | LBRACE RBRACE
-// ;
-//variable_initializers ::=
-// variable_initializer
-// | variable_initializers COMMA variable_initializer
-// ;
+array_initializer ::=
+ LBRACE variable_initializers:var_init_list COMMA RBRACE {:
+ RESULT=var_init_list;
+ :}
+ | LBRACE variable_initializers:var_init_list RBRACE {:
+ RESULT=var_init_list;
+ :}
+ | LBRACE COMMA RBRACE {:
+ RESULT=new ParseNode("empty");
+ :}
+ | LBRACE RBRACE {:
+ RESULT=new ParseNode("empty");
+ :}
+ ;
+variable_initializers ::=
+ variable_initializer:var_init {:
+ ParseNode pn=new ParseNode("var_init_list");
+ pn.addChild(var_init);
+ RESULT=pn;
+ :}
+ | variable_initializers:var_init_list COMMA variable_initializer:var_init {:
+ var_init_list.addChild(var_init);
+ RESULT=var_init_list;
+ :}
+ ;
// 19.11) Blocks and Statements
block ::= LBRACE block_statements_opt:bso RBRACE {:
--- /dev/null
+public class ArrayInitializerTest {
+ static public void main( String[] args ) {
+
+ int[] a = { 1, 2, 3 };
+
+ System.out.println( "a[2] should be 3: "+a[2] );
+ }
+}
\ No newline at end of file