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