This update adds initializers, but not for arrays
authordavid <david>
Thu, 6 Jan 2011 23:16:19 +0000 (23:16 +0000)
committerdavid <david>
Thu, 6 Jan 2011 23:16:19 +0000 (23:16 +0000)
Robust/src/IR/FieldDescriptor.java
Robust/src/IR/Tree/BuildIR.java
Robust/src/Tests/DoTests

index ecd6777a7a84ec7a1163cc98396950ae8f9d1991..4a6932287957a374db36fecf6bdc9b9899a5e2f6 100644 (file)
@@ -26,7 +26,10 @@ public class FieldDescriptor extends Descriptor {
     this.safename = "___" + name + "___";
     this.uniqueid=count++;
     this.isglobal=isglobal;
-    if (en!=null) throw new Error("Field initializers not implemented");
+  }
+
+  public ExpressionNode getExpressionNode(){
+      return en;
   }
 
   public boolean isStatic() {
index aef1075662b7ebc614296305149ae54e4fa213ed..96b2537edd7583e9c8cebb67b65ae176af331151 100644 (file)
@@ -52,6 +52,7 @@ public class BuildIR {
          continue;
        if (isNode(type_pn,"class_declaration")) {
          ClassDescriptor cn=parseTypeDecl(type_pn);
+         parseInitializers(cn);
          if (toanalyze!=null)
            toanalyze.add(cn);
          state.addClass(cn);
@@ -67,7 +68,7 @@ public class BuildIR {
         while(!tovisit.isEmpty()) {
           ClassDescriptor cd = (ClassDescriptor)tovisit.iterator().next();
           tovisit.remove(cd);
-          
+         parseInitializers(cd);
           if(toanalyze != null) {
             toanalyze.add(cd);
           }
@@ -133,7 +134,26 @@ public class BuildIR {
       }
     }
   }
-  
+
+ public void parseInitializers(ClassDescriptor cn){
+       Vector fv=cn.getFieldVec();
+       for(int i=0;i<fv.size();i++) {
+           FieldDescriptor fd=(FieldDescriptor)fv.get(i);
+           if(fd.getExpressionNode()!=null) {
+               Iterator methodit = cn.getMethods();
+               while(methodit.hasNext()){
+                   MethodDescriptor currmd=(MethodDescriptor)methodit.next();
+                   if(currmd.isConstructor()){
+                       BlockNode bn=state.getMethodBody(currmd);
+                       NameNode nn=new NameNode(new NameDescriptor(fd.getSymbol()));
+                       AssignmentNode an=new AssignmentNode(nn,fd.getExpressionNode(),new AssignOperation(1));
+                       bn.addFirstBlockStatement(new BlockExpressionNode(an));                 
+                   }
+               }
+           }
+       }
+    }  
+
   private ClassDescriptor parseEnumDecl(ClassDescriptor cn, ParseNode pn) {
     ClassDescriptor ecd=new ClassDescriptor(pn.getChild("name").getTerminal(), false);
     ecd.setAsEnum();
index 052cac2f4f6aad908e1df6c37aa6d9d8c9c8a84f..3ebdab0c8f65171969ef3e1a112cfeb3a2fdc8ac 100755 (executable)
@@ -15,4 +15,5 @@ dotest CommandLineTest CommandLineTest.java hello hi
 dotest WriteFile WriteFile.java
 dotest ReadFile ReadFile.java
 dotest FileLength FileLength.java
-dotest IntegerTest IntegerTest.java
\ No newline at end of file
+dotest IntegerTest IntegerTest.java
+dotest InitializerTest InitializerTest.java
\ No newline at end of file