01: /*
02: * This file is part of PFIXCORE.
03: *
04: * PFIXCORE is free software; you can redistribute it and/or modify
05: * it under the terms of the GNU Lesser General Public License as published by
06: * the Free Software Foundation; either version 2 of the License, or
07: * (at your option) any later version.
08: *
09: * PFIXCORE is distributed in the hope that it will be useful,
10: * but WITHOUT ANY WARRANTY; without even the implied warranty of
11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: * GNU Lesser General Public License for more details.
13: *
14: * You should have received a copy of the GNU Lesser General Public License
15: * along with PFIXCORE; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: */
18:
19: package de.schlund.pfixcore.scriptedflow.compiler;
20:
21: import java.util.ArrayList;
22: import java.util.List;
23:
24: import de.schlund.pfixcore.scriptedflow.vm.Instruction;
25: import de.schlund.pfixcore.scriptedflow.vm.JumpCondFalseInstruction;
26: import de.schlund.pfixcore.scriptedflow.vm.JumpUncondInstruction;
27: import de.schlund.pfixcore.scriptedflow.vm.NopInstruction;
28:
29: /**
30: * Statement used to branch on different conditions
31: *
32: * @author Sebastian Marsching <sebastian.marsching@1und1.de>
33: */
34: public class ChooseStatement extends AbstractStatement {
35:
36: private List<Instruction> jumpInstr = null;
37:
38: private List<Instruction> jumpEndInstr = null;
39:
40: private List<Instruction> nopInstr = null;
41:
42: private List<String> conditions = new ArrayList<String>();
43:
44: private List<Statement> childs = new ArrayList<Statement>();
45:
46: public ChooseStatement(Statement parent) {
47: super (parent);
48: }
49:
50: public void addBranch(String condition, Statement child) {
51: if (conditions.size() > 0
52: && conditions.get(conditions.size() - 1) == null) {
53: throw new RuntimeException(
54: "No branch after default branch is allowed");
55: }
56: conditions.add(condition);
57: childs.add(child);
58: }
59:
60: public Instruction[] getInstructions() {
61: if (jumpInstr == null) {
62: jumpInstr = new ArrayList<Instruction>();
63: nopInstr = new ArrayList<Instruction>();
64: for (String condition : conditions) {
65: Instruction lastNopInstr = new NopInstruction();
66: nopInstr.add(lastNopInstr);
67: if (condition != null) {
68: jumpInstr.add(new JumpCondFalseInstruction(
69: condition, lastNopInstr));
70: } else {
71: jumpInstr.add(new NopInstruction());
72: }
73: }
74: jumpEndInstr = new ArrayList<Instruction>();
75: for (int i = 0; i < jumpInstr.size(); i++) {
76: jumpEndInstr.add(new JumpUncondInstruction(nopInstr
77: .get(nopInstr.size() - 1)));
78: }
79: }
80: List<Instruction> temp = new ArrayList<Instruction>();
81: for (int i = 0; i < jumpInstr.size(); i++) {
82: temp.add(jumpInstr.get(i));
83: Instruction[] childInstr = childs.get(i).getInstructions();
84: for (int j = 0; j < childInstr.length; j++) {
85: temp.add(childInstr[j]);
86: }
87: temp.add(jumpEndInstr.get(i));
88: temp.add(nopInstr.get(i));
89: }
90: Instruction[] temp2 = new Instruction[temp.size()];
91: for (int i = 0; i < temp2.length; i++) {
92: temp2[i] = temp.get(i);
93: }
94: return temp2;
95: }
96: }
|