added tertiary expression
authorjjenista <jjenista>
Tue, 10 Feb 2009 20:47:50 +0000 (20:47 +0000)
committerjjenista <jjenista>
Tue, 10 Feb 2009 20:47:50 +0000 (20:47 +0000)
Robust/src/IR/Flat/BuildFlat.java
Robust/src/IR/Tree/BuildIR.java
Robust/src/IR/Tree/Kind.java
Robust/src/IR/Tree/SemanticCheck.java
Robust/src/IR/Tree/TertiaryNode.java [new file with mode: 0644]
Robust/src/Parse/java14.cup
Robust/src/Tests/TertiaryTest.java [new file with mode: 0644]

index 7ba03259a57f3af87644492fccdf3053331ae45f..0b0fb9228b17e3382d65855fedd7a6d4c25756fe 100644 (file)
@@ -782,10 +782,13 @@ public class BuildFlat {
       return flattenNameNode((NameNode)en,out_temp);
 
     case Kind.OpNode:
-      return flattenOpNode((OpNode)en,out_temp);
+      return flattenOpNode((OpNode)en,out_temp);      
 
     case Kind.OffsetNode:
       return flattenOffsetNode((OffsetNode)en,out_temp);
+
+    case Kind.TertiaryNode:
+      return flattenTertiaryNode((TertiaryNode)en,out_temp);
     }
     throw new Error();
   }
