1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 }