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 BinaryOpExpr< A extends Action > extends Expr< A > implements OneResult {
36  
37      /**
38       * The left hand side expression
39       */
40      private final Expr< ? > lhs;
41  
42      /**
43       * The right hand side expression
44       */
45      private final Expr< ? > rhs;
46  
47      /**
48       * Creates a new binary operation expression with the specified left and
49       * right hand side expressions.
50       *
51       * @param   a   the left hand side expression, which must return a single
52       *              result
53       * @param   b   the right hand side expression, which must return a single
54       *              result
55       */
56      public BinaryOpExpr( final Expr< ? > a, final Expr< ? > b ) {
57          this.lhs = CheckExpr.make( a );
58          this.rhs = CheckExpr.make( b );
59      }
60  
61      /**
62       * @see org.millscript.millscript.expr.Expr#compileIt()
63       */
64      @Override
65      public final A compileIt() {
66          // This method is a convenience to simplify implementing a binary
67          // operation expressions. It handles the compilation of the left and
68          // right hand side expressions, passing them to the newAction method to
69          // compile the binary operation expression.
70          return newAction( lhs.compile(), rhs.compile() );
71      }
72  
73      /**
74       * Compiles this expression into an action. This is method performs the
75       * expression specific compilation, but does not need to handle the left
76       * and right hand side expression compilation.
77       *
78       * @param   a   the compiled left hand side expression
79       * @param   b   the compiled right hand side expression
80       * @return  the {@link Action} for this expression
81       * @see #compileIt
82       */
83      public abstract A newAction( final Action a, final Action b );
84  
85      /**
86       * @see org.millscript.millscript.expr.Expr#resolve(org.millscript.millscript.vm.CompilerState)
87       */
88      @Override
89      public void resolve( final CompilerState state ) {
90          this.lhs.resolve( state );
91          this.rhs.resolve( state );
92      }
93  
94      /**
95       * @see org.millscript.millscript.expr.Expr#showComponents(int)
96       */
97      @Override
98      void showComponents( final int n ) {
99          this.lhs.show( n );
100         this.rhs.show( n );
101     }
102 
103 }