KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > program > SwitchStatement


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

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 JavaDoc;
39
40 /**
41  * Represents a switch statement.
42  */

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 JavaDoc<Expr[]> caseList,
54                          ArrayList JavaDoc<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   /**
71    * Executes the 'switch' statement, returning any value.
72    */

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 JavaDoc e) {
108       rethrow(e, RuntimeException JavaDoc.class);
109     }
110
111     return null;
112   }
113
114   /**
115    * Returns true if control can go past the statement.
116    */

117   public int fallThrough()
118   {
119     return FALL_THROUGH;
120     /* php/367t, php/367u
121     if (_defaultBlock == null)
122       return FALL_THROUGH;
123
124     int fallThrough = _defaultBlock.fallThrough();
125
126     for (int i = 0; i < _blocks.length; i++) {
127       fallThrough &= _blocks[i].fallThrough();
128     }
129
130     if (fallThrough == BREAK_FALL_THROUGH)
131       return 0;
132     else
133       return fallThrough;
134     */

135   }
136 }
137
138
Popular Tags