@@ -1076,6 +1079,36 @@ public class BuildFlat {
       return new NodePair(fn,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());
+    TempDescriptor fals_temp=TempDescriptor.tempFactory("tert_fals",tn.getFalseExpr().getType());
+
+    NodePair cond=flattenExpressionNode(tn.getCond(),cond_temp);
+    FlatCondBranch fcb=new FlatCondBranch(cond_temp);
+
+    NodePair trueExpr=flattenExpressionNode(tn.getTrueExpr(),true_temp);
+    FlatOpNode fonT=new FlatOpNode(out_temp, true_temp, null, new Operation(Operation.ASSIGN));
+
+    NodePair falseExpr=flattenExpressionNode(tn.getFalseExpr(),fals_temp);
+    FlatOpNode fonF=new FlatOpNode(out_temp, fals_temp, null, new Operation(Operation.ASSIGN));
+
+    FlatNop nopend=new FlatNop();
+
+    cond.getEnd().addNext(fcb);
+
+    fcb.addTrueNext(trueExpr.getBegin());
+    fcb.addFalseNext(falseExpr.getBegin());
+
+    trueExpr.getEnd().addNext(fonT);
+    fonT.addNext(nopend);
+
+    falseExpr.getEnd().addNext(fonF);
+    fonF.addNext(nopend);
+    
+    return new NodePair(cond.getBegin(), nopend);
+  }
+
   private NodePair flattenBlockStatementNode(BlockStatementNode bsn) {
     switch(bsn.kind()) {
     case Kind.BlockExpressionNode:
index 6b6631662ff7c143ac384a4f1f27f5d61facc094..deb48958b54ee370040eea360b02bdb385a9fabe 100644 (file)
@@ -480,6 +480,10 @@ public class BuildIR {
       String fieldname = pn.getChild("field").getTerminal();
       //System.out.println("Checking the values of: "+ " td.toString()= " + td.toString()+ "  fieldname= " + fieldname);
       return new OffsetNode(td, fieldname);
+    } else if (isNode(pn, "tert")) {
+      return new TertiaryNode(parseExpression(pn.getChild("cond").getFirstChild()),
+                             parseExpression(pn.getChild("trueexpr").getFirstChild()),
+                             parseExpression(pn.getChild("falseexpr").getFirstChild()) );
     } else {
       System.out.println("---------------------");
       System.out.println(pn.PPrint(3,true));
index 6d46fa8593bacfa44fdc8542f6d6656054474d8b..7f26f7ffc3c47a95788e3ada54af8ec7c077faaa 100644 (file)
@@ -25,4 +25,5 @@ public class Kind {
   public final static int OffsetNode=22;
   public final static int SESENode=23;
   public final static int ContinueBreakNode=24;
+  public final static int TertiaryNode=25;
 }
index 82c732ed29b4d459b802f25d4838fa06dc9b6adf..dac024e0bdaf8dca1b64b92ce36fc75eaf002df0 100644 (file)
@@ -395,6 +395,10 @@ public class SemanticCheck {
     case Kind.OffsetNode:
       checkOffsetNode(md, nametable, (OffsetNode)en, new TypeDescriptor(TypeDescriptor.OFFSET));
       return;
+
+    case Kind.TertiaryNode:
+      checkTertiaryNode(md, nametable, (TertiaryNode)en, td);
+      return;
     }
     throw new Error();
   }
@@ -546,6 +550,14 @@ public class SemanticCheck {
     ofn.setType(td);
   }
 
+
+  void checkTertiaryNode(Descriptor md, SymbolTable nametable, TertiaryNode tn, TypeDescriptor td) {
+    checkExpressionNode(md, nametable, tn.getCond(), new TypeDescriptor(TypeDescriptor.BOOLEAN));
+    checkExpressionNode(md, nametable, tn.getTrueExpr(), td );
+    checkExpressionNode(md, nametable, tn.getFalseExpr(), td );
+  }
+
+
   void checkAssignmentNode(Descriptor md, SymbolTable nametable, AssignmentNode an, TypeDescriptor td) {
     boolean postinc=true;
     if (an.getOperation().getBaseOp()==null||
diff --git a/Robust/src/IR/Tree/TertiaryNode.java b/Robust/src/IR/Tree/TertiaryNode.java
new file mode 100644 (file)
index 0000000..0998286
--- /dev/null
@@ -0,0 +1,40 @@
+package IR.Tree;
+import IR.TypeDescriptor;
+
+public class TertiaryNode extends ExpressionNode {
+  ExpressionNode cond;
+  ExpressionNode trueExpr;
+  ExpressionNode falseExpr;
+
+  public TertiaryNode( ExpressionNode cond,
+                      ExpressionNode trueExpr,
+                      ExpressionNode falseExpr ) {
+    this.cond = cond;
+    this.trueExpr = trueExpr;
+    this.falseExpr = falseExpr;
+  }
+
+  public ExpressionNode getCond() {
+    return cond;
+  }
+
+  public ExpressionNode getTrueExpr() {
+    return trueExpr;
+  }
+
+  public ExpressionNode getFalseExpr() {
+    return falseExpr;
+  }
+  
+  public String printNode(int indent) {
+    return cond.printNode(indent)+" ? "+trueExpr.printNode(indent)+" : "+falseExpr.printNode(indent);
+  }
+
+  public TypeDescriptor getType() {
+    return trueExpr.getType();
+  }
+
+  public int kind() {
+    return Kind.TertiaryNode;
+  }
+}
\ No newline at end of file
index 17c47a16d60fe6ec023bf7cd8099601e424fe1ec..d17ed6369f5283f948eb76ba0f9f93215e70da10 100644 (file)
@@ -1850,7 +1850,7 @@ conditional_expression ::=
                        pn.addChild("cond").addChild(condor);
                        pn.addChild("trueexpr").addChild(exptrue);
                        pn.addChild("falseexpr").addChild(expfalse);
-                       RETULT=pn;
+                       RESULT=pn;
                        :}
        ;
 getoffset_expression ::=
diff --git a/Robust/src/Tests/TertiaryTest.java b/Robust/src/Tests/TertiaryTest.java
new file mode 100644 (file)
index 0000000..83bd607
--- /dev/null
@@ -0,0 +1,10 @@
+public class TertiaryTest {
+  static public void main( String[] args ) {
+    int x = 3;
+    int y = x<5 ? 6 : 1000;
+    int z = x>1 ? y>7 ? 2000 : 8 : 3000;
+    System.printString( "x should be 3: "+x+"\n" );
+    System.printString( "y should be 6: "+y+"\n" );
+    System.printString( "z should be 8: "+z+"\n" );
+  }
+}
\ No newline at end of file