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.action.AppendAction;
25  
26  /**
27   * This class implements an append expression. The left and right hand side
28   * expressions must produce single results that are compatible for appending.
29   *
30   * @see org.millscript.millscript.syntax.AppendSyntax
31   * @see AppendAction
32   */
33  public final class AppendExpr extends BinaryOpExpr< AppendAction > {
34  
35      /**
36       * Creates a new append expression with the specified left and right hand
37       * side expressions.
38       *
39       * @param a the left hand expression
40       * @param b the right hand expression, to append to the left
41       */
42      public AppendExpr( final Expr< ? > a, final Expr< ? > b ) {
43          super( a, b );
44      }
45  
46      /**
47       * Returns an expression which appends the specified left and right hand
48       * side expressions. This method provides some compile-time optimisation, as
49       * it avoids creating additional unnecessary objects in the expression tree.
50       * If the left and right hand side expression are constant Strings, we
51       * create a new constant expression by appending the right to the left.
52       * Otherwise we create a normal apply expression.
53       *
54       * @param   a   the left hand side expression
55       * @param   b   the right hand side expression
56       * @return  a {@link ConstantExpr} if the left and right hand side
57       *          expressions are both constant Strings, otherwise an
58       *          {@link AppendExpr}
59       */
60      public static Expr make( final Expr a, final Expr b ) {
61          if ( a instanceof ConstantExpr && b instanceof ConstantExpr ) {
62              Object x = ((ConstantExpr)a).getValue();
63              Object y = ((ConstantExpr)b).getValue();
64              if ( x instanceof String && y instanceof String ) {
65                  return new ConstantExpr( ((String)x) + ((String)y) );
66              } else {
67                  return new AppendExpr( a, b );
68              }
69          } else {
70              return new AppendExpr( a, b );
71          }
72      }
73  
74      /**
75       * @see org.millscript.millscript.expr.BinaryOpExpr#newAction(org.millscript.millscript.action.Action, org.millscript.millscript.action.Action)
76       */
77      @Override
78      public AppendAction newAction( final Action a, final Action b ) {
79          return new AppendAction( a, b );
80      }
81  }