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.vm.CompilerState;
25  
26  /**
27   * This class represents binary opereration syntax. A binary operation is one
28   * which returns a single result, from two single inputs. This means the left
29   * and right hand side expressions must produce single results, and that the
30   * expression as a whole will return a single result.
31   *
32   * @see AppendExpr
33   * @see org.millscript.millscript.action.BinaryOpAction
34   */
35  public abstract class UnaryOpExpr< A extends Action > extends Expr< A > implements OneResult {
36  
37      /**
38       * The single expression
39       */
40      private final Expr< ? > expr;
41  
42      /**
43       * Creates a new unary operation expression for the specified expression.
44       *
45       * @param   a   the single expression, which must return a single result
46       */
47      public UnaryOpExpr( final Expr< ? > a ) {
48          this.expr = CheckExpr.make( a );
49      }
50  
51      /**
52       * @see org.millscript.millscript.expr.Expr#compileIt()
53       */
54      @Override
55      public final A compileIt() {
56          // This method is a convenience to simplify implementing a unary
57          // operation expressions. It handles the compilation of the
58          // expressions, passing it to the newAction method to compile the
59          // binary operation expression.
60          return newAction( expr.compile() );
61      }
62  
63      /**
64       * Compiles this expression into an action. This is method performs the
65       * expression specific compilation, but does not need to handle the single
66       * expression compilation.
67       *
68       * @param   a   the compiled single expression
69       * @return  the {@link Action} for this expression
70       */
71      public abstract A newAction( final Action a );
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.expr.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.expr.show( n );
87      }
88  
89  }