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.NotAction;
24  import org.millscript.millscript.vm.CompilerState;
25  
26  /**
27   * This class implements a <code>not</code> expression. This expression always
28   * returns one result.
29   *
30   * @see org.millscript.millscript.syntax.NotSyntax
31   * @see NotAction
32   */
33  public final class NotExpr extends Expr< NotAction > implements OneResult {
34  
35      /**
36       * Returns an expression which negates the specified expression. This method
37       * provides some compile-time optimisation, as it avoids creating additional
38       * unnecessary objects in the expression tree. If the specified expression
39       * is already a not expression, we return it's expression to be negated.
40       * Otherwise we return a new not expression.
41       *
42       * @param   x   the expression whose result to be negated
43       * @return  a {@link NotExpr} if the specified expression is not already
44       *          {@link NotExpr}, otherwise we return the specified expressions
45       *          body expression.
46       */
47      public static Expr< ? > make( final Expr x ) {
48          // FIXME - When Sun fix their java compiler the method parameter can be
49          // made Expr< ? > and the suppress warnings can be removed
50          // Check if the specified expression is already a not expression
51          if ( x instanceof NotExpr ) {
52              // It is, so return it's body expression
53              return ((NotExpr) x).getCont();
54          } else {
55              // No it isn't, so return a new not expression
56              return new NotExpr( x );
57          }
58      }
59  
60      /**
61       * The expression whose result should be negated.
62       */
63      private final Expr< ? > cont;
64  
65      /**
66       * Creates a new <code>not</code> expression to negate the result of the
67       * specified expression.
68       *
69       * @param   x   the expression whose result to negate
70       */
71      public NotExpr( final Expr< ? > x ) {
72          cont = CheckExpr.make( x );
73      }
74  
75      /**
76       * Returns this instances expression to be negated.
77       *
78       * @return  this instances expression to be negated.
79       */
80      Expr< ? > getCont() {
81          return cont;
82      }
83  
84      /**
85       * @see org.millscript.millscript.expr.Expr#compileIt()
86       */
87      @Override
88      public NotAction compileIt() {
89          return new NotAction( cont.compile() );
90      }
91  
92      /**
93       * @see org.millscript.millscript.expr.Expr#resolve(org.millscript.millscript.vm.CompilerState)
94       */
95      @Override
96      public void resolve( final CompilerState state ) {
97          this.cont.resolve( state );
98      }
99  
100     /**
101      * @see org.millscript.millscript.expr.Expr#showComponents(int)
102      */
103     @Override
104     void showComponents( final int n ) {
105         this.cont.show( n );
106     }
107 
108 }