View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2001-2004 Open World Ltd
4   // Copyright (C) 2005 Kevin Rogers
5   //
6   // This file is part of MillScript.
7   //
8   // MillScript is free software; you can redistribute it and/or modify it under
9   // the terms of the GNU General Public License as published by the Free
10  // Software Foundation; either version 2 of the License, or (at your option)
11  // any later version.
12  //
13  // MillScript is distributed in the hope that it will be useful, but WITHOUT
14  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  // more details.
17  //
18  // You should have received a copy of the GNU General Public License along with
19  // MillScript; if not, write to the Free Software Foundation, Inc., 59 Temple
20  // Place, Suite 330, Boston, MA  02111-1307  USA
21  ////////////////////////////////////////////////////////////////////////////////
22  package org.millscript.millscript.action;
23  
24  import org.millscript.millscript.alert.Alerts;
25  import org.millscript.millscript.vm.Machine;
26  
27  /**
28   * The class represents the base of all for loop condition actions.
29   */
30  public abstract class ForConditionAction extends Action {
31  
32      /**
33       * This class represents the conditions, hence bindings in for loops.
34       * Instances of this class can perform bindings, control loop termination.
35       * They also serve as a location to store an action which can be executed
36       * when a for loop terminates. This class is a relativly simple extension
37       * of an Iterator.
38       */
39      abstract class ForIterator {
40  
41          /**
42           * The binding action for this iterator.
43           *
44           * @param   mc  the machine to perform binding operations on
45           */
46          public abstract void bindAction( final Machine mc );
47  
48          /**
49           * @see org.millscript.commons.util.MapIterator#hasNext()
50           */
51          public abstract boolean hasNext();
52  
53          /**
54           * The termination action for this iterator. This is performed by the
55           * for loop action, if this iterator causes the for loop to terminate.
56           *
57           * @param   mc  the machine to perform the termination action on
58           * @see #terminatesLoop()
59           * @see ForAction#action(Machine)
60           */
61          public void terminateAction( final Machine mc ) {
62              finallyAction.act( mc );
63          }
64  
65          /**
66           * Determines if this iterator causes the termination of the for loop.
67           * The for loop action calls this method when this iterator is
68           * exhausted. By default a ForIterator will terminate the for loop when
69           * there are no more values.
70           *
71           * @return  a boolean indicating if this iterator terminates the loop
72           * @see #hasNext()
73           * @see ForAction#action(Machine)
74           */
75          public boolean terminatesLoop() {
76              return true;
77          }
78  
79      }
80  
81      /**
82       * This action is executed when the loop terminates. There is no default
83       * action.
84       *
85       * @see ForAction#action(Machine)
86       */
87      Action finallyAction = new SkipAction();
88  
89      /**
90       * Returns a for loop iterator for this loop condition.
91       *
92       * @param mc    the machine to operate the for interator with
93       * @return  a ForIterator for this condition
94       */
95      abstract ForIterator getForIterator( final Machine mc );
96  
97      /**
98       * @see org.millscript.millscript.action.Action#action(org.millscript.millscript.vm.Machine)
99       */
100     @Override
101     public void action( final Machine mc ) {
102         throw(
103             Alerts.fault( "Calling a ForConditionAction?" ).mishap()
104         );
105     }
106 
107     /**
108      * Sets the finally(termination) action for this for loop condition. This
109      * action will be performed if this condition causes the for loop to
110      * terminate.
111      *
112      * @param   a   the finally action
113      */
114     public void setFinallyAction( final Action a ) {
115         // the existing finally action is nothing, so we can safely update it
116         // with something possibly more specific. i.e. if there is a proper
117         // action already in place, leave it alone.
118         if ( finallyAction instanceof SkipAction ) {
119             finallyAction = a;
120         }
121     }
122 
123 }