View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2001-2004 Open World Ltd
4   //
5   // This file is part of MillScript.
6   //
7   // MillScript is free software; you can redistribute it and/or modify it under
8   // the terms of the GNU General Public License as published by the Free
9   // Software Foundation; either version 2 of the License, or (at your option)
10  // any later version.
11  //
12  // MillScript is distributed in the hope that it will be useful, but WITHOUT
13  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  // more details.
16  //
17  // You should have received a copy of the GNU General Public License along with
18  // MillScript; if not, write to the Free Software Foundation, Inc., 59 Temple
19  // Place, Suite 330, Boston, MA  02111-1307  USA
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          // Get the value for the switch choice
94          final Object obj = choice.act1( mc );
95          // We have to handle null in a special way...
96          if ( obj == null ) {
97              // Loop through each predicate
98              for ( int i = 0; i < size; i++ ) {
99                  // Calculate the predicate and check if it's null
100                 if ( preds[ i ].act1( mc ) == null ) {
101                     // We've found a match, so perform the relevant case-body
102                     // action
103                     acts[ switches[ i ] ].act( mc );
104                     return;
105                 }
106             }
107         } else {
108             // Loop through each predicate
109             for ( int i = 0; i < size; i++ ) {
110                 // Calculate the predicate and check if it's equal to the
111                 // choice value
112                 if ( obj.equals( preds[ i ].act1( mc ) ) ) {
113                     // We've found a match, so perform the relevant case-body
114                     // action
115                     acts[ switches[ i ] ].act( mc );
116                     return;
117                 }
118             }
119         }
120         // We didn't find a match, so perform the default action
121         def.act( mc );
122     }
123 }