1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.millscript.millscript.action;
22
23 import org.millscript.millscript.vm.Machine;
24
25 /**
26 * This class implements the action for a <code>switch</code> expression.
27 *
28 * @see org.millscript.millscript.expr.SwitchExpr
29 * @see org.millscript.millscript.syntax.SwitchSyntax
30 */
31 public final class SwitchAction extends Action {
32
33 /**
34 * The action for the value we want to use as our choice of the available
35 * case's.
36 */
37 private final Action choice;
38
39 /**
40 * An array of all the predicates, i.e. each case test value-action.
41 */
42 private final Action[] preds;
43
44 /**
45 * An array used to lookup from a predicate to the relevant case body
46 * action.
47 *
48 * @todo - I think this is probably overkill and we could simply make the
49 * acts array the same size as preds and duplicate the pointers to the
50 * relevant case body action
51 */
52 private final int[] switches;
53
54 /**
55 * An array of the case-body actions.
56 */
57 private final Action[] acts;
58
59 /**
60 * The default action, if no case predicate matches the choice.
61 */
62 private final Action def;
63
64 /**
65 * The total number of predicates in the switch action.
66 */
67 private final int size;
68
69 /**
70 * Constructs a new <code>switch</code> action with the specified choice,
71 * predicate, case-body lookup, case-body and default actions.
72 *
73 * @param a the choice action
74 * @param b the array of predicate actions
75 * @param s the array for predicate to case-body action lookups
76 * @param c the array of case-body actions
77 * @param d the default action
78 */
79 public SwitchAction( final Action a, final Action[] b, final int[] s, final Action[] c, final Action d ) {
80 this.choice = a;
81 this.preds = b;
82 this.switches = s;
83 this.acts = c;
84 this.def = d;
85 this.size = this.preds.length;
86 }
87
88 /**
89 * @see org.millscript.millscript.action.Action#action(org.millscript.millscript.vm.Machine)
90 */
91 @Override
92 public void action( final Machine mc ) {
93
94 final Object obj = choice.act1( mc );
95
96 if ( obj == null ) {
97
98 for ( int i = 0; i < size; i++ ) {
99
100 if ( preds[ i ].act1( mc ) == null ) {
101
102
103 acts[ switches[ i ] ].act( mc );
104 return;
105 }
106 }
107 } else {
108
109 for ( int i = 0; i < size; i++ ) {
110
111
112 if ( obj.equals( preds[ i ].act1( mc ) ) ) {
113
114
115 acts[ switches[ i ] ].act( mc );
116 return;
117 }
118 }
119 }
120
121 def.act( mc );
122 }
123 }