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.Action;
24  import org.millscript.millscript.action.BlockAction;
25  import org.millscript.millscript.vm.CompilerState;
26  
27  import java.util.Stack;
28  
29  /**
30   * This class implements a block expression. A block is a simple wrapper for
31   * its body expression, except that it introduces a new scope to the compiler.
32   *
33   * @see BlockAction
34   */
35  public final class Block extends Expr< Action > {
36  
37      /**
38       * The body expression for this block.
39       */
40      private final Expr< ? > body;
41  
42      /**
43       * Boolean indicating if this block is used.
44       */
45      private boolean isUsed = false;
46  
47      /**
48       * Creates a new block expression with the specified body.
49       *
50       * @param   e   the body expression for this block
51       */
52      public Block( final Expr< ? > e ) {
53          body = e;
54      }
55  
56      /**
57       * @see org.millscript.millscript.expr.Expr#compileIt()
58       */
59      @Override
60      public Action compileIt() {
61          // Compile the body
62          Action alpha = body.compile();
63          // Check if this block is used, if it is return a new block action,
64          // otherwise just return the compiled body.
65          return isUsed ? new BlockAction( alpha ) : alpha;
66      }
67  
68      /**
69       * @see org.millscript.millscript.expr.Expr#resolve(org.millscript.millscript.vm.CompilerState)
70       */
71      @Override
72      public void resolve( final CompilerState state ) {
73          // Introduces a new scope prior to renaming the body of this block.
74          // After renaming the body, the scope is reset to it's prior state.
75          // This allows use to have variables of the same name within nested
76          // blocks of code, but ensure that the correct ones are shared.
77          Stack< Expr > scopes = state.getScopes();
78          int n = scopes.size();
79          scopes.push( this );
80          body.resolve( state );
81          scopes.setSize( n );
82      }
83  
84      /**
85       * Marks this block as used.
86       */
87      public void setIsUsed() {
88          isUsed = true;
89      }
90  
91      /**
92       * @see org.millscript.millscript.expr.Expr#showComponents(int)
93       */
94      @Override
95      void showComponents( final int n ) {
96          this.body.show( n );
97      }
98  
99  }