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.NoneAction;
24  import org.millscript.millscript.vm.CompilerState;
25  
26  /**
27   * This class implements a <code>none</code> expression. This expression will
28   * never return any results.
29   *
30   * @see org.millscript.millscript.syntax.NoneSyntax
31   * @see NoneAction
32   */
33  public final class NoneExpr extends Expr< NoneAction > implements ZeroResults {
34  
35      /**
36       * Returns the specified expression ensuring it will return zero results.
37       * This method provides some compile-time optimisation, as it avoids
38       * creating additional unnecessary objects in the expression tree. If the
39       * specified expression is already returns zero results, we simply return
40       * it, otherwise we proceed to wrap it in a new none expression.
41       *
42       * @param   x   the expression for which to discard results
43       * @return  the specified expression if it returns zero results, or a new
44       *          {@link NoneExpr} for the specified expression
45       */
46      public static Expr< ? > make( final Expr< ? > x ) {
47          return x.arity() == 0 ? x : new NoneExpr( x );
48      }
49  
50      /**
51       * The expression to evaluate, but dispose of results.
52       */
53      private final Expr< ? > cont;
54  
55      /**
56       * Creates a new <code>none</code> expression for the specified expression.
57       *
58       * @param   x   the expression to evaluate
59       */
60      public NoneExpr( final Expr< ? > x ) {
61          cont = x;
62      }
63  
64      /**
65       * @see org.millscript.millscript.expr.Expr#compileIt()
66       */
67      @Override
68      public NoneAction compileIt() {
69          return new NoneAction( cont.compile() );
70      }
71  
72      /**
73       * @see org.millscript.millscript.expr.Expr#resolve(org.millscript.millscript.vm.CompilerState)
74       */
75      @Override
76      public void resolve( final CompilerState state ) {
77          this.cont.resolve( state );
78      }
79  
80      /**
81       * @see org.millscript.millscript.expr.Expr#showComponents(int)
82       */
83      @Override
84      void showComponents( final int n ) {
85          this.cont.show( n );
86      }
87  
88  }