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