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();
}
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:
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));
public final static int OffsetNode=22;
public final static int SESENode=23;
public final static int ContinueBreakNode=24;
+ public final static int TertiaryNode=25;
}
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();
}
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||
--- /dev/null
+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
pn.addChild("cond").addChild(condor);
pn.addChild("trueexpr").addChild(exptrue);
pn.addChild("falseexpr").addChild(expfalse);
- RETULT=pn;
+ RESULT=pn;
:}
;
getoffset_expression ::=
--- /dev/null
+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