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.commons.alert.alerts.Fault;
24  import org.millscript.millscript.expr.Ident;
25  import org.millscript.millscript.tools.CastLibrary;
26  import org.millscript.millscript.vm.Machine;
27  import org.millscript.millscript.vm.Ref;
28  
29  /**
30   * This class represents a for loop from counter binding action. A from counter
31   * action binds a counter to a name, where the counter increases on each
32   * iteration. This condition should never cause the for loop to terminate, as
33   * the counter should continue to infinity.
34   */
35  public final class BindingFromAction extends BindingAction {
36  
37      /**
38       * This class implements the underlying iterator for the <code>from</code>
39       * binding in a <code>for</code> loop.
40       */
41      private final class BindingFromIterator extends ForIterator {
42  
43          /**
44           * The current value of this iterator.
45           */
46          private int count;
47  
48          /**
49           * Constructs a new from iterator to start counting from the specified
50           * value.
51           *
52           * @param fromInt   the value to start counting from
53           */
54          private BindingFromIterator( final int fromInt ) {
55              this.count = fromInt;
56          }
57  
58          /**
59           * @see org.millscript.millscript.action.ForConditionAction.ForIterator#bindAction(org.millscript.millscript.vm.Machine)
60           */
61          @Override
62          public void bindAction( final Machine mc ) {
63              // Save the original value of this reference
64              mc.saveRef( ref );
65              if ( count > Integer.MAX_VALUE ) {
66                  throw new Fault( "Exceeded range of an int" ).mishap();
67              } else {
68                  // Set the reference to the next value
69                  ref.value = new Integer( count++ );
70              }
71          }
72  
73          /**
74           * @see org.millscript.millscript.action.ForConditionAction.ForIterator#hasNext()
75           */
76          @Override
77          public boolean hasNext() {
78              // A from counter always has another value.
79              return true;
80          }
81  
82      }
83  
84      /**
85       * The ident reference this from action is bound to.
86       */
87      Ref ref;
88  
89      /**
90       * The ident this from action is bound to.
91       */
92      private Ident var;
93  
94      /**
95       * The action that generates the starting point for the counter.
96       */
97      private Action fromAct;
98  
99      /**
100      * Constructs a new from counter binding action, with the supplied ident and
101      * starting point action.
102      *
103      * @param   a   Ident of the variable this action is bound to
104      * @param   b   Action which generates starting point for the counter
105      */
106     public BindingFromAction( final Ident a, final Action b ) {
107         var = a;
108         ref = var.getRef();
109         fromAct = b;
110     }
111 
112     /**
113      * @see org.millscript.millscript.action.ForConditionAction#getForIterator(org.millscript.millscript.vm.Machine)
114      */
115     @Override
116     ForIterator getForIterator( final Machine mc ) {
117         return new BindingFromIterator(
118             CastLibrary.toInt( fromAct.act1( mc ) )
119         ); 
120     }
121 
122 }