1 package com.genimen.djeneric.tools.generator.core.nodes; 2 3 import com.genimen.djeneric.tools.generator.core.DjentelParserEngine; 4 import com.genimen.djeneric.tools.generator.core.ParseException; 5 import com.genimen.djeneric.tools.generator.core.SimpleNode; 6 import com.genimen.djeneric.tools.generator.core.util.ParseContext; 7 8 public class IfNode extends SimpleNode 9 { 10 11 public IfNode(int i) 12 { 13 super(i); 14 } 15 16 public IfNode(DjentelParserEngine p, int i) 17 { 18 super(p, i); 19 } 20 21 public String getName() 22 { 23 return "if"; 24 } 25 26 public String toString() 27 { 28 return ""; 29 } 30 31 public String evaluate(ParseContext context) throws ParseException 32 { 33 if (!(getChild(0) instanceof BooleanExpression)) 34 { 35 if (getChild(0) instanceof ValueExpression) 36 { 37 ValueExpression ve = (ValueExpression) getChild(0); 38 Object o = ve.getValue(context); 39 if (o instanceof Boolean ) 40 { 41 Boolean b = (Boolean ) o; 42 if (b.booleanValue()) return handleTrue(context); 43 else return handleFalse(context); 44 } 45 } 46 throw new ParseException("Not a boolean expression", beginLine, beginColumn); 47 } 48 49 BooleanExpression expr = (BooleanExpression) getChild(0); 50 if (expr.isTrue(context)) return handleTrue(context); 51 else return handleFalse(context); 52 } 53 54 String handleTrue(ParseContext context) throws ParseException 55 { 56 return getChild(1).evaluate(context); 57 } 58 59 String handleFalse(ParseContext context) throws ParseException 60 { 61 if (getChildCount() == 3) 63 { 64 return getChild(2).evaluate(context); 65 } 66 return ""; 67 } 68 } | Popular Tags |