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.expr;
22  
23  import org.millscript.millscript.action.BindingFromAction;
24  import org.millscript.millscript.vm.CompilerState;
25  
26  import java.util.Stack;
27  
28  /**
29   * This class represents a for loop from counter binding expression. A from
30   * counter expression binds a counter to a name, where the counter increases on
31   * each iteration. This condition should never cause the for loop to terminate,
32   * as the counter should continue to infinity.
33   *
34   * @see org.millscript.millscript.syntax.ForSyntax
35   * @see BindingFromAction
36   */
37  public final class BindingFromExpr extends BindingExpr< BindingFromAction > {
38  
39      /**
40       * The expression that generates the starting point for the counter.
41       */
42      private final Expr< ? > fromExpr;
43  
44      /**
45       * The name expression to bind the counter to.
46       */
47      private final NameExpr name;
48  
49      /**
50       * Constructs a new from counter binding expression, with the suplied name
51       * and starting point expression.
52       *
53       * @param   n   the name expression to bind to
54       * @param   f   the counter starting point expression
55       */
56      public BindingFromExpr( final NameExpr n, final Expr< ? > f ) {
57          name = n;
58          fromExpr = CheckExpr.make( f );
59      }
60  
61      /**
62       * @see org.millscript.millscript.expr.Expr#compileIt()
63       */
64      @Override
65      public BindingFromAction compileIt() {
66          return new BindingFromAction( name.getIdent(), fromExpr.compile() );
67      }
68  
69      /**
70       * @see org.millscript.millscript.expr.BindingExpr#pushNames(java.util.Stack)
71       */
72      @Override
73      void pushNames( final Stack< Expr > s ) {
74          s.push( name );
75      }
76  
77      /**
78       * @see org.millscript.millscript.expr.Expr#resolve(org.millscript.millscript.vm.CompilerState)
79       */
80      @Override
81      public void resolve( final CompilerState state ) {
82          this.fromExpr.resolve( state );
83      }
84  
85      /**
86       * @see org.millscript.millscript.expr.Expr#showComponents(int)
87       */
88      @Override
89      void showComponents( final int n ) {
90          this.fromExpr.show( n );
91      }
92  
93  }