From: jjenista Date: Tue, 10 Feb 2009 20:47:50 +0000 (+0000) Subject: added tertiary expression X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=21fb3a46af7ff94815d3dd49a3c91e730429473f;p=IRC.git added tertiary expression --- diff --git a/Robust/src/IR/Flat/BuildFlat.java b/Robust/src/IR/Flat/BuildFlat.java index 7ba03259..0b0fb922 100644 --- a/Robust/src/IR/Flat/BuildFlat.java +++ b/Robust/src/IR/Flat/BuildFlat.java @@ -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: diff --git a/Robust/src/IR/Tree/BuildIR.java b/Robust/src/IR/Tree/BuildIR.java index 6b663166..deb48958 100644 --- a/Robust/src/IR/Tree/BuildIR.java +++ b/Robust/src/IR/Tree/BuildIR.java @@ -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)); diff --git a/Robust/src/IR/Tree/Kind.java b/Robust/src/IR/Tree/Kind.java index 6d46fa85..7f26f7ff 100644 --- a/Robust/src/IR/Tree/Kind.java +++ b/Robust/src/IR/Tree/Kind.java @@ -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; } diff --git a/Robust/src/IR/Tree/SemanticCheck.java b/Robust/src/IR/Tree/SemanticCheck.java index 82c732ed..dac024e0 100644 --- a/Robust/src/IR/Tree/SemanticCheck.java +++ b/Robust/src/IR/Tree/SemanticCheck.java @@ -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 index 00000000..09982865 --- /dev/null +++ b/Robust/src/IR/Tree/TertiaryNode.java @@ -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 diff --git a/Robust/src/Parse/java14.cup b/Robust/src/Parse/java14.cup index 17c47a16..d17ed636 100644 --- a/Robust/src/Parse/java14.cup +++ b/Robust/src/Parse/java14.cup @@ -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 index 00000000..83bd6074 --- /dev/null +++ b/Robust/src/Tests/TertiaryTest.java @@ -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