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.BindingMapAction;
24  import org.millscript.millscript.vm.CompilerState;
25  
26  import java.util.Stack;
27  
28  /**
29   * This class represents a for loop map binding expression. A map binding
30   * expression binds successive key-value pairs from a map to given names.
31   */
32  public final class BindingMapExpr extends BindingExpr< BindingMapAction > {
33  
34      /**
35       * The expression that generates the map to bind elements from.
36       */
37      private final Expr< ? > inExpr;
38  
39      /**
40       * The name expression to bind successive keys to.
41       */
42      private final NameExpr key;
43  
44      /**
45       * The name expression to bind successive values to.
46       */
47      private final NameExpr val;
48  
49      /**
50       * Constructs a new map binding expression, with the supplied key and value
51       * name expressions and map expression.
52       *
53       * @param   k   the name expression for the key to bind to
54       * @param   v   the name expression for the value to bind to
55       * @param   i   the expression to generate the map to bind from
56       */
57      public BindingMapExpr( final NameExpr k, final NameExpr v, final Expr< ? > i ) {
58          key = k;
59          val = v;
60          inExpr = CheckExpr.make( i );
61      }
62  
63      /**
64       * @see org.millscript.millscript.expr.Expr#compileIt()
65       */
66      @Override
67      public BindingMapAction compileIt() {
68          return new BindingMapAction(
69              key.getIdent(),
70              val.getIdent(),
71              inExpr.compile()
72          );
73      }
74  
75      /**
76       * @see org.millscript.millscript.expr.BindingExpr#pushNames(java.util.Stack)
77       */
78      @Override
79      void pushNames( final Stack< Expr > s ) {
80          s.push( key );
81          s.push( val );
82      }
83  
84      /**
85       * @see org.millscript.millscript.expr.Expr#resolve(org.millscript.millscript.vm.CompilerState)
86       */
87      @Override
88      public void resolve( final CompilerState state ) {
89          this.inExpr.resolve( state );
90      }
91  
92      /**
93       * @see org.millscript.millscript.expr.Expr#showComponents(int)
94       */
95      @Override
96      void showComponents( final int n ) {
97          this.inExpr.show( n );
98      }
99  
100 }