1 29 30 package com.caucho.quercus.program; 31 32 import com.caucho.quercus.Location; 33 import com.caucho.quercus.env.BreakValue; 34 import com.caucho.quercus.env.Env; 35 import com.caucho.quercus.env.Value; 36 import com.caucho.quercus.expr.Expr; 37 38 import java.util.ArrayList ; 39 40 43 public class SwitchStatement extends Statement { 44 protected final Expr _value; 45 46 protected final Expr[][] _cases; 47 protected final BlockStatement[] _blocks; 48 49 protected final Statement _defaultBlock; 50 51 public SwitchStatement(Location location, 52 Expr value, 53 ArrayList <Expr[]> caseList, 54 ArrayList <BlockStatement> blockList, 55 Statement defaultBlock) 56 { 57 super(location); 58 59 _value = value; 60 61 _cases = new Expr[caseList.size()][]; 62 caseList.toArray(_cases); 63 64 _blocks = new BlockStatement[blockList.size()]; 65 blockList.toArray(_blocks); 66 67 _defaultBlock = defaultBlock; 68 } 69 70 73 public Value execute(Env env) 74 { 75 try { 76 Value testValue = _value.eval(env); 77 78 int len = _cases.length; 79 80 for (int i = 0; i < len; i++) { 81 Expr []values = _cases[i]; 82 83 for (int j = 0; j < values.length; j++) { 84 Value caseValue = values[j].eval(env); 85 86 if (testValue.eq(caseValue)) { 87 Value retValue = _blocks[i].execute(env); 88 89 if (retValue instanceof BreakValue) 90 return null; 91 else 92 return retValue; 93 } 94 } 95 } 96 97 if (_defaultBlock != null) { 98 Value retValue = _defaultBlock.execute(env); 99 100 if (retValue instanceof BreakValue) 101 return null; 102 else 103 return retValue; 104 } 105 106 } 107 catch (RuntimeException e) { 108 rethrow(e, RuntimeException .class); 109 } 110 111 return null; 112 } 113 114 117 public int fallThrough() 118 { 119 return FALL_THROUGH; 120 135 } 136 } 137 138 | Popular Tags |